From 265bdc5e63721cc2e4d878275ae30c63f6854aa8 Mon Sep 17 00:00:00 2001 From: "David E. Wheeler" Date: Mon, 26 Aug 2024 15:51:48 -0400 Subject: [PATCH] Put Rust and pgrx tests in separate modules Since they compile separately, different dependencies can lead to warnings (see pgcentralfoundation/pgrx#1774 for details). So move shared functions to a new `test_util` module, keep the Rust-only tests in `mod test`, and keep the pgrx tests in `mod tests`. I wish I could put the pgrx tests in the required `pg_test` schema, but it appears that they require that the module be named "tests". Thanks @eeeebbbbrrrr for suggesting this solution. --- src/lib.rs | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2466f20..e799eeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -293,34 +293,31 @@ fn validate(id: &str, schemas: &[Value], instance: Value) -> Result Value { + pub fn user_schema() -> Value { let bytes = include_bytes!("../eg/user-profile.schema.json"); assert!(!bytes.is_empty()); serde_json::from_slice(bytes).unwrap() } - fn addr_schema() -> Value { + pub fn addr_schema() -> Value { let bytes = include_bytes!("../eg/address.schema.json"); assert!(!bytes.is_empty()); serde_json::from_slice(bytes).unwrap() } +} + +// Rust-only tests. +#[cfg(test)] +mod test { + use super::*; + use crate::test_util::*; + use serde_json::json; + use std::error::Error; // Make sure our Draft enum converts properly into boon's. #[test] @@ -509,6 +506,24 @@ mod tests { Ok(()) } +} + +// pgrx tests. +#[cfg(any(test, feature = "pg_test"))] +#[pg_schema] +mod tests { + use super::*; + use crate::test_util::*; + use pgrx::pg_sys::panic::CaughtError::PostgresError; + use pgrx::{spi::SpiError, Json, JsonB}; + use serde_json::json; + + // Enum used to record handling expected errors. + #[derive(Debug, Eq, PartialEq)] + enum ErrorCaught { + True, + False, + } #[pg_test] fn test_jsonschema_is_valid() {