Skip to content

Commit

Permalink
Merge pull request #510 from adamchainz/time_machine
Browse files Browse the repository at this point in the history
Switch from freezegun to time-machine.
  • Loading branch information
foarsitter authored Mar 22, 2024
2 parents d51f5c7 + 262ac38 commit 26336b4
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 42 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

tox
sphinx
time-machine
twine
freezegun
24 changes: 12 additions & 12 deletions tests/test_fields/test_monitor_field.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
from datetime import datetime
from datetime import datetime, timezone

import time_machine
from django.test import TestCase
from freezegun import freeze_time

from model_utils.fields import MonitorField
from tests.models import DoubleMonitored, Monitored, MonitorWhen, MonitorWhenEmpty


class MonitorFieldTests(TestCase):
def setUp(self):
with freeze_time(datetime(2016, 1, 1, 10, 0, 0)):
with time_machine.travel(datetime(2016, 1, 1, 10, 0, 0, tzinfo=timezone.utc)):
self.instance = Monitored(name='Charlie')
self.created = self.instance.name_changed

Expand All @@ -18,10 +18,10 @@ def test_save_no_change(self):
self.assertEqual(self.instance.name_changed, self.created)

def test_save_changed(self):
with freeze_time(datetime(2016, 1, 1, 12, 0, 0)):
with time_machine.travel(datetime(2016, 1, 1, 12, 0, 0, tzinfo=timezone.utc)):
self.instance.name = 'Maria'
self.instance.save()
self.assertEqual(self.instance.name_changed, datetime(2016, 1, 1, 12, 0, 0))
self.assertEqual(self.instance.name_changed, datetime(2016, 1, 1, 12, 0, 0, tzinfo=timezone.utc))

def test_double_save(self):
self.instance.name = 'Jose'
Expand Down Expand Up @@ -52,7 +52,7 @@ class MonitorWhenFieldTests(TestCase):
Will record changes only when name is 'Jose' or 'Maria'
"""
def setUp(self):
with freeze_time(datetime(2016, 1, 1, 10, 0, 0)):
with time_machine.travel(datetime(2016, 1, 1, 10, 0, 0, tzinfo=timezone.utc)):
self.instance = MonitorWhen(name='Charlie')
self.created = self.instance.name_changed

Expand All @@ -61,16 +61,16 @@ def test_save_no_change(self):
self.assertEqual(self.instance.name_changed, self.created)

def test_save_changed_to_Jose(self):
with freeze_time(datetime(2016, 1, 1, 12, 0, 0)):
with time_machine.travel(datetime(2016, 1, 1, 12, 0, 0, tzinfo=timezone.utc)):
self.instance.name = 'Jose'
self.instance.save()
self.assertEqual(self.instance.name_changed, datetime(2016, 1, 1, 12, 0, 0))
self.assertEqual(self.instance.name_changed, datetime(2016, 1, 1, 12, 0, 0, tzinfo=timezone.utc))

def test_save_changed_to_Maria(self):
with freeze_time(datetime(2016, 1, 1, 12, 0, 0)):
with time_machine.travel(datetime(2016, 1, 1, 12, 0, 0, tzinfo=timezone.utc)):
self.instance.name = 'Maria'
self.instance.save()
self.assertEqual(self.instance.name_changed, datetime(2016, 1, 1, 12, 0, 0))
self.assertEqual(self.instance.name_changed, datetime(2016, 1, 1, 12, 0, 0, tzinfo=timezone.utc))

def test_save_changed_to_Pedro(self):
self.instance.name = 'Pedro'
Expand Down Expand Up @@ -123,7 +123,7 @@ def test_recursion_error_with_defer(self):

def test_monitor_still_works_with_deferred_fields_filtered_out_of_save_initial(self):
obj = DoubleMonitored.objects.defer('name').get(name='Charlie')
with freeze_time("2016-12-01"):
with time_machine.travel(datetime(2016, 12, 1, tzinfo=timezone.utc)):
obj.name = 'Charlie2'
obj.save()
self.assertEqual(obj.name_changed, datetime(2016, 12, 1))
self.assertEqual(obj.name_changed, datetime(2016, 12, 1, tzinfo=timezone.utc))
18 changes: 9 additions & 9 deletions tests/test_models/test_status_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
from datetime import datetime, timezone

import time_machine
from django.test.testcases import TestCase
from freezegun import freeze_time

from tests.models import Status, StatusCustomManager, StatusPlainTuple

Expand All @@ -13,7 +13,7 @@ def setUp(self):
self.active = Status.STATUS.active

def test_created(self):
with freeze_time(datetime(2016, 1, 1)):
with time_machine.travel(datetime(2016, 1, 1)):
c1 = self.model.objects.create()
self.assertTrue(c1.status_changed, datetime(2016, 1, 1))

Expand Down Expand Up @@ -43,29 +43,29 @@ def test_save_with_update_fields_overrides_status_changed_provided(self):
accordingly when update_fields is used as an argument
and status_changed is provided
'''
with freeze_time(datetime(2020, 1, 1)):
with time_machine.travel(datetime(2020, 1, 1, tzinfo=timezone.utc)):
t1 = Status.objects.create()

with freeze_time(datetime(2020, 1, 2)):
with time_machine.travel(datetime(2020, 1, 2, tzinfo=timezone.utc)):
t1.status = Status.on_hold
t1.save(update_fields=['status', 'status_changed'])

self.assertEqual(t1.status_changed, datetime(2020, 1, 2))
self.assertEqual(t1.status_changed, datetime(2020, 1, 2, tzinfo=timezone.utc))

def test_save_with_update_fields_overrides_status_changed_not_provided(self):
'''
Tests if the save method updated status_changed field
accordingly when update_fields is used as an argument
with status and status_changed is not provided
'''
with freeze_time(datetime(2020, 1, 1)):
with time_machine.travel(datetime(2020, 1, 1, tzinfo=timezone.utc)):
t1 = Status.objects.create()

with freeze_time(datetime(2020, 1, 2)):
with time_machine.travel(datetime(2020, 1, 2, tzinfo=timezone.utc)):
t1.status = Status.on_hold
t1.save(update_fields=['status'])

self.assertEqual(t1.status_changed, datetime(2020, 1, 2))
self.assertEqual(t1.status_changed, datetime(2020, 1, 2, tzinfo=timezone.utc))


class StatusModelPlainTupleTests(StatusModelTests):
Expand Down
38 changes: 19 additions & 19 deletions tests/test_models/test_timestamped_model.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone

import time_machine
from django.test import TestCase
from freezegun import freeze_time

from tests.models import TimeStamp, TimeStampWithStatusModel


class TimeStampedModelTests(TestCase):
def test_created(self):
with freeze_time(datetime(2016, 1, 1)):
with time_machine.travel(datetime(2016, 1, 1, tzinfo=timezone.utc)):
t1 = TimeStamp.objects.create()
self.assertEqual(t1.created, datetime(2016, 1, 1))
self.assertEqual(t1.created, datetime(2016, 1, 1, tzinfo=timezone.utc))

def test_created_sets_modified(self):
'''
Expand All @@ -20,13 +20,13 @@ def test_created_sets_modified(self):
self.assertEqual(t1.created, t1.modified)

def test_modified(self):
with freeze_time(datetime(2016, 1, 1)):
with time_machine.travel(datetime(2016, 1, 1, tzinfo=timezone.utc)):
t1 = TimeStamp.objects.create()

with freeze_time(datetime(2016, 1, 2)):
with time_machine.travel(datetime(2016, 1, 2, tzinfo=timezone.utc)):
t1.save()

self.assertEqual(t1.modified, datetime(2016, 1, 2))
self.assertEqual(t1.modified, datetime(2016, 1, 2, tzinfo=timezone.utc))

def test_overriding_created_via_object_creation_also_uses_creation_date_for_modified(self):
"""
Expand Down Expand Up @@ -104,12 +104,12 @@ def test_save_with_update_fields_overrides_modified_provided_within_a(self):

for update_fields in tests:
with self.subTest(update_fields=update_fields):
with freeze_time(datetime(2020, 1, 1)):
with time_machine.travel(datetime(2020, 1, 1, tzinfo=timezone.utc)):
t1 = TimeStamp.objects.create()

with freeze_time(datetime(2020, 1, 2)):
with time_machine.travel(datetime(2020, 1, 2, tzinfo=timezone.utc)):
t1.save(update_fields=update_fields)
self.assertEqual(t1.modified, datetime(2020, 1, 2))
self.assertEqual(t1.modified, datetime(2020, 1, 2, tzinfo=timezone.utc))

def test_save_is_skipped_for_empty_update_fields_iterable(self):
tests = (
Expand All @@ -120,31 +120,31 @@ def test_save_is_skipped_for_empty_update_fields_iterable(self):

for update_fields in tests:
with self.subTest(update_fields=update_fields):
with freeze_time(datetime(2020, 1, 1)):
with time_machine.travel(datetime(2020, 1, 1, tzinfo=timezone.utc)):
t1 = TimeStamp.objects.create()

with freeze_time(datetime(2020, 1, 2)):
with time_machine.travel(datetime(2020, 1, 2, tzinfo=timezone.utc)):
t1.test_field = 1
t1.save(update_fields=update_fields)

t1.refresh_from_db()
self.assertEqual(t1.test_field, 0)
self.assertEqual(t1.modified, datetime(2020, 1, 1))
self.assertEqual(t1.modified, datetime(2020, 1, 1, tzinfo=timezone.utc))

def test_save_updates_modified_value_when_update_fields_explicitly_set_to_none(self):
with freeze_time(datetime(2020, 1, 1)):
with time_machine.travel(datetime(2020, 1, 1, tzinfo=timezone.utc)):
t1 = TimeStamp.objects.create()

with freeze_time(datetime(2020, 1, 2)):
with time_machine.travel(datetime(2020, 1, 2, tzinfo=timezone.utc)):
t1.save(update_fields=None)

self.assertEqual(t1.modified, datetime(2020, 1, 2))
self.assertEqual(t1.modified, datetime(2020, 1, 2, tzinfo=timezone.utc))

def test_model_inherit_timestampmodel_and_statusmodel(self):
with freeze_time(datetime(2020, 1, 1)):
with time_machine.travel(datetime(2020, 1, 1, tzinfo=timezone.utc)):
t1 = TimeStampWithStatusModel.objects.create()

with freeze_time(datetime(2020, 1, 2)):
with time_machine.travel(datetime(2020, 1, 2, tzinfo=timezone.utc)):
t1.save(update_fields=['test_field', 'status'])

self.assertEqual(t1.modified, datetime(2020, 1, 2))
self.assertEqual(t1.modified, datetime(2020, 1, 2, tzinfo=timezone.utc))
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ python =

[testenv]
deps =
freezegun==0.3.12
time-machine==2.4.1
-rrequirements-test.txt
dj32: Django==3.2.*
dj40: Django==4.0.*
Expand Down

0 comments on commit 26336b4

Please sign in to comment.