Skip to content

Commit

Permalink
Rename model's entity_cls option to part_of
Browse files Browse the repository at this point in the history
  • Loading branch information
subhashb committed Jul 8, 2024
1 parent d48fa2d commit febe311
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 39 deletions.
14 changes: 7 additions & 7 deletions src/protean/adapters/repository/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ElasticsearchModel(Document):
def from_entity(cls, entity) -> "ElasticsearchModel":
"""Convert the entity to a Elasticsearch record"""
item_dict = {}
for attribute_obj in attributes(cls.meta_.entity_cls).values():
for attribute_obj in attributes(cls.meta_.part_of).values():
if isinstance(attribute_obj, Reference):
item_dict[attribute_obj.relation.attribute_name] = (
attribute_obj.relation.value
Expand All @@ -61,7 +61,7 @@ def from_entity(cls, entity) -> "ElasticsearchModel":

# Elasticsearch stores identity in a special field `meta.id`.
# Set `meta.id` to the identifier set in entity
id_field_name = id_field(cls.meta_.entity_cls).field_name
id_field_name = id_field(cls.meta_.part_of).field_name

if id_field_name in item_dict:
model_obj.meta.id = item_dict[id_field_name]
Expand All @@ -75,7 +75,7 @@ def to_entity(cls, item: "ElasticsearchModel"):

# Convert the values in ES Model as a dictionary
values = item.to_dict()
for field_name in attributes(cls.meta_.entity_cls):
for field_name in attributes(cls.meta_.part_of):
item_dict[field_name] = values.get(field_name, None)

identifier = None
Expand All @@ -90,14 +90,14 @@ def to_entity(cls, item: "ElasticsearchModel"):

# Elasticsearch stores identity in a special field `meta.id`.
# Extract identity from `meta.id` and set identifier
id_field_name = id_field(cls.meta_.entity_cls).field_name
id_field_name = id_field(cls.meta_.part_of).field_name
item_dict[id_field_name] = identifier

# Set version from document meta, only if `_version` attr is present
if hasattr(cls.meta_.entity_cls, "_version"):
if hasattr(cls.meta_.part_of, "_version"):
item_dict["_version"] = item.meta.version

entity_obj = cls.meta_.entity_cls(item_dict)
entity_obj = cls.meta_.part_of(item_dict)

return entity_obj

Expand Down Expand Up @@ -439,7 +439,7 @@ def construct_model_class(self, entity_cls):
model_cls = self._model_classes[entity_cls.meta_.schema_name]
else:
meta_ = Options()
meta_.entity_cls = entity_cls
meta_.part_of = entity_cls

# Construct Inner Index class with options
options = {}
Expand Down
8 changes: 4 additions & 4 deletions src/protean/adapters/repository/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def derive_schema_name(model_cls):
if hasattr(model_cls.meta_, "schema_name"):
return model_cls.meta_.schema_name
else:
return model_cls.meta_.entity_cls.meta_.schema_name
return model_cls.meta_.part_of.meta_.schema_name


class MemoryModel(BaseModel):
Expand All @@ -43,7 +43,7 @@ def from_entity(cls, entity) -> "MemoryModel":
@classmethod
def to_entity(cls, item: "MemoryModel"):
"""Convert the dictionary record to an entity"""
return cls.meta_.entity_cls(**item)
return cls.meta_.part_of(**item)


class MemorySession:
Expand Down Expand Up @@ -148,7 +148,7 @@ def decorate_model_class(self, entity_cls, model_cls):
}

meta_ = Options()
meta_.entity_cls = entity_cls
meta_.part_of = entity_cls

custom_attrs.update({"meta_": meta_})
# FIXME Ensure the custom model attributes are constructed properly
Expand All @@ -170,7 +170,7 @@ def construct_model_class(self, entity_cls):
model_cls = self._model_classes[entity_cls.meta_.schema_name]
else:
meta_ = Options()
meta_.entity_cls = entity_cls
meta_.part_of = entity_cls

attrs = {
"meta_": meta_,
Expand Down
14 changes: 7 additions & 7 deletions src/protean/adapters/repository/sqlalchemy.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def derive_schema_name(model_cls):
):
return model_cls.meta_.schema_name
else:
return model_cls.meta_.entity_cls.meta_.schema_name
return model_cls.meta_.part_of.meta_.schema_name


class SqlalchemyModel(orm.DeclarativeBase, BaseModel):
Expand Down Expand Up @@ -169,7 +169,7 @@ def field_mapping_for(field_obj: Field):

# Update the class attrs with the entity attributes
if "meta_" in subclass.__dict__:
entity_cls = subclass.__dict__["meta_"].entity_cls
entity_cls = subclass.__dict__["meta_"].part_of
for _, field_obj in attributes(entity_cls).items():
attribute_name = field_obj.attribute_name

Expand Down Expand Up @@ -243,7 +243,7 @@ def __tablename__(cls):
def from_entity(cls, entity):
"""Convert the entity to a model object"""
item_dict = {}
for attribute_obj in attributes(cls.meta_.entity_cls).values():
for attribute_obj in attributes(cls.meta_.part_of).values():
if isinstance(attribute_obj, Reference):
item_dict[attribute_obj.relation.attribute_name] = (
attribute_obj.relation.value
Expand All @@ -258,9 +258,9 @@ def from_entity(cls, entity):
def to_entity(cls, model_obj: "SqlalchemyModel"):
"""Convert the model object to an entity"""
item_dict = {}
for field_name in attributes(cls.meta_.entity_cls):
for field_name in attributes(cls.meta_.part_of):
item_dict[field_name] = getattr(model_obj, field_name, None)
return cls.meta_.entity_cls(item_dict)
return cls.meta_.part_of(item_dict)


class SADAO(BaseDAO):
Expand Down Expand Up @@ -683,7 +683,7 @@ def decorate_model_class(self, entity_cls, model_cls):
custom_attrs = {**custom_attrs, **columns}

meta_ = Options(model_cls.meta_)
meta_.entity_cls = entity_cls
meta_.part_of = entity_cls
meta_.schema_name = (
schema_name if meta_.schema_name is None else meta_.schema_name
)
Expand Down Expand Up @@ -711,7 +711,7 @@ def construct_model_class(self, entity_cls):
else:
# Construct a new Meta object with existing values
meta_ = Options()
meta_.entity_cls = entity_cls
meta_.part_of = entity_cls
# If schema_name is not provided, sqlalchemy can throw
# sqlalchemy.exc.InvalidRequestError: Class does not
# have a __table__ or __tablename__ specified and
Expand Down
4 changes: 2 additions & 2 deletions src/protean/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __new__(cls, *args, **kwargs):
def _default_options(cls):
return [
("database", None),
("entity_cls", None),
("part_of", None),
("schema_name", None),
]

Expand All @@ -39,7 +39,7 @@ def to_entity(cls, *args, **kwargs):
def model_factory(element_cls, **kwargs):
element_cls = derive_element_class(element_cls, BaseModel, **kwargs)

if not element_cls.meta_.entity_cls:
if not element_cls.meta_.part_of:
raise IncorrectUsageError(
{
"_entity": [
Expand Down
2 changes: 1 addition & 1 deletion src/protean/domain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ def _register_element(self, element_type, element_cls, **kwargs): # noqa: C901

if element_type == DomainObjects.MODEL:
# Remember model association with aggregate/entity class, for easy fetching
self._models[fqn(new_cls.meta_.entity_cls)] = new_cls
self._models[fqn(new_cls.meta_.part_of)] = new_cls

# Register element with domain
self._domain_registry.register_element(new_cls)
Expand Down
4 changes: 2 additions & 2 deletions tests/adapters/model/dict_model/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class TestCustomModel:
def test_that_custom_model_is_associated_with_entity(self, test_domain):
test_domain.register(Provider)
test_domain.register(
ProviderCustomModel, entity_cls=Provider, schema_name="adults"
ProviderCustomModel, part_of=Provider, schema_name="adults"
)

assert (
Expand All @@ -98,7 +98,7 @@ def test_that_model_can_be_registered_with_domain_annotation(self, test_domain):

test_domain.register(Receiver)

@test_domain.model(entity_cls=Receiver)
@test_domain.model(part_of=Receiver)
class ReceiverInlineModel:
about = Text()

Expand Down
2 changes: 1 addition & 1 deletion tests/adapters/model/elasticsearch_model/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def setup_db():
domain.register(ComplexUser)
domain.register(Provider)
domain.register_model(
ProviderCustomModel, entity_cls=Provider, schema_name="providers"
ProviderCustomModel, part_of=Provider, schema_name="providers"
)
domain.init(traverse=False)

Expand Down
18 changes: 9 additions & 9 deletions tests/adapters/model/elasticsearch_model/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ class Index:
name = "people"

test_domain.register(Person)
test_domain.register_model(PeopleModel, entity_cls=Person)
test_domain.register_model(PeopleModel, part_of=Person)

model_cls = test_domain.repository_for(Person)._model
assert model_cls.__name__ == "PeopleModel"
Expand Down Expand Up @@ -173,7 +173,7 @@ class Index:
name = "custom-people"

test_domain.register(Person)
test_domain.register_model(PeopleModel, entity_cls=Person)
test_domain.register_model(PeopleModel, part_of=Person)

model_cls = test_domain.repository_for(Person)._model
assert model_cls.__name__ == "PeopleModel"
Expand Down Expand Up @@ -210,7 +210,7 @@ class Index:
settings = {"number_of_shards": 2}

test_domain.register(Person)
test_domain.register_model(PeopleModel, entity_cls=Person)
test_domain.register_model(PeopleModel, part_of=Person)

model_cls = test_domain.repository_for(Person)._model
assert model_cls.__name__ == "PeopleModel"
Expand Down Expand Up @@ -269,7 +269,7 @@ class TestCustomModel:
def test_that_custom_model_can_be_associated_with_entity(self, test_domain):
test_domain.register(Provider)
test_domain.register_model(
ProviderCustomModel, entity_cls=Provider, schema_name="providers"
ProviderCustomModel, part_of=Provider, schema_name="providers"
)

model_cls = test_domain.repository_for(Provider)._model
Expand All @@ -280,7 +280,7 @@ def test_that_explicit_schema_name_takes_precedence_over_generated(
):
test_domain.register(Provider)
test_domain.register_model(
ProviderCustomModel, entity_cls=Provider, schema_name="providers"
ProviderCustomModel, part_of=Provider, schema_name="providers"
)

# FIXME Should schema name be equated to the overridden name in the model?
Expand All @@ -293,7 +293,7 @@ def test_that_explicit_schema_name_takes_precedence_over_generated(
def test_that_custom_model_is_persisted_via_dao(self, test_domain):
test_domain.register(Provider)
test_domain.register_model(
ProviderCustomModel, entity_cls=Provider, schema_name="providers"
ProviderCustomModel, part_of=Provider, schema_name="providers"
)

provider_dao = test_domain.repository_for(Provider)._dao
Expand All @@ -303,7 +303,7 @@ def test_that_custom_model_is_persisted_via_dao(self, test_domain):
def test_that_custom_model_is_retrievable_via_dao(self, test_domain):
test_domain.register(Provider)
test_domain.register_model(
ProviderCustomModel, entity_cls=Provider, schema_name="providers"
ProviderCustomModel, part_of=Provider, schema_name="providers"
)

provider_dao = test_domain.repository_for(Provider)._dao
Expand All @@ -318,7 +318,7 @@ def test_that_model_can_be_registered_with_domain_annotation(self, test_domain):

test_domain.register(Receiver)

@test_domain.model(entity_cls=Receiver)
@test_domain.model(part_of=Receiver)
class ReceiverInlineModel:
name = Text(fields={"raw": Keyword()})

Expand All @@ -343,7 +343,7 @@ def test_persistence_via_model_registered_with_domain_annotation(self, test_doma

test_domain.register(Receiver)

@test_domain.model(entity_cls=Receiver)
@test_domain.model(part_of=Receiver)
class ReceiverInlineModel:
id = Keyword()
name = Text(fields={"raw": Keyword()})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def setup_db():
domain.register(IntegerListUser)

domain.register_model(
ProviderCustomModel, entity_cls=Provider, schema_name="adults"
ProviderCustomModel, part_of=Provider, schema_name="adults"
)
domain.init(traverse=False)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_conversation_from_model_to_entity(self, test_domain):
class TestCustomModel:
def test_that_custom_model_can_be_associated_with_entity(self, test_domain):
test_domain.register(
ProviderCustomModel, entity_cls=Provider, schema_name="adults"
ProviderCustomModel, part_of=Provider, schema_name="adults"
)
model_cls = test_domain.repository_for(Provider)._model
assert model_cls.__name__ == "ProviderCustomModel"
Expand All @@ -110,7 +110,7 @@ def test_that_model_can_be_registered_with_domain_annotation(self, test_domain):

test_domain.register(Receiver)

@test_domain.model(entity_cls=Receiver)
@test_domain.model(part_of=Receiver)
class ReceiverInlineModel:
name = Column(Text)

Expand Down
2 changes: 1 addition & 1 deletion tests/adapters/model/sqlalchemy_model/sqlite/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setup_db():
domain.register(Provider)
domain.register(User)
domain.register_model(
ProviderCustomModel, entity_cls=Provider, schema_name="adults"
ProviderCustomModel, part_of=Provider, schema_name="adults"
)
domain.init(traverse=False)

Expand Down
4 changes: 2 additions & 2 deletions tests/adapters/model/sqlalchemy_model/sqlite/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_conversation_from_model_to_entity(self, test_domain):
class TestCustomModel:
def test_that_custom_model_can_be_associated_with_entity(self, test_domain):
test_domain.register(
ProviderCustomModel, entity_cls=Provider, schema_name="adults"
ProviderCustomModel, part_of=Provider, schema_name="adults"
)
model_cls = test_domain.repository_for(Provider)._model
assert model_cls.__name__ == "ProviderCustomModel"
Expand All @@ -110,7 +110,7 @@ def test_that_model_can_be_registered_with_domain_annotation(self, test_domain):

test_domain.register(Receiver)

@test_domain.model(entity_cls=Receiver)
@test_domain.model(part_of=Receiver)
class ReceiverInlineModel:
name = Column(Text)

Expand Down

0 comments on commit febe311

Please sign in to comment.