def _diff(self, base:Metadata, comp:Metadata, keys:Optional[List[str]] = None): """ Calculate the difference between two metadata dictionaries, optionally specific to a list of keys @param base Metadata dictionary that forms the basis of comparison @param comp Metadata dictionary to check against basis @param keys Interested keys (optional; check all if omitted) """ # Common keys for common in set(comp.keys()).intersection(set(base.keys())): if not keys or common in keys: if base[common] != comp[common]: self.additions[common] = comp[common] self.deletions[common] = base[common] # New keys for added in (comp.keys() - base.keys()): if not keys or added in keys: self.additions[added] = comp[added] # Deleted keys for deleted in (base.keys() - comp.keys()): if not keys or deleted in keys: self.deletions[deleted] = base[deleted]
class TestMetadata(unittest.TestCase): """ Tests for `Metadata`. """ _TEST_VALUES = {1: 2, 3: 4} def setUp(self): self.metadata = Metadata(TestMetadata._TEST_VALUES) def test_init_with_no_values(self): self.assertEqual(len(Metadata()), 0) def test_init_with_values(self): self.assertCountEqual(self.metadata.keys(), TestMetadata._TEST_VALUES.keys()) self.assertCountEqual(self.metadata.values(), TestMetadata._TEST_VALUES.values()) def test_get(self): self.assertEqual(self.metadata.get(1), TestMetadata._TEST_VALUES[1]) self.assertEqual(self.metadata[1], TestMetadata._TEST_VALUES[1]) def test_rename(self): self.metadata.rename(1, 10) self.assertNotIn(1, self.metadata) self.assertEqual(self.metadata[10], 2) def test_rename_non_existent(self): self.assertRaises(KeyError, self.metadata.rename, 10, 20) def test_rename_to_same_name(self): self.metadata.rename(1, 1) self.assertEqual(self.metadata[1], 2) def test_pop(self): self.metadata.pop(1) self.assertEqual(self.metadata, Metadata({3: 4})) def test_clear(self): self.metadata.clear() self.assertEqual(self.metadata, Metadata()) def test_delete(self): del self.metadata[1] self.assertEqual(self.metadata, Metadata({3: 4})) def test_len(self): self.assertEqual(len(self.metadata), 2) def test_items(self): self.assertCountEqual(self.metadata.items(), [(1, 2), (3, 4)]) def test_values(self): self.assertCountEqual(self.metadata.values(), [2, 4]) def test_keys(self): self.assertCountEqual(self.metadata.keys(), [1, 3]) def test_eq_when_equal(self): self.assertEqual(Metadata(TestMetadata._TEST_VALUES), Metadata(TestMetadata._TEST_VALUES)) def test_eq_when_not_eqal(self): self.assertNotEqual(Metadata(TestMetadata._TEST_VALUES), Metadata()) def test_repr(self): string_representation = repr(self.metadata) self.assertTrue(isinstance(string_representation, str)) def test_contains(self): self.assertIn(1, self.metadata) self.assertNotIn("a", self.metadata) def test_copy(self): self.assertEqual(copy.copy(self.metadata), self.metadata) def test_deepcopy(self): self.assertEqual(copy.deepcopy(self.metadata), self.metadata)