spulec / freezegun

Let your Python tests travel through time
Apache License 2.0
4.17k stars 269 forks source link

Django accepts freezing only year, month, and day #191

Open elcolie opened 7 years ago

elcolie commented 7 years ago

django==1.10.6 freezegun==0.3.9 python3.6.0

    @freeze_time("2012-01-14 03:21:34", tz_offset=+7)
    def test_post_full_order(self):
        mommy.make(Branch, _quantity=1, name="Elcolie Branch")
        mommy.make(Dealer, _quantity=1, branch=Branch.objects.first())
        response = self.client.post(reverse('api:order-list'), data=self.data, format='json')
        assert 201 == response.status_code

Run command on specific unit

(eneos-pos-web) Sarits-MacBook-Air-2:eneos-pos-web el$ python manage.py test eneos.apps.orders.tests.OrderAPITest.test_post_full_order --nomigrations --settings=eneos.config.settings.local
Hey, I'm Local!
Creating test database for alias 'default'...
/Users/el/.pyenv/versions/eneos-pos-web/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1372: RuntimeWarning: DateTimeField Order.created_at received a naive datetime (2012-01-14 00:00:00) while time zone support is active.
  RuntimeWarning)
.
----------------------------------------------------------------------
Ran 1 test in 0.672s

settings.py

USE_TZ = True

TIME_ZONE = 'Asia/Tokyo'

DISPLAY_TIME_ZONE_PTZ = 'Asia/Tokyo'
spulec commented 7 years ago

Hmmm, that doesn't seem right. Here is an example of the test generally working: https://github.com/spulec/freezegun/blob/master/tests/test_datetimes.py#L64-L77

Can you give more details of your model files or anything else? Anywhere you might accidentally be casting to a date?

elcolie commented 7 years ago

OSX: 10.12.5

class Order(AbstractSoftModelController):
     ...

class AbstractSoftModelController(AbstractSoftDeletionModel, AbstractModelController):
    """
    Shortcut for mixing Soft Delete and Model Controller
    """

    class Meta:
        abstract = True

class AbstractTimeStampMarker(models.Model):
    created_at = models.DateTimeField(auto_now_add=True, verbose_name=_("Created at"))
    updated_at = models.DateTimeField(auto_now=True, verbose_name=_("Updated at"))

    class Meta:
        abstract = True

class AbstractModelController(AbstractTimeStampMarker):
    created_user = models.ForeignKey(User, related_name="%(class)s_created_user", verbose_name=_("Created User"))
    updated_user = models.ForeignKey(User, related_name="%(class)s_updated_user", verbose_name=_("Updated User"))

    class Meta:
        abstract = True
python manage.py showmigrations  --settings=eneos.config.settings.local

orders
 [X] 0001_initial
 [X] 0002_order_suggestion_for_customer
 [X] 0003_auto_20170330_2012
 [X] 0004_order_sales_datetime
 [X] 0005_auto_20170425_2120
 [X] 0006_auto_20170518_1857
 [X] 0007_auto_20170518_2039
 [X] 0008_auto_20170605_2158

I would like to make sure that I am not missed any migrations.

python manage.py makemigrations --settings=eneos.config.settings.local
Hey, I'm Local!
No changes detected
axper commented 7 years ago

I would like to inform that I am also experiencing the same RuntimeWarning in Django.

elcolie commented 7 years ago

Hope it will be gone in Django2

marcelofern commented 2 years ago

Faced this error today. Is this being worked on? The issue seems to be very old.

boxed commented 2 years ago

@marcelofern this is an open source project. There is no "being worked on" unless a volunteer does it. PRs are of course welcome.