django-model-utils/tests/test_models/test_timestamped_model.py
Daniel Andrlik 25743141bc Ensure TimeStampedModel modified equals created on initial creation. (#319)
*  Ensure TimeStampedModel modified equals created on initial creation.

Add logic to AutoLastModifiedField which checks to see if the associated created field of the correct cField class type is also present. If the instance has not yet been saved (missing a pk), then set the value to modified to be equal to created.

Fixes #247

📚 Update changes and authors list related to changes.

* 🚑 Set TimeStampedModel modified to be equal to created during first save.

If instance does not yet have a pk, before defaulting the last modified to the current time, iterate over the the fields of the model, and instead use whatever value is found in the first occurance of the AutoCreatedField.

Fixes #247

* Move changelog up to unreleased section.
2018-12-08 12:23:04 +06:00

32 lines
885 B
Python

from __future__ import unicode_literals
from datetime import datetime
from freezegun import freeze_time
from django.test import TestCase
from tests.models import TimeStamp
class TimeStampedModelTests(TestCase):
def test_created(self):
with freeze_time(datetime(2016, 1, 1)):
t1 = TimeStamp.objects.create()
self.assertEqual(t1.created, datetime(2016, 1, 1))
def test_created_sets_modified(self):
'''
Ensure that on creation that modifed is set exactly equal to created.
'''
t1 = TimeStamp.objects.create()
self.assertEqual(t1.created, t1.modified)
def test_modified(self):
with freeze_time(datetime(2016, 1, 1)):
t1 = TimeStamp.objects.create()
with freeze_time(datetime(2016, 1, 2)):
t1.save()
self.assertEqual(t1.modified, datetime(2016, 1, 2))