From f4aedbfb2b004b182c527a13a548e803bbf04f98 Mon Sep 17 00:00:00 2001 From: collerek Date: Thu, 10 Dec 2020 16:22:01 +0100 Subject: [PATCH 1/2] fix issue 70 --- docs/releases.md | 7 ++- ormar/__init__.py | 2 +- ormar/models/model.py | 3 +- tests/test_saving_nullable_fields.py | 67 ++++++++++++++++++++++++++++ tests/test_saving_related.py | 2 +- 5 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 tests/test_saving_nullable_fields.py diff --git a/docs/releases.md b/docs/releases.md index ac31dff..a09424b 100644 --- a/docs/releases.md +++ b/docs/releases.md @@ -1,3 +1,7 @@ +# 0.7.2 + +* Fix for overwriting related models with pk only in `Model.update() with fields passed as parameters` (fix [#70][#70]) + # 0.7.1 * Fix for overwriting related models with pk only in `Model.save()` (fix [#68][#68]) @@ -183,4 +187,5 @@ Add queryset level methods [#19]: https://github.com/collerek/ormar/issues/19 [#60]: https://github.com/collerek/ormar/issues/60 -[#68]: https://github.com/collerek/ormar/issues/68 \ No newline at end of file +[#68]: https://github.com/collerek/ormar/issues/68 +[#70]: https://github.com/collerek/ormar/issues/70 \ No newline at end of file diff --git a/ormar/__init__.py b/ormar/__init__.py index 173c4f5..0319847 100644 --- a/ormar/__init__.py +++ b/ormar/__init__.py @@ -44,7 +44,7 @@ class UndefinedType: # pragma no cover Undefined = UndefinedType() -__version__ = "0.7.1" +__version__ = "0.7.2" __all__ = [ "Integer", "BigInteger", diff --git a/ormar/models/model.py b/ormar/models/model.py index d412a89..8e7a8f5 100644 --- a/ormar/models/model.py +++ b/ormar/models/model.py @@ -269,8 +269,7 @@ class Model(NewBaseModel): async def update(self: T, **kwargs: Any) -> T: if kwargs: - new_values = {**self.dict(), **kwargs} - self.from_dict(new_values) + self.from_dict(kwargs) if not self.pk: raise ModelPersistenceError( diff --git a/tests/test_saving_nullable_fields.py b/tests/test_saving_nullable_fields.py new file mode 100644 index 0000000..cc8eb36 --- /dev/null +++ b/tests/test_saving_nullable_fields.py @@ -0,0 +1,67 @@ +from typing import Optional + +import databases +import sqlalchemy +from sqlalchemy import create_engine + +import ormar +import pytest + +from tests.settings import DATABASE_URL + +db = databases.Database(DATABASE_URL, force_rollback=True) +metadata = sqlalchemy.MetaData() + + +class PrimaryModel(ormar.Model): + class Meta: + metadata = metadata + database = db + tablename = "primary_models" + + id: int = ormar.Integer(primary_key=True) + name: str = ormar.String(max_length=255, index=True) + some_text: str = ormar.Text() + # NOTE: Removing nullable=True makes the test pass. + some_other_text: Optional[str] = ormar.Text(nullable=True) + + +class SecondaryModel(ormar.Model): + class Meta: + metadata = metadata + database = db + tablename = "secondary_models" + + id: int = ormar.Integer(primary_key=True) + name: str = ormar.String(max_length=100) + primary_model: PrimaryModel = ormar.ForeignKey( + PrimaryModel, related_name="secondary_models", + ) + + +@pytest.fixture(autouse=True, scope="module") +def create_test_database(): + engine = create_engine(DATABASE_URL) + metadata.create_all(engine) + yield + metadata.drop_all(engine) + + +@pytest.mark.asyncio +async def test_create_models(): + primary = await PrimaryModel( + name="Foo", some_text="Bar", some_other_text="Baz" + ).save() + assert primary.id == 1 + + secondary = await SecondaryModel(name="Foo", primary_model=primary).save() + assert secondary.id == 1 + assert secondary.primary_model.id == 1 + + +@pytest.mark.asyncio +async def test_update_secondary(): + secondary = await SecondaryModel.objects.get(id=1) + assert secondary.name == "Foo" + await secondary.update(name="Updated") + assert secondary.name == "Updated" diff --git a/tests/test_saving_related.py b/tests/test_saving_related.py index 26c7000..6dd4fd2 100644 --- a/tests/test_saving_related.py +++ b/tests/test_saving_related.py @@ -55,7 +55,7 @@ async def test_model_relationship(): assert ws.topic == "Topic 1" assert ws.category.name == "Foo" - ws.topic = 'Topic 2' + ws.topic = "Topic 2" await ws.update() assert ws.id == 1 From 099615c69040bbb30f0bbadd82148fc07835b240 Mon Sep 17 00:00:00 2001 From: collerek Date: Thu, 10 Dec 2020 16:27:19 +0100 Subject: [PATCH 2/2] fix tests --- tests/test_saving_nullable_fields.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/tests/test_saving_nullable_fields.py b/tests/test_saving_nullable_fields.py index cc8eb36..061e578 100644 --- a/tests/test_saving_nullable_fields.py +++ b/tests/test_saving_nullable_fields.py @@ -49,19 +49,18 @@ def create_test_database(): @pytest.mark.asyncio async def test_create_models(): - primary = await PrimaryModel( - name="Foo", some_text="Bar", some_other_text="Baz" - ).save() - assert primary.id == 1 + async with db: + async with db.transaction(force_rollback=True): + primary = await PrimaryModel( + name="Foo", some_text="Bar", some_other_text="Baz" + ).save() + assert primary.id == 1 - secondary = await SecondaryModel(name="Foo", primary_model=primary).save() - assert secondary.id == 1 - assert secondary.primary_model.id == 1 + secondary = await SecondaryModel(name="Foo", primary_model=primary).save() + assert secondary.id == 1 + assert secondary.primary_model.id == 1 - -@pytest.mark.asyncio -async def test_update_secondary(): - secondary = await SecondaryModel.objects.get(id=1) - assert secondary.name == "Foo" - await secondary.update(name="Updated") - assert secondary.name == "Updated" + secondary = await SecondaryModel.objects.get() + assert secondary.name == "Foo" + await secondary.update(name="Updated") + assert secondary.name == "Updated"