From 7bb29809463cec2ba7e43c4ade112b9b5ecfe5bb Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Mon, 30 Jan 2023 12:12:15 -0800 Subject: [PATCH 01/21] test firebase data converters --- cellpack/autopack/DBRecipeHandler.py | 18 ++++++---- cellpack/tests/test_firebase_upload.py | 49 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 cellpack/tests/test_firebase_upload.py diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index ca78cd645..ae6195392 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -43,7 +43,8 @@ def should_write(self, collection, name, data): docs = self.db.get_doc_by_name(collection, name) if docs and len(docs) >= 1: for doc in docs: - full_doc_data = self.convert_sub_doc(doc) + doc_data = doc.to_dict() + full_doc_data = self.convert_sub_doc(doc_data) ddiff = DeepDiff(full_doc_data, data, ignore_order=True) if not ddiff: return doc, doc.id @@ -52,7 +53,9 @@ def should_write(self, collection, name, data): # add documents with auto IDs def to_db(self, collection, data, id=None): # check if we need to convert part of the data(2d arrays and objs to dict) + print("data to unpack>>", data ) modified_data = DBRecipeHandler.flatten_and_unpack(data) + print("unpack_data>>", modified_data) if id is None: name = modified_data["name"] _, doc_id = self.should_write(collection, name, modified_data) @@ -110,6 +113,7 @@ def update_reference( @staticmethod def convert_positions_in_representation(data): + print("position data)))", data) convert_data = {} for key, value in data.items(): if isinstance(value, list): @@ -120,15 +124,16 @@ def convert_positions_in_representation(data): ) else: data[key] = value + print("position converted", convert_data) return convert_data # get doc from database, convert it back to the original text # i.e. in object, convert lists back to tuples in representations/packing/positions # i.e. in comp, replace firebase link with the actual data - def convert_sub_doc(self, doc_ref): - doc = doc_ref.to_dict() - convert_doc = copy.deepcopy(doc) - for doc_key, doc_value in doc.items(): + def convert_sub_doc(self, doc_data): + print("db--", doc_data) + convert_doc = copy.deepcopy(doc_data) + for doc_key, doc_value in doc_data.items(): if ( doc_key == "representations" and "packing" in doc_value @@ -145,7 +150,7 @@ def convert_sub_doc(self, doc_ref): sub_doc, _ = self.db.get_doc_by_id(sub_doc_collection, sub_doc_id) convert_doc[doc_key] = sub_doc["name"] if doc_key == "regions": - for region_name, region_array in doc["regions"].items(): + for region_name, region_array in doc_data["regions"].items(): for region_item in region_array: if isinstance(region_item, dict): if "object" in region_item and self.db.is_firebase( @@ -176,6 +181,7 @@ def convert_sub_doc(self, doc_ref): convert_doc[doc_key][region_name][ region_array.index(region_item) ] = sub_doc["name"] + print("convert---", convert_doc) return convert_doc def divide_recipe_into_collections( diff --git a/cellpack/tests/test_firebase_upload.py b/cellpack/tests/test_firebase_upload.py new file mode 100644 index 000000000..144161308 --- /dev/null +++ b/cellpack/tests/test_firebase_upload.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Docs: https://docs.pytest.org/en/latest/example/simple.html + https://docs.pytest.org/en/latest/plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file +""" +import pytest +from cellpack.autopack.DBRecipeHandler import DBRecipeHandler +from cellpack.autopack.FirebaseHandler import FirebaseHandler +from cellpack.autopack.loaders.recipe_loader import RecipeLoader + +@pytest.mark.parametrize( + "input_data, converted_data", + [( + { + "bounding_box": [[0,0,0], [1000, 1000, 1]], + "positions": [[(0.0, 0.0, 0.0)], [(0.0, 50.0, 0.0), (43.26, -25.07, 0.0)]], + "max_jitter": [1,1,0] + }, + { + "bounding_box": {"0": [0,0,0], "1": [1000, 1000,1]}, + "positions": {"0": {"0": (0.0, 0.0, 0.0)}, "1": {"0": (0.0, 50.0, 0.0), "1": (43.26, -25.07, 0.0)}}, + "max_jitter": [1,1,0] + }, + ) + # TODO: add test case for representations object + ] +) + +def test_flatten_and_unpack_data(input_data, converted_data): + new_data = DBRecipeHandler.flatten_and_unpack(input_data) + assert new_data == converted_data + +@pytest.mark.parametrize( + "position_data_db, converted_position_data", + [( + { + "positions": {'0': {'0': [0.0, 0.0, 0.0]}, '1': {'0': [0.0, 50.0, 0.0], '1': [43.26, -25.07, 0.0]}} + }, + { + "positions": {'0': {'0': (0.0, 0.0, 0.0)}, '1': {'0': (0.0, 50.0, 0.0), '1': (43.26, -25.07, 0.0)}} + }, + )] +) + +def test_convert_position_in_representation(position_data_db, converted_position_data): + convert_position_to_tuple = DBRecipeHandler.convert_positions_in_representation(position_data_db) + assert convert_position_to_tuple == converted_position_data From dc0062bcd5a042165f67328badcba78be6680741 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 28 Mar 2023 20:50:37 -0700 Subject: [PATCH 02/21] test object doc and DB handler --- cellpack/tests/mocks/mock_db.py | 7 +++-- cellpack/tests/test_object_doc.py | 50 +++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/cellpack/tests/mocks/mock_db.py b/cellpack/tests/mocks/mock_db.py index 916b65bae..b258f4442 100644 --- a/cellpack/tests/mocks/mock_db.py +++ b/cellpack/tests/mocks/mock_db.py @@ -14,11 +14,14 @@ def doc_to_dict(doc): return doc def get_doc_by_name(self, _, name): - if name in self.data: - return self.data[name] + if len(self.data) >= 1: + if name in self.data["name"]: + return [self.data] else: return None def doc_id(self, doc): + if doc: + doc["id"] = "test_id" return doc["id"] diff --git a/cellpack/tests/test_object_doc.py b/cellpack/tests/test_object_doc.py index 25047d97a..4ac6ce2f6 100644 --- a/cellpack/tests/test_object_doc.py +++ b/cellpack/tests/test_object_doc.py @@ -1,10 +1,56 @@ -from cellpack.autopack.DBRecipeHandler import ObjectDoc +from cellpack.autopack.DBRecipeHandler import ObjectDoc, DBRecipeHandler from cellpack.tests.mocks.mock_db import MockDB mock_db = MockDB({}) -def test_object_doc_should_write(): +def test_object_doc_should_write_with_no_existing_doc(): object_doc = ObjectDoc("test", {"test_setting": "test_value"}) doc, doc_id = object_doc.should_write(mock_db) assert doc_id is None + assert doc is None + + +def test_object_doc_should_write_with_existing_doc(): + existing_doc = { + "name": "test", + "test_setting": "test_value" + } + mock_db.data = existing_doc + object_doc = ObjectDoc("test", {"test_setting": "test_value"}) + doc, doc_id = object_doc.should_write(mock_db) + assert doc_id is not None + assert doc == existing_doc + + +def test_convert_position_in_representation(): + position_data_db = { + "positions": {'0': {'0': [0.0, 0.0, 0.0]}, '1': {'0': [0.0, 50.0, 0.0], '1': [43.26, -25.07, 0.0]}} + } + converted_position_data = { + "positions": {'0': {'0': (0.0, 0.0, 0.0)}, '1': {'0': (0.0, 50.0, 0.0), '1': (43.26, -25.07, 0.0)}} + } + convert_position_to_tuple = ObjectDoc.convert_positions_in_representation(position_data_db) + assert convert_position_to_tuple == converted_position_data + +def test_convert_representation_with_object(): + obj = ObjectDoc("test", {"test_setting": "test_value"}) + doc = vars(obj) + expected_result = {"name": "test", "settings": {"test_setting": "test_value"}} + result = ObjectDoc.convert_representation(doc, mock_db) + assert result == expected_result + + +def test_prep_data_for_db(): + input_data = { + "bounding_box": [[0,0,0], [1000, 1000, 1]], + "positions": [[(0.0, 0.0, 0.0)], [(0.0, 50.0, 0.0), (43.26, -25.07, 0.0)]], + "max_jitter": [1,1,0] + } + converted_data = { + "bounding_box": {"0": [0,0,0], "1": [1000, 1000,1]}, + "positions": {"0": {"0": (0.0, 0.0, 0.0)}, "1": {"0": (0.0, 50.0, 0.0), "1": (43.26, -25.07, 0.0)}}, + "max_jitter": [1,1,0] + } + new_data = DBRecipeHandler.prep_data_for_db(input_data) + assert new_data == converted_data \ No newline at end of file From 1e6ab551212f8ed55578c0b14c49cf7dbaf17225 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 28 Mar 2023 20:50:57 -0700 Subject: [PATCH 03/21] merge and delete old test file --- cellpack/tests/test_firebase_upload.py | 49 -------------------------- 1 file changed, 49 deletions(-) delete mode 100644 cellpack/tests/test_firebase_upload.py diff --git a/cellpack/tests/test_firebase_upload.py b/cellpack/tests/test_firebase_upload.py deleted file mode 100644 index 144161308..000000000 --- a/cellpack/tests/test_firebase_upload.py +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -""" -Docs: https://docs.pytest.org/en/latest/example/simple.html - https://docs.pytest.org/en/latest/plugins.html#requiring-loading-plugins-in-a-test-module-or-conftest-file -""" -import pytest -from cellpack.autopack.DBRecipeHandler import DBRecipeHandler -from cellpack.autopack.FirebaseHandler import FirebaseHandler -from cellpack.autopack.loaders.recipe_loader import RecipeLoader - -@pytest.mark.parametrize( - "input_data, converted_data", - [( - { - "bounding_box": [[0,0,0], [1000, 1000, 1]], - "positions": [[(0.0, 0.0, 0.0)], [(0.0, 50.0, 0.0), (43.26, -25.07, 0.0)]], - "max_jitter": [1,1,0] - }, - { - "bounding_box": {"0": [0,0,0], "1": [1000, 1000,1]}, - "positions": {"0": {"0": (0.0, 0.0, 0.0)}, "1": {"0": (0.0, 50.0, 0.0), "1": (43.26, -25.07, 0.0)}}, - "max_jitter": [1,1,0] - }, - ) - # TODO: add test case for representations object - ] -) - -def test_flatten_and_unpack_data(input_data, converted_data): - new_data = DBRecipeHandler.flatten_and_unpack(input_data) - assert new_data == converted_data - -@pytest.mark.parametrize( - "position_data_db, converted_position_data", - [( - { - "positions": {'0': {'0': [0.0, 0.0, 0.0]}, '1': {'0': [0.0, 50.0, 0.0], '1': [43.26, -25.07, 0.0]}} - }, - { - "positions": {'0': {'0': (0.0, 0.0, 0.0)}, '1': {'0': (0.0, 50.0, 0.0), '1': (43.26, -25.07, 0.0)}} - }, - )] -) - -def test_convert_position_in_representation(position_data_db, converted_position_data): - convert_position_to_tuple = DBRecipeHandler.convert_positions_in_representation(position_data_db) - assert convert_position_to_tuple == converted_position_data From d8fab8defb8f364ee2f20e4b12e9ea45a075b562 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Thu, 30 Mar 2023 12:53:16 -0700 Subject: [PATCH 04/21] test object doc --- cellpack/autopack/DBRecipeHandler.py | 13 +++++++------ cellpack/tests/mocks/mock_db.py | 2 +- cellpack/tests/test_object_doc.py | 24 +++++++++++++++++------- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index 8c828a2a9..3767dda2b 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -214,6 +214,13 @@ def __init__( super().__init__() self.name = name self.settings = settings + + def as_dict(self): + data = dict() + data["name"] = self.name + for key in self.settings: + data[key] = self.settings[key] + return data @staticmethod def convert_positions_in_representation(data): @@ -252,12 +259,6 @@ def convert_representation(doc, db): ] = ObjectDoc.convert_positions_in_representation(position_value) return convert_doc - def as_dict(self): - data = dict() - data["name"] = self.name - for key in self.settings: - data[key] = self.settings[key] - return data def should_write(self, db): docs = db.get_doc_by_name("objects", self.name) diff --git a/cellpack/tests/mocks/mock_db.py b/cellpack/tests/mocks/mock_db.py index b258f4442..b949ccdd6 100644 --- a/cellpack/tests/mocks/mock_db.py +++ b/cellpack/tests/mocks/mock_db.py @@ -24,4 +24,4 @@ def doc_id(self, doc): if doc: doc["id"] = "test_id" return doc["id"] - + \ No newline at end of file diff --git a/cellpack/tests/test_object_doc.py b/cellpack/tests/test_object_doc.py index 4ac6ce2f6..96b4527cc 100644 --- a/cellpack/tests/test_object_doc.py +++ b/cellpack/tests/test_object_doc.py @@ -5,7 +5,7 @@ def test_object_doc_should_write_with_no_existing_doc(): - object_doc = ObjectDoc("test", {"test_setting": "test_value"}) + object_doc = ObjectDoc("test", {"test_key": "test_value"}) doc, doc_id = object_doc.should_write(mock_db) assert doc_id is None assert doc is None @@ -14,10 +14,11 @@ def test_object_doc_should_write_with_no_existing_doc(): def test_object_doc_should_write_with_existing_doc(): existing_doc = { "name": "test", - "test_setting": "test_value" + "test_key": "test_value" } mock_db.data = existing_doc - object_doc = ObjectDoc("test", {"test_setting": "test_value"}) + object_doc = ObjectDoc("test", {"test_key": "test_value"}) + doc, doc_id = object_doc.should_write(mock_db) assert doc_id is not None assert doc == existing_doc @@ -34,10 +35,19 @@ def test_convert_position_in_representation(): assert convert_position_to_tuple == converted_position_data def test_convert_representation_with_object(): - obj = ObjectDoc("test", {"test_setting": "test_value"}) - doc = vars(obj) - expected_result = {"name": "test", "settings": {"test_setting": "test_value"}} - result = ObjectDoc.convert_representation(doc, mock_db) + test_data = { + "name": "test_name", + "other_value": "other_value", + "representations": { + "packing": {"positions" : {"0": {}, "1": {}} + }}} + expected_result = { + "name": "test_name", + "other_value": "other_value", + "representations": { + "packing": {"positions" : {"0": {}, "1": {}} + }}} + result = ObjectDoc.convert_representation(test_data, mock_db) assert result == expected_result From 2fa0d16f38cbf18334ea45874c3578ff6bf3f21b Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Mon, 3 Apr 2023 17:40:52 -0700 Subject: [PATCH 05/21] [wip] test comp doc --- cellpack/autopack/DBRecipeHandler.py | 6 +- cellpack/tests/mocks/mock_db.py | 7 ++ cellpack/tests/test_comp_doc.py | 67 +++++++++++++++++++ cellpack/tests/test_db_recipe_handler.py | 19 ++++++ cellpack/tests/test_object_doc.py | 85 +++++++++++------------- 5 files changed, 136 insertions(+), 48 deletions(-) create mode 100644 cellpack/tests/test_comp_doc.py create mode 100644 cellpack/tests/test_db_recipe_handler.py diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index 3767dda2b..46e13964b 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -200,8 +200,8 @@ def should_write(self, db, recipe_data): ignore_type_in_groups=[tuple, list], ) if not difference: - return False, db.doc_id(doc) - return True, None + return doc, db.doc_id(doc) + return None, None class ObjectDoc(DataDoc): @@ -462,7 +462,7 @@ def upload_recipe(self, recipe_meta_data, recipe_data): recipe, _ = self.db.get_doc_by_id("recipes", recipe_id) if recipe: print(f"{recipe_id} is already in firestore") - return + # return recipe_to_save = self.upload_collections(recipe_meta_data, recipe_data) key = self.get_recipe_id(recipe_to_save) self.upload_data("recipes", recipe_to_save, key) diff --git a/cellpack/tests/mocks/mock_db.py b/cellpack/tests/mocks/mock_db.py index b949ccdd6..a838d95a1 100644 --- a/cellpack/tests/mocks/mock_db.py +++ b/cellpack/tests/mocks/mock_db.py @@ -16,6 +16,7 @@ def doc_to_dict(doc): def get_doc_by_name(self, _, name): if len(self.data) >= 1: if name in self.data["name"]: + print("self.data--- ", self.data) return [self.data] else: return None @@ -24,4 +25,10 @@ def doc_id(self, doc): if doc: doc["id"] = "test_id" return doc["id"] + + def if_reference(path): + return True + + def get_doc_by_ref(obj): + return {}, None \ No newline at end of file diff --git a/cellpack/tests/test_comp_doc.py b/cellpack/tests/test_comp_doc.py new file mode 100644 index 000000000..5e7ee7d0d --- /dev/null +++ b/cellpack/tests/test_comp_doc.py @@ -0,0 +1,67 @@ +from cellpack.autopack.DBRecipeHandler import CompositionDoc +from cellpack.tests.mocks.mock_db import MockDB + +mock_db = MockDB({}) + + +def test_composition_doc_as_dict(): + composition_doc = CompositionDoc( + name="test", + count=1, + object={"test_key": "test_value"}, + regions={}, + molarity=None, + ) + expected_dict = { + "name": "test", + "count": 1, + "object": {"test_key": "test_value"}, + "regions": {}, + "molarity": None, + } + assert composition_doc.as_dict() == expected_dict + +def test_composition_oc_should_write_with_no_existing_doc(): + recipe_data = { + "bounding_box": [[0,0,0], [10,10,10]], + "name": "test", + "count": 1, + "objects": None, + "regions": {} + } + composition_doc = CompositionDoc( + name="test", + count=1, + object=None, + regions={}, + molarity=None, + ) + doc, doc_id = composition_doc.should_write(mock_db,recipe_data) + assert doc_id is None + assert doc is None + +def test_composition_doc_should_write_with_existing_doc(): + existing_doc = { + "name": "test", + "count": 1, + "object": None, + "regions": {}, + "molarity": None, + } + mock_db.data = existing_doc + recipe_data = { + "name": "test", + "count": 1, + "objects": None, + } + composition_doc = CompositionDoc( + name="test", + count=1, + object=None, + regions={}, + molarity=None, + ) + + doc, doc_id = composition_doc.should_write(mock_db,recipe_data) + assert doc_id is not None + assert doc == existing_doc diff --git a/cellpack/tests/test_db_recipe_handler.py b/cellpack/tests/test_db_recipe_handler.py new file mode 100644 index 000000000..4c46eea9d --- /dev/null +++ b/cellpack/tests/test_db_recipe_handler.py @@ -0,0 +1,19 @@ +from cellpack.autopack.DBRecipeHandler import DBRecipeHandler +from cellpack.tests.mocks.mock_db import MockDB + +mock_db = MockDB({}) + + +def test_prep_data_for_db(): + input_data = { + "bounding_box": [[0,0,0], [1000, 1000, 1]], + "positions": [[(0.0, 0.0, 0.0)], [(0.0, 50.0, 0.0), (43.26, -25.07, 0.0)]], + "max_jitter": [1,1,0] + } + converted_data = { + "bounding_box": {"0": [0,0,0], "1": [1000, 1000,1]}, + "positions": {"0": {"0": (0.0, 0.0, 0.0)}, "1": {"0": (0.0, 50.0, 0.0), "1": (43.26, -25.07, 0.0)}}, + "max_jitter": [1,1,0] + } + new_data = DBRecipeHandler.prep_data_for_db(input_data) + assert new_data == converted_data \ No newline at end of file diff --git a/cellpack/tests/test_object_doc.py b/cellpack/tests/test_object_doc.py index 96b4527cc..6b82d2eac 100644 --- a/cellpack/tests/test_object_doc.py +++ b/cellpack/tests/test_object_doc.py @@ -1,66 +1,61 @@ -from cellpack.autopack.DBRecipeHandler import ObjectDoc, DBRecipeHandler +from cellpack.autopack.DBRecipeHandler import ObjectDoc from cellpack.tests.mocks.mock_db import MockDB mock_db = MockDB({}) -def test_object_doc_should_write_with_no_existing_doc(): +def test_object_doc_as_dict(): object_doc = ObjectDoc("test", {"test_key": "test_value"}) - doc, doc_id = object_doc.should_write(mock_db) - assert doc_id is None - assert doc is None - - -def test_object_doc_should_write_with_existing_doc(): - existing_doc = { - "name": "test", - "test_key": "test_value" - } - mock_db.data = existing_doc - object_doc = ObjectDoc("test", {"test_key": "test_value"}) - - doc, doc_id = object_doc.should_write(mock_db) - assert doc_id is not None - assert doc == existing_doc + expected_dict = {"name": "test", "test_key": "test_value"} + assert object_doc.as_dict() == expected_dict def test_convert_position_in_representation(): position_data_db = { - "positions": {'0': {'0': [0.0, 0.0, 0.0]}, '1': {'0': [0.0, 50.0, 0.0], '1': [43.26, -25.07, 0.0]}} + "positions": { + "0": {"0": [0.0, 0.0, 0.0]}, + "1": {"0": [0.0, 50.0, 0.0], "1": [43.26, -25.07, 0.0]}, } + } converted_position_data = { - "positions": {'0': {'0': (0.0, 0.0, 0.0)}, '1': {'0': (0.0, 50.0, 0.0), '1': (43.26, -25.07, 0.0)}} + "positions": { + "0": {"0": (0.0, 0.0, 0.0)}, + "1": {"0": (0.0, 50.0, 0.0), "1": (43.26, -25.07, 0.0)}, } - convert_position_to_tuple = ObjectDoc.convert_positions_in_representation(position_data_db) + } + convert_position_to_tuple = ObjectDoc.convert_positions_in_representation( + position_data_db + ) assert convert_position_to_tuple == converted_position_data -def test_convert_representation_with_object(): + +def test_convert_representation(): test_data = { - "name": "test_name", - "other_value": "other_value", - "representations": { - "packing": {"positions" : {"0": {}, "1": {}} - }}} + "name": "test_name", + "other_value": "other_value", + "representations": {"packing": {"positions": {"0": {}, "1": {}}}}, + } expected_result = { - "name": "test_name", - "other_value": "other_value", - "representations": { - "packing": {"positions" : {"0": {}, "1": {}} - }}} + "name": "test_name", + "other_value": "other_value", + "representations": {"packing": {"positions": {"0": {}, "1": {}}}}, + } result = ObjectDoc.convert_representation(test_data, mock_db) assert result == expected_result -def test_prep_data_for_db(): - input_data = { - "bounding_box": [[0,0,0], [1000, 1000, 1]], - "positions": [[(0.0, 0.0, 0.0)], [(0.0, 50.0, 0.0), (43.26, -25.07, 0.0)]], - "max_jitter": [1,1,0] - } - converted_data = { - "bounding_box": {"0": [0,0,0], "1": [1000, 1000,1]}, - "positions": {"0": {"0": (0.0, 0.0, 0.0)}, "1": {"0": (0.0, 50.0, 0.0), "1": (43.26, -25.07, 0.0)}}, - "max_jitter": [1,1,0] - } - new_data = DBRecipeHandler.prep_data_for_db(input_data) - assert new_data == converted_data \ No newline at end of file +def test_object_doc_should_write_with_no_existing_doc(): + object_doc = ObjectDoc("test", {"test_key": "test_value"}) + doc, doc_id = object_doc.should_write(mock_db) + assert doc_id is None + assert doc is None + + +def test_object_doc_should_write_with_existing_doc(): + existing_doc = {"name": "test", "test_key": "test_value"} + mock_db.data = existing_doc + object_doc = ObjectDoc("test", {"test_key": "test_value"}) + + doc, doc_id = object_doc.should_write(mock_db) + assert doc_id is not None + assert doc == existing_doc From 9be219ea32be0d3b8aebebafaed67d773ea1c137 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 4 Apr 2023 13:01:49 -0700 Subject: [PATCH 06/21] remove old comment --- cellpack/tests/test_recipe_version_migration.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cellpack/tests/test_recipe_version_migration.py b/cellpack/tests/test_recipe_version_migration.py index f93d36afd..9ef4b9e9c 100644 --- a/cellpack/tests/test_recipe_version_migration.py +++ b/cellpack/tests/test_recipe_version_migration.py @@ -329,7 +329,6 @@ def test_convert_v1_to_v2( assert new_recipe["composition"] == expected_composition_dict -# to-do: fix duplicate / @pytest.mark.parametrize( "converted_data, expected_data", [ From fb5bb3ec23744e2ffed750a92df8dc515ad4c3fb Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 4 Apr 2023 14:46:15 -0700 Subject: [PATCH 07/21] [wip] testing comp doc --- cellpack/tests/mocks/mock_db.py | 12 ++- cellpack/tests/test_comp_doc.py | 182 +++++++++++++++++++++++++++++++- 2 files changed, 186 insertions(+), 8 deletions(-) diff --git a/cellpack/tests/mocks/mock_db.py b/cellpack/tests/mocks/mock_db.py index a838d95a1..ae3e094e7 100644 --- a/cellpack/tests/mocks/mock_db.py +++ b/cellpack/tests/mocks/mock_db.py @@ -16,7 +16,6 @@ def doc_to_dict(doc): def get_doc_by_name(self, _, name): if len(self.data) >= 1: if name in self.data["name"]: - print("self.data--- ", self.data) return [self.data] else: return None @@ -26,9 +25,14 @@ def doc_id(self, doc): doc["id"] = "test_id" return doc["id"] - def if_reference(path): + @staticmethod + def is_reference(path): return True - def get_doc_by_ref(obj): - return {}, None + @staticmethod + def get_doc_by_ref(key): + if key: + return {"test": "downloaded_data"}, key + else: + return {}, None \ No newline at end of file diff --git a/cellpack/tests/test_comp_doc.py b/cellpack/tests/test_comp_doc.py index 5e7ee7d0d..77567ea91 100644 --- a/cellpack/tests/test_comp_doc.py +++ b/cellpack/tests/test_comp_doc.py @@ -21,13 +21,186 @@ def test_composition_doc_as_dict(): } assert composition_doc.as_dict() == expected_dict + +def test_get_reference_data_with_dict(): + composition_db_doc = CompositionDoc( + name="test", + count=1, + object={"object": "firebase:objects/testuid"}, + regions={}, + molarity=None, + ) + downloaded_data, key = composition_db_doc.get_reference_data( + composition_db_doc.as_dict()["object"], mock_db + ) + assert downloaded_data == {"test": "downloaded_data"} + assert key == "firebase:objects/testuid" + + +def test_get_reference_data_with_key(): + composition_db_doc = CompositionDoc( + name="test", + count=1, + object="firebase:objects/testuid", + regions={}, + molarity=None, + ) + downloaded_data, key = composition_db_doc.get_reference_data( + composition_db_doc.as_dict()["object"], mock_db + ) + assert downloaded_data == {"test": "downloaded_data"} + assert key == None + + +def test_get_reference_data_with_none(): + composition_db_doc = CompositionDoc( + name="test", + count=1, + object=None, + regions={}, + molarity=None, + ) + downloaded_data, key = composition_db_doc.get_reference_data( + composition_db_doc.as_dict()["object"], mock_db + ) + assert downloaded_data == {} + assert key == None + + +def test_resolve_db_regions(): + composition_db_doc = CompositionDoc( + name="test", + count=1, + object=None, + regions={ + "test_region_name": [ + "firebase:composition/testuid", + {"count": 1, "object": "firebase:objects/testuid"}, + ] + }, + molarity=None, + ) + resolved_data = { + "name": "test", + "object": None, + "count": 1, + "molarity": None, + "regions": { + "test_region_name": [ + {"test": "downloaded_data"}, + {"count": 1, "object": {"test": "downloaded_data"}}, + ] + }, + } + composition_db_doc.resolve_db_regions(composition_db_doc.as_dict(), mock_db) + assert composition_db_doc.as_dict() == resolved_data + + +def test_resolve_db_regions_with_none(): + composition_db_doc = CompositionDoc( + name="test", + count=1, + object=None, + regions=None, + molarity=None, + ) + resolved_data = { + "name": "test", + "object": None, + "count": 1, + "molarity": None, + "regions": {}, + } + composition_db_doc.resolve_db_regions(composition_db_doc.as_dict(), mock_db) + assert composition_db_doc.as_dict() == resolved_data + + +def test_resolve_local_regions(): + full_recipe_data = { + "name": "one_sphere", + "objects": { + "sphere_25": { + "type": "single_sphere", + "max_jitter": [1, 1, 0], + }, + }, + "composition": { + "space": { + "regions": { + "interior": [ + "A" + ] + } + }, + "A": { + "object": "sphere_25", + "count": 500 + }, + }, + } + + local_data = CompositionDoc( + name="space", + count=None, + object=None, + regions={ + "interior": [ + "A", + ] + }, + molarity=None, + ) + + resolved_data = { + "name": "space", + "object": None, + "count": None, + "molarity": None, + "regions": { + "interior": [ + { + "name": "A", + "object": {"type": "single_sphere", "max_jitter": [1, 1, 0]}, + "count": 500, + "molarity": None, + "regions": {}, + } + ] + }, + } + local_data.resolve_local_regions(local_data.as_dict(), full_recipe_data, mock_db) + assert local_data.as_dict() == resolved_data + +# def test_check_and_replace_references(): +# objects_to_path_map = {"test_obj": "firebase:objects/testuid"} +# references_to_update = { +# "test_comp": { +# "index": "regions.interior", +# "name": "nested_comp", +# "comp_id": "firebase:composition/testuid", +# } +# } +# composition_doc = CompositionDoc( +# name="test", +# count=1, +# object={"object": "firebase:objects/testuid"}, +# regions={}, +# molarity=None, +# ) +# composition_doc.check_and_replace_references( +# objects_to_path_map, references_to_update, mock_db +# ) +# print("6", composition_doc.as_dict()) +# assert composition_doc.as_dict()["object"] == {"test": "downloaded_data"} + + def test_composition_oc_should_write_with_no_existing_doc(): recipe_data = { - "bounding_box": [[0,0,0], [10,10,10]], + "bounding_box": [[0, 0, 0], [10, 10, 10]], "name": "test", "count": 1, "objects": None, - "regions": {} + "regions": {}, } composition_doc = CompositionDoc( name="test", @@ -36,10 +209,11 @@ def test_composition_oc_should_write_with_no_existing_doc(): regions={}, molarity=None, ) - doc, doc_id = composition_doc.should_write(mock_db,recipe_data) + doc, doc_id = composition_doc.should_write(mock_db, recipe_data) assert doc_id is None assert doc is None + def test_composition_doc_should_write_with_existing_doc(): existing_doc = { "name": "test", @@ -62,6 +236,6 @@ def test_composition_doc_should_write_with_existing_doc(): molarity=None, ) - doc, doc_id = composition_doc.should_write(mock_db,recipe_data) + doc, doc_id = composition_doc.should_write(mock_db, recipe_data) assert doc_id is not None assert doc == existing_doc From 165dc0b1bcaa391078701da609887f4db6b0dbbe Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 4 Apr 2023 15:25:48 -0700 Subject: [PATCH 08/21] little reorg in comp class --- cellpack/autopack/DBRecipeHandler.py | 77 ++++++++++++++++++---------- 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index 46e13964b..45bb481dc 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -54,7 +54,8 @@ def as_dict(self): data["regions"] = self.regions return data - def get_reference_data(self, key_or_dict, db): + @staticmethod + def get_reference_data(key_or_dict, db): """ Returns the db data for a reference, and the key if it exists. Key --> the name of a composition @@ -166,6 +167,30 @@ def check_and_replace_references( elif not db.is_reference(region_item["object"]): obj_name = region_item["object"] region_item["object"] = objects_to_path_map.get(obj_name) + @staticmethod + def update_reference( + db, + composition_id, + referring_comp_id, + index, + remove_comp_name, + update_in_array=False, + ): + """ + Update comp references in the recipe + """ + doc, doc_ref = db.get_doc_by_id("composition", composition_id) + if doc is None: + return + else: + _, new_item_ref = db.get_doc_by_id("composition", referring_comp_id) + update_ref_path = f"{db.name}:{new_item_ref.path}" + if update_in_array: + db.update_elements_in_array( + doc_ref, index, update_ref_path, remove_comp_name + ) + else: + db.update_reference_on_doc(doc_ref, index, update_ref_path) def should_write(self, db, recipe_data): """ @@ -400,29 +425,29 @@ def get_recipe_id(self, recipe_data): key = f"{recipe_name}_v{recipe_version}" return key - def update_reference( - self, - composition_id, - referring_comp_id, - index, - remove_comp_name, - update_in_array=False, - ): - """ - Update comp references in the recipe - """ - doc, doc_ref = self.db.get_doc_by_id("composition", composition_id) - if doc is None: - return - else: - _, new_item_ref = self.db.get_doc_by_id("composition", referring_comp_id) - update_ref_path = f"{self.db.name}:{new_item_ref.path}" - if update_in_array: - self.db.update_elements_in_array( - doc_ref, index, update_ref_path, remove_comp_name - ) - else: - self.db.update_reference_on_doc(doc_ref, index, update_ref_path) + # def update_reference( + # self, + # composition_id, + # referring_comp_id, + # index, + # remove_comp_name, + # update_in_array=False, + # ): + # """ + # Update comp references in the recipe + # """ + # doc, doc_ref = self.db.get_doc_by_id("composition", composition_id) + # if doc is None: + # return + # else: + # _, new_item_ref = self.db.get_doc_by_id("composition", referring_comp_id) + # update_ref_path = f"{self.db.name}:{new_item_ref.path}" + # if update_in_array: + # self.db.update_elements_in_array( + # doc_ref, index, update_ref_path, remove_comp_name + # ) + # else: + # self.db.update_reference_on_doc(doc_ref, index, update_ref_path) def upload_collections(self, recipe_meta_data, recipe_data): """ @@ -448,8 +473,8 @@ def upload_collections(self, recipe_meta_data, recipe_data): name = inner_data["name"] item_id = self.comp_to_path_map[name]["id"] - self.update_reference( - comp_id, item_id, index, name, update_in_array=True + CompositionDoc.update_reference( + self.db, comp_id, item_id, index, name, update_in_array=True ) return recipe_to_save From 978e5329a07de43c708921ba864f4e954207f55e Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 4 Apr 2023 20:58:02 -0700 Subject: [PATCH 09/21] small fix on get_reference_data --- cellpack/autopack/DBRecipeHandler.py | 30 ++-------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index 45bb481dc..47a802c74 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -65,14 +65,13 @@ def get_reference_data(key_or_dict, db): key = key_or_dict downloaded_data, _ = db.get_doc_by_ref(key) return downloaded_data, None - else: + elif key_or_dict and isinstance(key_or_dict, dict): object_dict = key_or_dict if "object" in object_dict and db.is_reference(object_dict["object"]): key = object_dict["object"] downloaded_data, _ = db.get_doc_by_ref(key) return downloaded_data, key - else: - return {}, None + return {}, None def resolve_db_regions(self, db_data, db): """ @@ -163,7 +162,6 @@ def check_and_replace_references( "index": update_field_path, "name": region_item, } - elif not db.is_reference(region_item["object"]): obj_name = region_item["object"] region_item["object"] = objects_to_path_map.get(obj_name) @@ -425,30 +423,6 @@ def get_recipe_id(self, recipe_data): key = f"{recipe_name}_v{recipe_version}" return key - # def update_reference( - # self, - # composition_id, - # referring_comp_id, - # index, - # remove_comp_name, - # update_in_array=False, - # ): - # """ - # Update comp references in the recipe - # """ - # doc, doc_ref = self.db.get_doc_by_id("composition", composition_id) - # if doc is None: - # return - # else: - # _, new_item_ref = self.db.get_doc_by_id("composition", referring_comp_id) - # update_ref_path = f"{self.db.name}:{new_item_ref.path}" - # if update_in_array: - # self.db.update_elements_in_array( - # doc_ref, index, update_ref_path, remove_comp_name - # ) - # else: - # self.db.update_reference_on_doc(doc_ref, index, update_ref_path) - def upload_collections(self, recipe_meta_data, recipe_data): """ Separate collections from recipe data and upload them to db From b8d66e96580460ebb88e934aede61888fb7055ec Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 4 Apr 2023 20:58:48 -0700 Subject: [PATCH 10/21] complete comp testing --- cellpack/tests/mocks/mock_db.py | 6 +++++- cellpack/tests/test_comp_doc.py | 36 ++++++++++++++------------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/cellpack/tests/mocks/mock_db.py b/cellpack/tests/mocks/mock_db.py index ae3e094e7..176bc3931 100644 --- a/cellpack/tests/mocks/mock_db.py +++ b/cellpack/tests/mocks/mock_db.py @@ -27,7 +27,11 @@ def doc_id(self, doc): @staticmethod def is_reference(path): - return True + if path is None: + return False + if path.startswith("firebase:"): + return True + return False @staticmethod def get_doc_by_ref(key): diff --git a/cellpack/tests/test_comp_doc.py b/cellpack/tests/test_comp_doc.py index 77567ea91..3368733f9 100644 --- a/cellpack/tests/test_comp_doc.py +++ b/cellpack/tests/test_comp_doc.py @@ -171,27 +171,21 @@ def test_resolve_local_regions(): local_data.resolve_local_regions(local_data.as_dict(), full_recipe_data, mock_db) assert local_data.as_dict() == resolved_data -# def test_check_and_replace_references(): -# objects_to_path_map = {"test_obj": "firebase:objects/testuid"} -# references_to_update = { -# "test_comp": { -# "index": "regions.interior", -# "name": "nested_comp", -# "comp_id": "firebase:composition/testuid", -# } -# } -# composition_doc = CompositionDoc( -# name="test", -# count=1, -# object={"object": "firebase:objects/testuid"}, -# regions={}, -# molarity=None, -# ) -# composition_doc.check_and_replace_references( -# objects_to_path_map, references_to_update, mock_db -# ) -# print("6", composition_doc.as_dict()) -# assert composition_doc.as_dict()["object"] == {"test": "downloaded_data"} +def test_check_and_replace_references(): + objects_to_path_map = {"test_obj": "firebase:objects/testuid"} + references_to_update = {} + composition_doc = CompositionDoc( + name="test", + count=1, + object="firebase:objects/testuid", + regions={"interior": [{"object": "test_obj", "count": 1}]}, + molarity=None, + ) + composition_doc.check_and_replace_references( + objects_to_path_map, references_to_update, mock_db + ) + assert composition_doc.as_dict()["object"] == "firebase:objects/testuid" + assert composition_doc.as_dict()["regions"] == {"interior": [{"object": "firebase:objects/testuid", "count": 1}]} def test_composition_oc_should_write_with_no_existing_doc(): From 325e889fa2bd4c92a6ab144a48bd2f5efc42c938 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Mon, 10 Apr 2023 16:03:54 -0700 Subject: [PATCH 11/21] add new mock functions in MockDB --- cellpack/tests/mocks/mock_db.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/cellpack/tests/mocks/mock_db.py b/cellpack/tests/mocks/mock_db.py index 176bc3931..ac057d730 100644 --- a/cellpack/tests/mocks/mock_db.py +++ b/cellpack/tests/mocks/mock_db.py @@ -1,14 +1,21 @@ +from unittest.mock import Mock + + class MockDB(object): def __init__(self, data) -> None: for index, name in enumerate(data): obj = data[name] obj["id"] = index self.data = data + self.name = "test_db" @staticmethod def is_firebase_obj(obj): return True + def db_name(self): + return "test_db" + @staticmethod def doc_to_dict(doc): return doc @@ -39,4 +46,21 @@ def get_doc_by_ref(key): return {"test": "downloaded_data"}, key else: return {}, None + + def upload_doc(self, collection, doc): + return ("test_datetime", doc) + + def set_doc(self, collection, id, data): + doc_ref = Mock() + return doc_ref + + def get_path_from_ref(self, doc): + return "firebase:test_collection/test_id" + + def create_path(self, collection, doc_id): + return f"firebase:{collection}/{doc_id}" + + def get_doc_by_id(self, collection, id): + doc_ref = Mock() + return self.data, doc_ref \ No newline at end of file From 1f4ee4b7dffe78bd00152f1dfd50ddb9c74bb309 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Mon, 10 Apr 2023 16:04:33 -0700 Subject: [PATCH 12/21] minor reorgs in DBRecipeHandler --- cellpack/autopack/DBRecipeHandler.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index 47a802c74..808d18e38 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -182,7 +182,7 @@ def update_reference( return else: _, new_item_ref = db.get_doc_by_id("composition", referring_comp_id) - update_ref_path = f"{db.name}:{new_item_ref.path}" + update_ref_path = f"{db.db_name()}:{db.get_path_from_ref(new_item_ref)}" if update_in_array: db.update_elements_in_array( doc_ref, index, update_ref_path, remove_comp_name @@ -339,7 +339,7 @@ def prep_data_for_db(data): modified_data[key] = value return modified_data - # + def upload_data(self, collection, data, id=None): """ If should_write is true, upload the data to the database @@ -350,7 +350,7 @@ def upload_data(self, collection, data, id=None): name = modified_data["name"] doc = self.db.upload_doc(collection, modified_data) # doc is a tuple, e.g (DatetimeWithNanoseconds, data_obj) - doc_path = doc[1].path + doc_path = self.db.get_path_from_ref(doc[1]) doc_id = self.db.doc_id(doc[1]) print(f"successfully uploaded {name} to path: {doc_path}") return doc_id, self.db.create_path(collection, doc_id) @@ -365,7 +365,7 @@ def upload_objects(self, objects): object_doc = ObjectDoc(name=obj_name, settings=objects[obj_name]) _, doc_id = object_doc.should_write(self.db) if doc_id: - print(f"objects/{object_doc.name} is already exists in firestore") + print(f"objects/{object_doc.name} is already in firestore") else: _, obj_path = self.upload_data("objects", object_doc.as_dict()) self.objects_to_path_map[obj_name] = obj_path @@ -392,7 +392,7 @@ def upload_compositions(self, compositions, recipe_to_save, recipe_data): path = self.db.create_path("composition", doc_id) self.comp_to_path_map[comp_name]["path"] = path self.comp_to_path_map[comp_name]["id"] = doc_id - print(f"composition/{comp_name} is already exists in firestore") + print(f"composition/{comp_name} is already in firestore") else: # replace with paths for outer objs in comp, then upload comp_doc.check_and_replace_references( From ece039f627b9daf7e89ef53dd05a7ac807604ef3 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Mon, 10 Apr 2023 16:05:20 -0700 Subject: [PATCH 13/21] rename test id --- cellpack/tests/test_comp_doc.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cellpack/tests/test_comp_doc.py b/cellpack/tests/test_comp_doc.py index 3368733f9..554b3cbba 100644 --- a/cellpack/tests/test_comp_doc.py +++ b/cellpack/tests/test_comp_doc.py @@ -26,7 +26,7 @@ def test_get_reference_data_with_dict(): composition_db_doc = CompositionDoc( name="test", count=1, - object={"object": "firebase:objects/testuid"}, + object={"object": "firebase:objects/test_id"}, regions={}, molarity=None, ) @@ -34,14 +34,14 @@ def test_get_reference_data_with_dict(): composition_db_doc.as_dict()["object"], mock_db ) assert downloaded_data == {"test": "downloaded_data"} - assert key == "firebase:objects/testuid" + assert key == "firebase:objects/test_id" def test_get_reference_data_with_key(): composition_db_doc = CompositionDoc( name="test", count=1, - object="firebase:objects/testuid", + object="firebase:objects/test_id", regions={}, molarity=None, ) @@ -74,8 +74,8 @@ def test_resolve_db_regions(): object=None, regions={ "test_region_name": [ - "firebase:composition/testuid", - {"count": 1, "object": "firebase:objects/testuid"}, + "firebase:composition/test_id", + {"count": 1, "object": "firebase:objects/test_id"}, ] }, molarity=None, @@ -172,20 +172,20 @@ def test_resolve_local_regions(): assert local_data.as_dict() == resolved_data def test_check_and_replace_references(): - objects_to_path_map = {"test_obj": "firebase:objects/testuid"} + objects_to_path_map = {"test_obj": "firebase:objects/test_id"} references_to_update = {} composition_doc = CompositionDoc( name="test", count=1, - object="firebase:objects/testuid", + object="firebase:objects/test_id", regions={"interior": [{"object": "test_obj", "count": 1}]}, molarity=None, ) composition_doc.check_and_replace_references( objects_to_path_map, references_to_update, mock_db ) - assert composition_doc.as_dict()["object"] == "firebase:objects/testuid" - assert composition_doc.as_dict()["regions"] == {"interior": [{"object": "firebase:objects/testuid", "count": 1}]} + assert composition_doc.as_dict()["object"] == "firebase:objects/test_id" + assert composition_doc.as_dict()["regions"] == {"interior": [{"object": "firebase:objects/test_id", "count": 1}]} def test_composition_oc_should_write_with_no_existing_doc(): From 2d10a4c7ffa104cd235ff49792321ac672e96696 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Mon, 10 Apr 2023 16:05:45 -0700 Subject: [PATCH 14/21] [wip]recipe handler testing --- cellpack/autopack/FirebaseHandler.py | 8 +- cellpack/tests/test_db_recipe_handler.py | 124 +++++++++++++++++++++-- 2 files changed, 122 insertions(+), 10 deletions(-) diff --git a/cellpack/autopack/FirebaseHandler.py b/cellpack/autopack/FirebaseHandler.py index 81cf41571..c8934181d 100644 --- a/cellpack/autopack/FirebaseHandler.py +++ b/cellpack/autopack/FirebaseHandler.py @@ -15,7 +15,9 @@ def __init__(self, cred_path): @staticmethod def doc_to_dict(doc): return doc.to_dict() - + + def db_name(self): + return self.name @staticmethod def doc_id(doc): @@ -24,6 +26,10 @@ def doc_id(doc): @staticmethod def create_path(collection, doc_id): return f"firebase:{collection}/{doc_id}" + + @staticmethod + def get_path_from_ref(doc): + return doc.path @staticmethod def get_collection_id_from_path(path): diff --git a/cellpack/tests/test_db_recipe_handler.py b/cellpack/tests/test_db_recipe_handler.py index 4c46eea9d..54a474600 100644 --- a/cellpack/tests/test_db_recipe_handler.py +++ b/cellpack/tests/test_db_recipe_handler.py @@ -4,16 +4,122 @@ mock_db = MockDB({}) +def test_is_nested_list(): + assert DBRecipeHandler.is_nested_list([]) == False + assert DBRecipeHandler.is_nested_list([[], []]) == True + assert DBRecipeHandler.is_nested_list([[1, 2], [3, 4]]) == True + + def test_prep_data_for_db(): input_data = { - "bounding_box": [[0,0,0], [1000, 1000, 1]], - "positions": [[(0.0, 0.0, 0.0)], [(0.0, 50.0, 0.0), (43.26, -25.07, 0.0)]], - "max_jitter": [1,1,0] - } + "bounding_box": [[0, 0, 0], [1000, 1000, 1]], + "positions": [[(0.0, 0.0, 0.0)], [(0.0, 50.0, 0.0), (43.26, -25.07, 0.0)]], + "max_jitter": [1, 1, 0], + } converted_data = { - "bounding_box": {"0": [0,0,0], "1": [1000, 1000,1]}, - "positions": {"0": {"0": (0.0, 0.0, 0.0)}, "1": {"0": (0.0, 50.0, 0.0), "1": (43.26, -25.07, 0.0)}}, - "max_jitter": [1,1,0] - } + "bounding_box": {"0": [0, 0, 0], "1": [1000, 1000, 1]}, + "positions": { + "0": {"0": (0.0, 0.0, 0.0)}, + "1": {"0": (0.0, 50.0, 0.0), "1": (43.26, -25.07, 0.0)}, + }, + "max_jitter": [1, 1, 0], + } new_data = DBRecipeHandler.prep_data_for_db(input_data) - assert new_data == converted_data \ No newline at end of file + assert new_data == converted_data + + +def test_upload_data_with_recipe_and_id(): + collection = "recipe" + data = { + "name": "test", + "bounding_box": [[0, 0, 0], [1000, 1000, 1]], + "version": "1.0.0", + "composition": {"test": {"inherit": "firebase:test_collection/test_id"}}, + } + id = "test_id" + recipe_doc = DBRecipeHandler(mock_db) + expected_result = recipe_doc.upload_data(collection, data, id) + + assert expected_result[0] == "test_id" + assert expected_result[1] == "firebase:recipe/test_id" + + +def test_upload_data_with_object(): + collection = "objects" + data = { + "name": "test", + "test_key": "test_value", + } + object_doc = DBRecipeHandler(mock_db) + expected_result = object_doc.upload_data(collection, data) + + assert expected_result[0] == "test_id" + assert expected_result[1] == "firebase:objects/test_id" + + +def test_upload_objects(): + data = {"test": {"test_key": "test_value"}} + object_doc = DBRecipeHandler(mock_db) + object_doc.upload_objects(data) + + assert object_doc.objects_to_path_map == {"test": "firebase:objects/test_id"} + + +def test_upload_compositions(): + composition = { + "space": {"regions": {"interior": ["A"]}}, + } + recipe_to_save = {"format_version": "2.1", "name": "one_sphere", "composition": {}} + recipe_data = { + "name": "one_sphere", + "objects": { + "sphere_25": { + "type": "single_sphere", + "max_jitter": [1, 1, 0], + }, + }, + "composition": { + "space": {"regions": {"interior": ["A"]}}, + }, + } + + composition_doc = DBRecipeHandler(mock_db) + references_to_update = composition_doc.upload_compositions(composition, recipe_to_save, recipe_data) + assert composition_doc.comp_to_path_map == { + "space": {"id": "test_id", "path": "firebase:composition/test_id"}, + } + assert references_to_update == {"space": {"comp_id": "test_id", "index": "regions.interior", "name": "A"}} + +def test_get_recipe_id(): + recipe_data = { + "name": "test", + "version": "1.0.0", + "objects": None, + "composition": {}, +} + recipe_doc = DBRecipeHandler(mock_db) + assert recipe_doc.get_recipe_id(recipe_data) == "test_v1.0.0" + +# def test_upload_collections(): +# recipe_meta_data = { +# "name": "one_sphere", +# "version": "1.0.0", +# "composition": {}, +# } +# recipe_data = { +# "name": "one_sphere", +# "objects": { +# "sphere_25": { +# "type": "single_sphere", +# "max_jitter": [1, 1, 0], +# }, +# }, +# "composition": { +# "space": {"regions": {"interior": ["A"]}}, +# "A": {"object": "sphere_25", "count": 1}, +# }, +# } + +# collection_doc = DBRecipeHandler(mock_db) +# collection_doc.upload_collections(recipe_meta_data, recipe_data) +# assert collection_doc.recipe_to_save == {"test_v1.0.0": "firebase:recipe/test_v1.0.0"} \ No newline at end of file From aa3d28fe7a6ea22d863d11e936a792cacee8c057 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Mon, 10 Apr 2023 21:23:33 -0700 Subject: [PATCH 15/21] complete db recipe handler tests --- cellpack/tests/mocks/mock_db.py | 7 +- cellpack/tests/test_db_recipe_handler.py | 104 ++++++++++++++++------- 2 files changed, 80 insertions(+), 31 deletions(-) diff --git a/cellpack/tests/mocks/mock_db.py b/cellpack/tests/mocks/mock_db.py index ac057d730..0d627eb83 100644 --- a/cellpack/tests/mocks/mock_db.py +++ b/cellpack/tests/mocks/mock_db.py @@ -63,4 +63,9 @@ def create_path(self, collection, doc_id): def get_doc_by_id(self, collection, id): doc_ref = Mock() return self.data, doc_ref - \ No newline at end of file + + def update_reference_on_doc(self, doc_ref, index, new_item_ref): + return True + + def update_elements_in_array(self, doc_ref, index, new_item_ref, remove_item): + return True \ No newline at end of file diff --git a/cellpack/tests/test_db_recipe_handler.py b/cellpack/tests/test_db_recipe_handler.py index 54a474600..a7fadb4c3 100644 --- a/cellpack/tests/test_db_recipe_handler.py +++ b/cellpack/tests/test_db_recipe_handler.py @@ -84,42 +84,86 @@ def test_upload_compositions(): } composition_doc = DBRecipeHandler(mock_db) - references_to_update = composition_doc.upload_compositions(composition, recipe_to_save, recipe_data) + references_to_update = composition_doc.upload_compositions( + composition, recipe_to_save, recipe_data + ) assert composition_doc.comp_to_path_map == { "space": {"id": "test_id", "path": "firebase:composition/test_id"}, } - assert references_to_update == {"space": {"comp_id": "test_id", "index": "regions.interior", "name": "A"}} + assert references_to_update == { + "space": {"comp_id": "test_id", "index": "regions.interior", "name": "A"} + } + def test_get_recipe_id(): recipe_data = { - "name": "test", - "version": "1.0.0", - "objects": None, - "composition": {}, -} + "name": "test", + "version": "1.0.0", + "objects": None, + "composition": {}, + } recipe_doc = DBRecipeHandler(mock_db) assert recipe_doc.get_recipe_id(recipe_data) == "test_v1.0.0" -# def test_upload_collections(): -# recipe_meta_data = { -# "name": "one_sphere", -# "version": "1.0.0", -# "composition": {}, -# } -# recipe_data = { -# "name": "one_sphere", -# "objects": { -# "sphere_25": { -# "type": "single_sphere", -# "max_jitter": [1, 1, 0], -# }, -# }, -# "composition": { -# "space": {"regions": {"interior": ["A"]}}, -# "A": {"object": "sphere_25", "count": 1}, -# }, -# } - -# collection_doc = DBRecipeHandler(mock_db) -# collection_doc.upload_collections(recipe_meta_data, recipe_data) -# assert collection_doc.recipe_to_save == {"test_v1.0.0": "firebase:recipe/test_v1.0.0"} \ No newline at end of file + +def test_upload_collections(): + recipe_meta_data = { + "name": "one_sphere", + "version": "1.0.0", + "composition": {}, + } + recipe_data = { + "name": "one_sphere", + "objects": { + "sphere_25": { + "type": "single_sphere", + "max_jitter": [1, 1, 0], + }, + }, + "composition": { + "space": {"regions": {"interior": ["A"]}}, + "A": {"object": "sphere_25", "count": 1}, + }, + } + + recipe_doc = DBRecipeHandler(mock_db) + expected_result = { + "name": "one_sphere", + "version": "1.0.0", + "composition": { + "space": {"inherit": "firebase:composition/test_id"}, + "A": {"inherit": "firebase:composition/test_id"}, + }, + } + recipe_to_save = recipe_doc.upload_collections(recipe_meta_data, recipe_data) + assert recipe_to_save == expected_result + + +def test_upload_recipe(): + recipe_meta_data = { + "name": "one_sphere", + "version": "1.0.0", + "composition": {}, + } + recipe_data = { + "name": "one_sphere", + "version": "1.0.0", + "objects": { + "sphere_25": { + "type": "single_sphere", + "max_jitter": [1, 1, 0], + }, + }, + "composition": { + "space": {"regions": {"interior": ["A"]}}, + "A": {"object": "sphere_25", "count": 1}, + }, + } + + recipe_doc = DBRecipeHandler(mock_db) + recipe_doc.upload_recipe(recipe_meta_data, recipe_data) + assert recipe_doc.comp_to_path_map == { + "space": {"path": "firebase:composition/test_id", "id": "test_id"}, + "A": {"path": "firebase:composition/test_id", "id": "test_id"}, + } + assert recipe_doc.objects_to_path_map == {"sphere_25": "firebase:objects/test_id"} From 5d758704d68fca0f72ce1ee8fe0d9215e1c3029b Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Tue, 11 Apr 2023 10:34:14 -0700 Subject: [PATCH 16/21] format --- cellpack/autopack/DBRecipeHandler.py | 28 ++++++++++------------------ cellpack/autopack/FirebaseHandler.py | 7 ++++--- cellpack/tests/mocks/mock_db.py | 28 ++++++++++++++-------------- cellpack/tests/test_comp_doc.py | 5 ++++- 4 files changed, 32 insertions(+), 36 deletions(-) diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index 808d18e38..722e446f8 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -21,11 +21,13 @@ def should_write(): def is_key(string_or_dict): return not isinstance(string_or_dict, dict) + class CompositionDoc(DataDoc): """ Declares required attributes for comps in the constructor, set default values. Handles the logic for comparing the local and db data to determine the uploading process. """ + SHALLOW_MATCH = ["object", "count", "molarity"] DEFAULT_VALUES = {"object": None, "count": None, "regions": {}, "molarity": None} @@ -165,6 +167,7 @@ def check_and_replace_references( elif not db.is_reference(region_item["object"]): obj_name = region_item["object"] region_item["object"] = objects_to_path_map.get(obj_name) + @staticmethod def update_reference( db, @@ -228,16 +231,11 @@ def should_write(self, db, recipe_data): class ObjectDoc(DataDoc): - def __init__( - self, - name, - settings - - ): + def __init__(self, name, settings): super().__init__() self.name = name self.settings = settings - + def as_dict(self): data = dict() data["name"] = self.name @@ -252,9 +250,7 @@ def convert_positions_in_representation(data): if isinstance(value, list): convert_data[key] = tuple(value) elif isinstance(value, dict): - convert_data[key] = ObjectDoc.convert_positions_in_representation( - value - ) + convert_data[key] = ObjectDoc.convert_positions_in_representation(value) else: data[key] = value return convert_data @@ -282,15 +278,12 @@ def convert_representation(doc, db): ] = ObjectDoc.convert_positions_in_representation(position_value) return convert_doc - def should_write(self, db): docs = db.get_doc_by_name("objects", self.name) if docs and len(docs) >= 1: for doc in docs: # if there is repr in the obj doc from db - full_doc_data = ObjectDoc.convert_representation( - doc, db - ) + full_doc_data = ObjectDoc.convert_representation(doc, db) # unpack objects to dicts in local data for comparison local_data = DBRecipeHandler.prep_data_for_db(self.as_dict()) difference = DeepDiff(full_doc_data, local_data, ignore_order=True) @@ -339,7 +332,6 @@ def prep_data_for_db(data): modified_data[key] = value return modified_data - def upload_data(self, collection, data, id=None): """ If should_write is true, upload the data to the database @@ -362,7 +354,7 @@ def upload_data(self, collection, data, id=None): def upload_objects(self, objects): for obj_name in objects: objects[obj_name]["name"] = obj_name - object_doc = ObjectDoc(name=obj_name, settings=objects[obj_name]) + object_doc = ObjectDoc(name=obj_name, settings=objects[obj_name]) _, doc_id = object_doc.should_write(self.db) if doc_id: print(f"objects/{object_doc.name} is already in firestore") @@ -454,14 +446,14 @@ def upload_collections(self, recipe_meta_data, recipe_data): def upload_recipe(self, recipe_meta_data, recipe_data): """ - After all other collections are checked or uploaded, upload the recipe with references into db + After all other collections are checked or uploaded, upload the recipe with references into db """ recipe_id = self.get_recipe_id(recipe_data) # if the recipe is already exists in db, just return recipe, _ = self.db.get_doc_by_id("recipes", recipe_id) if recipe: print(f"{recipe_id} is already in firestore") - # return + return recipe_to_save = self.upload_collections(recipe_meta_data, recipe_data) key = self.get_recipe_id(recipe_to_save) self.upload_data("recipes", recipe_to_save, key) diff --git a/cellpack/autopack/FirebaseHandler.py b/cellpack/autopack/FirebaseHandler.py index c8934181d..02fabc310 100644 --- a/cellpack/autopack/FirebaseHandler.py +++ b/cellpack/autopack/FirebaseHandler.py @@ -6,6 +6,7 @@ class FirebaseHandler(object): """ Retrieve data and perform common tasks when working with firebase. """ + def __init__(self, cred_path): login = credentials.Certificate(cred_path) firebase_admin.initialize_app(login) @@ -15,18 +16,18 @@ def __init__(self, cred_path): @staticmethod def doc_to_dict(doc): return doc.to_dict() - + def db_name(self): return self.name @staticmethod def doc_id(doc): return doc.id - + @staticmethod def create_path(collection, doc_id): return f"firebase:{collection}/{doc_id}" - + @staticmethod def get_path_from_ref(doc): return doc.path diff --git a/cellpack/tests/mocks/mock_db.py b/cellpack/tests/mocks/mock_db.py index 0d627eb83..a8c0d6cbc 100644 --- a/cellpack/tests/mocks/mock_db.py +++ b/cellpack/tests/mocks/mock_db.py @@ -12,26 +12,26 @@ def __init__(self, data) -> None: @staticmethod def is_firebase_obj(obj): return True - + def db_name(self): return "test_db" - + @staticmethod def doc_to_dict(doc): return doc - + def get_doc_by_name(self, _, name): if len(self.data) >= 1: if name in self.data["name"]: return [self.data] else: return None - + def doc_id(self, doc): if doc: doc["id"] = "test_id" return doc["id"] - + @staticmethod def is_reference(path): if path is None: @@ -39,33 +39,33 @@ def is_reference(path): if path.startswith("firebase:"): return True return False - + @staticmethod def get_doc_by_ref(key): if key: return {"test": "downloaded_data"}, key else: return {}, None - + def upload_doc(self, collection, doc): return ("test_datetime", doc) - + def set_doc(self, collection, id, data): doc_ref = Mock() return doc_ref - + def get_path_from_ref(self, doc): return "firebase:test_collection/test_id" - + def create_path(self, collection, doc_id): return f"firebase:{collection}/{doc_id}" - + def get_doc_by_id(self, collection, id): doc_ref = Mock() return self.data, doc_ref - + def update_reference_on_doc(self, doc_ref, index, new_item_ref): return True - + def update_elements_in_array(self, doc_ref, index, new_item_ref, remove_item): - return True \ No newline at end of file + return True diff --git a/cellpack/tests/test_comp_doc.py b/cellpack/tests/test_comp_doc.py index 554b3cbba..9c1618ca4 100644 --- a/cellpack/tests/test_comp_doc.py +++ b/cellpack/tests/test_comp_doc.py @@ -171,6 +171,7 @@ def test_resolve_local_regions(): local_data.resolve_local_regions(local_data.as_dict(), full_recipe_data, mock_db) assert local_data.as_dict() == resolved_data + def test_check_and_replace_references(): objects_to_path_map = {"test_obj": "firebase:objects/test_id"} references_to_update = {} @@ -185,7 +186,9 @@ def test_check_and_replace_references(): objects_to_path_map, references_to_update, mock_db ) assert composition_doc.as_dict()["object"] == "firebase:objects/test_id" - assert composition_doc.as_dict()["regions"] == {"interior": [{"object": "firebase:objects/test_id", "count": 1}]} + assert composition_doc.as_dict()["regions"] == { + "interior": [{"object": "firebase:objects/test_id", "count": 1}] + } def test_composition_oc_should_write_with_no_existing_doc(): From 04fd488f3af063142b4d9becff0cf3f624329406 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Wed, 19 Apr 2023 16:04:52 -0700 Subject: [PATCH 17/21] lint format --- cellpack/autopack/loaders/recipe_loader.py | 2 +- cellpack/tests/test_comp_doc.py | 17 ++++------------- cellpack/tests/test_db_recipe_handler.py | 6 +++--- 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/cellpack/autopack/loaders/recipe_loader.py b/cellpack/autopack/loaders/recipe_loader.py index c9f08da06..5e0315d57 100644 --- a/cellpack/autopack/loaders/recipe_loader.py +++ b/cellpack/autopack/loaders/recipe_loader.py @@ -130,7 +130,7 @@ def _sanitize_format_version(recipe_data): else: format_version = recipe_data["format_version"] return format_version - + def get_only_recipe_metadata(self): recipe_meta_data = { "format_version": self.recipe_data["format_version"], diff --git a/cellpack/tests/test_comp_doc.py b/cellpack/tests/test_comp_doc.py index 9c1618ca4..43fd576c9 100644 --- a/cellpack/tests/test_comp_doc.py +++ b/cellpack/tests/test_comp_doc.py @@ -49,7 +49,7 @@ def test_get_reference_data_with_key(): composition_db_doc.as_dict()["object"], mock_db ) assert downloaded_data == {"test": "downloaded_data"} - assert key == None + assert key is None def test_get_reference_data_with_none(): @@ -64,7 +64,7 @@ def test_get_reference_data_with_none(): composition_db_doc.as_dict()["object"], mock_db ) assert downloaded_data == {} - assert key == None + assert key is None def test_resolve_db_regions(): @@ -125,17 +125,8 @@ def test_resolve_local_regions(): }, }, "composition": { - "space": { - "regions": { - "interior": [ - "A" - ] - } - }, - "A": { - "object": "sphere_25", - "count": 500 - }, + "space": {"regions": {"interior": ["A"]}}, + "A": {"object": "sphere_25", "count": 500}, }, } diff --git a/cellpack/tests/test_db_recipe_handler.py b/cellpack/tests/test_db_recipe_handler.py index a7fadb4c3..67e8adf8f 100644 --- a/cellpack/tests/test_db_recipe_handler.py +++ b/cellpack/tests/test_db_recipe_handler.py @@ -5,9 +5,9 @@ def test_is_nested_list(): - assert DBRecipeHandler.is_nested_list([]) == False - assert DBRecipeHandler.is_nested_list([[], []]) == True - assert DBRecipeHandler.is_nested_list([[1, 2], [3, 4]]) == True + assert DBRecipeHandler.is_nested_list([]) is False + assert DBRecipeHandler.is_nested_list([[], []]) is True + assert DBRecipeHandler.is_nested_list([[1, 2], [3, 4]]) is True def test_prep_data_for_db(): From cd9c69302d2d66eb16af1f06cea6906386a2d6c3 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Fri, 21 Apr 2023 16:48:11 -0700 Subject: [PATCH 18/21] add deepdiff to setup.py --- setup.py | 1 + 1 file changed, 1 insertion(+) diff --git a/setup.py b/setup.py index 46c268435..9c9ecf9bc 100644 --- a/setup.py +++ b/setup.py @@ -59,6 +59,7 @@ "pyembree>=0.1.8", "pymunk>=6.2.0", "trimesh>=3.9.34", + "deepdiff>=5.5.0", ] extra_requirements = { From 6d25f7b25ceb7395af7a68e0cb9710ce0e54a6cb Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Mon, 1 May 2023 17:07:41 -0700 Subject: [PATCH 19/21] format --- cellpack/autopack/DBRecipeHandler.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index 3845828d2..722e446f8 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -22,14 +22,12 @@ def is_key(string_or_dict): return not isinstance(string_or_dict, dict) - class CompositionDoc(DataDoc): """ Declares required attributes for comps in the constructor, set default values. Handles the logic for comparing the local and db data to determine the uploading process. """ - SHALLOW_MATCH = ["object", "count", "molarity"] DEFAULT_VALUES = {"object": None, "count": None, "regions": {}, "molarity": None} From 712c0c16d878eae44adc8d5778fff4c94b8b9182 Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Wed, 31 May 2023 11:47:18 -0700 Subject: [PATCH 20/21] minor fix --- cellpack/autopack/DBRecipeHandler.py | 15 +++++++-------- cellpack/tests/mocks/mock_db.py | 4 ++-- cellpack/tests/test_comp_doc.py | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index 722e446f8..ef930f299 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -183,15 +183,14 @@ def update_reference( doc, doc_ref = db.get_doc_by_id("composition", composition_id) if doc is None: return + _, new_item_ref = db.get_doc_by_id("composition", referring_comp_id) + update_ref_path = f"{db.db_name()}:{db.get_path_from_ref(new_item_ref)}" + if update_in_array: + db.update_elements_in_array( + doc_ref, index, update_ref_path, remove_comp_name + ) else: - _, new_item_ref = db.get_doc_by_id("composition", referring_comp_id) - update_ref_path = f"{db.db_name()}:{db.get_path_from_ref(new_item_ref)}" - if update_in_array: - db.update_elements_in_array( - doc_ref, index, update_ref_path, remove_comp_name - ) - else: - db.update_reference_on_doc(doc_ref, index, update_ref_path) + db.update_reference_on_doc(doc_ref, index, update_ref_path) def should_write(self, db, recipe_data): """ diff --git a/cellpack/tests/mocks/mock_db.py b/cellpack/tests/mocks/mock_db.py index a8c0d6cbc..0bb5c2ec8 100644 --- a/cellpack/tests/mocks/mock_db.py +++ b/cellpack/tests/mocks/mock_db.py @@ -4,8 +4,8 @@ class MockDB(object): def __init__(self, data) -> None: for index, name in enumerate(data): - obj = data[name] - obj["id"] = index + self.obj = data[name] + self.obj["id"] = index self.data = data self.name = "test_db" diff --git a/cellpack/tests/test_comp_doc.py b/cellpack/tests/test_comp_doc.py index 43fd576c9..29e429f24 100644 --- a/cellpack/tests/test_comp_doc.py +++ b/cellpack/tests/test_comp_doc.py @@ -182,7 +182,7 @@ def test_check_and_replace_references(): } -def test_composition_oc_should_write_with_no_existing_doc(): +def test_composition_doc_should_write_with_no_existing_doc(): recipe_data = { "bounding_box": [[0, 0, 0], [10, 10, 10]], "name": "test", From bc481b2ed04eb8554b80e4fec0cb47d9ab3162eb Mon Sep 17 00:00:00 2001 From: Ruge Li Date: Mon, 5 Jun 2023 17:52:47 -0700 Subject: [PATCH 21/21] exit loop and check the next doc --- cellpack/autopack/DBRecipeHandler.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cellpack/autopack/DBRecipeHandler.py b/cellpack/autopack/DBRecipeHandler.py index ef930f299..67e9c36be 100644 --- a/cellpack/autopack/DBRecipeHandler.py +++ b/cellpack/autopack/DBRecipeHandler.py @@ -209,9 +209,14 @@ def should_write(self, db, recipe_data): if db_docs and len(db_docs) >= 1: for doc in db_docs: db_data = db.doc_to_dict(doc) + shallow_match = True for item in CompositionDoc.SHALLOW_MATCH: if db_data[item] != local_data[item]: + print(db_data[item], local_data[item]) + shallow_match = False break + if not shallow_match: + continue if local_data["regions"] is None and db_data["regions"] is None: # found a match, so shouldn't write return False, db.doc_id(doc)