Skip to content

Commit

Permalink
Add more not tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acerbusace committed Nov 8, 2024
1 parent 2eab677 commit 2e7a553
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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" => {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2e7a553

Please sign in to comment.