From 0b411c8c2abe79d876908d2e8a4c34e5ed82aa90 Mon Sep 17 00:00:00 2001 From: Ruge Li <91452427+rugeli@users.noreply.github.com> Date: Wed, 17 Jul 2024 15:28:04 -0700 Subject: [PATCH] Firebase uploader refactor: update multiple compositions in the same region (#269) * fix multiple comps update within the same region * fix test --- cellpack/autopack/DBRecipeHandler.py | 33 ++++++++++++++-------------- cellpack/tests/test_db_uploader.py | 2 +- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index c5b9bf22..e1930672 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -218,15 +218,14 @@ def check_and_replace_references( # replace nested objs in comp["regions"] if DataDoc.is_key(region_item): update_field_path = f"regions.{region_name}" + update_data = { + "index": update_field_path, + "name": region_item, + } if self.name in references_to_update: - references_to_update[self.name].update( - {"index": update_field_path, "name": region_item} - ) + references_to_update[self.name].append(update_data) else: - references_to_update[self.name] = { - "index": update_field_path, - "name": region_item, - } + references_to_update[self.name] = [update_data] elif not db.is_reference(region_item["object"]): obj_name = region_item["object"] region_item["object"] = objects_to_path_map.get(obj_name) @@ -554,7 +553,8 @@ def upload_compositions(self, compositions, recipe_to_save, recipe_data): "inherit": self.comp_to_path_map[comp_name]["path"] } if comp_name in references_to_update: - references_to_update[comp_name].update({"comp_id": doc_id}) + for inner_data in references_to_update[comp_name]: + inner_data["comp_id"] = doc_id return references_to_update def _get_recipe_id(self, recipe_data): @@ -586,15 +586,14 @@ def upload_collections(self, recipe_meta_data, recipe_data): # update nested comp in composition if references_to_update: for comp_name in references_to_update: - inner_data = references_to_update[comp_name] - comp_id = inner_data["comp_id"] - index = inner_data["index"] - name = inner_data["name"] - - item_id = self.comp_to_path_map[name]["id"] - CompositionDoc.update_reference( - self.db, comp_id, item_id, index, name, update_in_array=True - ) + for inner_data in references_to_update[comp_name]: + comp_id = inner_data["comp_id"] + index = inner_data["index"] + name = inner_data["name"] + item_id = self.comp_to_path_map[name]["id"] + CompositionDoc.update_reference( + self.db, comp_id, item_id, index, name, update_in_array=True + ) return recipe_to_save def upload_recipe(self, recipe_meta_data, recipe_data): diff --git a/cellpack/tests/test_db_uploader.py b/cellpack/tests/test_db_uploader.py index 89a02e69..0e36c303 100644 --- a/cellpack/tests/test_db_uploader.py +++ b/cellpack/tests/test_db_uploader.py @@ -104,7 +104,7 @@ def test_upload_compositions(): "space": {"id": "test_id", "path": "firebase:composition/test_id"}, } assert references_to_update == { - "space": {"comp_id": "test_id", "index": "regions.interior", "name": "A"} + "space": [{"comp_id": "test_id", "index": "regions.interior", "name": "A"}] }