Skip to content

Commit

Permalink
Adding unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwaroquiers committed Mar 12, 2024
1 parent aa9fb2f commit 8b4bdfa
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/turbomoleio/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def gisnan(x):
This should be removed once this problem is solved at the Ufunc level.
"""
st = np.core.isnan(x)
if isinstance(st, type(NotImplemented)):
if isinstance(st, type(NotImplemented)): # pragma: no cover (to be solved at Ufunc)
raise TypeError("isnan not supported for this type")
return st

Expand Down Expand Up @@ -323,7 +323,7 @@ def assert_almost_equal(
# XXX: catch ValueError for subclasses of ndarray where iscomplex fail
try:
usecomplex = iscomplexobj(actual) or iscomplexobj(desired)
except (ValueError, TypeError):
except (ValueError, TypeError): # pragma: no cover (no idea when this can occur)
usecomplex = False

if usecomplex:
Expand Down
52 changes: 52 additions & 0 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
# along with turbomoleio (see ~turbomoleio/COPYING). If not,
# see <https://www.gnu.org/licenses/>.

import json
import os
import shutil

import numpy as np
import pytest
from monty.json import MSONable
from monty.os import makedirs_p
from monty.serialization import MontyEncoder
from pymatgen.core.structure import Molecule

from turbomoleio.testing import (
Expand All @@ -49,6 +51,7 @@
get_sp,
get_test_data_dir,
get_tfp,
gisnan,
temp_dir,
touch_file,
)
Expand All @@ -60,6 +63,28 @@ def __init__(self, a, b):
self.b = b


class ExplicitAsFromDictExample:
def __init__(self, d, e):
self.d = d
self.e = e

def as_dict(self):
return {
"@module": "tests.test_testing",
"@class": "ExplicitAsFromDictExample",
"@version": None,
"d": self.d,
"e": self.e,
}

@classmethod
def from_dict(cls, d):
return cls(d=d["d"], e=d["e"])

def to_json(self) -> str:
return json.dumps(self, cls=MontyEncoder)


class TestFunctions(object):
def test_temp_dir(self):
# test that the file is not deleted if delete=False
Expand All @@ -68,6 +93,11 @@ def test_temp_dir(self):
assert tmp_dir == os.getcwd()
shutil.rmtree(tmp_dir)

# test without changing to the directory
with temp_dir(delete=True, changedir=False) as tmp_dir:
assert os.path.exists(tmp_dir)
assert tmp_dir != os.getcwd()

def test_get_test_data_dir(self, delete_tmp_dir):
with temp_dir(delete_tmp_dir) as tmp_dir:
with pytest.raises(RuntimeError, match=r"test_data directory not found."):
Expand All @@ -91,6 +121,13 @@ def test_touch_file(self, delete_tmp_dir):
def test_assert_MSONable(self):
m = MSONableExample(1, 2)
assert_MSONable(m)
m = ExplicitAsFromDictExample(5, 12)
assert not isinstance(m, MSONable)
assert_MSONable(m, test_if_subclass=False)

def test_gisnan(self):
assert gisnan(np.nan)
assert not gisnan(1)

def test_get_tfp(self, test_data):
"""
Expand Down Expand Up @@ -153,6 +190,21 @@ def test_assert_almost_equal(self):
assert_almost_equal(d1, d2, rtol=1e-8)
assert_almost_equal(d1, d2, rtol=1e-3)

d1 = 1
d2 = {"b": 1}
with pytest.raises(AssertionError):
assert_almost_equal(d1, d2)

d1 = 1
d2 = complex(1, 0)
assert_almost_equal(d1, d2)
assert_almost_equal(d2, d1)

d1 = complex(3, 5)
d2 = complex(3, 5)
assert_almost_equal(d1, d2)
assert_almost_equal(d2, d1)

def test_compare_differences(self):
diffs = compare_differences({}, {})
assert len(diffs) == 0
Expand Down

0 comments on commit 8b4bdfa

Please sign in to comment.