Skip to content

Commit

Permalink
Put Rust and pgrx tests in separate modules
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
theory committed Aug 26, 2024
1 parent 280578f commit 265bdc5
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,34 +293,31 @@ fn validate(id: &str, schemas: &[Value], instance: Value) -> Result<bool, Compil
}
}

// Utility functions for the tests and pg_tests modules to use.
#[cfg(any(test, feature = "pg_test"))]
#[pg_schema]
mod tests {
use super::*;
use pgrx::pg_sys::panic::CaughtError::PostgresError;
use pgrx::{spi::SpiError, Json, JsonB};
use serde_json::json;
use std::error::Error;

// Enum used to record handling expected errors.
#[derive(Debug, Eq, PartialEq)]
enum ErrorCaught {
True,
False,
}

pub mod test_util {
use serde_json::Value;
// Load the user and address schemas (bytes loaded at compile time).
fn user_schema() -> 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]
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 265bdc5

Please sign in to comment.