diff --git a/src/protean/adapters/repository/elasticsearch.py b/src/protean/adapters/repository/elasticsearch.py index d9f15a49..1d0faca9 100644 --- a/src/protean/adapters/repository/elasticsearch.py +++ b/src/protean/adapters/repository/elasticsearch.py @@ -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 @@ -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] @@ -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 @@ -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 @@ -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 = {} diff --git a/src/protean/adapters/repository/memory.py b/src/protean/adapters/repository/memory.py index 7736a63d..ae4290be 100644 --- a/src/protean/adapters/repository/memory.py +++ b/src/protean/adapters/repository/memory.py @@ -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): @@ -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: @@ -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 @@ -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_, diff --git a/src/protean/adapters/repository/sqlalchemy.py b/src/protean/adapters/repository/sqlalchemy.py index 9c9afe17..7c42bd9d 100644 --- a/src/protean/adapters/repository/sqlalchemy.py +++ b/src/protean/adapters/repository/sqlalchemy.py @@ -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): @@ -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 @@ -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 @@ -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): @@ -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 ) @@ -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 diff --git a/src/protean/core/model.py b/src/protean/core/model.py index 3fcbb7f3..eee21774 100644 --- a/src/protean/core/model.py +++ b/src/protean/core/model.py @@ -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), ] @@ -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": [ diff --git a/src/protean/domain/__init__.py b/src/protean/domain/__init__.py index 25b0a4ec..2a1b8331 100644 --- a/src/protean/domain/__init__.py +++ b/src/protean/domain/__init__.py @@ -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) diff --git a/tests/adapters/model/dict_model/tests.py b/tests/adapters/model/dict_model/tests.py index ae4d5de0..5a906428 100644 --- a/tests/adapters/model/dict_model/tests.py +++ b/tests/adapters/model/dict_model/tests.py @@ -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 ( @@ -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() diff --git a/tests/adapters/model/elasticsearch_model/conftest.py b/tests/adapters/model/elasticsearch_model/conftest.py index 7c0b4a91..9369c193 100644 --- a/tests/adapters/model/elasticsearch_model/conftest.py +++ b/tests/adapters/model/elasticsearch_model/conftest.py @@ -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) diff --git a/tests/adapters/model/elasticsearch_model/tests.py b/tests/adapters/model/elasticsearch_model/tests.py index ca5df047..c2f37641 100644 --- a/tests/adapters/model/elasticsearch_model/tests.py +++ b/tests/adapters/model/elasticsearch_model/tests.py @@ -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" @@ -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" @@ -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" @@ -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 @@ -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? @@ -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 @@ -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 @@ -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()}) @@ -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()}) diff --git a/tests/adapters/model/sqlalchemy_model/postgresql/conftest.py b/tests/adapters/model/sqlalchemy_model/postgresql/conftest.py index 1357b2ea..dcffcd1c 100644 --- a/tests/adapters/model/sqlalchemy_model/postgresql/conftest.py +++ b/tests/adapters/model/sqlalchemy_model/postgresql/conftest.py @@ -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) diff --git a/tests/adapters/model/sqlalchemy_model/postgresql/test_model.py b/tests/adapters/model/sqlalchemy_model/postgresql/test_model.py index ce444908..1bee84e2 100644 --- a/tests/adapters/model/sqlalchemy_model/postgresql/test_model.py +++ b/tests/adapters/model/sqlalchemy_model/postgresql/test_model.py @@ -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" @@ -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) diff --git a/tests/adapters/model/sqlalchemy_model/sqlite/conftest.py b/tests/adapters/model/sqlalchemy_model/sqlite/conftest.py index 1912412f..a9890eea 100644 --- a/tests/adapters/model/sqlalchemy_model/sqlite/conftest.py +++ b/tests/adapters/model/sqlalchemy_model/sqlite/conftest.py @@ -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) diff --git a/tests/adapters/model/sqlalchemy_model/sqlite/test_model.py b/tests/adapters/model/sqlalchemy_model/sqlite/test_model.py index 21e81104..168a5a77 100644 --- a/tests/adapters/model/sqlalchemy_model/sqlite/test_model.py +++ b/tests/adapters/model/sqlalchemy_model/sqlite/test_model.py @@ -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" @@ -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)