File: test_inspector.py

package info (click to toggle)
python-fudge 1.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 744 kB
  • sloc: javascript: 3,300; python: 2,537; makefile: 82; sh: 5
file content (271 lines) | stat: -rw-r--r-- 8,855 bytes parent folder | download | duplicates (2)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# -*- coding: utf-8 -*-
import re
import unittest

from nose.tools import eq_, raises

import fudge
from fudge import inspector
from fudge.inspector import arg, arg_not
from fudge import Fake

class TestAnyValue(unittest.TestCase):

    def tearDown(self):
        fudge.clear_expectations()

    def test_any_value(self):
        db = Fake("db").expects("execute").with_args(arg.any())
        db.execute("delete from foo where 1")

    def test_repr(self):
        any = inspector.AnyValue()
        eq_(repr(any), "arg.any()")

    def test_str(self):
        any = inspector.AnyValue()
        eq_(str(any), "arg.any()")


class TestPassesTest(unittest.TestCase):

    def tearDown(self):
        fudge.clear_expectations()

    def test_passes(self):
        def isint(v):
            return isinstance(v,int)
        counter = Fake("counter").expects("increment").with_args(arg.passes_test(isint))
        counter.increment(25)

    @raises(AssertionError)
    def test_passes_fail(self):
        def is_str(v):
            return isinstance(v,str)
        counter = Fake("counter").expects("set_name").with_args(arg.passes_test(is_str))
        counter.set_name(25)

    def test_repr(self):
        class test(object):
            def __call__(self, v):
                return isinstance(v,int)
            def __repr__(self):
                return "v is an int"

        passes = inspector.PassesTest(test())
        eq_(repr(passes), "arg.passes_test(v is an int)")

    def test_str(self):
        class test(object):
            def __call__(self, v):
                return isinstance(v,int)
            def __repr__(self):
                return "v is an int"

        passes = inspector.PassesTest(test())
        eq_(str(passes), "arg.passes_test(v is an int)")

class TestIsInstance(unittest.TestCase):

    def tearDown(self):
        fudge.clear_expectations()

    def test_passes(self):
        counter = Fake("counter").expects("increment").with_args(arg.isinstance(int))
        counter.increment(25)

    @raises(AssertionError)
    def test_passes_fail(self):
        counter = Fake("counter").expects("set_name").with_args(arg.isinstance(str))
        counter.set_name(25)

    def test_repr(self):
        passes = inspector.IsInstance(int)
        eq_(repr(passes), "arg.isinstance('int')")

    def test_str(self):
        passes = inspector.IsInstance(str)
        eq_(str(passes), "arg.isinstance('str')")

    def test_list(self):
        passes = inspector.IsInstance((str, int))
        eq_(str(passes), "arg.isinstance(('str', 'int'))")

class TestObjectlike(unittest.TestCase):

    def tearDown(self):
        fudge.clear_expectations()

    def test_has_attr_ok(self):
        class Config(object):
            size = 12
            color = 'red'
            weight = 'heavy'

        widget = Fake("widget").expects("configure")\
                               .with_args(arg.has_attr(size=12,color='red'))
        widget.configure(Config())

    @raises(AssertionError)
    def test_has_attr_fail(self):
        class Config(object):
            color = 'red'

        widget = Fake("widget").expects("configure")\
                               .with_args(arg.has_attr(size=12))
        widget.configure(Config())

    @raises(AssertionError)
    def test_has_attr_fail_wrong_value(self):
        class Config(object):
            color = 'red'

        widget = Fake("widget").expects("configure")\
                               .with_args(arg.has_attr(color="green"))
        widget.configure(Config())

    def test_objectlike_str(self):
        o = inspector.HasAttr(one=1, two="two")
        eq_(str(o), "arg.has_attr(one=1, two='two')")

    def test_objectlike_repr(self):
        o = inspector.HasAttr(one=1, two="two")
        eq_(repr(o), "arg.has_attr(one=1, two='two')")

    def test_objectlike_unicode(self):
        o = inspector.HasAttr(one=1, ivan="Ivan_Krsti\u0107")
        eq_(repr(o), "arg.has_attr(ivan=%s, one=1)" % repr('Ivan_Krsti\u0107'))

    def test_objectlike_repr_long_val(self):
        o = inspector.HasAttr(
                bytes="011110101000101010011111111110000001010100000001110000000011")
        eq_(repr(o),
            "arg.has_attr(bytes='011110101000101010011111111110000001010100000...')")

class TestStringlike(unittest.TestCase):

    def tearDown(self):
        fudge.clear_expectations()

    def test_startswith_ok(self):
        db = Fake("db").expects("execute").with_args(arg.startswith("insert into"))
        db.execute("insert into foo values (1,2,3,4)")

    @raises(AssertionError)
    def test_startswith_fail(self):
        db = Fake("db").expects("execute").with_args(arg.startswith("insert into"))
        db.execute("select from")

    def test_startswith_ok_uni(self):
        db = Fake("db").expects("execute").with_args(arg.startswith("Ivan_Krsti\u0107"))
        db.execute("Ivan_Krsti\u0107(); foo();")

    def test_startswith_unicode(self):
        p = inspector.Startswith("Ivan_Krsti\u0107")
        eq_(repr(p), "arg.startswith(%s)" % repr('Ivan_Krsti\u0107'))

    def test_endswith_ok(self):
        db = Fake("db").expects("execute").with_args(arg.endswith("values (1,2,3,4)"))
        db.execute("insert into foo values (1,2,3,4)")

    def test_endswith_ok_uni(self):
        db = Fake("db").expects("execute").with_args(arg.endswith("Ivan Krsti\u0107"))
        db.execute("select Ivan Krsti\u0107")

    def test_endswith_unicode(self):
        p = inspector.Endswith("Ivan_Krsti\u0107")
        eq_(repr(p), "arg.endswith(%s)" % repr('Ivan_Krsti\u0107'))

    def test_startswith_repr(self):
        p = inspector.Startswith("_start")
        eq_(repr(p), "arg.startswith('_start')")

    def test_endswith_repr(self):
        p = inspector.Endswith("_ending")
        eq_(repr(p), "arg.endswith('_ending')")

    def test_startswith_str(self):
        p = inspector.Startswith("_start")
        eq_(str(p), "arg.startswith('_start')")

    def test_endswith_str(self):
        p = inspector.Endswith("_ending")
        eq_(str(p), "arg.endswith('_ending')")

    def test_startswith_str_long_value(self):
        p = inspector.Startswith(
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        )
        eq_(str(p),
            "arg.startswith('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...')" )

    def test_endswith_str_long_value(self):
        p = inspector.Endswith(
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
        )
        eq_(str(p),
            "arg.endswith('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...')" )

class TestContains(unittest.TestCase):

    def tearDown(self):
        fudge.clear_expectations()

    def test_contains_str(self):
        db = Fake("db").expects("execute").with_args(arg.contains("table foo"))
        db.execute("select into table foo;")
        db.execute("select * from table foo where bar = 1")
        fudge.verify()

    @raises(AssertionError)
    def test_contains_fail(self):
        db = Fake("db").expects("execute").with_args(arg.contains("table foo"))
        db.execute("select into table notyourmama;")
        fudge.verify()

    def test_contains_list(self):
        db = Fake("db").expects("execute_statements").with_args(
                                            arg.contains("select * from foo"))
        db.execute_statements([
            "update foo",
            "select * from foo",
            "drop table foo"
        ])
        fudge.verify()

    def test_str(self):
        c = inspector.Contains(":part:")
        eq_(str(c), "arg.contains(':part:')")

    def test_str_long_val(self):
        c = inspector.Contains(
            "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
        eq_(str(c), "arg.contains('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA...')")

    def test_repr(self):
        c = inspector.Contains(":part:")
        eq_(repr(c), "arg.contains(':part:')")

    def test_unicode(self):
        c = inspector.Contains("Ivan_Krsti\u0107")
        eq_(repr(c), "arg.contains(%s)" % repr('Ivan_Krsti\u0107'))

class TestMakeValueTest(unittest.TestCase):

    def test_no_invert_just_inits(self):
        vi = inspector.ValueInspector()
        vi.invert_eq = False
        def value_test(*args, **kwargs):
            return 'hello, friends'
        ret = vi._make_value_test(value_test, 'asdf', foo='bar')
        self.assertEqual(ret, 'hello, friends')

    def test_invert_returns_inverter(self):
        vi = inspector.ValueInspector()
        vi.invert_eq = True
        class vt(object):
            def __eq__(self, other):
                return True
        inverter = vi._make_value_test(vt)
        assert isinstance(inverter, vt)
        self.assertEqual(inverter.__eq__('whatever'), False)