File: test_patcher.py

package info (click to toggle)
python-fudge 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 716 kB
  • sloc: javascript: 3,300; python: 2,537; makefile: 84; sh: 5
file content (330 lines) | stat: -rw-r--r-- 9,085 bytes parent folder | download
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

import inspect
import unittest

from nose.exc import SkipTest
from nose.tools import eq_, raises

import fudge


class Freddie(object):
    pass

        
def test_patch_obj():
    class holder:
        exc = Exception()
    
    patched = fudge.patch_object(holder, "exc", Freddie())
    eq_(type(holder.exc), type(Freddie()))
    patched.restore()
    eq_(type(holder.exc), type(Exception()))


def test_patch_path():
    from os.path import join as orig_join
    patched = fudge.patch_object("os.path", "join", Freddie())
    import os.path
    eq_(type(os.path.join), type(Freddie()))
    patched.restore()
    eq_(type(os.path.join), type(orig_join))


def test_patch_builtin():
    import datetime
    orig_datetime = datetime.datetime
    now = datetime.datetime(2010, 11, 4, 8, 19, 11, 28778)
    fake = fudge.Fake('now').is_callable().returns(now)
    patched = fudge.patch_object(datetime.datetime, 'now', fake)
    try:
        eq_(datetime.datetime.now(), now)
    finally:
        patched.restore()
    eq_(datetime.datetime.now, orig_datetime.now)


def test_patch_long_path():
    import fudge.tests.support._for_patch
    orig = fudge.tests.support._for_patch.some_object.inner
    long_path = 'fudge.tests.support._for_patch.some_object.inner'
    with fudge.patch(long_path) as fake:
        assert isinstance(fake, fudge.Fake)
    eq_(fudge.tests.support._for_patch.some_object.inner, orig)


@raises(ImportError)
def test_patch_non_existant_path():
    with fudge.patch('__not_a_real_import_path.nested.one.two.three') as fake:
        pass


@raises(AttributeError)
def test_patch_non_existant_attribute():
    with fudge.patch('fudge.tests.support._for_patch.does.not.exist') as fake:
        pass


def test_patch_builtin_as_string():
    import datetime
    orig_datetime = datetime.datetime
    now = datetime.datetime(2006, 11, 4, 8, 19, 11, 28778)
    fake_dt = fudge.Fake('datetime').provides('now').returns(now)
    patched = fudge.patch_object('datetime', 'datetime', fake_dt)
    try:
        # timetuple is a workaround for strange Jython behavior!
        eq_(datetime.datetime.now().timetuple(), now.timetuple())
    finally:
        patched.restore()
    eq_(datetime.datetime.now, orig_datetime.now)


def test_decorator_on_def():
    class holder:
        test_called = False
        exc = Exception()
        
    @fudge.with_patched_object(holder, "exc", Freddie())
    def some_test():
        holder.test_called = True
        eq_(type(holder.exc), type(Freddie()))
    
    eq_(some_test.__name__, 'some_test')
    some_test()
    eq_(holder.test_called, True)
    eq_(type(holder.exc), type(Exception()))


def test_decorator_on_class():
    class holder:
        test_called = False
        exc = Exception()
    
    class SomeTest(object):
        
        @fudge.with_patched_object(holder, "exc", Freddie())
        def some_test(self):
            holder.test_called = True
            eq_(type(holder.exc), type(Freddie()))
    
    eq_(SomeTest.some_test.__name__, 'some_test')
    s = SomeTest()
    s.some_test()
    eq_(holder.test_called, True)
    eq_(type(holder.exc), type(Exception()))


def test_patched_context():
    if not hasattr(fudge, "patched_context"):
        raise SkipTest("Cannot test with patched_context() because not in 2.5")
    
    class Boo:
        fargo = "is over there"
    
    ctx = fudge.patched_context(Boo, 'fargo', 'is right here')
    # simulate with fudge.patched_context():
    ctx.__enter__()
    eq_(Boo.fargo, "is right here")
    ctx.__exit__(None, None, None)
    eq_(Boo.fargo, "is over there")


def test_base_class_attribute():
    class Base(object):
        foo = 'bar'
    class Main(Base):
        pass
    fake = fudge.Fake()
    p = fudge.patch_object(Main, 'foo', fake)
    eq_(Main.foo, fake)
    eq_(Base.foo, 'bar')
    p.restore()
    eq_(Main.foo, 'bar')
    assert 'foo' not in Main.__dict__, ('Main.foo was not restored correctly')


def test_bound_methods():
    class Klass(object):
        def method(self):
            return 'foozilate'
    instance = Klass()
    fake = fudge.Fake()
    p = fudge.patch_object(instance, 'method', fake)
    eq_(instance.method, fake)
    p.restore()
    eq_(instance.method(), Klass().method())
    assert inspect.ismethod(instance.method)
    assert 'method' not in instance.__dict__, (
                            'instance.method was not restored correctly')


def test_staticmethod_descriptor():
    class Klass(object):
        @staticmethod
        def static():
            return 'OK'
    fake = fudge.Fake()
    p = fudge.patch_object(Klass, 'static', fake)
    eq_(Klass.static, fake)
    p.restore()
    eq_(Klass.static(), 'OK')


def test_property():
    class Klass(object):
        @property
        def prop(self):
            return 'OK'
    exact_prop = Klass.prop
    instance = Klass()
    fake = fudge.Fake()
    p = fudge.patch_object(instance, 'prop', fake)
    eq_(instance.prop, fake)
    p.restore()
    eq_(instance.prop, 'OK')
    eq_(Klass.prop, exact_prop)


def test_inherited_property():
    class SubKlass(object):
        @property
        def prop(self):
            return 'OK'
    class Klass(SubKlass):
        pass
    exact_prop = SubKlass.prop
    instance = Klass()
    fake = fudge.Fake()
    p = fudge.patch_object(instance, 'prop', fake)
    eq_(instance.prop, fake)
    p.restore()
    eq_(instance.prop, 'OK')
    assert 'prop' not in Klass.__dict__, (
                                'Klass.prop was not restored properly')
    eq_(SubKlass.prop, exact_prop)


class TestPatch(unittest.TestCase):

    def setUp(self):
        fudge.clear_expectations()

    def test_decorator_on_def(self):

        class holder:
            test_called = False
        
        @fudge.patch('shutil.copy')
        def some_test(copy):
            import shutil
            holder.test_called = True
            assert isinstance(copy, fudge.Fake)
            eq_(copy, shutil.copy)
    
        eq_(some_test.__name__, 'some_test')
        some_test()
        eq_(holder.test_called, True)
        import shutil
        assert not isinstance(shutil.copy, fudge.Fake)


    def test_decorator_on_class(self):

        class holder:
            test_called = False
    
        class MyTest(object):

            @fudge.patch('shutil.copy')
            def some_test(self, copy):
                import shutil
                holder.test_called = True
                assert isinstance(copy, fudge.Fake)
                eq_(copy, shutil.copy)
    
        eq_(MyTest.some_test.__name__, 'some_test')
        m = MyTest()
        m.some_test()
        eq_(holder.test_called, True)
        import shutil
        assert not isinstance(shutil.copy, fudge.Fake)

    def test_patch_many(self):

        class holder:
            test_called = False
        
        @fudge.patch('shutil.copy',
                     'os.remove')
        def some_test(copy, remove):
            import shutil
            import os
            holder.test_called = True
            assert isinstance(copy, fudge.Fake)
            assert isinstance(remove, fudge.Fake)
            eq_(copy, shutil.copy)
            eq_(remove, os.remove)
    
        eq_(some_test.__name__, 'some_test')
        some_test()
        eq_(holder.test_called, True)
        import shutil
        assert not isinstance(shutil.copy, fudge.Fake)
        import os
        assert not isinstance(os.remove, fudge.Fake)

    def test_with_patch(self):

        class holder:
            test_called = False

        def run_test():
            with fudge.patch('shutil.copy') as copy:
                import shutil
                assert isinstance(copy, fudge.Fake)
                eq_(copy, shutil.copy)
                holder.test_called = True
        
        run_test()
        eq_(holder.test_called, True)
        import shutil
        assert not isinstance(shutil.copy, fudge.Fake)

    def test_with_multiple_patches(self):

        class holder:
            test_called = False

        def run_test():
            with fudge.patch('shutil.copy', 'os.remove') as fakes:
                copy, remove = fakes
                import shutil
                import os
                assert isinstance(copy, fudge.Fake)
                assert isinstance(remove, fudge.Fake)
                eq_(copy, shutil.copy)
                eq_(remove, os.remove)
                holder.test_called = True
        
        run_test()
        eq_(holder.test_called, True)
        import shutil
        assert not isinstance(shutil.copy, fudge.Fake)
        import os
        assert not isinstance(os.remove, fudge.Fake)

    def test_class_method_path(self):
        class ctx:
            sendmail = None

        @fudge.patch('smtplib.SMTP.sendmail')
        def test(fake_sendmail):
            import smtplib
            s = smtplib.SMTP()
            ctx.sendmail = s.sendmail

        test()
        assert isinstance(ctx.sendmail, fudge.Fake)
        import smtplib
        s = smtplib.SMTP()
        assert not isinstance(s.sendmail, fudge.Fake)