Coverage for tests/test_utils.py: 100%

35 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-19 09:48 -0700

1from pyEQL.salt_ion_match import Salt 

2import pytest 

3 

4from pchemdb.utils import formula_to_salt 

5 

6 

7@pytest.fixture( 

8 name="salt", 

9) 

10def fixture_salt(formula: str) -> Salt: 

11 return formula_to_salt(formula) 

12 

13 

14class TestFormulaToSalt: 

15 @staticmethod 

16 @pytest.mark.parametrize( 

17 ("formula", "cation", "anion"), 

18 [ 

19 ("NaCl", ("Na", 1), ("Cl", -1)), 

20 ("KBr", ("K", 1), ("Br", -1)), 

21 ("CsI", ("Cs", 1), ("I", -1)), 

22 ], 

23 ) 

24 def test_should_create_salts_with_monovalent_ions( 

25 cation: tuple[str, int], anion: tuple[str, int], salt: Salt 

26 ) -> None: 

27 assert f"{cation[0]}[{cation[1]:+}]" in salt.cation 

28 assert cation[1] == salt.z_cation 

29 assert f"{anion[0]}[{anion[1]:+}]" in salt.anion 

30 assert anion[1] == salt.z_anion 

31 

32 @staticmethod 

33 @pytest.mark.parametrize( 

34 ("formula", "cation", "anion"), 

35 [ 

36 ("CaCl2", ("Ca", 2), ("Cl", -1)), 

37 ("MgCl2", ("Mg", 2), ("Cl", -1)), 

38 ], 

39 ) 

40 def test_should_create_salts_with_polyvalent_ions( 

41 cation: tuple[str, int], anion: tuple[str, int], salt: Salt 

42 ) -> None: 

43 assert f"{cation[0]}[{cation[1]:+}]" in salt.cation 

44 assert cation[1] == salt.z_cation 

45 assert f"{anion[0]}[{anion[1]:+}]" in salt.anion 

46 assert anion[1] == salt.z_anion 

47 

48 @staticmethod 

49 @pytest.mark.parametrize( 

50 ("formula", "cation", "anion"), 

51 [ 

52 ("Na2CO3", ("Na", 1), ("CO3", -2)), 

53 ("AgNO3", ("Ag", 1), ("NO3", -1)), 

54 ("Ca(OH)2", ("Ca", 2), ("OH", -1)), 

55 ("MgCl2", ("Mg", 2), ("Cl", -1)), 

56 ("KSCN", ("K", 1), ("SCN", -1)), 

57 ("LiClO4", ("Li", 1), ("ClO4", -1)), 

58 ("H2SO4", ("H", 1), ("SO4", -2)), 

59 ], 

60 ) 

61 def test_should_create_salts_with_polyatomic_ions( 

62 cation: tuple[str, int], anion: tuple[str, int], salt: Salt 

63 ) -> None: 

64 assert f"{cation[0]}[{cation[1]:+}]" in salt.cation 

65 assert cation[1] == salt.z_cation 

66 assert f"{anion[0]}[{anion[1]:+}]" in salt.anion 

67 assert anion[1] == salt.z_anion 

68 

69 @staticmethod 

70 # @pytest.mark.xfail 

71 @pytest.mark.parametrize( 

72 ("formula", "cation", "anion"), [("NH4Cl", ("NH4", 1), ("Cl", -1))] 

73 ) 

74 def test_should_create_salts_with_polyatomic_cations( 

75 cation: tuple[str, int], anion: tuple[str, int], salt: Salt 

76 ) -> None: 

77 assert f"{cation[0]}[{cation[1]:+}]" in salt.cation 

78 assert cation[1] == salt.z_cation 

79 assert f"{anion[0]}[{anion[1]:+}]" in salt.anion 

80 assert anion[1] == salt.z_anion