Created
September 23, 2012 13:40
-
-
Save davebridges/3771041 to your computer and use it in GitHub Desktop.
Sample ModelTests for a Django App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This package contains the unit tests for the :mod:`app` app. | |
There are tests for each model in this app. | |
""" | |
from django.test import TestCase | |
from django.test.client import Client | |
from django.contrib.auth.models import User | |
from app.models import ExampleModel | |
MODELS = [ExampleModel,] | |
class ExampleModelTests(TestCase): | |
'''This class tests various aspects of the :class:`~app.models.ExampleModel` model.''' | |
fixtures = ['fixture_example_model', ] | |
def setUp(self): | |
'''Instantiate the test client. Creates a test user.''' | |
self.client = Client() | |
self.test_user = User.objects.create_user('testuser', '[email protected]', 'testpassword') | |
self.test_user.is_superuser = True | |
self.test_user.is_active = True | |
self.test_user.save() | |
self.assertEqual(self.test_user.is_superuser, True) | |
login = self.client.login(username='testuser', password='testpassword') | |
self.failUnless(login, 'Could not log in') | |
def tearDown(self): | |
'''Depopulate created model instances from test database.''' | |
for model in MODELS: | |
for obj in model.objects.all(): | |
obj.delete() | |
def test_create_new_model_minimum(self): | |
'''This test creates a :class:`~app.models.ExampleModel` with the required information only.''' | |
test_model = ExampleModel(name='Test Name') #repeat for all required fields | |
test_model.save() | |
self.assertEqual(test_model.pk, 2) #presumes one model loaded in fixture data | |
def test_create_new_model_all(self): | |
'''This test creates a :class:`~app.models.ExampleModel` with all information entered.''' | |
test_model = ExampleModel(name='Test Name') #repeat for all fields | |
test_model.save() | |
self.assertEqual(test_model.pk, 2) #presumes one model loaded in fixture data | |
def test_model_unicode(self): | |
'''This tests the unicode representation of a :class:`~app.models.ExampleModel`.''' | |
test_model = ExampleModel(name='Test Name') #repeat for all required fields | |
test_model.save() | |
self.assertEqual(test_model.__unicode__(), "Whatever the unicode name should be") | |
def test_model_object_slug(self): | |
'''This tests the slug field generation of a :class:`~app.models.ExampleModel`.''' | |
test_model = Publication(title='Test Name.') | |
test_model.save() | |
self.assertEqual(test_model.slug, "test-name") | |
def test_model_absolute_url(self): | |
'''This tests the absolute_url generation of a :class:`~app.models.ExampleModel`.''' | |
test_publication = ExampleModel(title='Test Name') | |
test_publication.save() | |
self.assertEqual(test_publication.get_absolute_url(), "/app/examplemodel/1/") #where the url should be |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment