~cr3/launchpad-results/trunk

« back to all changes in this revision

Viewing changes to launchpadresults/testing/cases.py

  • Committer: Marc Tardif
  • Date: 2010-09-23 23:40:07 UTC
  • Revision ID: marc.tardif@canonical.com-20100923234007-kxouzhvgv613957k
Added testing layers and factories.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
import re
 
5
import unittest
 
6
 
 
7
from storm.zope.zstorm import global_zstorm
 
8
 
 
9
from launchpadresults.testing.factories import TestFactory
 
10
 
 
11
 
 
12
__all__ = [
 
13
    "TestCase",
 
14
    "TestCaseWithFactory",
 
15
    ]
 
16
 
 
17
 
 
18
class DatabaseTestCase(unittest.TestCase):
 
19
 
 
20
    def _assertSQL(self, func, expected_statement, all_statements):
 
21
        regexp = re.compile("\s")
 
22
 
 
23
        def crush(statement):
 
24
            statement = regexp.sub("", statement)
 
25
            return statement
 
26
 
 
27
        func(crush(expected_statement),
 
28
             [crush(statement) for statement in all_statements])
 
29
 
 
30
    def assertSQLIn(self, expected_statement, all_statements):
 
31
        """Ensure that C{expected_statement} is in C{all_statements}.
 
32
 
 
33
        Whitespace is completely collapsed before statements are compared.
 
34
        """
 
35
        self._assertSQL(self.assertIn, expected_statement, all_statements)
 
36
 
 
37
    def assertSQLNotIn(self, expected_statement, all_statements):
 
38
        """Ensure that C{expected_statement} is not in C{all_statements}.
 
39
 
 
40
        Whitespace is completely collapsed before statements are compared.
 
41
        """
 
42
        self._assertSQL(self.assertNotIn, expected_statement, all_statements)
 
43
 
 
44
    def tearDown(self):
 
45
        # Flush stores to try to catch improper statements unflushed.
 
46
        for name, store in global_zstorm.iterstores():
 
47
            store.flush()
 
48
 
 
49
        # Clear the alive cache *before* abort is called, to prevent an useless
 
50
        # loop over those objects in invalidate. Ideally, it whould be in
 
51
        # DatabaseLayer, but FunctionalTestCase calls abort before that
 
52
        # DatabaseLayer.testTearDown is called.
 
53
        for name, store in global_zstorm.iterstores():
 
54
            store._alive.clear()
 
55
        super(DatabaseTestCase, self).tearDown()
 
56
 
 
57
 
 
58
class TestCase(DatabaseTestCase):
 
59
 
 
60
    pass
 
61
 
 
62
 
 
63
class TestCaseWithFactory(TestCase):
 
64
 
 
65
    def setUp(self):
 
66
        super(TestCaseWithFactory, self).setUp()
 
67
        self.factory = TestFactory()