Mocking both Model.objects.bulk_create and Model.save() - python

I'm attempting to mock both bulk_create and save for a dry run.
I've created code which in essence does following:
#mock.patch.object(SomeModel.objects, 'bulk_create')
#mock.patch.object(SomeModel, 'save')
def a_dry_run(mocked_bulk_create, mocked_save):
def print_bulk(obj_list):
print 'doing bulk_create'
for obj in obj_list:
print obj
def print_save(instance):
print 'doing save'
print instance
mocked_bulk_create.side_effect = print_bulk
mocked_bulk_create.return_value = True
mocked_save.side_effect = print_save
mocked_save.return_value = True
The problem is that when I do bulk_create instead of print_bulk it executes print_save. How do I fix that or is there a cleaner way to do that?

The order of argument should be reversed. As you can see at Quick Guide where describe patch the order of mock arguments passed to the function follow patch's decorator from bottom to top.
In you case simply change the patch order or the function signature like
def a_dry_run(mocked_save, mocked_bulk_create):

Related

PYTHON: AttributeError - calling function in a member function of a class

I'm having trouble with classes in Python 2.7:
First, I really do not know how to use the __init__ properly, so I have just used a dummy call to print. What should I do instead?
Second, I would like the member function readAnyFormat to call a few functions (later I will create a kind of case statement). My attempt produced an AttributeError. How do I do this correctly?
My class is as follows:
class ArangeData:
def __init__(
self):
print ' '
def readAnyFormat(
self,
config = True,
mypath='text.txt',
data_format='ASCII',
data_shape='shaped'):
#Here the function should be called:'
#NOT WORKING:
#AttributeError: ArangeData instance has no attribute 'readASCII'
if data_format=='HDF5':
readHDF5()
elif data_format=='ASCII':
readASCII()
def readASCII():
'doing stuff in here'
def readHDF5():
'doing other stuff in here, which is not the business of readASCII'
def otherMemberFunction(self):
'do not care what they are doing above!'
You should move the definition of readASCII and readHDF5 so they are above the two if statements.
You don't need to have the dummy print statement in __init__. If you have nothing to initialize you can simply use pass, or better yet as #chepner commented don't even define __init__.

python unittest mock - checking a function returned at the correct point

I have a method which looks like this:
def foo(self):
if not self.started:
self.start()
return None
# do some other stuff...
self.bar()
return self.baz()
I am writing a test called test_foo_calls_start_if_not_already_started_and_then_returns_immediately.
Testing if we called start is easy:
with mock.patch.object(Thing, 'start') as mock_start:
thing = Thing()
thing.foo()
self.assertTrue(mock_start.called)
But how would I test that the function then returns straight after? I guess I could mock bar and baz and check that they aren't called, but that would be a pain to maintain.
It is hard to know without seeing more code, but I would say that a technique is to use the very next function call on self and assert that was not called. And also if None is only returned if not started, you have strong point there.
Assuming self.bar() is the immediate call, patch bar and then just check that it was not called, as easy as...
assert not mock_start.calls
I however prefer to have very short unittests, and monkey-patch the methods (if not using some kind of dependency injection), instead of using the context manager. The partially mocked object is anyway discarded right away, so it is safe to do so. Something like this:
def test1(self):
thing = Thing()
thing.started = False # Maybe it would make sense to include this
thing.start = Mock()
thing.bar = Mock()
res = thing.foo()
assert res is None
assert thing.start.called
assert not thing.bar.called
The mocking frameworks can ease a lot the testing activities, but at the end the testability boils down to your code and its quality. General hints are: return meaningful values, re-factor your code into smaller methods that allow checking calls more precisely, either mutate state or return a value, but not both, etc.

Destroy a mock in Python after the test

Let's say I have a couple of tests like these:
class TestMyTest(unittest.TestCase):
def SetUpClass(cls):
cls.my_lib = MyLib()
def my_first_test(self):
self.my_lib.my_function = Mock(return_value=True)
self.assertTrue(self.my_lib.run_my_function(), 'my function failed')
def my_second_test(self):
# Some other test that calls self.my_lib.my_function...
And let's say I have something like this in MyLib:
class MyLib(Object):
def my_function(self):
# This function does a whole bunch of stuff using an external API
# ...
def run_my_function(self):
result = self.my_function()
# Does some more stuff
# ...
In my_first_test I am mocking my_lib.my_function and returning a True when the function is executed. In this example, my assertion is calling run_my_function(), which is another function from the same library that among other things, it calls my_lib.my_function. But when my_second_test is executed I don't want the mocked function to be called but the real one. So I guess I would need to destroy the mock somehow after running my_first_test, maybe during tearDown(). How do I destroy that mock?
I edited my original question to add more details since looks like it was not that clear, sorry about that.
You can do this:
class TestYourLib(unittest.TestCase):
def setUp(self):
self.my_lib = MyLib()
def test_my_first_test(self):
self.my_lib.my_function = Mock(return_value=True)
self.assertTrue(self.run_my_function(), 'my function failed')
def test_my_second_test(self):
# Some other test that calls self.my_lib.my_function...
Then the Mock is "destroyed" by passing out of scope when setUp is called for the next test case.
Destroying the mock won't do it. You'll either have to re-assign self.my_lib.my_function or call Mock(return_value=True) in a different manner.
The first is what Patrick seems to suggest.

Python: can a decorator determine if a function is being defined inside a class?

I'm writing a decorator, and for various annoying reasons[0] it would be expedient to check if the function it is wrapping is being defined stand-alone or as part of a class (and further which classes that new class is subclassing).
For example:
def my_decorator(f):
defined_in_class = ??
print "%r: %s" %(f, defined_in_class)
#my_decorator
def foo(): pass
class Bar(object):
#my_decorator
def bar(self): pass
Should print:
<function foo …>: False
<function bar …>: True
Also, please note:
At the point decorators are applied the function will still be a function, not an unbound method, so testing for instance/unbound method (using typeof or inspect) will not work.
Please only offer suggestions that solve this problem — I'm aware that there are many similar ways to accomplish this end (ex, using a class decorator), but I would like them to happen at decoration time, not later.
[0]: specifically, I'm writing a decorator that will make it easy to do parameterized testing with nose. However, nose will not run test generators on subclasses of unittest.TestCase, so I would like my decorator to be able to determine if it's being used inside a subclass of TestCase and fail with an appropriate error. The obvious solution - using isinstance(self, TestCase) before calling the wrapped function doesn't work, because the wrapped function needs to be a generator, which doesn't get executed at all.
Take a look at the output of inspect.stack() when you wrap a method. When your decorator's execution is underway, the current stack frame is the function call to your decorator; the next stack frame down is the # wrapping action that is being applied to the new method; and the third frame will be the class definition itself, which merits a separate stack frame because the class definition is its own namespace (that is wrapped up to create a class when it is done executing).
I suggest, therefore:
defined_in_class = (len(frames) > 2 and
frames[2][4][0].strip().startswith('class '))
If all of those crazy indexes look unmaintainable, then you can be more explicit by taking the frame apart piece by piece, like this:
import inspect
frames = inspect.stack()
defined_in_class = False
if len(frames) > 2:
maybe_class_frame = frames[2]
statement_list = maybe_class_frame[4]
first_statment = statement_list[0]
if first_statment.strip().startswith('class '):
defined_in_class = True
Note that I do not see any way to ask Python about the class name or inheritance hierarchy at the moment your wrapper runs; that point is "too early" in the processing steps, since the class creation is not yet finished. Either parse the line that begins with class yourself and then look in that frame's globals to find the superclass, or else poke around the frames[1] code object to see what you can learn — it appears that the class name winds up being frames[1][0].f_code.co_name in the above code, but I cannot find any way to learn what superclasses will be attached when the class creation finishes up.
A little late to the party here, but this has proven to be a reliable means of determining if a decorator is being used on a function defined in a class:
frames = inspect.stack()
className = None
for frame in frames[1:]:
if frame[3] == "<module>":
# At module level, go no further
break
elif '__module__' in frame[0].f_code.co_names:
className = frame[0].f_code.co_name
break
The advantage of this method over the accepted answer is that it works with e.g. py2exe.
Some hacky solution that I've got:
import inspect
def my_decorator(f):
args = inspect.getargspec(f).args
defined_in_class = bool(args and args[0] == 'self')
print "%r: %s" %(f, defined_in_class)
But it relays on the presence of self argument in function.
you can use the package wrapt to check for
- instance/class methods
- classes
- freestanding functions/static methods:
See the project page of wrapt: https://pypi.org/project/wrapt/
You could check if the decorator itself is being called at the module level or nested within something else.
defined_in_class = inspect.currentframe().f_back.f_code.co_name != "<module>"
I think the functions in the inspect module will do what you want, particularly isfunction and ismethod:
>>> import inspect
>>> def foo(): pass
...
>>> inspect.isfunction(foo)
True
>>> inspect.ismethod(foo)
False
>>> class C(object):
... def foo(self):
... pass
...
>>> inspect.isfunction(C.foo)
False
>>> inspect.ismethod(C.foo)
True
>>> inspect.isfunction(C().foo)
False
>>> inspect.ismethod(C().foo)
True
You can then follow the Types and Members table to access the function inside the bound or unbound method:
>>> C.foo.im_func
<function foo at 0x1062dfaa0>
>>> inspect.isfunction(C.foo.im_func)
True
>>> inspect.ismethod(C.foo.im_func)
False

Is it possible to have a Python class decorator with arguments?

What I'd like to do is this:
#add_cache(cache_this, cache_that, cache_this_and_that)
class MyDjangoModel(models.Model):
blah
But fails because it seems that the first argument is implicitly the actual class object. Is it possible to get around this or am I forced to use the ugly syntax as opposed to this beautiful syntax?
Your arg_cache definition needs to do something like:
def arg_cache(cthis, cthat, cthisandthat):
def f(obj):
obj.cache_this = cthis
obj.cache_that = cthat
obj.thisandthat = cthisandthat
return obj
return f
#arg_cache(cache_this, cache_that, cache_this_and_that)
...
The example assumes you just want to set some properties on the decorated class. You could of course do something else with the three parameters.
Write a callable that returns an appropriate decorator.

Categories