1
# Copyright 2012 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Test related classes and functions for maas and its applications."""
6
from __future__ import (
19
from django.conf import settings
20
from django.core.management import call_command
21
from django.db.models import loading
27
class TestCase(testtools.TestCase, django.test.TestCase):
28
"""`TestCase` for Metal as a Service.
30
Supports test resources and fixtures.
33
# testresources.ResourcedTestCase does something similar to this class
34
# (with respect to setUpResources and tearDownResources) but it explicitly
35
# up-calls to unittest.TestCase instead of using super() even though it is
36
# not guaranteed that the next class in the inheritance chain is
42
super(TestCase, self).setUp()
45
def setUpResources(self):
46
testresources.setUpResources(
47
self, self.resources, testresources._get_result())
50
self.tearDownResources()
51
super(TestCase, self).tearDown()
53
def tearDownResources(self):
54
testresources.tearDownResources(
55
self, self.resources, testresources._get_result())
57
# Django's implementation for this seems to be broken and was
58
# probably only added to support compatibility with python 2.6.
59
assertItemsEqual = unittest.TestCase.assertItemsEqual
62
class TestModelTestCase(TestCase):
63
"""A custom test case that adds support for test-only models.
65
For instance, if you want to have a model object used solely for testing
66
in your application 'myapp1' you would create a test case that uses
67
TestModelTestCase as its base class and:
68
- initialize self.app with 'myapp1.tests'
69
- define the models used for testing in myapp1.tests.models
71
This way the models defined in myapp1.tests.models will be available in
72
this test case (and this test case only).
75
# Set the appropriate application to be loaded.
79
# Add the models to the db.
80
self._original_installed_apps = list(settings.INSTALLED_APPS)
81
assert self.app is not None, "TestCase.app must be defined!"
82
settings.INSTALLED_APPS.append(self.app)
83
loading.cache.loaded = False
84
call_command('syncdb', interactive=False, verbosity=0)
85
super(TestModelTestCase, self)._pre_setup()
87
def _post_teardown(self):
88
super(TestModelTestCase, self)._post_teardown()
89
# Restore the settings.
90
settings.INSTALLED_APPS = self._original_installed_apps
91
loading.cache.loaded = False