graphql-engine/v3/crates/metadata-resolve/tests/metadata_golden_tests.rs
Daniel Harvey 0123c558f1 Enable BooleanExpressionTypes (#783)
<!-- The PR description should answer 2 (maybe 3) important questions:
-->

### What

# BooleanExpressionType

A new metadata kind `BooleanExpressionType` can now be defined. These
can be used in place of `ObjectBooleanExpressionType` and
`DataConnectorScalarRepresentation`, and allow more granular control of
comparison operators and how they are used.

The old metadata types still work, but will eventually be deprecated.

```yaml
kind: BooleanExpressionType
version: v1
definition:
  name: album_bool_exp
  operand:
    object:
      type: Album
      comparableFields:
        - fieldName: AlbumId
          booleanExpressionType: pg_int_comparison_exp
        - fieldName: ArtistId
          booleanExpressionType: pg_int_comparison_exp_with_is_null
        - field: Address
          booleanExpressionType: address_bool_exp
      comparableRelationships:
        - relationshipName: Artist
          booleanExpressionType: artist_bool_exp
  logicalOperators:
    enable: true
  isNull:
    enable: true
  graphql:
    typeName: app_album_bool_exp
```

```yaml
kind: BooleanExpressionType
version: v1
definition:
  name: pg_int_comparison_exp
  operand:
    scalar:
      type: Int
      comparisonOperators:
        - name: equals
          argumentType: String!
        - name: _in
          argumentType: [String!]!
      dataConnectorOperatorMapping:
        - dataConnectorName: postgres_db
          dataConnectorScalarType: String
          operatorMapping:
            equals: _eq
  logicalOperators:
    enable: true
  isNull:
    enable: true
  graphql:
    typeName: app_postgres_int_bool_exp
```

<!-- What is this PR trying to accomplish (and why, if it's not
obvious)? -->

<!-- Consider: do we need to add a changelog entry? -->

### How

Remove feature flag, unhide JsonSchema items, fix a few missing bits of
JsonSchema the tests didn't warn us about before.

<!-- How is it trying to accomplish it (what are the implementation
steps)? -->

V3_GIT_ORIGIN_REV_ID: dd3055d926fdeb7446cd57085679f2492a4358a1
2024-07-01 15:29:14 +00:00

103 lines
4.0 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;
use metadata_resolve::configuration;
#[test]
fn test_passing_metadata() {
insta::glob!("passing/**/metadata.json", |path| {
let directory = path.parent().unwrap();
insta::with_settings!({
snapshot_path => directory,
snapshot_suffix => "",
prepend_module_to_snapshot => false,
}, {
let configuration = read_test_configuration(directory)
.unwrap_or_else(|error| panic!("{}: Could not read configuration: {error}",directory.display()));
let metadata_json_text = std::fs::read_to_string(path)
.unwrap_or_else(|error| panic!("{}: Could not read file {path:?}: {error}", directory.display()));
let metadata_json_value = serde_json::from_str(&metadata_json_text)
.unwrap_or_else(|error| panic!("{}: Could not parse JSON: {error}",directory.display()));
let metadata = open_dds::traits::OpenDd::deserialize(metadata_json_value)
.unwrap_or_else(|error| panic!("{}: Could not deserialize metadata: {error}", directory.display()));
let resolved = metadata_resolve::resolve(metadata, configuration)
.unwrap_or_else(|error| panic!("{}: Could not resolve metadata: {error}",directory.display()));
insta::assert_debug_snapshot!("resolved", resolved);
});
});
}
#[test]
fn test_failing_metadata() {
insta::glob!("failing/**/metadata.json", |path| {
let directory = path.parent().unwrap();
insta::with_settings!({
snapshot_path => directory,
snapshot_suffix => "",
prepend_module_to_snapshot => false,
}, {
let configuration = read_test_configuration(directory)
.unwrap_or_else(|error| panic!("{}: Could not read configuration: {error}", directory.display()));
let metadata_json_text = std::fs::read_to_string(path)
.unwrap_or_else(|error| panic!("{}: Could not read file {path:?}: {error}", directory.display()));
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, configuration) {
Ok(_) => {
panic!("{}: Unexpected success when resolving {path:?}.", directory.display());
}
Err(msg) => {
insta::assert_snapshot!("resolve_error", msg);
}
}
}
Err(msg) => {
insta::assert_snapshot!("deserialize_error", msg);
}
};
}
Err(msg) => {
insta::assert_snapshot!("parse_error", msg);
}
};
});
});
}
fn read_test_configuration(
directory: &Path,
) -> Result<configuration::Configuration, Box<dyn std::error::Error>> {
let unstable_features = configuration::UnstableFeatures {
enable_order_by_expressions: false,
enable_ndc_v02_support: false,
};
let configuration_path = directory.join("configuration.json");
if configuration_path.exists() {
let reader = fs::File::open(configuration_path)?;
let configuration = serde_json::from_reader(reader)?;
Ok(configuration::Configuration {
unstable_features,
..configuration
})
} else {
Ok(configuration::Configuration {
allow_unknown_subgraphs: false,
unstable_features,
})
}
}