Skip to content

Commit

Permalink
Merge pull request #322 from frankrousseau/master
Browse files Browse the repository at this point in the history
[sync] Allow to build model map on any field
  • Loading branch information
EvanBldy authored May 2, 2024
2 parents 4f58dfe + be17e8e commit 550c9bd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
16 changes: 9 additions & 7 deletions gazu/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand Down
32 changes: 25 additions & 7 deletions tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 550c9bd

Please sign in to comment.