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 update on save() for fields #129

Merged
merged 1 commit into from
Jan 24, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions docs/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ class MyModel(saffier.Model):

##### Parameters

* **auto_now** - A boolean indicating the `auto_now` enabled.
* **auto_now_add** - A boolean indicating the `auto_now_add` enabled.

* **auto_now** - A boolean indicating the `auto_now` enabled. Useful for auto updates.
* **auto_now_add** - A boolean indicating the `auto_now_add` enabled. This will ensure that its
only added once.

#### DateTimeField

Expand All @@ -199,9 +199,9 @@ class MyModel(saffier.Model):

##### Parameters

* **auto_now** - A boolean indicating the `auto_now` enabled.
* **auto_now_add** - A boolean indicating the `auto_now_add` enabled.

* **auto_now** - A boolean indicating the `auto_now` enabled. Useful for auto updates.
* **auto_now_add** - A boolean indicating the `auto_now_add` enabled. This will ensure that its
only added once.

#### DecimalField

Expand Down
13 changes: 2 additions & 11 deletions saffier/core/db/models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,8 @@ async def _save(self, **kwargs: typing.Any) -> "Model":
saffier_setattr(self, self.pkname, awaitable)
return self

async def _update(self, **kwargs: typing.Any) -> typing.Any:
"""
Performs the save instruction.
"""
pk_column = getattr(self.table.c, self.pkname)
expression = self.table.update().values(**kwargs).where(pk_column == self.pk)
awaitable = await self.database.execute(expression)
return awaitable

async def save(
self: typing.Any,
self,
force_save: bool = False,
values: typing.Any = None,
**kwargs: typing.Any,
Expand Down Expand Up @@ -162,7 +153,7 @@ async def save(
await self._save(**kwargs)
else:
await self.signals.pre_update.send(sender=self.__class__, instance=self, kwargs=kwargs)
await self._update(**kwargs)
await self.update(**kwargs)
await self.signals.post_update.send(sender=self.__class__, instance=self)

# Refresh the results
Expand Down
66 changes: 66 additions & 0 deletions tests/models/test_datetime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import datetime
import time

import pytest

import saffier
from saffier.testclient import DatabaseTestClient as Database
from tests.settings import DATABASE_URL

database = Database(url=DATABASE_URL)
models = saffier.Registry(database=database)

pytestmark = pytest.mark.anyio


class User(saffier.Model):
name: str = saffier.CharField(max_length=255, secret=True)
created_at: datetime.datetime = saffier.DateTimeField(auto_now_add=True)
updated_at: datetime.datetime = saffier.DateTimeField(auto_now=True)

class Meta:
registry = models


@pytest.fixture(autouse=True, scope="module")
async def create_test_database():
await models.create_all()
yield
await models.drop_all()


@pytest.fixture(autouse=True)
async def rollback_transactions():
with database.force_rollback():
async with database:
yield


async def test_creates_and_updates_only_updated_at():
user = await User.query.create(name="Test")

last_created_datetime = user.created_at
last_updated_datetime = user.updated_at

time.sleep(2)

await user.update(name="Test 2")

assert user.created_at == last_created_datetime
assert user.updated_at != last_updated_datetime


async def test_creates_and_updates_only_updated_at_on_save():
user = await User.query.create(name="Test")

last_created_datetime = user.created_at
last_updated_datetime = user.updated_at

time.sleep(2)

user.name = "Test 2"
await user.save()

assert user.name == "Test 2"
assert user.created_at == last_created_datetime
assert user.updated_at != last_updated_datetime