1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
import os
import sys
import unittest
from functools import wraps
datastore = "/"
XI_NAMESPACE = "http://www.w3.org/2001/XInclude"
XI = "{%s}" % XI_NAMESPACE
if sys.hexversion >= 0x03000000:
inPy3k = True
else:
inPy3k = False
try:
from django.core.management import setup_environ
has_django = True
os.environ['DJANGO_SETTINGS_MODULE'] = "Bcfg2.settings"
import Bcfg2.settings
Bcfg2.settings.DATABASE_NAME = \
os.path.join(os.path.dirname(os.path.abspath(__file__)), "test.sqlite")
Bcfg2.settings.DATABASES['default']['NAME'] = Bcfg2.settings.DATABASE_NAME
except ImportError:
has_django = False
try:
from mock import call
except ImportError:
def call(*args, **kwargs):
""" the Mock call object is a fairly recent addition, but it's
very very useful, so we create our own function to create Mock
calls """
return (args, kwargs)
if inPy3k:
builtins = "builtins"
else:
builtins = "__builtin__"
if hasattr(unittest.TestCase, "assertItemsEqual"):
TestCase = unittest.TestCase
else:
def assertion(predicate, default_msg=None):
@wraps(predicate)
def inner(*args, **kwargs):
if 'msg' in kwargs:
msg = kwargs['msg']
del kwargs['msg']
else:
msg = default_msg % args
assert predicate(*args, **kwargs), msg
return inner
class TestCase(unittest.TestCase):
# versions of TestCase before python 2.7 lacked a lot of the
# really handy convenience methods, so we provide them -- at
# least the easy ones and the ones we use.
assertIs = assertion(lambda a, b: a is b, "%s is not %s")
assertIsNot = assertion(lambda a, b: a is not b, "%s is %s")
assertIsNone = assertion(lambda x: x is None, "%s is not None")
assertIsNotNone = assertion(lambda x: x is not None, "%s is None")
assertIn = assertion(lambda a, b: a in b, "%s is not in %s")
assertNotIn = assertion(lambda a, b: a not in b, "%s is in %s")
assertIsInstance = assertion(isinstance, "%s is not %s")
assertNotIsInstance = assertion(lambda a, b: not isinstance(a, b),
"%s is %s")
assertGreater = assertion(lambda a, b: a > b,
"%s is not greater than %s")
assertGreaterEqual = assertion(lambda a, b: a >= b,
"%s is not greater than or equal to %s")
assertLess = assertion(lambda a, b: a < b, "%s is not less than %s")
assertLessEqual = assertion(lambda a, b: a <= b,
"%s is not less than or equal to %s")
assertItemsEqual = assertion(lambda a, b: sorted(a) == sorted(b),
"Items do not match:\n%s\n%s")
if hasattr(unittest, "skip"):
can_skip = True
skip = unittest.skip
skipIf = unittest.skipIf
skipUnless = unittest.skipUnless
else:
# we can't actually skip tests, we just make them pass
can_skip = False
def skip(msg):
def decorator(func):
@wraps(func)
def inner(*args, **kwargs):
pass
return inner
return decorator
def skipIf(condition, msg):
def decorator(func):
if condition:
return func
@wraps(func)
def inner(*args, **kwargs):
pass
return inner
return decorator
def skipUnless(condition, msg):
def decorator(func):
if not condition:
return func
@wraps(func)
def inner(*args, **kwargs):
pass
return inner
return decorator
class Bcfg2TestCase(TestCase):
def assertXMLEqual(self, el1, el2, msg=None):
self.assertEqual(el1.tag, el2.tag, msg=msg)
self.assertEqual(el1.text, el2.text, msg=msg)
self.assertItemsEqual(el1.attrib, el2.attrib, msg=msg)
self.assertEqual(len(el1.getchildren()),
len(el2.getchildren()))
for child1 in el1.getchildren():
cname = child1.get("name")
self.assertIsNotNone(cname,
msg="Element %s has no 'name' attribute" %
child1.tag)
children2 = el2.xpath("*[@name='%s']" % cname)
self.assertEqual(len(children2), 1,
msg="More than one element named %s" % cname)
self.assertXMLEqual(child1, children2[0], msg=msg)
class DBModelTestCase(Bcfg2TestCase):
models = []
@unittest.skipUnless(has_django, "Django not found, skipping")
def test_syncdb(self):
# create the test database
setup_environ(Bcfg2.settings)
from django.core.management.commands import syncdb
cmd = syncdb.Command()
cmd.handle_noargs(interactive=False)
self.assertTrue(os.path.exists(Bcfg2.settings.DATABASE_NAME))
@unittest.skipUnless(has_django, "Django not found, skipping")
def test_cleandb(self):
""" ensure that we a) can connect to the database; b) start with a
clean database """
for model in self.models:
model.objects.all().delete()
self.assertItemsEqual(list(model.objects.all()), [])
def syncdb(modeltest):
inst = modeltest(methodName='test_syncdb')
inst.test_syncdb()
inst.test_cleandb()
|