From 0e0ac03143f1582b4b3dad20c432bd180f8bf6ff Mon Sep 17 00:00:00 2001 From: Toon Verstraelen Date: Wed, 10 Jul 2024 11:13:27 +0200 Subject: [PATCH] Add AI test suggestions --- iodata/convert.py | 10 +++++--- iodata/test/test_convert.py | 49 +++++++++++++++++++++++++++++++------ 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/iodata/convert.py b/iodata/convert.py index 3ac23b60..8fb1cf0f 100644 --- a/iodata/convert.py +++ b/iodata/convert.py @@ -74,7 +74,7 @@ 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] @@ -82,11 +82,11 @@ def _convert_convention_shell( 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}." ) @@ -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 diff --git a/iodata/test/test_convert.py b/iodata/test/test_convert.py index ba977d23..23f0f43e 100644 --- a/iodata/test/test_convert.py +++ b/iodata/test/test_convert.py @@ -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] @@ -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))), @@ -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"], @@ -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(): @@ -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():