Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion liminal/base/compare_operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@
class CompareOperation(BaseModel):
op: BaseOperation
reverse_op: BaseOperation
# Overrides op.order when an operation must run at a different point than its type's default.
order: int | None = None

model_config = ConfigDict(arbitrary_types_allowed=True)

@property
def sort_order(self) -> int:
return self.op.order if self.order is None else self.order

def __lt__(self, other: "CompareOperation") -> bool:
return self.op.order < other.op.order
return self.sort_order < other.sort_order
5 changes: 5 additions & 0 deletions liminal/base/properties/base_field_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ def unset_unit_name(self) -> BaseFieldProperties:
self.__pydantic_fields_set__.remove("unit_name")
return self

def unset_parent_link(self) -> BaseFieldProperties:
if "parent_link" in self.__pydantic_fields_set__:
self.__pydantic_fields_set__.remove("parent_link")
return self

def set_type(self, new_type: BenchlingFieldType) -> BaseFieldProperties:
self.type = new_type
return self
Expand Down
70 changes: 51 additions & 19 deletions liminal/entity_schemas/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
from liminal.orm.column import Column
from liminal.utils import to_snake_case

# A new schema's name template and template based naming strategies run after field updates,
# which set link targets the template can depend on.
NEW_SCHEMA_NAME_TEMPLATE_ORDER = UpdateEntitySchemaField.order + 1
NEW_SCHEMA_NAMING_STRATEGY_ORDER = UpdateEntitySchemaField.order + 2

LOGGER = logging.getLogger(__name__)


Expand Down Expand Up @@ -300,10 +305,23 @@ def compare_entity_schemas(
for p in field_props
if (p.unit_name and p.warehouse_name)
}
# A link field is created without a target, and Benchling only accepts
# parent_link on a field that has one, so it is set with the target.
parent_links_to_update = {
p.warehouse_name
for p in field_props
if (p.parent_link and p.entity_link and p.warehouse_name)
}
field_props = [
f.unset_tooltip().unset_entity_link().unset_unit_name()
for f in field_props
]
field_props = [
f.unset_parent_link()
if f.warehouse_name in parent_links_to_update
else f
for f in field_props
]
field_props = [
f.set_type(BenchlingFieldType.ENTITY_LINK) if f.entity_link else f
for f in field_props
Expand Down Expand Up @@ -337,32 +355,20 @@ def compare_entity_schemas(
)
)
# Benchling api does not allow for setting a custom warehouse_name.
# Benchling api also does not allow for setting the name template.
# Because of this, we need to run a separate UpdateEntitySchema to set the warehouse_name if it is different from the snakecase version of the model name.
# We also need to set the template based naming_strategies separately, since the name template must first be set.
new_schema_props = BaseSchemaProperties()
rollback_schema_props = BaseSchemaProperties()
# Later operations on this schema use the model's warehouse_name.
if model_wh_name != benchling_given_wh_name:
new_schema_props.warehouse_name = model_wh_name
rollback_schema_props.warehouse_name = benchling_given_wh_name
if (
model.__schema_properties__.naming_strategies
- standard_naming_strategies
):
new_schema_props.naming_strategies = (
model.__schema_properties__.naming_strategies
)
rollback_schema_props.naming_strategies = standard_naming_strategies
if new_schema_props.model_dump(exclude_unset=True) != {}:
ops.append(
CompareOperation(
op=UpdateEntitySchema(
benchling_given_wh_name,
new_schema_props,
BaseSchemaProperties(warehouse_name=model_wh_name),
),
reverse_op=UpdateEntitySchema(
model_wh_name,
rollback_schema_props,
BaseSchemaProperties(
warehouse_name=benchling_given_wh_name
),
),
)
)
Expand All @@ -382,6 +388,8 @@ def compare_entity_schemas(
)
rollback_field_props.entity_link = None
rollback_field_props.type = BenchlingFieldType.ENTITY_LINK
if wh_field_name in parent_links_to_update:
new_field_props.parent_link = True
if tooltip_value := tooltips_to_update.get(wh_field_name):
new_field_props.tooltip = tooltip_value
rollback_field_props.tooltip = None
Expand All @@ -403,25 +411,49 @@ def compare_entity_schemas(
)
# Benchling api also does not allow for setting the name template when Creating an Entity Schema, since it is a different endpoint.
# Because of this, we need to run a separate UpdateEntitySchemaNameTemplate using the name template endpoint to set the name template on the newly created Entity Schema.
# A template part can depend on a link field, such as a parent's lot number, so the template is set after the field updates above.
if benchling_given_name_template != model.__name_template__:
ops.append(
CompareOperation(
op=UpdateEntitySchemaNameTemplate(
benchling_given_wh_name,
model_wh_name,
BaseNameTemplate(
**benchling_given_name_template.merge(
model.__name_template__
)
),
),
reverse_op=UpdateEntitySchemaNameTemplate(
benchling_given_wh_name,
model_wh_name,
BaseNameTemplate(
**model.__name_template__.merge(
benchling_given_name_template
)
),
),
order=NEW_SCHEMA_NAME_TEMPLATE_ORDER,
)
)
# Template based naming_strategies need the name template, so they are set last.
if (
model.__schema_properties__.naming_strategies
- standard_naming_strategies
):
ops.append(
CompareOperation(
op=UpdateEntitySchema(
model_wh_name,
BaseSchemaProperties(
naming_strategies=model.__schema_properties__.naming_strategies
),
),
reverse_op=UpdateEntitySchema(
model_wh_name,
BaseSchemaProperties(
naming_strategies=standard_naming_strategies
),
),
order=NEW_SCHEMA_NAMING_STRATEGY_ORDER,
)
)

Expand Down
51 changes: 51 additions & 0 deletions liminal/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,3 +519,54 @@ def __init__(
self.list_dropdown_field = list_dropdown_field

return [MockEntityOne, MockEntityTwo]


@pytest.fixture
def mock_cell_line_subclasses() -> list[type[BaseModel]]:
"""A parent schema and a batch schema whose name uses the parent's lot number."""
from liminal.orm.name_template_parts import (
FieldPart,
ParentLotNumberPart,
SeparatorPart,
)

class CellLine(BaseModel):
__schema_properties__ = SchemaProperties(
name="Cell Line",
warehouse_name="cell_line",
prefix="CL",
entity_type=BenchlingEntityType.CUSTOM_ENTITY,
naming_strategies=[BenchlingNamingStrategy.NEW_IDS],
)
species: SqlColumn = Column(name="Species", type=Type.TEXT, required=False)

def __init__(self, species: str | None = None) -> None:
self.species = species

class CellLineBatch(BaseModel):
__schema_properties__ = SchemaProperties(
name="Cell Line Batch",
warehouse_name="cl_batch",
prefix="CLB",
entity_type=BenchlingEntityType.CUSTOM_ENTITY,
naming_strategies=[BenchlingNamingStrategy.REPLACE_NAMES_WITH_TEMPLATE],
)
__name_template__ = NameTemplate(
parts=[
FieldPart(wh_field_name="parent_cell_line"),
SeparatorPart(value="-"),
ParentLotNumberPart(wh_field_name="parent_cell_line"),
]
)
parent_cell_line: SqlColumn = Column(
name="Parent Cell Line",
type=Type.ENTITY_LINK,
required=False,
entity_link="cell_line",
parent_link=True,
)

def __init__(self, parent_cell_line: str | None = None) -> None:
self.parent_cell_line = parent_cell_line

return [CellLine, CellLineBatch]
72 changes: 72 additions & 0 deletions liminal/tests/test_entity_schema_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
UpdateEntitySchemaNameTemplate,
)
from liminal.enums import BenchlingFieldType
from liminal.enums.benchling_naming_strategy import BenchlingNamingStrategy
from liminal.orm.name_template import NameTemplate
from liminal.orm.name_template_parts import TextPart

Expand Down Expand Up @@ -158,6 +159,8 @@ def test_compare_benchling_schema_fields( # type: ignore[no-untyped-def]
missing_field[0][2].pop("string_field_req")
mock_get_benchling_entity_schemas.return_value = missing_field
invalid_models = compare_entity_schemas(mock_benchling_sdk)
# Only new schemas override the default operation order.
assert all(c.order is None for c in invalid_models["mock_entity"])
assert len(invalid_models["mock_entity"]) == 1
assert isinstance(
invalid_models["mock_entity"][0].op, CreateEntitySchemaField
Expand Down Expand Up @@ -400,3 +403,72 @@ def test_compare_benchling_schema_fields( # type: ignore[no-untyped-def]
invalid_models["mock_entity"][0].op, UpdateEntitySchemaNameTemplate
)
assert invalid_models["mock_entity"][0].op.update_name_template.parts == []

def test_compare_benchling_schemas_create_link_order( # type: ignore[no-untyped-def]
self, mock_cell_line_subclasses
) -> None:
"""A new schema's name template can use a parent's lot number, so it runs after the parent link is set."""
with (
patch(
"liminal.entity_schemas.compare.get_converted_entity_schemas"
) as mock_get_benchling_entity_schemas,
patch(
"liminal.orm.base_model.BaseModel.get_all_subclasses"
) as mock_get_all_subclasses,
):
mock_get_all_subclasses.return_value = mock_cell_line_subclasses
mock_get_benchling_entity_schemas.return_value = []
compared = compare_entity_schemas(Mock())
batch_ops = [c.op for c in sorted(compared["cl_batch"])]

assert [type(op) for op in batch_ops] == [
CreateEntitySchema,
UpdateEntitySchema,
UpdateEntitySchemaField,
UpdateEntitySchemaNameTemplate,
UpdateEntitySchema,
]
create, rename, link, template, naming = batch_ops
assert isinstance(create, CreateEntitySchema)
assert isinstance(rename, UpdateEntitySchema)
assert isinstance(link, UpdateEntitySchemaField)
assert isinstance(template, UpdateEntitySchemaNameTemplate)
assert isinstance(naming, UpdateEntitySchema)

# The link is created without a target, so parent_link waits for the target.
created_field = create.fields[0]
assert "entity_link" not in created_field.model_fields_set
assert "parent_link" not in created_field.model_fields_set

assert rename.wh_schema_name == "cell_line_batch"
assert rename.update_props.model_dump(exclude_unset=True) == {
"warehouse_name": "cl_batch"
}

assert link.wh_schema_name == "cl_batch"
assert link.update_props.entity_link == "cell_line"
assert link.update_props.parent_link is True

assert template.wh_schema_name == "cl_batch"
assert naming.wh_schema_name == "cl_batch"
assert naming.update_props.model_dump(exclude_unset=True) == {
"naming_strategies": {
BenchlingNamingStrategy.REPLACE_NAMES_WITH_TEMPLATE
}
}

# Across schemas, every field update runs before any new schema's template.
all_ops = [
c.op for c in sorted(compared["cl_batch"] + compared["cell_line"])
]
last_field_update = max(
i
for i, op in enumerate(all_ops)
if isinstance(op, UpdateEntitySchemaField)
)
first_template = min(
i
for i, op in enumerate(all_ops)
if isinstance(op, UpdateEntitySchemaNameTemplate)
)
assert last_field_update < first_template