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 manual verification of elements_ratios #76

Merged
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
2 changes: 1 addition & 1 deletion optimade/models/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def elements_must_be_alphabetical(cls, v):
@validator("elements_ratios", whole=True)
def ratios_must_sum_to_one(cls, v):
assert (
abs(sum(v) - 1) < EPS
abs(sum(v) - 1) <= EPS
), f"elements_ratios MUST sum to 1 within floating point accuracy. It sums to: {sum(v)}"
return v

Expand Down
2 changes: 1 addition & 1 deletion optimade/models/toplevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class ResponseMeta(Meta):

class StructureResponseOne(Success):
meta: ResponseMeta = Schema(...)
data: Union[StructureResource, Dict[str, Any]] = Schema(...)
data: Union[StructureResource, Dict[str, Any], None] = Schema(...)


class StructureResponseMany(Success):
Expand Down
4 changes: 2 additions & 2 deletions optimade/server/entry_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def find(
else:
more_data_available = False
data_available = self.count(**criteria)
if data_available != 1:
if data_available > 1:
raise HTTPException(
status_code=404,
detail=f"Instead of a single entry, {data_available} entries were found",
Expand All @@ -121,7 +121,7 @@ def find(
results.append(self.resource_cls(**StructureMapper.map_back(doc)))

if isinstance(params, SingleEntryQueryParams):
results = results[0]
results = results[0] if results else None

return results, more_data_available, data_available, all_fields - fields

Expand Down
5 changes: 4 additions & 1 deletion optimade/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,14 @@ def get_single_structure(
links = ToplevelLinks(next=None)
if fields and results is not None:
results = handle_response_fields(results, fields)[0]

data_returned = 1 if results else 0

return StructureResponseOne(
links=links,
data=results,
meta=meta_values(
str(request.url), data_available, data_available, more_data_available
str(request.url), data_returned, data_available, more_data_available
),
)

Expand Down
11 changes: 11 additions & 0 deletions optimade/server/tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,14 @@ def test_structures_endpoint_data(self):
self.assertTrue(
"_exmpl__mp_chemsys" in self.json_response["data"]["attributes"]
)


class SingleStructureEndpointEmptyTest(EndpointTests, unittest.TestCase):

test_id = "non_existent_id"
request_str = f"/structures/{test_id}"
response_cls = StructureResponseOne

def test_structures_endpoint_data(self):
self.assertTrue("data" in self.json_response)
self.assertEqual(self.json_response["data"], None)