graphql-engine/v3/crates/metadata-resolve/tests/metadata_golden_tests.rs
Daniel Harvey d34c419929 Allow nested metadata-resolve tests (#697)
<!-- Thank you for submitting this PR! :) -->

## Description

We have a set of tests in `metadata-resolve` that uses globs to find
files in either `passing` or `failing` folders and check them. This
allows those files to live in nested folders for tidyness. Functional
no-op.

V3_GIT_ORIGIN_REV_ID: 22d42e986676a110f455c59c3df9e6946c5d996c
2024-06-11 14:11:10 +00:00

70 lines
2.5 KiB
Rust

//! Tests that attempt to resolve different metadata files and assert that they parse successfully
//! or fail in the expected way.
use std::fs;
use std::path::{Path, PathBuf};
use serde_json::Value;
use metadata_resolve::MetadataResolveFlagsInternal;
#[test_each::path(glob = "crates/metadata-resolve/tests/passing/**/", name(segments = 2))]
#[allow(clippy::needless_pass_by_value)] // must receive a `PathBuf`
fn test_passing_metadata(comparison_folder_path: PathBuf) -> anyhow::Result<()> {
let passing_example = comparison_folder_path.join("metadata.json");
let metadata_resolve_flags_internal = MetadataResolveFlagsInternal {
enable_boolean_expression_types: true,
};
let metadata_json_value = read_json(&passing_example)?;
let metadata = open_dds::traits::OpenDd::deserialize(metadata_json_value)?;
let resolved = metadata_resolve::resolve(metadata, &metadata_resolve_flags_internal);
match resolved {
Ok(_) => Ok(()),
Err(msg) => panic!("{msg}"),
}
}
#[test_each::path(glob = "crates/metadata-resolve/tests/failing/**/", name(segments = 2))]
#[allow(clippy::needless_pass_by_value)] // must receive a `PathBuf`
fn test_failing_metadata(comparison_folder_path: PathBuf) -> anyhow::Result<()> {
let failing_example = comparison_folder_path.join("metadata.json");
let failing_reason = comparison_folder_path.join("expected_error.txt");
let metadata_resolve_flags_internal = MetadataResolveFlagsInternal {
enable_boolean_expression_types: true,
};
let error_untrimmed = fs::read_to_string(failing_reason).unwrap();
let error = error_untrimmed.trim();
match read_json(&failing_example) {
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!("Expected to fail with {error}"),
Err(msg) => similar_asserts::assert_eq!(error, msg.to_string()),
}
}
Err(msg) => similar_asserts::assert_eq!(msg.to_string(), error),
};
}
Err(msg) => {
similar_asserts::assert_eq!(msg.to_string(), error);
}
};
Ok(())
}
fn read_json(path: &Path) -> anyhow::Result<Value> {
let json_string = fs::read_to_string(path)?;
let value = serde_json::from_str(&json_string)?;
Ok(value)
}