Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rugeli committed Sep 5, 2023
1 parent f864fc6 commit 64dc318
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 205 deletions.
36 changes: 36 additions & 0 deletions cellpack/tests/test_data_doc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from cellpack.autopack.DBRecipeHandler import DataDoc
from cellpack.tests.mocks.mock_db import MockDB

mock_db = MockDB({})

object_example = {
"count": 121,
"object": {
"gradient": {
"mode": "surface",
"name": "nucleus_surface_gradient",
},
"name": "peroxisome",
},
}

composition_example = {
"count": 1,
"regions": {"interior": []},
"object": {
"name": "mean_nucleus",
"partners": {"all_partners": []},
},
"name": "nucleus",
}


def test_is_nested_list():
assert DataDoc.is_nested_list([]) is False
assert DataDoc.is_nested_list([[], []]) is True
assert DataDoc.is_nested_list([[1, 2], [3, 4]]) is True


def test_is_obj():
assert DataDoc.is_obj(object_example) is True
assert DataDoc.is_obj(composition_example) is False
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
from cellpack.autopack.DBRecipeHandler import DBHandler
from cellpack.autopack.DBRecipeHandler import DBUploader
from cellpack.tests.mocks.mock_db import MockDB
from unittest.mock import MagicMock, patch

mock_db = MockDB({})


def test_is_nested_list():
assert DBHandler.is_nested_list([]) is False
assert DBHandler.is_nested_list([[], []]) is True
assert DBHandler.is_nested_list([[1, 2], [3, 4]]) is True


def test_prep_data_for_db():
input_data = {
"bounding_box": [[0, 0, 0], [1000, 1000, 1]],
Expand All @@ -25,7 +19,7 @@ def test_prep_data_for_db():
},
"max_jitter": [1, 1, 0],
}
new_data = DBHandler.prep_data_for_db(input_data)
new_data = DBUploader.prep_data_for_db(input_data)
assert new_data == converted_data


Expand All @@ -38,7 +32,7 @@ def test_upload_data_with_recipe_and_id():
"composition": {"test": {"inherit": "firebase:test_collection/test_id"}},
}
id = "test_id"
recipe_doc = DBHandler(mock_db)
recipe_doc = DBUploader(mock_db)
expected_result = recipe_doc.upload_data(collection, data, id)

assert expected_result[0] == "test_id"
Expand All @@ -51,7 +45,7 @@ def test_upload_data_with_object():
"name": "test",
"test_key": "test_value",
}
object_doc = DBHandler(mock_db)
object_doc = DBUploader(mock_db)
expected_result = object_doc.upload_data(collection, data)

assert expected_result[0] == "test_id"
Expand All @@ -60,14 +54,14 @@ def test_upload_data_with_object():

def test_upload_objects():
data = {"test": {"test_key": "test_value"}}
object_doc = DBHandler(mock_db)
object_doc = DBUploader(mock_db)
object_doc.upload_objects(data)
assert object_doc.objects_to_path_map == {"test": "firebase:objects/test_id"}


def test_upload_objects_with_gradient():
data = {"test": {"test_key": "test_value", "gradient": "test_grad_name"}}
object_handler = DBHandler(mock_db)
object_handler = DBUploader(mock_db)
object_handler.grad_to_path_map = {"test_grad_name": "firebase:gradients/test_id"}

with patch(
Expand Down Expand Up @@ -102,7 +96,7 @@ def test_upload_compositions():
},
}

composition_doc = DBHandler(mock_db)
composition_doc = DBUploader(mock_db)
references_to_update = composition_doc.upload_compositions(
composition, recipe_to_save, recipe_data
)
Expand All @@ -116,7 +110,7 @@ def test_upload_compositions():

def test_upload_gradients():
data = [{"name": "test_grad_name", "test_key": "test_value"}]
gradient_doc = DBHandler(mock_db)
gradient_doc = DBUploader(mock_db)
gradient_doc.upload_gradients(data)
assert gradient_doc.grad_to_path_map == {
"test_grad_name": "firebase:gradients/test_id"
Expand All @@ -130,8 +124,8 @@ def test_get_recipe_id():
"objects": None,
"composition": {},
}
recipe_doc = DBHandler(mock_db)
assert recipe_doc.get_recipe_id(recipe_data) == "test_v-1.0.0"
recipe_doc = DBUploader(mock_db)
assert recipe_doc._get_recipe_id(recipe_data) == "test_v-1.0.0"


def test_upload_collections():
Expand All @@ -154,7 +148,7 @@ def test_upload_collections():
},
}

recipe_doc = DBHandler(mock_db)
recipe_doc = DBUploader(mock_db)
expected_result = {
"name": "one_sphere",
"version": "1.0.0",
Expand Down Expand Up @@ -188,7 +182,7 @@ def test_upload_recipe():
},
}

recipe_doc = DBHandler(mock_db)
recipe_doc = DBUploader(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"},
Expand Down
192 changes: 192 additions & 0 deletions cellpack/tests/test_db_recipe_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import pytest
from cellpack.autopack.DBRecipeHandler import DBRecipeLoader
from cellpack.tests.mocks.mock_db import MockDB

mock_db = MockDB({})

downloaded_data_from_firebase = {
"version": "linear",
"format_version": "2.1",
"composition": {
"membrane": {
"count": 1,
"regions": {
"interior": [
{
"count": 121,
"object": {
"gradient": {
"mode": "surface",
"name": "nucleus_surface_gradient",
},
"name": "peroxisome",
},
},
{
"count": 1,
"regions": {"interior": []},
"object": {
"name": "mean_nucleus",
"partners": {"all_partners": []},
},
"name": "nucleus",
},
]
},
"object": {
"name": "mean_membrane",
"type": "mesh",
},
"name": "membrane",
},
"nucleus": {
"count": 1,
"regions": {"interior": []},
"object": {
"name": "mean_nucleus",
"partners": {"all_partners": []},
},
"name": "nucleus",
},
"bounding_area": {
"count": None,
"regions": {
"interior": [
{
"count": 1,
"regions": {
"interior": [
{
"count": 121,
"object": {
"gradient": {
"mode": "surface",
"name": "nucleus_surface_gradient",
},
"name": "peroxisome",
},
},
{
"count": 1,
"regions": {"interior": []},
"object": {
"name": "mean_nucleus",
"partners": {"all_partners": []},
},
"name": "nucleus",
},
]
},
"object": {
"name": "mean_membrane",
"type": "mesh",
},
"name": "membrane",
}
]
},
"name": "bounding_area",
},
},
"version": "linear",
"bounding_box": [[-110, -45, -62], [110, 45, 62]],
"name": "test_recipe",
}


compiled_firebase_recipe_example = {
"name": "test_recipe",
"format_version": "2.1",
"version": "linear",
"bounding_box": [[-110, -45, -62], [110, 45, 62]],
"objects": {
"mean_membrane": {
"name": "mean_membrane",
"type": "mesh",
},
"peroxisome": {
"name": "peroxisome",
"gradient": "nucleus_surface_gradient",
},
"mean_nucleus": {
"name": "mean_nucleus",
"partners": {"all_partners": []},
},
},
"gradients": [
{
"name": "nucleus_surface_gradient",
"mode": "surface",
}
],
"composition": {
"bounding_area": {"regions": {"interior": ["membrane"]}},
"membrane": {
"count": 1,
"object": "mean_membrane",
"regions": {
"interior": [{"object": "peroxisome", "count": 121}, "nucleus"]
},
},
"nucleus": {
"count": 1,
"object": "mean_nucleus",
"regions": {"interior": []},
},
},
}


def test_get_grad_and_obj():
obj_data = downloaded_data_from_firebase["composition"]["membrane"]["regions"][
"interior"
][0]["object"]
obj_dict = {
"peroxisome": {
"gradient": {
"mode": "surface",
"name": "nucleus_surface_gradient",
},
"name": "peroxisome",
}
}
grad_dict = {}
obj_dict, grad_dict = DBRecipeLoader._get_grad_and_obj(
obj_data, obj_dict, grad_dict
)
assert obj_dict == {
"peroxisome": {"gradient": "nucleus_surface_gradient", "name": "peroxisome"}
}
assert grad_dict == {
"nucleus_surface_gradient": {
"mode": "surface",
"name": "nucleus_surface_gradient",
}
}


@pytest.fixture
def sort_data_from_composition():
return DBRecipeLoader._collect_and_sort_data(
downloaded_data_from_firebase["composition"]
)


def test_collect_and_sort_data(sort_data_from_composition):
objects, gradients, composition = sort_data_from_composition
assert objects == compiled_firebase_recipe_example["objects"]
assert gradients == {
"nucleus_surface_gradient": {
"name": "nucleus_surface_gradient",
"mode": "surface",
}
}
assert composition == compiled_firebase_recipe_example["composition"]


def test_compile_db_recipe_data(sort_data_from_composition):
objects, gradients, composition = sort_data_from_composition
compiled_recipe = DBRecipeLoader._compile_db_recipe_data(
downloaded_data_from_firebase, objects, gradients, composition
)
assert compiled_recipe == compiled_firebase_recipe_example
Loading

0 comments on commit 64dc318

Please sign in to comment.