Skip to content

Commit

Permalink
Upgrade to pgrx v0.11.4 and use include_bytes!()
Browse files Browse the repository at this point in the history
The new version of pgrx allows tests to be run as a different user,
which will allow pgrx testing support to be added to
/pgxn/docker-pgxn-tools. The change here lets me actually test it before
release.

As part of that testing, with the help of @eeeebbbbrrrr, switch to
loading the test JSON schemas into the test module at compile time
instead of runtime. This fixes an issue where the `CARGO_MANIFEST_DIR`
environment variable was not present while running tests as a different
user. It's more efficient anyway.
  • Loading branch information
theory committed Apr 19, 2024
1 parent 222a8fe commit d7cb0d6
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 32 deletions.
63 changes: 51 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ pg16 = ["pgrx/pg16", "pgrx-tests/pg16" ]
pg_test = []

[dependencies]
pgrx = "=0.11.3"
pgrx = "=0.11.4"
boon = "0.5"
serde_json = "1.0"

[dev-dependencies]
pgrx-tests = "=0.11.3"
pgrx-tests = "=0.11.4"

[profile.dev]
panic = "unwind"
Expand Down
2 changes: 1 addition & 1 deletion META.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"provides": {
"jsonschema": {
"abstract": "JSON Schema validation functions for PostgreSQL",
"file": "jsonschema.control",
"file": "src/lib.rs",
"docfile": "doc/jsonschema.md",
"version": "@CARGO_VERSION@"
}
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include $(PGXS)

all: package

.DEFAULT_GOAL: package # Build jsonschmea for the PostgreSQL cluster identified by pg_config.
.DEFAULT_GOAL: package # Build jsonschema for the PostgreSQL cluster identified by pg_config.
package:
@cargo pgrx package --pg-config "$(PG_CONFIG)"

Expand Down
38 changes: 22 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ mod tests {
use pgrx::pg_sys::panic::CaughtError::PostgresError;
use pgrx::{spi::SpiError, Json, JsonB};
use serde_json::json;
use std::{env, error::Error, fs::File, path::Path};
use std::error::Error;

// Enum used to record handling expected errors.
#[derive(Debug, Eq, PartialEq)]
Expand All @@ -322,11 +322,17 @@ mod tests {
False,
}

// Load the named JSON file into a serde_json::Value.
fn load_json(name: &str) -> Value {
let root_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let root = Path::new(&root_dir);
serde_json::from_reader(File::open(root.join("eg").join(name)).unwrap()).unwrap()
// Load the user and address schemas (bytes loaded at compile time).
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 {
let bytes = include_bytes!("../eg/address.schema.json");
assert!(!bytes.is_empty());
serde_json::from_slice(bytes).unwrap()
}

// Make sure our Draft enum converts properly into boon's.
Expand All @@ -341,8 +347,8 @@ mod tests {

#[test]
fn test_compiles() -> Result<(), Box<dyn Error>> {
let address = load_json("address.schema.json");
let user = load_json("user-profile.schema.json");
let address = addr_schema();
let user = user_schema();

assert!(compiles(
"https://example.com/address.schema.json",
Expand Down Expand Up @@ -370,8 +376,8 @@ mod tests {

#[test]
fn test_new_compiler() -> Result<(), Box<dyn Error>> {
let address = load_json("address.schema.json");
let user = load_json("user-profile.schema.json");
let address = addr_schema();
let user = user_schema();
let id = String::from("https://example.com/user-profile.schema.json");
let c = new_compiler(&id, &[address.clone(), user.clone()]);
assert!(c.is_ok());
Expand Down Expand Up @@ -437,8 +443,8 @@ mod tests {

#[test]
fn test_validate() -> Result<(), Box<dyn Error>> {
let address_schema = load_json("address.schema.json");
let user_schema = load_json("user-profile.schema.json");
let address_schema = addr_schema();
let user_schema = user_schema();
let address = json!({
"postOfficeBox": "123",
"streetAddress": "456 Main St",
Expand Down Expand Up @@ -566,8 +572,8 @@ mod tests {

#[pg_test]
fn test_jsonschema_is_valid_multi() -> spi::Result<()> {
let address_schema = load_json("address.schema.json");
let user_schema = load_json("user-profile.schema.json");
let address_schema = addr_schema();
let user_schema = user_schema();
let address_id = address_schema.get("$id").unwrap().as_str().unwrap();
let user_id = user_schema.get("$id").unwrap().as_str().unwrap();

Expand Down Expand Up @@ -759,8 +765,8 @@ mod tests {

#[pg_test]
fn test_jsonschema_validates_multi() -> spi::Result<()> {
let address_schema = load_json("address.schema.json");
let user_schema = load_json("user-profile.schema.json");
let address_schema = addr_schema();
let user_schema = user_schema();
let address_id = address_schema.get("$id").unwrap().as_str().unwrap();
let user_id = user_schema.get("$id").unwrap().as_str().unwrap();
let address = json!({
Expand Down

0 comments on commit d7cb0d6

Please sign in to comment.