From 2e7a5534d76622b6fd9a9003cfba842642c6a39e Mon Sep 17 00:00:00 2001 From: Alex Patel Date: Fri, 8 Nov 2024 11:02:22 -0500 Subject: [PATCH] Add more not tests --- .../graphql/datastore_query/filtering_spec.rb | 22 ++++++++++++------- .../graphql/datastore_query/filtering_spec.rb | 9 ++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/elasticgraph-graphql/spec/integration/elastic_graph/graphql/datastore_query/filtering_spec.rb b/elasticgraph-graphql/spec/integration/elastic_graph/graphql/datastore_query/filtering_spec.rb index e9f64415..3cd97594 100644 --- a/elasticgraph-graphql/spec/integration/elastic_graph/graphql/datastore_query/filtering_spec.rb +++ b/elasticgraph-graphql/spec/integration/elastic_graph/graphql/datastore_query/filtering_spec.rb @@ -898,7 +898,6 @@ def search_datastore(**options, &before_msearch) expect(search_with_filter("id", "equal_to_any_of", [])).to eq [] expect(search_with_filter("id", "any_of", [])).to eq [] - expect(search_with_freeform_filter({"not" => {"any_of" => []}})).to eq ids_of(widget1, widget2) expect(search_with_filter("id", "any_of", [{"any_of" => []}])).to eq [] expect(search_with_filter("id", "equal_to_any_of", nil)).to eq ids_of(widget1, widget2) @@ -913,8 +912,6 @@ def search_datastore(**options, &before_msearch) expect(search_with_freeform_filter({"id" => {}})).to eq ids_of(widget1, widget2) expect(search_with_freeform_filter({"any_of" => [{"id" => nil}]})).to eq ids_of(widget1, widget2) expect(search_with_freeform_filter({"any_of" => [{"id" => nil}, {"amount_cents" => {"lt" => 0}}]})).to eq ids_of(widget1, widget2) - expect(search_with_freeform_filter({"not" => {"any_of" => [{"id" => nil}]}})).to eq [] - expect(search_with_freeform_filter({"not" => {"any_of" => [{"id" => nil}, {"amount_cents" => {"lt" => 1000}}]}})).to eq [] end specify "`not: {any_of: []}` matches all documents, but `not: {any_of: [field: nil, ...]}` is treated as `false` matching no documents" do @@ -925,10 +922,10 @@ def search_datastore(**options, &before_msearch) ) expect(search_with_freeform_filter({"not" => {"any_of" => []}})).to eq ids_of(widget1, widget2) + expect(search_with_freeform_filter({"not" => {"not" => {"any_of" => []}}})).to eq [] - # pending "`not` doesn't yet compose correctly with an `any_of: [{}, ...]`, so these currently fail" expect(search_with_freeform_filter({"not" => {"any_of" => [{"id" => nil}]}})).to eq [] - expect(search_with_freeform_filter({"not" => {"any_of" => [{"id" => nil}, {"amount_cents" => {"lt" => 0}}]}})).to eq [] + expect(search_with_freeform_filter({"not" => {"any_of" => [{"id" => nil}, {"amount_cents" => {"lt" => 1000}}]}})).to eq [] end it "`equal_to_any_of:` with `nil` matches documents with null values or not null values" do @@ -1180,9 +1177,9 @@ def search_datastore(**options, &before_msearch) it "returns the standard always false filter when set to nil" do index_into( graphql, - widget1 = build(:widget, amount_cents: 100), - widget2 = build(:widget, amount_cents: 205), - widget3 = build(:widget, amount_cents: 400) + build(:widget, amount_cents: 100), + build(:widget, amount_cents: 205), + build(:widget, amount_cents: 400) ) inner_not_result1 = search_with_freeform_filter({"amount_cents" => { @@ -1215,9 +1212,18 @@ def search_datastore(**options, &before_msearch) "amount_cents" => {"equal_to_any_of" => nil} }}) + inner_not_result4 = search_with_freeform_filter({"amount_cents" => { + "not" => {} + }}) + + outer_not_result4 = search_with_freeform_filter({"not" => { + "amount_cents" => {} + }}) + expect(inner_not_result1).to eq(outer_not_result1).and eq [] expect(inner_not_result2).to eq(outer_not_result2).and eq [] expect(inner_not_result3).to eq(outer_not_result3).and eq [] + expect(inner_not_result4).to eq(outer_not_result4).and eq [] end it "is pruned when set to nil inside `any_of` treating it as `true`" do diff --git a/elasticgraph-graphql/spec/unit/elastic_graph/graphql/datastore_query/filtering_spec.rb b/elasticgraph-graphql/spec/unit/elastic_graph/graphql/datastore_query/filtering_spec.rb index 602c2276..40bd9a09 100644 --- a/elasticgraph-graphql/spec/unit/elastic_graph/graphql/datastore_query/filtering_spec.rb +++ b/elasticgraph-graphql/spec/unit/elastic_graph/graphql/datastore_query/filtering_spec.rb @@ -1485,6 +1485,15 @@ def expect_script_query_with_params(query, expected_params) expect(datastore_body_of(query)).to query_datastore_with({bool: {must_not: [always_false_condition]}}) end + it "filters to a false condition when given `not: {not: {any_of: []}}` on a sub field" do + query = new_query(filter: { + "age" => {"not" => {"not" => {"any_of" => []}}} + }) + + expect(datastore_body_of(query)).to query_datastore_with(always_false_condition) + end + + # Note: the GraphQL schema does not allow `any_of: {}` (`any_of` is a list field). However, we're testing # it here for completeness--as a defense-in-depth measure, it's good for the filter interpreter to handle # whatever is thrown at it. Including these tests allows us to exercise an edge case in the code that