def test_mismatch(self): """ Check mismatching base and mask data cancel out. """ empty = from_io({ "version": self.version, }) first = from_io({ "version": self.version, "revisions": [IO_REVISION1,], "builds": [{"id": "origin:1", "origin": "origin", "revision_id": IO_REVISION1["id"]},], "tests": [{"id": "origin:1", "origin": "origin", "build_id": "origin:1"},], }) second = from_io({ "version": self.version, "revisions": [IO_REVISION2,], "builds": [{"id": "origin:2", "origin": "origin", "revision_id": IO_REVISION2["id"]},], "tests": [{"id": "origin:2", "origin": "origin", "build_id": "origin:2"},], }) self.assertEqual(apply_mask(first, second), empty)
def test_empty_non_empty(self): """ Check empty/non-empty base/mask are handled correctly. """ empty = from_io({ "version": self.version, }) non_empty = from_io({ "version": self.version, "revisions": [IO_REVISION1,], }) self.assertEqual(apply_mask(empty, empty), empty) self.assertEqual(apply_mask(non_empty, empty), empty) self.assertEqual(apply_mask(empty, non_empty), empty) self.assertEqual(apply_mask(non_empty, non_empty), non_empty)
def match_new_io(base_io, new_io, match_map=None, copy=True): """ Generate notifications for new I/O data being added to base I/O data. Args: base_io: The existing (base) I/O data being added to, and possibly referred to by the arriving (new) I/O data. Will be used to complete the data being notified about. Can already contain the new I/O data, it will be considered "new" regardless. new_io: The arriving (new) I/O data being added to the existing (base) data. Can refer to the existing I/O data. The new I/O data can already be added to the base I/O data, anything in the new data will be considered "new" regardless. match_map: The map of subscription match functions: a dictionary with OO data object list names and a list of tuples, each containing the name of the subscription and a match function. Each function must accept an object from the corresponding object list in OO data, and return an iterable producing kcidb.monitor.output.NotificationMessage objects, or None, which is equivalent to an empty iterable. The default is a dictionary of matching functions from all kcidb.subscriptions.* modules, where each is called "match_<OBJ_NAME>", where "<OBJ_NAME>" is an object list name without the "s" ending. copy: True, if the data should be copied before referencing/modifying. False, if the data could be referenced and modified in-place. Optional, default is True. Returns: The list of notifications: kcidb.monitor.output.Notification objects. """ assert LIGHT_ASSERTS or io.schema.is_valid(base_io) assert LIGHT_ASSERTS or io.schema.is_valid(new_io) # Merge the new data into the base (*copy* new data as we'll need it) merged_io = io.merge(base_io, [new_io], copy_target=copy, copy_sources=True) # Convert both to OO representation merged_oo = oo.from_io(merged_io, copy=False) new_oo = oo.from_io(new_io, copy=copy) # Remove all objects with missing parents from the merged data rooted_oo = oo.remove_orphans(merged_oo) # Delist everything except loaded or modified objects, but keep references masked_oo = oo.apply_mask(rooted_oo, new_oo) # Generate notifications return match_oo(masked_oo, match_map)
def test_match(self): """ Check fully-matching base and mask data stay unchanged. """ first = from_io({ "version": self.version, "revisions": [ { "id": "origin:1", }, ], "builds": [ { "id": "origin:1", "revision_id": "origin:1" }, ], "tests": [ { "id": "origin:1", "build_id": "origin:1" }, ], }) second = from_io({ "version": self.version, "revisions": [ { "id": "origin:1", }, ], "builds": [ { "id": "origin:1", "revision_id": "origin:1" }, ], "tests": [ { "id": "origin:1", "build_id": "origin:1" }, ], }) self.assertEqual(apply_mask(first, second), first)
def test_partial_match(self): """ Check partially-matching base and mask data work correctly. """ first = from_io({ "version": self.version, "revisions": [IO_REVISION1, IO_REVISION2,], "builds": [ {"id": "origin:1-1", "origin": "origin", "revision_id": IO_REVISION1["id"]}, {"id": "origin:1-2", "origin": "origin", "revision_id": IO_REVISION1["id"]}, {"id": "origin:2-1", "origin": "origin", "revision_id": IO_REVISION2["id"]}, {"id": "origin:2-2", "origin": "origin", "revision_id": IO_REVISION2["id"]}, ], "tests": [ {"id": "origin:1-1-1", "origin": "origin", "build_id": "origin:1-1"}, {"id": "origin:1-1-2", "origin": "origin", "build_id": "origin:1-1"}, {"id": "origin:1-2-1", "origin": "origin", "build_id": "origin:1-2"}, {"id": "origin:1-2-2", "origin": "origin", "build_id": "origin:1-2"}, {"id": "origin:2-1-1", "origin": "origin", "build_id": "origin:2-1"}, {"id": "origin:2-1-2", "origin": "origin", "build_id": "origin:2-1"}, {"id": "origin:2-2-1", "origin": "origin", "build_id": "origin:2-2"}, {"id": "origin:2-2-2", "origin": "origin", "build_id": "origin:2-2"}, ], }) second = from_io({ "version": self.version, "revisions": [IO_REVISION1, IO_REVISION3,], "builds": [ {"id": "origin:1-1", "origin": "origin", "revision_id": IO_REVISION1["id"]}, {"id": "origin:1-X", "origin": "origin", "revision_id": IO_REVISION1["id"]}, {"id": "origin:2-1", "origin": "origin", "revision_id": IO_REVISION3["id"]}, {"id": "origin:2-X", "origin": "origin", "revision_id": IO_REVISION3["id"]}, ], "tests": [ {"id": "origin:1-1-1", "origin": "origin", "build_id": "origin:1-1"}, {"id": "origin:1-1-X", "origin": "origin", "build_id": "origin:1-1"}, {"id": "origin:1-2-1", "origin": "origin", "build_id": "origin:1-2"}, {"id": "origin:1-2-X", "origin": "origin", "build_id": "origin:1-2"}, {"id": "origin:2-1-1", "origin": "origin", "build_id": "origin:2-1"}, {"id": "origin:2-1-X", "origin": "origin", "build_id": "origin:2-1"}, {"id": "origin:2-2-1", "origin": "origin", "build_id": "origin:2-2"}, {"id": "origin:2-2-X", "origin": "origin", "build_id": "origin:2-2"}, ], }) expected_output = { "version": first["version"], "revisions": { k: first["revisions"][k] for k in (IO_REVISION1["id"], IO_REVISION2["id"],) }, "builds": { k: first["builds"][k] for k in ( "origin:1-1", "origin:1-2", "origin:2-1", "origin:2-2", ) }, "tests": { k: first["tests"][k] for k in ( "origin:1-1-1", "origin:1-2-1", "origin:2-1-1", "origin:2-2-1", ) }, } output = apply_mask(first, second) self.assertEqual(output, expected_output) del first["tests"] del second["tests"] expected_output = { "version": first["version"], "revisions": { k: first["revisions"][k] for k in (IO_REVISION1["id"], IO_REVISION2["id"],) }, "builds": { k: first["builds"][k] for k in ("origin:1-1", "origin:2-1",) }, } output = apply_mask(first, second) self.assertEqual(output, expected_output) del first["builds"] del second["builds"] expected_output = { "version": first["version"], "revisions": { k: first["revisions"][k] for k in (IO_REVISION1["id"],) }, } output = apply_mask(first, second) self.assertEqual(output, expected_output)