Skip to content

Commit

Permalink
Add more tests for __hash__ and __eq__ for pyjess classes
Browse files Browse the repository at this point in the history
  • Loading branch information
althonos committed Nov 29, 2024
1 parent 94536ad commit 3d21ee9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/pyjess/tests/test_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,18 @@ def test_init_invalid_residue_name(self):
def test_hash(self):
a1 = self._create_atom()
a2 = self._create_atom()
self.assertEqual(a1, a2)
self.assertEqual(hash(a1), hash(a2))
self.assertIsNot(a1, a2)
a3 = self._create_atom(x=1.0)
self.assertNotEqual(hash(a1), hash(a3))

def test_eq(self):
a1 = self._create_atom()
a2 = self._create_atom()
self.assertEqual(a1, a2)
self.assertIsNot(a1, a2)
a3 = self._create_atom(x=1.0)
self.assertNotEqual(a1, a3)

def test_repr_roundtrip(self):
atom = self._create_atom()
Expand Down
16 changes: 15 additions & 1 deletion src/pyjess/tests/test_jess.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,23 @@ def test_hash(self):
template2 = Template.load(f)
j1 = Jess([template1, template2])
j2 = Jess([template1, template2])
self.assertEqual(j1, j2)
self.assertEqual(hash(j1), hash(j2))
self.assertIsNot(j1, j2)
j3 = Jess([template1])
self.assertNotEqual(hash(j1), hash(j3))

@unittest.skipUnless(files, "importlib.resources not available")
def test_eq(self):
with files(data).joinpath("template_01.qry").open() as f:
template1 = Template.load(f)
with files(data).joinpath("template_02.qry").open() as f:
template2 = Template.load(f)
j1 = Jess([template1, template2])
j2 = Jess([template1, template2])
self.assertEqual(j1, j2)
self.assertIsNot(j1, j2)
j3 = Jess([template1])
self.assertNotEqual(j1, j3)

@unittest.skipUnless(files, "importlib.resources not available")
def test_pickle_roundtrip(self):
Expand Down
16 changes: 16 additions & 0 deletions src/pyjess/tests/test_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,23 @@ def test_hash(self):
mol1 = Molecule(atoms)
mol2 = Molecule(atoms)
self.assertEqual(hash(mol1), hash(mol2))
self.assertIsNot(mol1, mol2)
mol3 = Molecule(atoms[:-1])
self.assertNotEqual(hash(mol1), hash(mol3))

def test_eq(self):
atoms = [
self._create_atom(serial=1, name='N'),
self._create_atom(serial=2, name='CA'),
self._create_atom(serial=3, name='C'),
self._create_atom(serial=4, name='O'),
]
mol1 = Molecule(atoms)
mol2 = Molecule(atoms)
self.assertEqual(mol1, mol2)
self.assertIsNot(mol1, mol2)
mol3 = Molecule(atoms[:-1])
self.assertNotEqual(mol1, mol3)

def test_copy(self):
atoms = [
Expand Down
10 changes: 10 additions & 0 deletions src/pyjess/tests/test_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ def test_hash(self):
self.assertEqual(tpl1, tpl2)
self.assertEqual(hash(tpl1), hash(tpl2))
self.assertIsNot(tpl1, tpl2)
tpl3 = Template.loads(TEMPLATE, id="tpl3")
self.assertNotEqual(hash(tpl1), hash(tpl3))

def test_eq(self):
tpl1 = Template.loads(TEMPLATE, id="tpl1")
tpl2 = Template.loads(TEMPLATE, id="tpl1")
self.assertEqual(tpl1, tpl2)
self.assertIsNot(tpl1, tpl2)
tpl3 = Template.loads(TEMPLATE, id="tpl3")
self.assertNotEqual(tpl1, tpl3)

def test_copy(self):
tpl1 = Template.loads(TEMPLATE, id="tpl1")
Expand Down
11 changes: 10 additions & 1 deletion src/pyjess/tests/test_template_atom.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,18 @@ def _create_atom(self, **kwargs):
def test_hash(self):
a1 = self._create_atom()
a2 = self._create_atom()
self.assertEqual(a1, a2)
self.assertEqual(hash(a1), hash(a2))
self.assertIsNot(a1, a2)
a3 = self._create_atom(x=1.0)
self.assertNotEqual(hash(a1), hash(a3))

def test_eq(self):
a1 = self._create_atom()
a2 = self._create_atom()
self.assertEqual(a1, a2)
self.assertIsNot(a1, a2)
a3 = self._create_atom(x=1.0)
self.assertNotEqual(a1, a3)

@unittest.skipUnless(sys.implementation.name == "cpython", "only available on CPython")
def test_sizeof(self):
Expand Down

0 comments on commit 3d21ee9

Please # to comment.