diff --git a/tests/testPickingResults.py b/tests/testPickingResults.py index 6331d4cd..5d863f8d 100644 --- a/tests/testPickingResults.py +++ b/tests/testPickingResults.py @@ -2,9 +2,20 @@ import unittest from pylot.core.pick.autopick import PickingResults class TestPickingResults(unittest.TestCase): + def setUp(self): self.pr = PickingResults() + def test_non_existing_key_dot_access(self): + """Accessing an attribute in the class that wasnt added to the dict should give a AttributeError""" + with self.assertRaises(AttributeError): + self.pr.doesntexist + + def test_non_existing_key_dict_access(self): + """Accessing a missing attribute in a dictionary throws a KeyError""" + with self.assertRaises(KeyError): + self.pr['keydoesnotexist'] + def test_dot_member_creation(self): self.pr.x = 0 self.assertEqual(self.pr.x, 0) @@ -12,21 +23,16 @@ class TestPickingResults(unittest.TestCase): self.assertEqual(self.pr.x, 42) def test_dot_builtin_member(self): - self.assertEqual(self.pr.Pflag, 0) - self.pr.Pflag = 99 - self.assertEqual(self.pr.Pflag, 99) + self.assertEqual(self.pr.weight, 4) + self.pr.weight = 99 + self.assertEqual(self.pr.weight, 99) def test_key_access(self): self.pr['y'] = 11 self.assertEqual(self.pr['y'], 11) def test_builtin_fields(self): - self.assertEqual(self.pr.Pflag, 0) - - def test_missing_attribute(self): - # accessing a missing attribute in a dictionary throws a KeyError - with self.assertRaises(KeyError): - self.pr['keydoesnotexist'] + self.assertEqual(self.pr['weight'], 4) def test_in(self): self.assertFalse('keydoesnotexist' in self.pr) @@ -48,6 +54,25 @@ class TestPickingResults(unittest.TestCase): def test_get_default(self): self.assertEqual(self.pr.get('keynotexisting', 42), 42) - pflag = self.pr.get('Pflag', -1) - self.assertEqual(pflag, 0) - self.assertNotEqual(pflag, -1) \ No newline at end of file + weight = self.pr.get('weight', -1) + self.assertEqual(weight, 4) + self.assertNotEqual(weight, -1) + + def test_dunder_attributes(self): + """Storing Pythons special dunder method in a dictionary is valid and should not override the instances dunder + methods""" + prev_len = len(self.pr) + try: + self.pr['__len__'] = None + except Exception: + self.fail("test_dunder_attributes failed to add a dunder attribute to the dictionary keys") + try: + curr_len = len(self.pr) + except Exception: + self.fail("test_dunder_attributes overwrote an instance internal dunder method") + self.assertEqual(prev_len+1, curr_len) # +1 for the added __len__ key/value-pair + + self.pr.__len__ = 42 + + self.assertEqual(42, self.pr['__len__']) + self.assertEqual(prev_len+1, curr_len, msg="__len__ was overwritten")