Skip to content

Commit

Permalink
add tests for json api
Browse files Browse the repository at this point in the history
  • Loading branch information
alchem0x2A committed Jan 16, 2024
1 parent e14007d commit b129798
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
21 changes: 13 additions & 8 deletions sparc/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
39 changes: 21 additions & 18 deletions tests/test_sparc_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit b129798

Please sign in to comment.