Skip to content

Commit

Permalink
Merge pull request #183 from Materials-Consortia/ml-evs/parser_issues
Browse files Browse the repository at this point in the history
Fix errors parsing IDs that contain slashes
  • Loading branch information
ml-evs committed Mar 4, 2020
2 parents 23159c7 + 5ff98d5 commit 98c9b4b
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion optimade/server/data/test_references.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"_id": {
"$oid": "5cfb441f053b174410701bea"
},
"id": "dummy2019",
"id": "dummy/2019",
"last_modified": {
"$date": "2019-11-23T14:24:37.332Z"
},
Expand Down
9 changes: 8 additions & 1 deletion optimade/server/data/test_structures.json
Original file line number Diff line number Diff line change
Expand Up @@ -3688,6 +3688,13 @@
"Ti"
],
"structure_features": [],
"task_id": "mpf_3819"
"task_id": "mpf_3819",
"relationships": {
"references": {
"data": [
{"type": "references", "id": "dummy/2019"}
]
}
}
}
]
2 changes: 1 addition & 1 deletion optimade/server/routers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def get_included_relationships(
included = {}
for entry_type in endpoint_includes:
compound_filter = " OR ".join(
["id={}".format(ref_id) for ref_id in endpoint_includes[entry_type]]
['id="{}"'.format(ref_id) for ref_id in endpoint_includes[entry_type]]
)
params = EntryListingQueryParams(
filter=compound_filter,
Expand Down
5 changes: 5 additions & 0 deletions tests/filterparser/test_filterparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ def test_operators(self):
)
self.assertIsInstance(self.parse("5 < 7"), Tree)

def test_id(self):
self.assertIsInstance(self.parse('id="example/1"'), Tree)
self.assertIsInstance(self.parse('"example/1" = id'), Tree)
self.assertIsInstance(self.parse('id="test/2" OR "example/1" = id'), Tree)

def test_string_operations(self):
# Substring comparisons
self.assertIsInstance(
Expand Down
10 changes: 10 additions & 0 deletions tests/filtertransformers/test_mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ def test_simple_comparisons(self):
self.assertEqual(self.transform("a=3"), {"a": {"$eq": 3}})
self.assertEqual(self.transform("a!=3"), {"a": {"$ne": 3}})

def test_id(self):
self.assertEqual(self.transform('id="example/1"'), {"id": {"$eq": "example/1"}})
self.assertEqual(
self.transform('"example/1" = id'), {"id": {"$eq": "example/1"}}
)
self.assertEqual(
self.transform('id="test/2" OR "example/1" = id'),
{"$or": [{"id": {"$eq": "test/2"}}, {"id": {"$eq": "example/1"}}]},
)

def test_operators(self):
# Basic boolean operations
# TODO: {"a": {"$not": {"$lt": 3}}} can be simplified to {"a": {"$gte": 3}}
Expand Down
7 changes: 7 additions & 0 deletions tests/server/routers/test_references.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ class SingleReferenceEndpointTests(EndpointTestsMixin, unittest.TestCase):
test_id = "dijkstra1968"
request_str = f"/references/{test_id}"
response_cls = ReferenceResponseOne


class SingleReferenceEndpointTestsDifficult(EndpointTestsMixin, unittest.TestCase):

test_id = "dummy/20.19"
request_str = f"/references/{test_id}"
response_cls = ReferenceResponseOne
8 changes: 6 additions & 2 deletions tests/server/test_query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ def check_response(
)

included_resources = [_["id"] for _ in response["included"]]
self.assertEqual(len(included_resources), len(expected_included_resources))
self.assertEqual(
len(included_resources),
len(expected_included_resources),
msg=response["included"],
)
self.assertEqual(
sorted(set(included_resources)), sorted(expected_included_resources)
)
Expand Down Expand Up @@ -119,7 +123,7 @@ def test_default_value(self):
"""
request = "/structures"
expected_types = ["references"]
expected_reference_ids = ["dijkstra1968", "maddox1988"]
expected_reference_ids = ["dijkstra1968", "maddox1988", "dummy/2019"]
self.check_response(request, expected_types, expected_reference_ids)

request = "/structures?include="
Expand Down

0 comments on commit 98c9b4b

Please sign in to comment.