Skip to content

Commit d74e87a

Browse files
committed
Upgraded to minimock 1.2.6
1 parent 6064eca commit d74e87a

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

minimocktest/minimock.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,23 @@
5959
def lookup_by_name(name, nsdicts):
6060
"""
6161
Look up an object by name from a sequence of namespace dictionaries.
62-
Returns a tuple of (nsdict, object, attributes); nsdict is the
63-
dictionary the name was found in, object is the name of the base object
64-
the name is bound to, and the attributes list is the chain of attributes
65-
of the object that complete the name.
62+
Returns a tuple of (nsdict, obj_name, attrs); nsdict is the namespace
63+
dictionary the name was found in, obj_name is the name of the base object
64+
the name is bound to, and the attrs list is the chain of attributes
65+
of the object that completes the name.
6666
6767
>>> import os
68-
>>> nsdict, name, attributes = lookup_by_name("os.path.isdir",
68+
>>> nsdict, obj_name, attrs = lookup_by_name("os.path.isdir",
6969
... (locals(),))
70-
>>> name, attributes
70+
>>> obj_name, attrs
7171
('os', ['path', 'isdir'])
72-
>>> nsdict, name, attributes = lookup_by_name("os.monkey", (locals(),))
72+
>>> getattr(getattr(nsdict[obj_name], attrs[0]), attrs[1])
73+
<function isdir at ...>
74+
>>> lookup_by_name("os.monkey", (locals(),))
7375
Traceback (most recent call last):
7476
...
7577
NameError: name 'os.monkey' is not defined
76-
78+
7779
"""
7880
for nsdict in nsdicts:
7981
attrs = name.split(".")
@@ -165,6 +167,22 @@ def mock(name, nsdicts=None, mock_obj=None, **kw):
165167
>>> Test.sm()
166168
'sm'
167169
170+
Test mocking a proxy object::
171+
172+
>>> class Proxy(object):
173+
... def __init__(self, obj):
174+
... self._obj = obj
175+
... def __getattr__(self, name):
176+
... return getattr(self._obj, name)
177+
>>> import os
178+
>>> os = Proxy(os)
179+
>>> os.path.isfile
180+
<function isfile at ...>
181+
>>> mock('os.path.isfile')
182+
>>> os.path.isfile
183+
<Mock ... os.path.isfile>
184+
>>> restore()
185+
168186
"""
169187
if nsdicts is None:
170188
stack = inspect.stack()
@@ -196,8 +214,14 @@ def mock(name, nsdicts=None, mock_obj=None, **kw):
196214
nsdict[obj_name] = mock_obj
197215
else:
198216
for attr in attrs[:-1]:
199-
tmp = tmp.__dict__[attr]
200-
original = tmp.__dict__[attrs[-1]]
217+
try:
218+
tmp = tmp.__dict__[attr]
219+
except KeyError:
220+
tmp = getattr(tmp, attr)
221+
try:
222+
original = tmp.__dict__[attrs[-1]]
223+
except KeyError:
224+
original = getattr(tmp, attrs[-1])
201225
setattr(tmp, attrs[-1], mock_obj)
202226

203227
mocked.append((original, nsdict, obj_name, attrs))
@@ -217,7 +241,10 @@ def restore():
217241
else:
218242
tmp = nsdict[name]
219243
for attr in attrs[:-1]:
220-
tmp = tmp.__dict__[attr]
244+
try:
245+
tmp = tmp.__dict__[attr]
246+
except KeyError:
247+
tmp = getattr(tmp, attr)
221248
setattr(tmp, attrs[-1], original)
222249

223250
def assert_same_trace(tracker, want):
@@ -504,7 +531,7 @@ def __setattr__(self, attr, value):
504531
'mock_returns_func',
505532
'mock_returns_iter',
506533
'mock_tracker',
507-
'show_attrs',
534+
'mock_show_attrs',
508535
)):
509536
if attr == 'mock_returns_iter' and value is not None:
510537
value = iter(value)
@@ -541,6 +568,10 @@ def __setattr__(self, attr, value):
541568
... pass
542569
... else:
543570
... raise AssertionError('m() should have raised ValueError')
571+
>>> m.mock_tracker = Printer(sys.stdout)
572+
>>> m.mock_show_attrs = True
573+
>>> m.a = 2
574+
Set mock_obj.a = 2
544575
""",
545576

546577
"mock" :

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sys, os
33

44
setup(name='MiniMockTest',
5-
version='0.1',
5+
version='0.2',
66
description="Custom unittest TestCase that wraps minimock",
77
long_description="""\
88
Custom unittest TestCase that wraps minimock for easier use """,

0 commit comments

Comments
 (0)