~lutostag/ubuntu/utopic/maas/1.5.2

« back to all changes in this revision

Viewing changes to src/maastesting/__init__.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-03-15 18:14:08 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120315181408-zgl94hzo0x4n99an
Tags: 0.1+bzr295+dfsg-0ubuntu2
* debian/patches:
  - 01-fix-database-settings.patch: Update to set PSERV_URL.
  - 02-pserv-config.patch: Set port to 8001.
* debian/maas.postinst: Run maas-import-isos on install.
* debian/control: Depends on rabbitmq-server.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2012 Canonical Ltd.  This software is licensed under the
2
 
# GNU Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""Test related classes and functions for maas and its applications."""
5
 
 
6
 
from __future__ import (
7
 
    print_function,
8
 
    unicode_literals,
9
 
    )
10
 
 
11
 
__metaclass__ = type
12
 
__all__ = [
13
 
    'TestCase',
14
 
    'TestModelTestCase',
15
 
    ]
16
 
 
17
 
import unittest
18
 
 
19
 
from django.conf import settings
20
 
from django.core.management import call_command
21
 
from django.db.models import loading
22
 
import django.test
23
 
import testresources
24
 
import testtools
25
 
 
26
 
 
27
 
class TestCase(testtools.TestCase, django.test.TestCase):
28
 
    """`TestCase` for Metal as a Service.
29
 
 
30
 
    Supports test resources and fixtures.
31
 
    """
32
 
 
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
37
 
    # unittest.TestCase.
38
 
 
39
 
    resources = ()
40
 
 
41
 
    def setUp(self):
42
 
        super(TestCase, self).setUp()
43
 
        self.setUpResources()
44
 
 
45
 
    def setUpResources(self):
46
 
        testresources.setUpResources(
47
 
            self, self.resources, testresources._get_result())
48
 
 
49
 
    def tearDown(self):
50
 
        self.tearDownResources()
51
 
        super(TestCase, self).tearDown()
52
 
 
53
 
    def tearDownResources(self):
54
 
        testresources.tearDownResources(
55
 
            self, self.resources, testresources._get_result())
56
 
 
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
60
 
 
61
 
 
62
 
class TestModelTestCase(TestCase):
63
 
    """A custom test case that adds support for test-only models.
64
 
 
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
70
 
 
71
 
    This way the models defined in myapp1.tests.models will be available in
72
 
    this test case (and this test case only).
73
 
    """
74
 
 
75
 
    # Set the appropriate application to be loaded.
76
 
    app = None
77
 
 
78
 
    def _pre_setup(self):
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()
86
 
 
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