How to test auto_now_add in django
Asked Answered
N

5

20

I have django 1.11 app and I want to write unit test for my solution.

I want to test registration date feature.

model.py:

class User(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    registration_date = models.DateTimeField(auto_now_add=True)

    def get_registration_date(self):
        return self.registration_date

I'm using also django-boy for models factories: factories.py

  class UserFactory(factory.DjangoModelFactory):
        class Meta:
            model = models.User
        first_name = 'This is first name'
        last_name = 'This is last name'
        registration_date = timezone.now()

test.py

def test_get_registration_date(self):
    user = factories.UserFactory.create()
    self.assertEqual(user.get_registration_date(), timezone.now())

Problem is that I recived AssertionError:

AssertionError: datetime.datetime(2018, 4, 17, 9, 39, 36, 707927, tzinfo=<UTC>) != datetime.datetime(2018, 4, 17, 9, 39, 36, 708069, tzinfo=<UTC>)
Nuno answered 17/4, 2018 at 9:50 Comment(1)
why dont give a dummy specific time at creation for simplificationLedge
A
38

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)
Aidaaidan answered 17/4, 2018 at 9:59 Comment(1)
Thanks! Works for auto_now fields as well.Neutralism
S
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"
        )
Sadism answered 17/4, 2018 at 10:3 Comment(0)
G
5

The universal way to use Factory Boy with Django with DateTime with auto_now_add=True

  1. 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)
  1. 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
  1. 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')
Gimpel answered 15/12, 2021 at 13:47 Comment(0)
C
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)
Centroid answered 12/9, 2021 at 11:2 Comment(0)
I
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
Iatric answered 22/1, 2019 at 16:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.