Wednesday, 14 October 2015

Loading fixtures in an APITestCase

Fixtures don't really have anything to do with the fact that you are testing the app. All you need is a fixture folder inside your app folder, which must contain a file called initial_data.json. When you run migrate or syncdb, the fixtures are automatically inserted into your db.
Here is how it should look:
your_app
  |
  |__ models.py
  |__ views.py
  |__ fixtures
         |
         |__ initial_data.json
You don't need to include/write any other things.


Providing initial data with fixtures

A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you’ve already got some data is to use the manage.py dumpdata command. Or, you can write fixtures by hand; fixtures can be written as JSON, XML or YAML (with PyYAML installed) documents. The serialization documentation has more details about each of these supported serialization formats.
As an example, though, here’s what a fixture for a simple Person model might look like in JSON:
[
  {
    "model": "myapp.person",
    "pk": 1,
    "fields": {
      "first_name": "John",
      "last_name": "Lennon"
    }
  },
  {
    "model": "myapp.person",
    "pk": 2,
    "fields": {
      "first_name": "Paul",
      "last_name": "McCartney"
    }
  }
]
And here’s that same fixture as YAML:
- model: myapp.person
  pk: 1
  fields:
    first_name: John
    last_name: Lennon
- model: myapp.person
  pk: 2
  fields:
    first_name: Paul
    last_name: McCartney
You’ll store this data in a fixtures directory inside your app.
Loading data is easy: just call manage.py loaddata <fixturename>, where <fixturename> is the name of the fixture file you’ve created. Each time you run loaddata, the data will be read from the fixture and re-loaded into the database. Note this means that if you change one of the rows created by a fixture and then run loaddata again, you’ll wipe out any changes you’ve made.
Fixtures 
Fixtures are very powerful to play with your database sample data during development process. After each python manage.py reset <myapp> command you need to populate database with sample data again and again using admin interface. It's quite boring, isn't it? With fixtures our life became more comfortable and easy. Look at this example. Lets imagine that you have some data in db. We can dump it, even if your models have ForeignKeys or any kind of *To* relations.
First we need to define fixtures dir in settings file:
FIXTURE_DIRS = (
   '/path/to/myapp/fixtures/',
)
Lets dump our data:
cd /path/to/my_project
python manage.py dumpdata --format=json myapp > /path/to/myapp/fixtures/initial_data.json
Reset:
python manage.py reset myapp

You have requested a database reset.
This will IRREVERSIBLY DESTROY any data for
the "myapp" application in the database "mydb".
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Now we have clean DB, lets populate it with our sample data:
python manage.py syncdb
Loading 'initial_data' fixtures...
Installing json fixture 'initial_data' from '/path/to/myapp/fixtures/'.
Installed 24 object(s) from 1 fixture(s)



The way you describe should work also. Make sure you're declaring the fixtures in the test class. An example below:


class MyViewsTestCase(APITestCase):

fixtures = ['some_testdata.json']

def test_random_thingy(self):
    variable = 'hello'
    self.assertEqual(variable, 'hello')
Every time you're test is run, the fixtures will be loaded in and removed after the test has run.


































15 comments: