Skip to content

Commit

Permalink
Add AI test suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
tovrstra committed Jul 10, 2024
1 parent 2fe57a2 commit 0e0ac03
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
10 changes: 6 additions & 4 deletions iodata/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,19 @@ def _convert_convention_shell(
"""
if len(conv1) != len(conv2):
raise TypeError("conv1 and conv2 must contain the same number of elements.")
raise ValueError("conv1 and conv2 must contain the same number of elements.")
# Get signs from both
signs1 = [1 - 2 * el1.startswith("-") for el1 in conv1]
signs2 = [1 - 2 * el2.startswith("-") for el2 in conv2]
# Strip signs from both
conv1 = [el1.lstrip("-") for el1 in conv1]
conv2 = [el2.lstrip("-") for el2 in conv2]
if len(conv1) != len(set(conv1)):
raise TypeError("Argument conv1 contains duplicates.")
raise ValueError("Argument conv1 contains duplicates.")
if len(conv2) != len(set(conv2)):
raise TypeError("Argument conv2 contains duplicates.")
raise ValueError("Argument conv2 contains duplicates.")
if set(conv1) != set(conv2):
raise TypeError(
raise ValueError(
"Without the minus signs, conv1 and conv2 must contain "
f"the same elements. Got {conv1} and {conv2}."
)
Expand Down Expand Up @@ -164,6 +164,8 @@ def iter_cart_alphabet(n: int) -> NDArray[int]:
The angular momentum, i.e. sum of Cartesian powers in this case.
"""
if n < 0:
raise ValueError(f"The angular momentum cannot be negative. Got {n}")
for nx in range(n, -1, -1):
for ny in range(n - nx, -1, -1):
nz = n - nx - ny
Expand Down
49 changes: 42 additions & 7 deletions iodata/test/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
def test_convert_convention_shell():
assert _convert_convention_shell("abc", "cba") == ([2, 1, 0], [1, 1, 1])
assert _convert_convention_shell(["a", "b", "c"], ["c", "b", "a"]) == ([2, 1, 0], [1, 1, 1])
assert _convert_convention_shell([], []) == ([], [])

permutation, signs = _convert_convention_shell(["-a", "b", "c"], ["c", "b", "a"])
assert permutation == [2, 1, 0]
Expand Down Expand Up @@ -73,8 +74,9 @@ def test_convert_convention_shell():
assert_equal(vec2[permutation] * signs, vec1)


def test_convert_convention_obasis():
obasis = MolecularBasis(
@pytest.fixture()
def obasis() -> MolecularBasis:
return MolecularBasis(
[
Shell(0, [0], ["c"], np.zeros(3), np.zeros((3, 1))),
Shell(0, [0, 1], ["c", "c"], np.zeros(3), np.zeros((3, 2))),
Expand All @@ -88,6 +90,9 @@ def test_convert_convention_obasis():
},
"L2",
)


def test_convert_convention_obasis(obasis: MolecularBasis):
new_convention = {
(0, "c"): ["-s"],
(1, "c"): ["x", "y", "z"],
Expand All @@ -104,17 +109,44 @@ def test_convert_convention_obasis():
assert_equal(vec1, vec3)


def test_convert_convention_obasis_empty(obasis: MolecularBasis):
with pytest.raises(KeyError):
convert_conventions(obasis, {})


def test_convert_convention_obasis_invalid(obasis: MolecularBasis):
new_convention = {
(0, "c"): ["-s"],
(1, "c"): ["a", "b", "c"],
(2, "p"): ["dc2", "dc1", "dc0", "ds1", "ds2"],
}
with pytest.raises(ValueError):
convert_conventions(obasis, new_convention)


def test_convert_convention_obasis_duplicate(obasis: MolecularBasis):
new_convention = {
(0, "c"): ["-s"],
(1, "c"): ["x", "y", "y"],
(2, "p"): ["dc2", "dc1", "dc0", "ds1", "ds2"],
}
with pytest.raises(ValueError):
convert_conventions(obasis, new_convention)


def test_convert_exceptions():
with pytest.raises(TypeError):
with pytest.raises(ValueError):
_convert_convention_shell("abc", "cb")
with pytest.raises(TypeError):
with pytest.raises(ValueError):
_convert_convention_shell("abc", "cbb")
with pytest.raises(TypeError):
with pytest.raises(ValueError):
_convert_convention_shell("aba", "cba")
with pytest.raises(TypeError):
with pytest.raises(ValueError):
_convert_convention_shell(["a", "b", "c"], ["a", "b", "d"])
with pytest.raises(TypeError):
with pytest.raises(ValueError):
_convert_convention_shell(["a", "b", "c"], ["a", "b", "-d"])
with pytest.raises(ValueError):
_convert_convention_shell([1, 2, 3], "cb")


def test_iter_cart_alphabet():
Expand All @@ -128,6 +160,9 @@ def test_iter_cart_alphabet():
[0, 1, 1],
[0, 0, 2],
]
for angmom in -1, -2, -3:
with pytest.raises(ValueError):
list(iter_cart_alphabet(angmom))


def test_conventions():
Expand Down

0 comments on commit 0e0ac03

Please sign in to comment.