From 6416c33e88bb36fcc8fe63e919a7a8863fab07ab Mon Sep 17 00:00:00 2001 From: Subhash Bhushan Date: Thu, 2 Sep 2021 21:03:51 -0700 Subject: [PATCH] Set Reference field name on Entity construction --- src/protean/core/entity.py | 9 +++++++++ src/protean/server.py | 2 +- tests/adapters/broker/celery_broker/config.py | 4 ++-- tests/adapters/broker/redis_broker/config.py | 4 ++-- tests/aggregate/test_aggregate_association.py | 7 +++++++ 5 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/protean/core/entity.py b/src/protean/core/entity.py index 5a352352..d6b47036 100644 --- a/src/protean/core/entity.py +++ b/src/protean/core/entity.py @@ -133,6 +133,7 @@ def __init_subclass__(subclass) -> None: super().__init_subclass__() subclass.__set_id_field() + subclass.__set_up_reference_fields() @classmethod def __set_id_field(new_class): @@ -180,6 +181,14 @@ def __create_id_field(new_class): field_objects["id"] = id_field setattr(new_class, _FIELDS, field_objects) + @classmethod + def __set_up_reference_fields(subclass): + """Walk through relation fields and setup shadow attributes""" + for _, field in fields(subclass).items(): + if isinstance(field, Reference): + shadow_field_name, shadow_field = field.get_shadow_field() + shadow_field.__set_name__(subclass, shadow_field_name) + def __init__(self, *template, **kwargs): # noqa: C901 """ Initialise the entity object. diff --git a/src/protean/server.py b/src/protean/server.py index 5fed1be8..1b6b3bc8 100644 --- a/src/protean/server.py +++ b/src/protean/server.py @@ -25,7 +25,7 @@ from protean.utils.importlib import import_from_full_path logging.basicConfig( - level=logging.DEBUG, # FIXME Pick up log level from config + level=logging.INFO, # FIXME Pick up log level from config format="%(threadName)10s %(name)18s: %(message)s", stream=sys.stderr, ) diff --git a/tests/adapters/broker/celery_broker/config.py b/tests/adapters/broker/celery_broker/config.py index 995054ea..6d876018 100644 --- a/tests/adapters/broker/celery_broker/config.py +++ b/tests/adapters/broker/celery_broker/config.py @@ -53,10 +53,10 @@ }, "handlers": { "console": { - "level": "DEBUG", + "level": "INFO", "class": "logging.StreamHandler", "formatter": "console", }, }, - "loggers": {"protean": {"handlers": ["console"], "level": "DEBUG"}}, + "loggers": {"protean": {"handlers": ["console"], "level": "INFO"}}, } diff --git a/tests/adapters/broker/redis_broker/config.py b/tests/adapters/broker/redis_broker/config.py index 4ee3e6cf..c26b000e 100644 --- a/tests/adapters/broker/redis_broker/config.py +++ b/tests/adapters/broker/redis_broker/config.py @@ -48,10 +48,10 @@ }, "handlers": { "console": { - "level": "DEBUG", + "level": "INFO", "class": "logging.StreamHandler", "formatter": "console", }, }, - "loggers": {"protean": {"handlers": ["console"], "level": "DEBUG"}}, + "loggers": {"protean": {"handlers": ["console"], "level": "INFO"}}, } diff --git a/tests/aggregate/test_aggregate_association.py b/tests/aggregate/test_aggregate_association.py index 0f49b446..327546f3 100644 --- a/tests/aggregate/test_aggregate_association.py +++ b/tests/aggregate/test_aggregate_association.py @@ -1,6 +1,8 @@ import mock import pytest +from protean.utils.container import attributes + from .elements import ( Account, AccountVia, @@ -240,3 +242,8 @@ def test_that_entities_up_to_configured_limit_value_are_retrieved( updated_post = test_domain.get_dao(Post).get(persisted_post.id) assert len(updated_post.comments) == 12 + + +class TestReference: + def test_that_reference_field_attribute_name_is_set_properly(self): + assert attributes(Author)["account_email"].attribute_name is not None