35👍
✅
You can use mock
:
import pytz
from unittest import mock
def test_get_registration_date(self):
mocked = datetime.datetime(2018, 4, 4, 0, 0, 0, tzinfo=pytz.utc)
with mock.patch('django.utils.timezone.now', mock.Mock(return_value=mocked)):
user = factories.UserFactory.create()
self.assertEqual(user.get_registration_date(), mocked)
9👍
You can use the package freeze gun. https://github.com/spulec/freezegun which patchs datetime.now().
from freezegun import freeze_time
...
@freeze_time("2017-06-23 07:28:00")
def test_get_registration_date(self):
user = factories.UserFactory.create()
self.assertEqual(
datetime.strftime(user.get_registration_date(), "%Y-%m-%d %H:%M:%S")
"2017-06-23 07:28:00"
)
- [Django]-Passing STATIC_URL to file javascript with django
- [Django]-Determine variable type within django template
- [Django]-How do I install psycopg2 for Python 3.x?
5👍
The universal way to use Factory Boy
with Django
with DateTime
with auto_now_add=True
- Introduce custom DjangoFactory, with arg
_fake_time
to freeze time during object creation
from contextlib import suppress
from factory.django import DjangoModelFactory
from freezegun import freeze_time
from functools import partial
class CustomDjangoModelFactory(DjangoModelFactory):
@classmethod
def create(cls, _fake_time=None, **kwargs):
wrapper = partial(freeze_time, time_to_freeze=_fake_time) if _fake_time else suppress
with wrapper(_fake_time):
return super().create(**kwargs)
- Add/change your factory to be based on
CustomDjangoModelFactory
import factory
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=255)
created = models.DateTimeField(auto_now_add=True)
class MyModelFactory(CustomDjangoModelFactory):
name = factory.Sequence(lambda n: "Model No. %03d" % n)
class Meta:
model = MyModel
- Use it with
_fake_time
or without
my_model1 = MyModelFactory(name='first', _fake_time='2020-12-1')
my_model2 = MyModelFactory(name='second', _fake_time=datetime(2020, 11, 30))
my_model3 = MyModelFactory(name='third')
- [Django]-How do I install psycopg2 for Python 3.x?
- [Django]-Create a field whose value is a calculation of other fields' values
- [Django]-Removing 'Sites' from Django admin page
1👍
To simplify your job with @neverwalkaloner ‘s answer, you can define a more specific context manager:
import pytz
from unittest import mock
from contextlib import contextmanager
@contextmanager
def current_time(dt):
with mock.patch('django.utils.timezone.now', mock.Mock(return_value=dt)):
yield dt
and use it as:
def test_get_registration_date(self):
with current_time(datetime(2021, 9, 12, 0, 0, 0, tzinfo=pytz.utc)) as mocked:
user = factories.UserFactory.create()
self.assertEqual(user.get_registration_date(), mocked)
- [Django]-In django, how do I sort a model on a field and then get the last item?
- [Django]-How to format time in django-rest-framework's serializer?
- [Django]-Name '_' is not defined
0👍
Just use the factory.post_generation
decorator:
class UserFactory(factory.DjangoModelFactory):
...
@factory.post_generation
def registration_date(self, create, extracted, **kwargs):
if extracted:
self.registration_date = extracted
👤Zulu
- [Django]-Django error: got multiple values for keyword argument
- [Django]-Http POST drops port in URL
- [Django]-Serializer call is showing an TypeError: Object of type 'ListSerializer' is not JSON serializable?
Source:stackexchange.com