Skip to content

Commit

Permalink
Unwind changes from 8050405 (discussion in #807)
Browse files Browse the repository at this point in the history
- Skip unknown length test for mongomock, temporarily
  • Loading branch information
ml-evs committed May 31, 2021
1 parent ff1c452 commit a7e1166
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
7 changes: 0 additions & 7 deletions optimade/filtertransformers/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,6 @@ def property_first_comparison(self, quantity, query):

return final_query

# Another workaround: in the case that there is no length alias, and the field
# itself does not exist in the database, then `{"$size": 1}` matches all documents,
# so we must add another existence check
if "$exists" not in query:
query["$exists"] = True

return {quantity: query}

def constant_first_comparison(self, arg):
Expand Down Expand Up @@ -316,7 +310,6 @@ def replace_with_relationship(subdict, prop, expr):
{
f"relationships.{_prop}.data": {
"$size": expr.pop("$size"),
"$exists": expr.pop("$exists"),
}
},
{f"relationships.{_prop}.data.{_field}": expr},
Expand Down
14 changes: 5 additions & 9 deletions tests/filtertransformers/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def test_filtering_on_relationships(self, mapper):

assert t.transform(p.parse('structures.id HAS ONLY "dummy/2019"')) == {
"$and": [
{"relationships.structures.data": {"$size": 1, "$exists": True}},
{"relationships.structures.data": {"$size": 1}},
{"relationships.structures.data.id": {"$all": ["dummy/2019"]}},
]
}
Expand All @@ -307,7 +307,6 @@ def test_filtering_on_relationships(self, mapper):
{
"relationships.structures.data": {
"$size": 1,
"$exists": True,
}
},
{"relationships.structures.data.id": {"$all": ["dummy/2019"]}},
Expand Down Expand Up @@ -346,9 +345,7 @@ def test_not_implemented(self):
with pytest.raises(VisitError, match="not implemented"):
self.transform("list HAS ANY > 3, < 6")

assert self.transform("list LENGTH 3") == {
"list": {"$size": 3, "$exists": True}
}
assert self.transform("list LENGTH 3") == {"list": {"$size": 3}}

with pytest.raises(VisitError):
self.transform("list:list HAS >=2:<=5")
Expand Down Expand Up @@ -461,7 +458,7 @@ def test_unaliased_length_operator(self):
"cartesian_site_positions.3": {"$exists": False}
}
assert self.transform("cartesian_site_positions LENGTH 3") == {
"cartesian_site_positions": {"$size": 3, "$exists": True}
"cartesian_site_positions": {"$size": 3}
}
assert self.transform("cartesian_site_positions LENGTH >= 10") == {
"cartesian_site_positions.10": {"$exists": True}
Expand Down Expand Up @@ -536,7 +533,7 @@ class MyMapper(mapper("StructureMapper")):
) == {"nsites": 3}

assert transformer.transform(parser.parse("elements_ratios LENGTH 3")) == {
"ratios": {"$size": 3, "$exists": True}
"ratios": {"$size": 3}
}

assert transformer.transform(
Expand Down Expand Up @@ -612,7 +609,7 @@ class MyStructureMapper(mapper("StructureMapper")):
def test_list_properties(self):
"""Test the HAS ALL, ANY and optional ONLY queries."""
assert self.transform('elements HAS ONLY "H","He","Ga","Ta"') == {
"elements": {"$all": ["H", "He", "Ga", "Ta"], "$size": 4, "$exists": True}
"elements": {"$all": ["H", "He", "Ga", "Ta"], "$size": 4}
}

assert self.transform('elements HAS ANY "H","He","Ga","Ta"') == {
Expand All @@ -634,7 +631,6 @@ def test_list_properties(self):
"elements": {
"$all": ["H", "He", "Ga", "Ta"],
"$size": 4,
"$exists": True,
}
},
{"elements": {"$in": ["H", "He", "Ga", "Ta"]}},
Expand Down
7 changes: 7 additions & 0 deletions tests/server/query_params/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ def test_filter_on_relationships(check_response, check_error_response):
)


@pytest.mark.xfail(
CONFIG.database_backend == SupportedBackend.MONGOMOCK,
reason=(
"mongomock<=3.22.1 has a bug that causes {'$size': 1} queries on missing fields to match all documents: "
"(https://github.com/mongomock/mongomock/issues/710). This check can be removed once mongomock 3.22.2 has been released."
),
)
def test_filter_on_unknown_fields(check_response, check_error_response):

request = "/structures?filter=unknown_field = 1"
Expand Down

0 comments on commit a7e1166

Please sign in to comment.