Skip to content

Commit

Permalink
Add tests for info endpoint and remove now defunct specific tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ml-evs committed Oct 2, 2023
1 parent d7fe95b commit de859f3
Showing 1 changed file with 10 additions and 53 deletions.
63 changes: 10 additions & 53 deletions src/mc_optimade/tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from pathlib import Path

import pytest
from optimade.models import EntryInfoResource, StructureResource
from optimade.models import EntryInfoResource

from mc_optimade.convert import convert_archive, inflate_archive
from mc_optimade.convert import convert_archive

EXAMPLE_ARCHIVES = (Path(__file__).parent.parent / "examples").glob("*")

Expand All @@ -32,12 +32,18 @@ def test_convert_example_archives(archive_path, tmp_path):
first_entry = json.loads(first_entry_path.read_text())

with open(jsonl_path, "r") as fhandle:
# check that header exists
# check that header exists as first line
header_jsonl = fhandle.readline()
header = json.loads(header_jsonl)
assert "x-optimade" in header

# if provided, check that the first entry matches
# check that info endpoint equivalent exists as next line
info = json.loads(fhandle.readline())
breakpoint()
assert EntryInfoResource(**info)

# now check for entry lines:
# if provided, check that the first entry matches the tabulated data
if first_entry is not None:
while next_line := fhandle.readline():
try:
Expand All @@ -63,52 +69,3 @@ def test_convert_example_archives(archive_path, tmp_path):
assert next_entry[key] == first_entry[key]

assert next_entry["attributes"] == pytest.approx(first_entry["attributes"])


def test_decompress_bz2(tmp_path):
archive_path = Path(__file__).parent.parent / "examples" / "bzipped_pymatgen"
# copy example into temporary path
tmp_path = tmp_path / archive_path.name
shutil.copytree(archive_path, tmp_path)

inflate_archive(tmp_path, "part_1.json.bz2")
assert (tmp_path / "part_1.json").exists()


def test_example_archive_structure_id(tmp_path):
STRUCT_ID = (
"structures.zip/structures/cifs/55c564f6-ac6a-4122-b8d9-0ad9fe61e961.cif"
)
archive_path = Path(__file__).parent.parent / "examples" / "folder_of_cifs"

# copy example into temporary path
tmp_path = tmp_path / archive_path.name
shutil.copytree(archive_path, tmp_path)

jsonl_path = convert_archive(tmp_path)
assert jsonl_path.exists()

with open(jsonl_path, "r") as fhandle:
header = json.loads(fhandle.readline())
assert "x-optimade" in header
info = json.loads(fhandle.readline())
# TODO: need to include default OPTIMADE properties
assert len(info["properties"]) == 3
assert EntryInfoResource(**info)

for _ in range(3):
structure_jsonl = fhandle.readline()
structure_dict = json.loads(structure_jsonl)
assert StructureResource(**structure_dict)

if structure_dict["id"] == STRUCT_ID:
assert structure_dict["id"] == STRUCT_ID
assert structure_dict["attributes"]["_mcloudarchive_energy"] == -0.54
assert structure_dict["attributes"]["_mcloudarchive_property_b"] == 0.99
assert (
structure_dict["attributes"]["_mcloudarchive_structure_description"]
== "some description"
)
break
else:
assert False, f"Could not find structure with id {STRUCT_ID}"

0 comments on commit de859f3

Please sign in to comment.