Skip to content

Commit

Permalink
Finish tests
Browse files Browse the repository at this point in the history
  • Loading branch information
theory committed Sep 6, 2024
1 parent 38bcc7d commit f63fc6f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
1 change: 1 addition & 0 deletions .ci/test-cover
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ grcov "${DESTDIR}" \
--ignore '**/tests/**' \
--ignore 'build.rs' \
--excl-start '#(\[cfg\(test\)\]|\[test\])' \
--excl-line 'unreachable\!\(\)' \
--llvm \
--binary-path "target/debug/" \
-s . \
Expand Down
4 changes: 2 additions & 2 deletions src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,13 @@ impl TryFrom<&[&Value]> for Meta {

// Find the version of the first doc.
let version =
util::get_version(meta[0]).ok_or("no spec version found in first meta value")?;
util::get_version(meta[0]).ok_or("No spec version found in first meta value")?;

// Convert the first doc to v2 if necessary.
let mut v2 = match version {
1 => v1::to_v2(meta[0])?,
2 => meta[0].clone(),
_ => return Err(Box::from(format!("Unknown meta version {version}"))),
_ => unreachable!(),
};

// Merge them.
Expand Down
83 changes: 67 additions & 16 deletions src/meta/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ fn test_try_merge_v1() -> Result<(), Box<dyn Error>> {
let contents: Value = serde_json::from_reader(File::open(&widget_file)?)?;

// expect maps a JSON pointer to an expected value.
for (name, patch, expect) in [
for (name, patches, expect) in [
(
"license",
json!({"license": "MIT"}),
vec![json!({"license": "MIT"})],
json!({"/license": "MIT"}),
),
(
"tle",
json!({"contents": {"extensions": {"widget": {"tle": true}}}}),
vec![json!({"contents": {"extensions": {"widget": {"tle": true}}}})],
json!({"/contents/extensions/widget": {
"control": "widget.control",
"sql": "sql/widget.sql.in",
Expand All @@ -131,28 +131,28 @@ fn test_try_merge_v1() -> Result<(), Box<dyn Error>> {
),
(
"categories",
json!({"classifications": {"categories": ["Analytics", "Connectors"]}}),
vec![json!({"classifications": {"categories": ["Analytics", "Connectors"]}})],
json!({"/classifications/categories": ["Analytics", "Connectors"]}),
),
(
"tags",
json!({"classifications": {"tags": ["hi", "go", "ick"]}}),
vec![json!({"classifications": {"tags": ["hi", "go", "ick"]}})],
json!({"/classifications/tags": ["hi", "go", "ick"]}),
),
(
"resources",
json!({"resources": {
vec![json!({"resources": {
"issues": "https://example.com/issues",
"repository": "https://example.com/repo",
}}),
}})],
json!({"/resources": {
"homepage": "http://widget.example.org/",
"issues": "https://example.com/issues",
"repository": "https://example.com/repo",
}}),
),
] {
run_merge_case(name, &contents, &patch, &expect)?;
run_merge_case(name, &contents, patches.as_slice(), &expect)?;
}

Ok(())
Expand All @@ -166,23 +166,34 @@ fn test_try_merge_v2() -> Result<(), Box<dyn Error>> {
let contents: Value = serde_json::from_reader(File::open(&widget_file)?)?;

// expect maps a JSON pointer to an expected value.
for (name, patch, expect) in [
for (name, patches, expect) in [
(
"license",
json!({"license": "MIT"}),
vec![json!({"license": "MIT"})],
json!({"/license": "MIT"}),
),
(
"tle",
json!({"contents": {"extensions": {"pair": {"tle": true}}}}),
vec![json!({"contents": {"extensions": {"pair": {"tle": true}}}})],
json!({"/contents/extensions/pair": {
"sql": "sql/pair.sql",
"control": "pair.control",
"tle": true,
}}),
),
(
"multiple patches",
vec![
json!({"license": "MIT"}),
json!({"classifications": {"categories": ["Analytics", "Connectors"]}}),
],
json!({
"/license": "MIT",
"/classifications/categories": ["Analytics", "Connectors"],
}),
),
] {
run_merge_case(name, &contents, &patch, &expect)?;
run_merge_case(name, &contents, patches.as_slice(), &expect)?;
}

Ok(())
Expand All @@ -191,17 +202,20 @@ fn test_try_merge_v2() -> Result<(), Box<dyn Error>> {
fn run_merge_case(
name: &str,
orig: &Value,
patch: &Value,
patches: &[Value],
expect: &Value,
) -> Result<(), Box<dyn Error>> {
let meta = vec![orig, patch];
let mut meta = vec![orig];
for p in patches {
meta.push(p);
}
match Meta::try_from(meta.as_slice()) {
Err(e) => panic!("widget.json patching {name} failed: {e}"),
Err(e) => panic!("patching {name} failed: {e}"),
Ok(m) => {
// Convert the Meta object to JSON.
let output: Result<Value, Box<dyn Error>> = m.try_into();
match output {
Err(e) => panic!("widget.json {name} serialization failed: {e}"),
Err(e) => panic!("{name} serialization failed: {e}"),
Ok(val) => {
// Compare expected values at pointers.
for (p, v) in expect.as_object().unwrap() {
Expand All @@ -214,3 +228,40 @@ fn run_merge_case(

Ok(())
}

#[test]
fn test_try_merge_err() -> Result<(), Box<dyn Error>> {
// Load invalid meta.
let dir: PathBuf = [env!("CARGO_MANIFEST_DIR"), "corpus"].iter().collect();
let widget_file = dir.join("invalid.json");
let invalid: Value = serde_json::from_reader(File::open(&widget_file)?)?;

let empty = json!({});
let bad_version = json!({"meta-spec": { "version": null}});

for (name, arg, err) in [
("no meta", vec![], "meta contains no values"),
(
"no version",
vec![&empty],
"No spec version found in first meta value",
),
(
"bad version",
vec![&bad_version],
"No spec version found in first meta value",
),
(
"invalid",
vec![&invalid],
"jsonschema validation failed with https://pgxn.org/meta/v2/distribution.schema.json#\n- at '': missing properties 'version'",
),
] {
match Meta::try_from(arg.as_slice()) {
Ok(_) => panic!("patching {name} unexpectedly succeeded"),
Err(e) => assert_eq!(err, e.to_string(), "{name}"),
}
}

Ok(())
}

0 comments on commit f63fc6f

Please sign in to comment.