16

I want to load fixtures into django. The data has some date fields - is it possible to create these data so they will always be e.g. yesterday or tomorrow? I want to make sure certain data is always fresh, but also so I can easily test edge cases (e.g. whether an object is enabled if the publishing date is today, etc).

Yoki32
  • 163
  • 1
  • 4

1 Answers1

8

Fixtures just load text data files (in JSON/XML/YAML) so, there's no real way to insert dynamically generated data by just loading a fixture. On the other hand, you can get around this using other methods.

One option is the package django-fixture-generator where you can write python/django code to create data and it will be inserted before your tests are called.

Another option is a previous SO question: How to load sql fixture in Django for User model?. This has some code on using SQL files for fixtures, where you can use a SQL expression for your date requirements (e.g. GETDATE()+1 or similar in your SQL dialect).

Community
  • 1
  • 1
ars
  • 120,335
  • 23
  • 147
  • 134