Testing Django Models with FileField

django, unit-testing

Solution

This is a partial answer to my question, and to help anyone else who found this question from a search.

Django includes a facility it refers to as 'fixtures' to handle the primary loading of data during testing. Creating a fixture is a 2 step process:

- Add some fake data to the app's model using the admin tool

- Run the following: manage.py dumpdata [appname] --indent=2 > filename.json

The fixture file *.json remains in your Django project root folder.

In your tests.py file, you can load the fixture on the Django TestCase class as so:

class YourTestCase(TestCase):    
    fixtures = ['filename.json','whatever.json',]

Once the fixture is loaded, you can use the data as you would normally use the ORM. Here's my working test case from the above code if you'd like an example.

from django.test import TestCase

from django.contrib.auth.models import User
from mediamanager.models import Media

class MediaManagerTestCase(TestCase):

    fixtures = ['auth_data.json','mediamanager_data.json',]

    def setUp(self):
        self.fakeuser = User.objects.get(username='fakeuser')
        self.fakestaff = User.objects.get(username='fakestaff')
        self.fakeadmin = User.objects.get(username='fakeadmin')

    def test_media_can_edit(self):
        um = Media.objects.get(pk=1)    # Media owned by fakeuser
        sm = Media.objects.get(pk=2)    # Media owned by fakstaff

        self.assertEquals(um.can_edit(self.fakeuser), True)
        self.assertEquals(sm.can_edit(self.fakeuser), False)

        self.assertEquals(um.can_edit(self.fakestaff), True)
        self.assertEquals(sm.can_edit(self.fakestaff), True)

        self.assertEquals(um.can_edit(self.fakeadmin), True)
        self.assertEquals(sm.can_edit(self.fakeadmin), True)

Problem

I'm trying to make the transition to testing with Django. This is the particular model in question for testing: ``` class Media(models.Model): file = models.FileField(upload_to='upload',) thumbnail = models.ImageField(upload_to='upload', blank=True,) # ... ``` PART 1: How do I deal with these FileFields? (Particularly in the sense that I need to generate fake entries to test bits of code) PART 2: Below is the testing code I've begun to write. Am I doing this correctly or should I be using a form of "mocking"? ``` from django.test import TestCase from django.test.client import Client from django.contrib.auth.models import User from mediamanager.models import Media class MediaManagerTestCase(TestCase): def setUp(self): self.fake_user = User.objects.create(username='fakeuser', is_staff=false) self.fake_staff = User.objects.create(username='fakestaff', is_staff=true) self.fake_admin = User.objects.create(username='fakeadmin', is_superuser=true) self.fake_media_image = Media.objects.create() # Hmmm... self.fake_media_video = Media.objects.create() # How do i do this... def testMediaCanEdit(self): perm = self.fake_media_image.can_edit(self.fake_user) self.assertEquals(perm, false) ```

Original source