Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filters on nested provider/aliased fields #285

Merged
merged 2 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion optimade/server/mappers/entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ def alias_for(cls, field: str) -> str:
:return: Aliased field as found in PROVIDER_ALIASES + ALIASES
:rtype: str
"""
return dict(cls.all_aliases()).get(field, field)
split = field.split(".")
alias = dict(cls.all_aliases()).get(split[0], None)
if alias is not None:
return alias + ("." + ".".join(split[1:]) if len(split) > 1 else "")
return field

@classmethod
def get_required_fields(cls) -> set:
Expand Down
12 changes: 12 additions & 0 deletions tests/filtertransformers/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,7 @@ class MyStructureMapper(BaseResourceMapper):
("A", "D"),
("B", "E"),
("C", "F"),
("_exmpl_nested_field", "nested_field"),
)

mapper = MyStructureMapper()
Expand All @@ -564,6 +565,17 @@ class MyStructureMapper(BaseResourceMapper):
test_filter = ["A", "elements", "C"]
self.assertEqual(t.postprocess(test_filter), ["A", "elements", "C"])

test_filter = {"_exmpl_nested_field.sub_property": {"$gt": 1234.5}}
self.assertEqual(
t.postprocess(test_filter), {"nested_field.sub_property": {"$gt": 1234.5}}
)

test_filter = {"_exmpl_nested_field.sub_property.x": {"$exists": False}}
self.assertEqual(
t.postprocess(test_filter),
{"nested_field.sub_property.x": {"$exists": False}},
)

def test_list_properties(self):
""" Test the HAS ALL, ANY and optional ONLY queries.

Expand Down
33 changes: 33 additions & 0 deletions tests/server/test_mappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,36 @@ class MyMapper(BaseResourceMapper):
toy_collection = mongomock.MongoClient()["fake"]["fake"]
with self.assertRaises(RuntimeError):
MongoCollection(toy_collection, StructureResource, mapper)

def test_property_aliases(self):
class MyMapper(BaseResourceMapper):
PROVIDER_FIELDS = ("dft_parameters", "test_field")
LENGTH_ALIASES = (("_exmpl_test_field", "test_field_len"),)
ALIASES = (("field", "completely_different_field"),)

mapper = MyMapper()
self.assertEqual(mapper.alias_for("_exmpl_dft_parameters"), "dft_parameters")
self.assertEqual(mapper.alias_for("_exmpl_test_field"), "test_field")
self.assertEqual(mapper.alias_for("field"), "completely_different_field")
self.assertEqual(mapper.length_alias_for("_exmpl_test_field"), "test_field_len")
self.assertEqual(mapper.length_alias_for("test_field"), None)
self.assertEqual(mapper.alias_for("test_field"), "test_field")

# nested properties
self.assertEqual(
mapper.alias_for("_exmpl_dft_parameters.nested.property"),
"dft_parameters.nested.property",
)
self.assertEqual(
mapper.alias_for("_exmpl_dft_parameters.nested_property"),
"dft_parameters.nested_property",
)

# test nonsensical query
self.assertEqual(mapper.alias_for("_exmpl_test_field."), "test_field.")

# test an awkward case that has no alias
self.assertEqual(
mapper.alias_for("_exmpl_dft_parameters_dft_parameters.nested.property"),
"_exmpl_dft_parameters_dft_parameters.nested.property",
)