~ubuntu-branches/ubuntu/raring/maas/raring-updates

« back to all changes in this revision

Viewing changes to src/maastesting/djangotestcase.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2012-07-03 17:42:37 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20120703174237-p8l0keuuznfg721k
Tags: 0.1+bzr709+dfsg-0ubuntu1
* New Upstream release
* debian/control:
  - Depends on python-celery, python-tempita, libjs-yui3-{full,min},
    libjs-raphael
* debian/maas.install:
  - Install apiclient, celeryconfig.py, maas-import-pxe-files, preseeds_v2.
  - Update to install various files from chroot, rather tha manually copy
    them from the source.
* debian/maas.links: symlink celeryconfig.py
* debian/maas.maas-celery.upstart: Add job.
* debian/rules:
  - Install celery upstart job.
  - Do not install jslibs as packages are now used.
  - Drop copying of maas_local_settings_sample.py as source now ships
    a maas_local_settings.py
* debian/patches:
  - 04-maas-http-fix.patch: Drop. Merged upstream.
  - 01-fix-database-settings.patch: Refreshed.
  - 99_enums_js.patch: Added until creation of enum.js / build process
    is fixed.
* debian/maas.postinst: Update bzr version to correctly handle upgrades.

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
"""Django-enabled test cases."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = [
 
14
    'DjangoTestCase',
 
15
    'TestModelTestCase',
 
16
    'TestModelTransactionalTestCase',
 
17
    'TransactionTestCase',
 
18
    ]
 
19
 
 
20
from django.conf import settings
 
21
from django.core.management import call_command
 
22
from django.core.management.commands import syncdb
 
23
from django.db import (
 
24
    connections,
 
25
    DEFAULT_DB_ALIAS,
 
26
    )
 
27
from django.db.models import loading
 
28
import django.test
 
29
from maastesting.testcase import TestCase
 
30
import testtools
 
31
 
 
32
 
 
33
class DjangoTestCase(TestCase, django.test.TestCase):
 
34
    """`TestCase` for Metal as a Service.
 
35
 
 
36
    Supports test resources and fixtures.
 
37
    """
 
38
 
 
39
    def assertAttributes(self, tested_object, attributes):
 
40
        """Check multiple attributes of `tested_objects` against a dict.
 
41
 
 
42
        :param tested_object: Any object whose attributes should be checked.
 
43
        :param attributes: A dict of attributes to test, and their expected
 
44
            values.  Only these attributes will be checked.
 
45
        """
 
46
        matcher = testtools.matchers.MatchesStructure.byEquality(**attributes)
 
47
        self.assertThat(tested_object, matcher)
 
48
 
 
49
 
 
50
class TransactionTestCase(TestCase, django.test.TransactionTestCase):
 
51
    """`TransactionTestCase` for Metal as a Service.
 
52
 
 
53
    A version of TestCase that supports transactions.
 
54
 
 
55
    The basic Django TestCase class uses transactions to speed up tests
 
56
    so this class should be used when tests involve transactions.
 
57
    """
 
58
 
 
59
    def _fixture_teardown(self):
 
60
        # Force a flush of the db: this is done by
 
61
        # django.test.TransactionTestCase at the beginning of each
 
62
        # TransactionTestCase test but not at the end.  The Django test runner
 
63
        # avoids any problem by running all the TestCase tests and *then*
 
64
        # all the TransactionTestCase tests.  Since we use nose, we don't
 
65
        # have that ordering and thus we need to manually flush the db after
 
66
        # each TransactionTestCase test.  Le Sigh.
 
67
        if getattr(self, 'multi_db', False):
 
68
            databases = connections
 
69
        else:
 
70
            databases = [DEFAULT_DB_ALIAS]
 
71
        for db in databases:
 
72
            call_command('flush', verbosity=0, interactive=False, database=db)
 
73
 
 
74
 
 
75
class TestModelMixin:
 
76
    # Set the appropriate application to be loaded.
 
77
    app = None
 
78
 
 
79
    def _pre_setup(self):
 
80
        # Add the models to the db.
 
81
        self._original_installed_apps = settings.INSTALLED_APPS
 
82
        assert self.app is not None, "TestCase.app must be defined!"
 
83
        settings.INSTALLED_APPS = list(settings.INSTALLED_APPS)
 
84
        settings.INSTALLED_APPS.append(self.app)
 
85
        loading.cache.loaded = False
 
86
        # Use Django's 'syncdb' rather than South's.
 
87
        syncdb.Command().handle_noargs(
 
88
            verbosity=0, interactive=False, database=DEFAULT_DB_ALIAS)
 
89
        super(TestModelMixin, self)._pre_setup()
 
90
 
 
91
    def _post_teardown(self):
 
92
        super(TestModelMixin, self)._post_teardown()
 
93
        # Restore the settings.
 
94
        settings.INSTALLED_APPS = self._original_installed_apps
 
95
        loading.cache.loaded = False
 
96
 
 
97
 
 
98
class TestModelTestCase(TestModelMixin, TestCase):
 
99
    """A custom test case that adds support for test-only models.
 
100
 
 
101
    For instance, if you want to have a model object used solely for testing
 
102
    in your application 'myapp1' you would create a test case that uses
 
103
    TestModelTestCase as its base class and:
 
104
    - initialize self.app with 'myapp1.tests'
 
105
    - define the models used for testing in myapp1.tests.models
 
106
 
 
107
    This way the models defined in myapp1.tests.models will be available in
 
108
    this test case (and this test case only).
 
109
    """
 
110
 
 
111
 
 
112
class TestModelTransactionalTestCase(TestModelMixin, TransactionTestCase):
 
113
    """A TestCase Similar to `TestModelTestCase` but with transaction
 
114
    support.
 
115
    """