Package: django-override-autonow
django-override-autonowis a testing tool for Django’s project.django-override-autonowmakes it easy to create model instance usingDateFieldorDateTimeFieldwithauto_noworauto_now_addoptions.An example of django’s model:fromdjango.dbimportmodelsclassOrder(models.Model):amount=models.IntegerField()status=models.CharField(max_length=100)created_time=models.DateTimeField(auto_now_add=True)updated_time=models.DateTimeField(auto_now=True)To test this model:fromunittestimportmockfromdjango.testimportTestCasefromdjango.utilsimporttimezonefromoverride_autonowimportoverride_autonowfrom.modelsimportOrderclassTestOrder(TestCase):@override_autonowdeftest_with_django_override_autonow(self):# We need a order with desired created_time and updated_time for testorder=Order.objects.create(amount=200,status='PAID',created_time=timezone.datetime(year=2022,month=1,day=1,hour=23,minute=59,second=59),updated_time=timezone.datetime(year=2022,month=1,day=2,hour=0,minute=0,second=0),)# test orderdeftest_without_django_override_autonow(self):# Django's standard way is to mock timezone.now and save the model.# First, create object with mocking timezone.now and set desired value to created_time fieldwithmock.patch('django.db.models.fields.timezone')asmock_timezone:mock_timezone.now.return_value=timezone.datetime(year=2022,month=1,day=1,hour=23,minute=59,second=59)order=Order.objects.create(amount=200,status='PAID',)# Second, save object again with mocking timezone.now and set desired value to updated_time fieldwithmock.patch('django.db.models.fields.timezone')asmock_timezone:mock_timezone.now.return_value=timezone.datetime(year=2022,month=1,day=2,hour=0,minute=0,second=0)order.save()# test orderFeaturesSupport Django 2.2+ and Python 3.7+Compatible with major testing packagespytest’s class based test and function based test withpytest-djangofactory-boy’s model factoryChoose suitable style for your situationMethod decoratorClass decoratorContext managerFlexible override target selectionAffects only specified field names and exclude specified field namesAffects only specified models and exclude specified field namesAffects onlyauto_nowoption orauto_now_addoptionAffects onlyDateFieldorDateTimeFieldInstallationInstalling from PyPI:pip install django-override-autonowUse CasesTest with Django’s TestCase:fromdjango.testimportTestCasefromoverride_autonowimportoverride_autonowfrom.modelsimportOrderclassTestOrder(TestCase):# as method decorator@override_autonowdeftest_with_method_decorator(self):order=Order.objects.create(amount=200,status='PAID',created_time=timezone.datetime(year=2022,month=1,day=1,hour=23,minute=59,second=59),updated_time=timezone.datetime(year=2022,month=1,day=2,hour=0,minute=0,second=0),)# test orderdeftest_with_context_manager(self):# as context managerwithoverride_autonow():order_with_override=Order.objects.create(amount=200,status='PAID',created_time=timezone.datetime(year=2022,month=1,day=1,hour=23,minute=59,second=59),updated_time=timezone.datetime(year=2022,month=1,day=2,hour=0,minute=0,second=0),)order_without_override=Order.objects.create(amount=200,status='PAID',)# test order# as class decorator@override_autonowclassTestWithClassDecorator(TestCase):deftest_with_class_decorator(self):order=Order.objects.create(amount=200,status='PAID',created_time=timezone.datetime(year=2022,month=1,day=1,hour=23,minute=59,second=59),updated_time=timezone.datetime(year=2022,month=1,day=2,hour=0,minute=0,second=0),)# test orderTest with pytest(pytest-django):importpytestfromoverride_autonowimportoverride_autonowfrom.modelsimportOrder@pytest.mark.django_dbclassTestOrder:# as method decorator@override_autonowdeftest_with_method_decorator(self):order=Order.objects.create(amount=200,status='PAID',created_time=timezone.datetime(year=2022,month=1,day=1,hour=23,minute=59,second=59),updated_time=timezone.datetime(year=2022,month=1,day=2,hour=0,minute=0,second=0),)# test orderdeftest_with_context_manager(self):# as context managerwithoverride_autonow():order_without_autonow=Order.objects.create(amount=200,status='PAID',created_time=timezone.datetime(year=2022,month=1,day=1,hour=23,minute=59,second=59),updated_time=timezone.datetime(year=2022,month=1,day=2,hour=0,minute=0,second=0),)order_with_autonow=Order.objects.create(amount=200,status='PAID',)# test order# as class decorator@override_autonow@pytest.mark.django_dbclassTestWithClassDecorator:deftest_with_class_decorator(self):order=Order.objects.create(amount=200,status='PAID',created_time=timezone.datetime(year=2022,month=1,day=1,hour=23,minute=59,second=59),updated_time=timezone.datetime(year=2022,month=1,day=2,hour=0,minute=0,second=0),)# test orderOverride specific targets:fromdjango.testimportTestCasefromoverride_autonowimportoverride_autonowfrom.modelsimportOrderclassTestOrder(TestCase):@override_autonow(exclude_auto_now=True)deftest_exclude_auto_now_option(self):# Override only auto_now_add option...@override_autonow(exclude_auto_now_add=True)deftest_exclude_auto_now_add_option(self):# Override only auto_now option...@override_autonow(exclude_date_field=True)deftest_exclude_date_field(self):# Override only DateTimeField...@override_autonow(exclude_datetime_field=True)deftest_exclude_datetime_field(self):# Override only DateField...@override_autonow(exclude_field_names={'created_time'})deftest_exclude_field_names(self):# Override except fields named created_time...@override_autonow(exclude_models=(Order,))deftest_exclude_models(self):# Override except the Order model...@override_autonow(override_field_names={'created_time'})deftest_override_field_names(self):# Override only fields named created_time...@override_autonow(override_models=(Order,))deftest_override_models(self):# Override only the Order model...Test with factory-bot:fromdjango.testimportTestCasefromdjango.utilsimporttimezonefromfactory.djangoimportDjangoModelFactoryfromoverride_autonowimportoverride_autonowfrom.modelsimportOrderclassOrderFactory:classMeta:model=Orderamount=200,status='PAID',created_time=timezone.datetime(year=2022,month=1,day=1,hour=23,minute=59,second=59)updated_time=timezone.datetime(year=2022,month=1,day=2,hour=0,minute=0,second=0)classTestOrder(TestCase):@override_autonowdeftest_with_method_decorator(self):# Override created_time and updated_time with factoryorder=OrderFactory()# test order