Skip to content

Commit

Permalink
test: add more integration tests for expressions that require escapin…
Browse files Browse the repository at this point in the history
…g field names (#114)

I wanted to make another pass on test coverage for the recent changes I made, especially to `make_selector.rs`.
  • Loading branch information
hallettj authored Oct 29, 2024
1 parent e7cba70 commit f9aad06
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 23 deletions.
122 changes: 115 additions & 7 deletions crates/integration-tests/src/tests/expressions.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,45 @@
use insta::assert_yaml_snapshot;
use ndc_models::ExistsInCollection;
use ndc_models::{ExistsInCollection, Expression};
use ndc_test_helpers::{
asc, binop, exists, field, query, query_request, relation_field, relationship, target, value,
array, asc, binop, exists, field, object, query, query_request, relation_field, relationship,
target, value,
};

use crate::{connector::Connector, graphql_query, run_connector_query};

#[tokio::test]
async fn evaluates_field_name_that_requires_escaping_in_nested_expression() -> anyhow::Result<()> {
async fn evaluates_field_name_that_requires_escaping() -> anyhow::Result<()> {
assert_yaml_snapshot!(
graphql_query(
r#"
query Filtering {
extendedJsonTestData(where: { value: { _regex: "hello" } }) {
type
value
query {
testCases_weirdFieldNames(where: { invalidName: { _eq: 3 } }) {
invalidName
}
}
"#
)
.run()
.await?
);
Ok(())
}

#[tokio::test]
async fn evaluates_field_name_that_requires_escaping_in_complex_expression() -> anyhow::Result<()> {
assert_yaml_snapshot!(
graphql_query(
r#"
query {
testCases_weirdFieldNames(
where: {
_and: [
{ invalidName: { _gt: 2 } },
{ invalidName: { _lt: 4 } }
]
}
) {
invalidName
}
}
"#
Expand Down Expand Up @@ -55,3 +80,86 @@ async fn evaluates_exists_with_predicate() -> anyhow::Result<()> {
);
Ok(())
}

#[tokio::test]
async fn exists_with_predicate_with_escaped_field_name() -> anyhow::Result<()> {
assert_yaml_snapshot!(
run_connector_query(
Connector::TestCases,
query_request().collection("weird_field_names").query(
query()
.predicate(exists(
ExistsInCollection::NestedCollection {
column_name: "$invalid.array".into(),
arguments: Default::default(),
field_path: Default::default(),
},
binop("_lt", target!("$invalid.element"), value!(3)),
))
.fields([
field!("_id"),
field!("invalid_array" => "$invalid.array", array!(object!([
field!("invalid_element" => "$invalid.element")
])))
])
.order_by([asc!("$invalid.name")]),
)
)
.await?
);
Ok(())
}

#[tokio::test]
async fn exists_in_nested_collection_without_predicate() -> anyhow::Result<()> {
assert_yaml_snapshot!(
run_connector_query(
Connector::TestCases,
query_request().collection("nested_collection").query(
query()
.predicate(Expression::Exists {
in_collection: ExistsInCollection::NestedCollection {
column_name: "staff".into(),
arguments: Default::default(),
field_path: Default::default(),
},
predicate: None,
})
.fields([field!("_id"), field!("institution")])
.order_by([asc!("institution")]),
)
)
.await?
);
Ok(())
}

#[tokio::test]
async fn exists_in_nested_collection_without_predicate_with_escaped_field_name(
) -> anyhow::Result<()> {
assert_yaml_snapshot!(
run_connector_query(
Connector::TestCases,
query_request().collection("weird_field_names").query(
query()
.predicate(Expression::Exists {
in_collection: ExistsInCollection::NestedCollection {
column_name: "$invalid.array".into(),
arguments: Default::default(),
field_path: Default::default(),
},
predicate: None,
})
.fields([
field!("_id"),
field!("invalid_array" => "$invalid.array", array!(object!([
field!("invalid_element" => "$invalid.element")
])))
])
.order_by([asc!("$invalid.name")]),
)
)
.await?
);
Ok(())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/integration-tests/src/tests/expressions.rs
expression: "graphql_query(r#\"\n query {\n testCases_weirdFieldNames(where: { invalidName: { _eq: 3 } }) {\n invalidName\n }\n }\n \"#).run().await?"
---
data:
testCases_weirdFieldNames:
- invalidName: 3
errors: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: crates/integration-tests/src/tests/expressions.rs
expression: "graphql_query(r#\"\n query {\n testCases_weirdFieldNames(\n where: { \n _and: [\n { invalidName: { _gt: 2 } },\n { invalidName: { _lt: 4 } } \n ] \n }\n ) {\n invalidName\n }\n }\n \"#).run().await?"
---
data:
testCases_weirdFieldNames:
- invalidName: 3
errors: ~

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/integration-tests/src/tests/expressions.rs
expression: "run_connector_query(Connector::TestCases,\n query_request().collection(\"nested_collection\").query(query().predicate(Expression::Exists {\n in_collection: ExistsInCollection::NestedCollection {\n column_name: \"staff\".into(),\n arguments: Default::default(),\n field_path: Default::default(),\n },\n predicate: None,\n }).fields([field!(\"_id\"),\n field!(\"institution\")]).order_by([asc!(\"institution\")]))).await?"
---
- rows:
- _id: 6705a1cec2df58ace3e67807
institution: Aperture Science
- _id: 6705a1c2c2df58ace3e67806
institution: Black Mesa
- _id: 6705a1d7c2df58ace3e67808
institution: City 17
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/integration-tests/src/tests/expressions.rs
expression: "run_connector_query(Connector::TestCases,\n query_request().collection(\"weird_field_names\").query(query().predicate(Expression::Exists {\n in_collection: ExistsInCollection::NestedCollection {\n column_name: \"$invalid.array\".into(),\n arguments: Default::default(),\n field_path: Default::default(),\n },\n predicate: None,\n }).fields([field!(\"_id\"),\n field!(\"invalid_array\" => \"$invalid.array\",\n array!(object!([field!(\"invalid_element\" =>\n \"$invalid.element\")])))]).order_by([asc!(\"$invalid.name\")]))).await?"
---
- rows:
- _id: 66cf91a0ec1dfb55954378bd
invalid_array:
- invalid_element: 1
- _id: 66cf9230ec1dfb55954378be
invalid_array:
- invalid_element: 2
- _id: 66cf9274ec1dfb55954378bf
invalid_array:
- invalid_element: 3
- _id: 66cf9295ec1dfb55954378c0
invalid_array:
- invalid_element: 4
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: crates/integration-tests/src/tests/expressions.rs
expression: "run_connector_query(Connector::TestCases,\n query_request().collection(\"weird_field_names\").query(query().predicate(exists(ExistsInCollection::NestedCollection {\n column_name: \"$invalid.array\".into(),\n arguments: Default::default(),\n field_path: Default::default(),\n },\n binop(\"_lt\", target!(\"$invalid.element\"),\n value!(3)))).fields([field!(\"_id\"),\n field!(\"invalid_array\" => \"$invalid.array\",\n array!(object!([field!(\"invalid_element\" =>\n \"$invalid.element\")])))]).order_by([asc!(\"$invalid.name\")]))).await?"
---
- rows:
- _id: 66cf91a0ec1dfb55954378bd
invalid_array:
- invalid_element: 1
- _id: 66cf9230ec1dfb55954378be
invalid_array:
- invalid_element: 2
16 changes: 16 additions & 0 deletions fixtures/hasura/test_cases/connector/schema/weird_field_names.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"objectTypes": {
"weird_field_names": {
"fields": {
"$invalid.array": {
"type": {
"arrayOf": {
"object": "weird_field_names_$invalid.array"
}
}
},
"$invalid.name": {
"type": {
"scalar": "int"
Expand All @@ -30,6 +37,15 @@
}
}
},
"weird_field_names_$invalid.array": {
"fields": {
"$invalid.element": {
"type": {
"scalar": "int"
}
}
}
},
"weird_field_names_$invalid.object.name": {
"fields": {
"valid_name": {
Expand Down
6 changes: 3 additions & 3 deletions fixtures/mongodb/test_cases/nested_collection.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{ "institution": "Black Mesa", "staff": [{ "name": "Freeman" }, { "name": "Calhoun" }] }
{ "institution": "Aperture Science", "staff": [{ "name": "GLaDOS" }, { "name": "Chell" }] }
{ "institution": "City 17", "staff": [{ "name": "Alyx" }, { "name": "Freeman" }, { "name": "Breen" }] }
{ "_id": { "$oid": "6705a1c2c2df58ace3e67806" }, "institution": "Black Mesa", "staff": [{ "name": "Freeman" }, { "name": "Calhoun" }] }
{ "_id": { "$oid": "6705a1cec2df58ace3e67807" }, "institution": "Aperture Science", "staff": [{ "name": "GLaDOS" }, { "name": "Chell" }] }
{ "_id": { "$oid": "6705a1d7c2df58ace3e67808" }, "institution": "City 17", "staff": [{ "name": "Alyx" }, { "name": "Freeman" }, { "name": "Breen" }] }
8 changes: 4 additions & 4 deletions fixtures/mongodb/test_cases/weird_field_names.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ "_id": { "$oid": "66cf91a0ec1dfb55954378bd" }, "$invalid.name": 1, "$invalid.object.name": { "valid_name": 1 }, "valid_object_name": { "$invalid.nested.name": 1 } }
{ "_id": { "$oid": "66cf9230ec1dfb55954378be" }, "$invalid.name": 2, "$invalid.object.name": { "valid_name": 2 }, "valid_object_name": { "$invalid.nested.name": 2 } }
{ "_id": { "$oid": "66cf9274ec1dfb55954378bf" }, "$invalid.name": 3, "$invalid.object.name": { "valid_name": 3 }, "valid_object_name": { "$invalid.nested.name": 3 } }
{ "_id": { "$oid": "66cf9295ec1dfb55954378c0" }, "$invalid.name": 4, "$invalid.object.name": { "valid_name": 4 }, "valid_object_name": { "$invalid.nested.name": 4 } }
{ "_id": { "$oid": "66cf91a0ec1dfb55954378bd" }, "$invalid.name": 1, "$invalid.object.name": { "valid_name": 1 }, "valid_object_name": { "$invalid.nested.name": 1 }, "$invalid.array": [{ "$invalid.element": 1 }] }
{ "_id": { "$oid": "66cf9230ec1dfb55954378be" }, "$invalid.name": 2, "$invalid.object.name": { "valid_name": 2 }, "valid_object_name": { "$invalid.nested.name": 2 }, "$invalid.array": [{ "$invalid.element": 2 }] }
{ "_id": { "$oid": "66cf9274ec1dfb55954378bf" }, "$invalid.name": 3, "$invalid.object.name": { "valid_name": 3 }, "valid_object_name": { "$invalid.nested.name": 3 }, "$invalid.array": [{ "$invalid.element": 3 }] }
{ "_id": { "$oid": "66cf9295ec1dfb55954378c0" }, "$invalid.name": 4, "$invalid.object.name": { "valid_name": 4 }, "valid_object_name": { "$invalid.nested.name": 4 }, "$invalid.array": [{ "$invalid.element": 4 }] }

0 comments on commit f9aad06

Please sign in to comment.