/
test.py
executable file
·206 lines (181 loc) · 6.23 KB
/
test.py
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
try:
import coverage
except ImportError:
coverage = None
if coverage:
coverage.erase()
coverage.start()
import mockit
import os
import unittest
class MockitTestCase(unittest.TestCase):
def tearDown(self):
mockit.unmock_all()
return unittest.TestCase.tearDown(self)
def test_attributes(self):
m = mockit.MockObject()
m.bar = 3
assert(m._mock.raw.bar)
assert(m.bar != 3)
assert(m.bar is m.bar)
m.bar.bam = 4
# raw attributes
assert(m.bar != m.bar.bam)
assert(m.bar.bam == m.bar.bam)
def test_raw(self):
m = mockit.MockObject()
m._mock.raw.bar = 3
assert(m.bar == 3)
def test_enable(self):
m = mockit.MockObject()
m._mock.enable_attrs('foo')
try:
m.foo
assert(0)
except AttributeError:
pass
assert(not hasattr(m, 'foo'))
m.foo = 4
assert(m.foo == 4)
def test_calls(self):
m = mockit.MockObject()
assert(isinstance(m.foo('abc'), mockit.MockObject))
m.blah._mock.default_return_value = 43
assert(m.blah() == 43)
m._mock.add_return_value(54, 3, 4, a=23)
m._mock.add_return_value(42, 3, 4, a=23)
m._mock.default_return_value = 12
assert(m(3,4, a=23) == 54)
assert(m(3,4, a=23) == 42)
assert(m(3,4, a=23) == 12)
assert(m(32) == 12)
def test_dict(self):
m = mockit.MockObject()
assert(len(m) == 0)
m['foo'] = 12
m['bar'] = 13
assert(m['foo'] != 12)
assert(m['foo'] is m['foo'])
assert(len(m) == 2)
def test_raise_error(self):
m = mockit.MockObject()
error = RuntimeError('foo')
m._mock.raise_error_on_access(error)
try:
m.foo
assert(0)
except RuntimeError, e:
assert(error == e)
m._mock.raise_error_on_access(error)
try:
m()
assert(0)
except RuntimeError, e:
assert(error == e)
m._mock.raise_error_on_access(error)
try:
m[0] = 3
assert(0)
except RuntimeError, e:
assert(error == e)
m._mock.raise_error_on_access(error)
try:
m[0]
assert(0)
except RuntimeError, e:
assert(error == e)
def test_bad_attr(self):
m = mockit.MockObject()
try:
m.raise_error_on_access('foo')
assert(0)
except RuntimeError, e:
assert(str(e) == 'accessing method %r also defined in mock object - prepend'
' _mock to access, or enable attribute' % ('raise_error_on_access', ))
def test_record(self):
m = mockit.MockObject()
m.foo._mock.record.assert_no_calls()
try:
m.foo._mock.record.assert_called('a', b='c', d='e')
assert(0)
except AssertionError, e:
self.assertEquals(str(e), "expected call arguments to be ('a', b='c', d='e'), got no calls")
m.foo('a', b='c', d='e')
m.foo._mock.record.assert_called('a', b='c', d='e')
try:
m.foo._mock.record.assert_called('a', b='1', d='2')
assert(0)
except AssertionError, e:
self.assertEquals(str(e), "expected call arguments to be ('a', b='1', d='2'), got ('a', b='c', d='e')")
try:
m.foo._mock.record.assert_no_calls()
assert(0)
except AssertionError, e:
assert(str(e) == 'Invalid assertion: 1 call(s) made')
call = m.foo._mock.record.pop_call()
self.assertEquals(call.args, ('a',))
self.assertEquals(call.kwargs, (('b','c'), ('d', 'e')))
def test_call_repr(self):
call = mockit.Call('a', 'b', foo='bar')
assert(repr(call) == "Call('a', 'b', foo='bar')")
assert(str(call) == "('a', 'b', foo='bar')")
def test_list(self):
m = mockit.MockObject()
m._mock.set_list_items([1,2,3,4])
assert(m[0] == 1)
assert(m[3] == 4)
assert(len(m) == 4)
def test_mock_instance(self):
class Foo(object):
def __init__(self):
# NOTE: this initialization is not called by default with the mock
# object.
self.one = 'a'
self.two = 'b'
def method(self, param):
# this method is enabled by calling _mock.enableMethod
param.bar('print some data')
self.print_me('some other data', self.one)
return self.two
def print_me(self, otherParam):
# this method is not enabled and so is stubbed out in the MockInstance.
print otherParam
m = mockit.MockInstance(Foo)
m._mock.raw.two = 123
m._mock.enable_method('method')
param = mockit.MockObject()
rv = m.method(param)
self.assertEquals(rv, 123) #m.two is returned
# note that param.bar is created on the fly as it is accessed, and
# stores how it was called.
param.bar._mock.record.assert_called('print some data')
# m.one and m.print_me were created on the fly as well
# m.print_me remembers how it was called.
m.print_me._mock.record.assert_called('some other data', m.one)
# attribute values are generated on the fly but are retained between
# accesses.
assert(m.foo is m.foo)
def test_mock(self):
path = '/tmp/thisfiledefinitely/doesntexist'
assert(not os.path.exists(path))
mockit.mock(os.path, 'exists')
os.path.exists._mock.default_return_value = True
assert(os.path.exists(path))
old_stat = os.stat
mockit.mock(os, 'stat')
os.stat = old_stat
mockit.unmock_all()
assert(not os.path.exists(path))
def test_mock_method(self):
class Foo(object):
def method(self, a, b):
return True
foo_instance = Foo()
mockit.mock_method(foo_instance.method)
# normally would give an error.
results = foo_instance.method()
if __name__ == '__main__':
unittest.main()
if coverage:
coverage.stop()