How to mock imwrite in a loop - python

I am trying to use Python unittest library to test the following code.
from cv2 import imwrite
import h5py
import lumpy as np
class Myclass:
def __init__(self):
self.data = []
def get_data(self):
return self.data
def load_data(self, path):
count = 10
for i in range(count):
mat_file = path + f'{i}.mat'
with h5py.File(mat_file, 'r') as fin:
data = np.array(fin['data'])
for j in range(len(data)):
filename = path + f'{i}_{j}.jpg'
imwrite(filename, data)
self.data = data
I tried to do the following. But I am getting AttributeError saying Myclass has no attribute 'imwrite'. And I do not know how to mock the nested loops with image writing imwrite.
from mock import MagicMock, patch
def test():
m = MagicMock()
m.__enter__.return_value = data
with patch("h5py.File", return_value=m):
with patch(Myclass.imwrite) as mock_imwrite:
testclass = Myclass()
testclass.load_data(path='test')
assert testclass.get_data() == data
I hope someone can help me out. Any help is very much appreciated

The issue here is that your class Myclass does not have a function imwrite. Only the module (aka the file the class is defined in) is aware of this function.
If you want to patch the imwrite function of the package cv2, you have to write:
(untested code)
with patch('cv2.imwrite') as mock_imwrite:
testclass = Myclass()
...
But the as mock_imwrite part is only needed, if you run an assert on the mock. Otherwise, you may just skip it.

Related

unit test of method of class with mocking data in python

I have some class SomeClass in file prog.py
class SomeClass:
def __init__(self, file_name):
self.file_name = file_name
self.data_dict = {}
def load(self):
"""
"""
if not os.path.exists(self.file_name):
return False
with open(self.file_name, "r", encoding='iso-8859-1') as file:
skip_lines(4, file)
reader = csv.DictReader(file)
for line in reader:
if line['Region'] == "":#to remove Copiright and disclaimer text at the end
break
label=re.sub('\s+/\s+',"/",line['Region']) #remove spaces around /
if label in self.data_dict.keys():
#if line['Region'] in self.data_dict.keys():
self.data_dict[label][line['Range']] = int(line['All people'])
#self.data_dict[line['Region']][line['Range']] = line['All people']
else:
self.data_dict[label] = {line['Range']:int(line['All people'])}
#self.data_dict[line['Region']] = {line['Range']:line['All people']}
return True
def regions(self):
return list(self.data_dict.keys())
I need to:
Create a file named test_someclass_data.py.
Use import to import the prog module.
In the test_someclass_data.py file, create a unit test class named
TestSomeClass that inherits from unittest.TestCase.
The unit test class should include member functions to test the
regions member function of the SomeClass class.
My code of test_someclass_data.py:
import unittest
import prog
class TestSomeClass(unittest.TestCase):
def test_regions(self):
"""
Test regions function of the SomeClass class.
"""
prog.SomeClass.data_dict ={"key1":"val1","key2":"val2"}
test_output = prog.SomeClass.regions()
expected_output =["key1", "key2"]
self.assertEqual(test_output, expected_output)
I need to test a function of the SomeClass, so I have to create an instance of the class, call method call to store data, then I can compare outputs. Could you advise me, please, how to avoid this and test functions through mocking? Thank you.

mocking external library return value throws access violation exception

I'm writing TC for my method using external library sklearn.neighbors.KDTree.
My test target method is below,
# target.py
from sklearn.neighbors import KDTree
#staticmethod
def mymethod(a, b):
...
dist, index = KDTree(a).query(b, k=3)
# manipulate the return value from KDTree.query
...
and, the code I tried as TC is this.
# mytest.py
from unittest import mock
#mock.patch('sklearn.neighbors.KDTree')
def test_mymethod(mock_kdtree):
# make test data and set mock
a = ...
b = ...
mock_kdtree.return_value.query.return_value = ...
# execute test target
mymethod(a, b)
assert mock_kdtree.called
When running TC it throws exception, Windows fatal exception: access violation on the line calling dist, index = KDTree(a).query(b, k=3).
Is there something wrong to mock KDTree return value?
It is not mocking the method correctly.
You typically skip the step where you set the mock instance of the class.
from unittest import mock
import sklearn
#patch('sklearn.neighbors.KDTree')
def test_mymethod(mock_kdtree):
# make test data and set mock
a = ...
b = ...
# First create a mock instance of the kdtree class
mock_kdtree_instance = mock.MagicMock()
mock_kdtree_instance.query.return_value = ...
# assign this instance to the class mock
mock_kdtree.return_value = mock_kdtree_instance
# execute test target
mymethod(a, b)
mock_kdtree_query.assert_called()
For someone who might be struggling with same problem, I share how I made my test.
The key is using monkeypatch, from the advice #jossefaz. Thank you!
# mytest.py
def test_mymethod(monkeypatch):
class MockKDTree(object):
def __init__(self, *fake_args):
pass
def query(self, *fake_args, **fake_kwargs):
return fake_kdtree_dists, None # This is what I wanted to return. You need to prepare.
monkeypatch.setattr("mypackage.mymodule.KDTree", MockKDTree)
# execute test target
mymethod(a, b)
# assertion

python mock global function that is used in class

I can't seem to get my head around mocking in Python. I have a global function:
a.py:
def has_permission(args):
ret_val = ...get-true-or-false...
return ret_val
b.py:
class MySerializer(HyperlinkedModelSerializer):
def get_fields():
fields = super().get_fields()
for f in :
if has_permission(...):
ret_val[f.name] = fields[f]
return ret_val
c.py:
class CountrySerializer(MySerializer):
class Meta:
model = Country
Question: Now i want to test c.py, but i want to mock the has_permission function that is defined in a.py, but is called in the get_fields-method of the class MySerializer that is defined in b.py ... How do i do that?
I've tried things like:
#patch('b.MySerializer.has_permission')
and
#patch('b.MySerializer.get_fields.has_permission')
and
#patch('a.has_permission')
But everything i try either just doesn't work and has_permission is still executed, or python complains about that it can't find the attribute 'has_permission'
with the patching done in:
test.py
class TestSerializerFields(TestCase):
#patch(... the above examples....)
def test_my_country_serializer():
s = CountrySerializer()
self..assertTrue(issubclass(my_serializer_fields.MyCharField, type(s.get_fields()['field1'])))
You need to patch the global in the b module:
#patch('b.has_permission')
because that's where your code looks for it.
Also see the Where to patch section of the mock documentation.
You need to patch the method where it exists at the time your test runs. If you try and patch the method where it is defined after the test code has already imported it, then the patch will have no effect. At the point where the #patch(...) executes, the test code under test has already grabbed the global method into its own module.
Here is an example:
app/util/config.py:
# This is the global method we want to mock
def is_search_enabled():
return True
app/service/searcher.py:
# Here is where that global method will be imported
# when this file is first imported
from app.util.config import is_search_enabled
class Searcher:
def __init__(self, api_service):
self._api_service = api_service
def search(self):
if not is_search_enabled():
return None
return self._api_service.perform_request('/search')
test/service/test_searcher.py:
from unittest.mock import patch, Mock
# The next line will cause the imports of `searcher.py` to execute...
from app.service.searcher import Searcher
# At this point, searcher.py has imported is_search_enabled into its module.
# If you later try and patch the method at its definition
# (app.util.config.is_search_enabled), it will have no effect because
# searcher.py won't look there again.
class MockApiService:
pass
class TestSearcher:
# By the time this executes, `is_search_enabled` has already been
# imported into `app.service.searcher`. So that is where we must
# patch it.
#patch('app.service.searcher.is_search_enabled')
def test_no_search_when_disabled(self, mock_is_search_enabled):
mock_is_search_enabled.return_value = False
mock_api_service = MockApiService()
mock_api_service.perform_request = Mock()
searcher = Searcher(mock_api_service)
results = searcher.search()
assert results is None
mock_api_service.perform_request.assert_not_called()
# (For completeness' sake, make sure the code actually works when search is enabled...)
def test_search(self):
mock_api_service = MockApiService()
mock_api_service.perform_request = mock_perform_request = Mock()
searcher = Searcher(mock_api_service)
expected_results = [1, 2, 3]
mock_perform_request.return_value = expected_results
actual_results = searcher.search()
assert actual_results == expected_results
mock_api_service.perform_request.assert_called_once_with('/search')

next string of a stream in VB.net

I have a some bunch of python files. I need to get all the classes from there and make a list.
its like I have to read with streamreader and then
Imports ActionBlock
I have to take the string ActionBlock and show it in a list. Listing and others hopefully I can do, but I am stuck in this point. Any suggestion please? Thank you.
You could use a regular expression to look for the parts you're interested in.
The following code
Dim path = "c:\path\to\your\file.py"
Dim content = File.ReadAllText(path)
Dim matchClass = "class (?<m>\w+)(:|\()+"
Dim matchImport = "(^|from \w+ )import ((?<m>\w+), )*(?<m>\w+)"
Dim result = Regex.Matches(content, String.Format("({0}|{1})", matchClass, matchImport), RegexOptions.Multiline) _
.Cast(Of Match) _
.SelectMany(Function(m) m.Groups("m").Captures.Cast(Of Capture).Select(Function(c) c.Value)) _
.ToList()
will, given a text file like
import os
import math
from time import clock
from random import randint
import DataArchiving
import TABasicFunctions
import HWDataConveterGate
import GeneralTestDataMapping
from something import FirstClass, SecondClass
def foo():
pass
def bar():
pass
class ClassOne(object):
class NestedClass:
pass
def thisisnotaclass(self):
v = [x.class for x in self]
v = [x.someimport for x in self]
class ClassTwo:
pass
class Class3:
pass
def main():
pass
if __name__ == '__main__':
main()
create a list that looks like:

How to make global imports from a function?

I fear that this is a messy way to approach the problem but...
let's say that I want to make some imports in Python based on some conditions.
For this reason I want to write a function:
def conditional_import_modules(test):
if test == 'foo':
import onemodule, anothermodule
elif test == 'bar':
import thirdmodule, and_another_module
else:
import all_the_other_modules
Now how can I have the imported modules globally available?
For example:
conditional_import_modules(test='bar')
thirdmodule.myfunction()
Imported modules are just variables - names bound to some values. So all you need is to import them and make them global with global keyword.
Example:
>>> math
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'math' is not defined
>>> def f():
... global math
... import math
...
>>> f()
>>> math
<module 'math' from '/usr/local/lib/python2.6/lib-dynload/math.so'>
You can make the imports global within a function like this:
def my_imports(module_name):
globals()[module_name] = __import__(module_name)
I've just had the similar problem, here is my solution:
class GlobalImport:
def __enter__(self):
return self
def __call__(self):
import inspect
self.collector = inspect.getargvalues(inspect.getouterframes(inspect.currentframe())[1].frame).locals
def __exit__(self, *args):
globals().update(self.collector)
then, anywhere in the code:
with GlobalImport() as gi:
import os, signal, atexit, threading, _thread
# whatever you want it won't remain local
# if only
gi()
# is called before the end of this block
# there you go: use os, signal, ... from whatever place of the module
You can use the built-in function __import__ to conditionally import a module with global scope.
To import a top level module (think: import foo):
def cond_import():
global foo
foo = __import__('foo', globals(), locals())
Import from a hierarchy (think: import foo.bar):
def cond_import():
global foo
foo = __import__('foo.bar', globals(), locals())
Import from a hierarchy and alias (think: import foo.bar as bar):
def cond_import():
global bar
foo = __import__('foo.bar', globals(), locals())
bar = foo.bar
I like #badzil approach.
def global_imports(modulename,shortname = None, asfunction = False):
if shortname is None:
shortname = modulename
if asfunction is False:
globals()[shortname] = __import__(modulename)
else:
globals()[shortname] = eval(modulename + "." + shortname)
So something that is traditionally in a class module:
import numpy as np
import rpy2
import rpy2.robjects as robjects
import rpy2.robjects.packages as rpackages
from rpy2.robjects.packages import importr
Can be transformed into a global scope:
global_imports("numpy","np")
global_imports("rpy2")
global_imports("rpy2.robjects","robjects")
global_imports("rpy2.robjects.packages","rpackages")
global_imports("rpy2.robjects.packages","importr",True)
May have some bugs, which I will verify and update. The last example could also have an alias which would be another "shortname" or a hack like "importr|aliasimportr"
I like #rafaƂ grabie approach. As it even support importing all.
i.e.
from os import *
(Despite it being bad practice XD )
Not allowed to comment, but here is a python 2.7 version.
Also removed the need to call the function at the end.
class GlobalImport:
def __enter__(self):
return self
def __exit__(self, *args):
import inspect
collector = inspect.getargvalues(inspect.getouterframes(inspect.currentframe())[1][0]).locals
globals().update(collector)
def test():
with GlobalImport() as gi:
## will fire a warning as its bad practice for python.
from os import *
test()
print path.exists(__file__)
I like the answer from #maxschlepzig.
There is a bug in the approach that if you directly import a function it will not work.
For example,
global_imports("tqdm", "tqdm, True)
does not work, because the module is not imported. And this
global_imports("tqdm")
global_imports("tqdm", "tqdm, True)
works.
I change #maxschlepzig's answer a bit. Using fromlist so you can load function or module with "From" statement in a uniform way.
def global_imports(object_name: str,
short_name: str = None,
context_module_name: str = None):
"""import from local function as global import
Use this statement to import inside a function,
but effective as import at the top of the module.
Args:
object_name: the object name want to import,
could be module or function
short_name: the short name for the import
context_module_name: the context module name in the import
example usage:
import os -> global_imports("os")
import numpy as np -> global_imports("numpy", "np")
from collections import Counter ->
global_imports("Counter", None, "collections")
from google.cloud import storage ->
global_imports("storage", None, "google.cloud")
"""
if not short_name:
short_name = object_name
if not context_module_name:
globals()[short_name] = __import__(object_name)
else:
context_module = __import__(context_module_name,
fromlist=[object_name])
globals()[short_name] = getattr(context_module, object_name)
You could have this function return the names of the modules you want to import, and then use
mod == __import__(module_name)
Step-1: config.py, config_v2.py, rnd.py in same directory/folder
Step-2: config.py
HIGH_ATTENDANCE_COUNT_MIN = 0
Step-3: config_v2.py
HIGH_ATTENDANCE_COUNT_MIN = 5
Step-4: rnd.py
def versioning_test(v):
global config
if v == 'v1':
config = __import__('config', globals(), locals())
if v == 'v2':
config = __import__('config_v2', globals(), locals())
def version_test_in_another_function():
print('version_test_in_another_function: HIGH_ATTENDANCE_COUNT_MIN: ', config.HIGH_ATTENDANCE_COUNT_MIN)
versioning_test("v2")
version_test_in_another_function()
Step-5: $ python3 rnd.py
<<output>>: version_test_in_another_function: HIGH_ATTENDANCE_COUNT_MIN: 5
It is now recommended (for Python 3), to use the importlib
https://docs.python.org/3/reference/import.html#importlib
eg: globals()["np"] = importlib.import_module("numpy")
and you can now execute "np.array([1,2,3])" afterwards.
There are also other ways of importing that you might prefer. Consider seeing the aforementioned documentation.

Categories

Resources