diff --git a/ionize/Ion/BaseIon.py b/ionize/Ion/BaseIon.py index e3b5ec0..2cdab8f 100644 --- a/ionize/Ion/BaseIon.py +++ b/ionize/Ion/BaseIon.py @@ -50,7 +50,7 @@ def __str__(self): def __hash__(self): """Return the hash value for the object.""" - return hash(json.dumps(self._state)) + return hash(self.serialize()) def __eq__(self, other): """Test equality between two ions.""" diff --git a/ionize/tests.py b/ionize/tests.py index 1c2c455..0840c4f 100644 --- a/ionize/tests.py +++ b/ionize/tests.py @@ -176,6 +176,16 @@ def test_equality(self): sol = Solution([hcl], [0.1]) self.assertEqual(hcl, hcl2) + def test_hash(self): + for ion in self.database.keys(): + ion1 = self.database[ion] + ion2 = self.database[ion] + ion1.context({'pH': 8, 'ionic_strength': 0.1, 'temperature': 28}) + ion2.context({'pH': 9, 'ionic_strength': 0.05, 'temperature': 23}) + self.assertTrue(hash(ion1)==hash(ion2), + "Ions from database aren't identitcal.", + ) + def test_serialize(self): for ion_name in self.database.keys(): ion = self.database.load(ion_name) @@ -365,6 +375,11 @@ def test_moderate(self): self.assertFalse(sol.moderate(), 'Unbuffered acid evaluated as safe.') self.assertTrue(sol.titrate('tris', 8).moderate(), 'Tris buffer evaluated as unsafe.') + def test_hash(self): + sol1 = Solution('chloride', 0.001) + sol2 = Solution('chloride', 0.001) + self.assertEqual(hash(sol1), hash(sol2)) + class TestNucleicAcid(unittest.TestCase): def test_mobility(self):