From b129798aeba82facc86ff61a5758dcdfbc648890 Mon Sep 17 00:00:00 2001 From: "T.Tian" Date: Wed, 17 Jan 2024 01:50:59 +0800 Subject: [PATCH] add tests for json api --- sparc/utils.py | 21 ++++++++++++-------- tests/test_sparc_bundle.py | 39 ++++++++++++++++++++------------------ 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/sparc/utils.py b/sparc/utils.py index ab2a2be..c088e29 100644 --- a/sparc/utils.py +++ b/sparc/utils.py @@ -141,15 +141,20 @@ def locate_api(json_file=None, doc_path=None): doc_root = os.environ.get("SPARC_DOC_PATH", None) if doc_root is not None: doc_path = Path(doc_root) / ".LaTeX" + else: + doc_path = Path(doc_path) - if doc_path is not None: - with tempfile.TemporaryDirectory() as tmpdir: - tmpdir = Path(tmpdir) - tmpfile = tmpdir / "parameters.json" - with open(tmpfile, "w") as fd: - fd.write(SPARCDocParser.json_from_directory(doc_path)) - api = SparcAPI(tmpfile) - return api + if (doc_path is not None) and doc_path.is_dir(): + try: + with tempfile.TemporaryDirectory() as tmpdir: + tmpdir = Path(tmpdir) + tmpfile = tmpdir / "parameters.json" + with open(tmpfile, "w") as fd: + fd.write(SPARCDocParser.json_from_directory(doc_path, include_subdirs=True)) + api = SparcAPI(tmpfile) + return api + except Exception: + pass api = SparcAPI() return api diff --git a/tests/test_sparc_bundle.py b/tests/test_sparc_bundle.py index a18c19d..548ea60 100644 --- a/tests/test_sparc_bundle.py +++ b/tests/test_sparc_bundle.py @@ -195,30 +195,33 @@ def test_bundle_diff_label(fs): sp = SparcBundle(directory="test.sparc", mode="r") -def test_bundle_write_multi(fs): +def test_bundle_write_multi(): import numpy as np from ase.build import bulk from ase.io import read, write from sparc.io import read_sparc, write_sparc - fs.create_dir("test.sparc") - atoms = bulk("Cu") * [4, 4, 4] - images = [atoms] - write_sparc("test.sparc", atoms) - write_sparc("test.sparc", images) - write("test.sparc", atoms, format="sparc") - write("test.sparc", images, format="sparc") - images.append(atoms.copy()) - with pytest.raises(Exception): - write_sparc("test.sparc", images) - - with pytest.raises(Exception): - write("test.sparc", images, format="sparc") - - atoms2 = read_sparc("test.sparc") - atoms2 = read("test.sparc", format="sparc") - assert np.isclose(atoms.positions, atoms2.positions).all() + with tempfile.TemporaryDirectory() as tmpdir: + tmpdir = Path(tmpdir) + testbundle = tmpdir / "test.sparc" + os.makedirs(testbundle, exist_ok=True) + atoms = bulk("Cu") * [4, 4, 4] + images = [atoms] + write_sparc(testbundle, atoms) + write_sparc(testbundle, images) + write(testbundle, atoms, format="sparc") + write(testbundle, images, format="sparc") + images.append(atoms.copy()) + with pytest.raises(Exception): + write_sparc(testbundle, images) + + with pytest.raises(Exception): + write(testbundle, images, format="sparc") + + atoms2 = read_sparc(testbundle) + atoms2 = read(testbundle, format="sparc") + assert np.isclose(atoms.positions, atoms2.positions).all() def test_bundle_psp():