graphql-engine/v3/crates/metadata-resolve/tests/metadata_golden_tests.rs
Samir Talwar 63ac02bc07 Take snapshots of passing and failing metadata resolution. (#763)
### What

In order to more easily monitor and review changes to metadata
resolution, this introduces snapshot testing for both successful and
failing calls to `resolve`. I used [Insta](https://insta.rs/) for this.

### How

For tests of the failure case, we already had a text file with the
expected error, so I have turned those files into snapshot files. I
wrote a small script to move the files rather than deleting and
recreating them so I could guarantee that the contents have not changed.
(Unfortunately, Git's diff doesn't always recognise the move as a move
because Insta has added a header.)

For tests of the successful case, I added a line to snapshot the
metadata rather than discarding it.

I also rewrote the tests to use `insta::glob` so we could get rid of
`test_each`.

V3_GIT_ORIGIN_REV_ID: 41bef4cf77bddb8d20d7c101df52ae149e8b0476
2024-06-26 08:15:29 +00:00

78 lines
3.1 KiB
Rust

//! Tests that attempt to resolve different metadata files and assert that they parse successfully
//! or fail in the expected way.
use metadata_resolve::MetadataResolveFlagsInternal;
#[test]
fn test_passing_metadata() {
insta::glob!("passing/**/metadata.json", |path| {
insta::with_settings!({
snapshot_path => path.parent().unwrap(),
snapshot_suffix => "",
prepend_module_to_snapshot => false,
}, {
let metadata_resolve_flags_internal = MetadataResolveFlagsInternal {
enable_boolean_expression_types: true,
enable_aggregate_relationships: true,
};
let metadata_json_text = std::fs::read_to_string(path)
.unwrap_or_else(|error| panic!("Could not read file {path:?}: {error}"));
let metadata_json_value = serde_json::from_str(&metadata_json_text)
.unwrap_or_else(|error| panic!("Could not parse JSON: {error}"));
let metadata = open_dds::traits::OpenDd::deserialize(metadata_json_value)
.unwrap_or_else(|error| panic!("Could not deserialize metadata: {error}"));
let resolved = metadata_resolve::resolve(metadata, metadata_resolve_flags_internal)
.unwrap_or_else(|error| panic!("Could not resolve metadata: {error}"));
insta::assert_debug_snapshot!("resolved", resolved);
});
});
}
#[test]
fn test_failing_metadata() {
insta::glob!("failing/**/metadata.json", |path| {
insta::with_settings!({
snapshot_path => path.parent().unwrap(),
snapshot_suffix => "",
prepend_module_to_snapshot => false,
}, {
let metadata_resolve_flags_internal = MetadataResolveFlagsInternal {
enable_boolean_expression_types: true,
enable_aggregate_relationships: true,
};
let metadata_json_text = std::fs::read_to_string(path)
.unwrap_or_else(|error| panic!("Could not read file {path:?}: {error}"));
match serde_json::from_str(&metadata_json_text) {
Ok(metadata_json_value) => {
match open_dds::traits::OpenDd::deserialize(metadata_json_value) {
Ok(metadata) => {
match metadata_resolve::resolve(metadata, metadata_resolve_flags_internal) {
Ok(_) => {
panic!("Unexpected success when resolving {path:?}.");
}
Err(msg) => {
insta::assert_snapshot!("resolve_error", msg);
}
}
}
Err(msg) => {
insta::assert_snapshot!("deserialize_error", msg);
}
};
}
Err(msg) => {
insta::assert_snapshot!("parse_error", msg);
}
};
});
});
}