From de859f3b7bb8f1ebaf9e62c4e2990d19bc004bea Mon Sep 17 00:00:00 2001 From: Matthew Evans Date: Mon, 2 Oct 2023 15:16:46 +0200 Subject: [PATCH] Add tests for info endpoint and remove now defunct specific tests --- src/mc_optimade/tests/test_convert.py | 63 +++++---------------------- 1 file changed, 10 insertions(+), 53 deletions(-) diff --git a/src/mc_optimade/tests/test_convert.py b/src/mc_optimade/tests/test_convert.py index e97dc2f..ff0d710 100644 --- a/src/mc_optimade/tests/test_convert.py +++ b/src/mc_optimade/tests/test_convert.py @@ -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("*") @@ -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: @@ -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}"