Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix property_fields not being inherited. Fix for #774. #891

Merged
merged 3 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ormar/models/metaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def update_attrs_from_base_meta( # noqa: CCR001
:type model_fields: Dict[str, BaseField]
"""

params_to_update = ["metadata", "database", "constraints"]
params_to_update = ["metadata", "database", "constraints", "property_fields"]
for param in params_to_update:
current_value = attrs.get("Meta", {}).__dict__.get(param, ormar.Undefined)
parent_value = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import databases
import pytest
import sqlalchemy
import sqlalchemy as sa

import ormar
from tests.settings import DATABASE_URL

metadata = sa.MetaData()
database = databases.Database(DATABASE_URL)


class BaseFoo(ormar.Model):
class Meta:
abstract = True

name: str = ormar.String(max_length=100)

@ormar.property_field
def prefixed_name(self) -> str:
return "prefix_" + self.name


class Foo(BaseFoo):
class Meta:
metadata = metadata
database = database

@ormar.property_field
def double_prefixed_name(self) -> str:
return "prefix2_" + self.name

id: int = ormar.Integer(primary_key=True)


class Bar(BaseFoo):
class Meta:
metadata = metadata
database = database

@ormar.property_field
def prefixed_name(self) -> str:
return "baz_" + self.name

id: int = ormar.Integer(primary_key=True)


@pytest.fixture(autouse=True, scope="module")
def create_test_database():
engine = sqlalchemy.create_engine(DATABASE_URL)
metadata.drop_all(engine)
metadata.create_all(engine)
yield
metadata.drop_all(engine)


def test_property_fields_are_inherited():
foo = Foo(name="foo")
assert foo.prefixed_name == "prefix_foo"
assert foo.dict() == {
"name": "foo",
"id": None,
"double_prefixed_name": "prefix2_foo",
"prefixed_name": "prefix_foo",
}

bar = Bar(name="bar")
assert bar.prefixed_name == "baz_bar"
assert bar.dict() == {"name": "bar", "id": None, "prefixed_name": "baz_bar"}