Wednesday, September 17, 2014

Loading timezone naive data into your test database with USE_TZ = True

I have a legacy database from which my Django application must migrate data into a Django database. The relevant date fields are actually TIMESTAMP columns in the database, but something (perhaps Django, or python's MySQL driver?) loads these columns as timezone naive datetime objects, rather than integers. So I wrote my migration code under the assumption that the dates coming out of the legacy database are timezone naive.

Unfortunately, now that I'm trying to write tests for this migrator, I can't find any way to load timezone naive datetimes into my test legacy database. I can't use integer timestamps, because the DateTimeField doesn't accept that kind of input (I get a JSON serialization error when I try), so I'm using datetime strings like this: "2014-08-01T00:00:00" in my fixture. But regardless of whether or not I include a UTC offset in the string, the datetime objects that come out of the database during my tests are somehow timezone aware. This causes my code to crash because it calls make_aware(), which throws ValueError('Not naive datetime (tzinfo is already set)'). 

It seems like having USE_TZ = True is forcibly making my fixture dates timezone aware, which I don't want. But USE_TZ will be True during the actual migration, so I can't just turn it off during the tests. So how can I load timezone naive dates into my test database?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/d0eb046a-ee9a-42ba-9dd7-40b92d4fdb52%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment