Skip to content

Commit

Permalink
Set Reference field name on Entity construction
Browse files Browse the repository at this point in the history
  • Loading branch information
subhashb committed Sep 3, 2021
1 parent 2ff3444 commit 6416c33
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/protean/core/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/protean/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 2 additions & 2 deletions tests/adapters/broker/celery_broker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
}
4 changes: 2 additions & 2 deletions tests/adapters/broker/redis_broker/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
}
7 changes: 7 additions & 0 deletions tests/aggregate/test_aggregate_association.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import mock
import pytest

from protean.utils.container import attributes

from .elements import (
Account,
AccountVia,
Expand Down Expand Up @@ -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

0 comments on commit 6416c33

Please sign in to comment.