From be17e8efc0bb5038efa946993cd99dcb78738d8b Mon Sep 17 00:00:00 2001 From: Frank Rousseau Date: Wed, 1 May 2024 23:15:43 +0200 Subject: [PATCH] [sync] Allow to build model map on any field Needed when the mapping occurs on the name. --- gazu/sync.py | 16 +++++++++------- tests/test_sync.py | 32 +++++++++++++++++++++++++------- 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/gazu/sync.py b/gazu/sync.py index 817f66bd..b7c7e1eb 100644 --- a/gazu/sync.py +++ b/gazu/sync.py @@ -88,7 +88,7 @@ def import_entity_links(links, client=default): return raw.post("import/kitsu/entity-links", links, client=client) -def get_model_list_diff(source_list, target_list): +def get_model_list_diff(source_list, target_list, id_field="id"): """ Args: source_list (list): List of models to compare. @@ -99,13 +99,15 @@ def get_model_list_diff(source_list, target_list): and one containing the models that should not be in the target list. """ missing = [] - source_ids = {m["id"]: True for m in source_list} - target_ids = {m["id"]: True for m in target_list} - for model in source_list: - if model["id"] not in target_ids: - missing.append(model) + source_ids = {m[id_field]: True for m in source_list} + target_ids = {m[id_field]: True for m in target_list} + missing = [ + model for model in source_list + if not target_ids.get(model[id_field], False) + ] unexpected = [ - model for model in target_list if model["id"] not in source_ids + model for model in target_list + if not source_ids.get(model[id_field], False) ] return (missing, unexpected) diff --git a/tests/test_sync.py b/tests/test_sync.py index bc54e579..0db83137 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -38,16 +38,34 @@ def test_import_entity_links(self): def test_get_model_list_diff(self): source_list = [ - {"id": "asset-1"}, - {"id": "asset-2"}, - {"id": "asset-3"}, + {"id": "asset-1", "name": "Asset 1"}, + {"id": "asset-2", "name": "Asset 2"}, + {"id": "asset-3", "name": "Asset 3"}, ] - target_list = [{"id": "asset-2"}, {"id": "asset-4"}] - (missing, unexpected) = gazu.sync.get_model_list_diff( + target_list = [ + {"id": "asset-2", "name": "Asset 2"}, + {"id": "asset-4", "name": "Asset 4"} + ] + missing, unexpected = gazu.sync.get_model_list_diff( source_list, target_list ) - self.assertEqual(missing, [{"id": "asset-1"}, {"id": "asset-3"}]) - self.assertEqual(unexpected, [{"id": "asset-4"}]) + self.assertEqual(missing, [ + {"id": "asset-1", "name": "Asset 1"}, + {"id": "asset-3", "name": "Asset 3"} + ]) + self.assertEqual(unexpected, [ + {"id": "asset-4", "name": "Asset 4"} + ]) + missing, unexpected = gazu.sync.get_model_list_diff( + source_list, target_list, id_field="name" + ) + self.assertEqual(missing, [ + {"id": "asset-1", "name": "Asset 1"}, + {"id": "asset-3", "name": "Asset 3"} + ]) + self.assertEqual(unexpected, [ + {"id": "asset-4", "name": "Asset 4"} + ]) source_list = [] target_list = [] (missing, unexpected) = gazu.sync.get_model_list_diff(