def test_init_(self): c = Composition({'Fe': 4, 'O': 16, 'P': 4}) charge = 4 self.assertEqual("Fe4 P4 O16 +4", Ion(c, charge).formula) f = {1: 1, 8: 1} charge = -1 self.assertEqual("H1 O1 -1", Ion(Composition(f), charge).formula) self.assertEqual("S2 O3 -2", Ion(Composition(S=2, O=3), -2).formula)
def test_equals(self): random_z = random.randint(1, 92) fixed_el = Element.from_Z(random_z) other_z = random.randint(1, 92) while other_z == random_z: other_z = random.randint(1, 92) comp1 = Ion(Composition({fixed_el: 1, Element.from_Z(other_z): 0}), 1) other_z = random.randint(1, 92) while other_z == random_z: other_z = random.randint(1, 92) comp2 = Ion(Composition({fixed_el: 1, Element.from_Z(other_z): 0}), 1) self.assertEqual( comp1, comp2, "Composition equality test failed. " + "%s should be equal to %s" % (comp1.formula, comp2.formula)) self.assertEqual(comp1.__hash__(), comp2.__hash__(), "Hashcode equality test failed!")
def test_mixed_valence(self): comp = Ion(Composition({"Fe2+": 2, "Fe3+": 4, "Li+": 8})) self.assertEqual(comp.reduced_formula, "Li4Fe3(aq)") self.assertEqual(comp.alphabetical_formula, "Fe6 Li8") self.assertEqual(comp.formula, "Li8 Fe6")