How to get direct return value instead of <MagicMock name='mock.xx' id='yy'> - unit-testing

This is my testing code :
import mock
import unittest
def check_method_return(input):
return_value = input.ops.list()
if not return_value:
return False
return return_value
def check_method_len(input):
return_value = input.ops.list()
if len(return_value) < 1:
return False
return return_value
class TestMockReturnValue(unittest.TestCase):
def test_mock_return(self):
fake_input = mock.MagicMock()
fake_input().ops.list.return_value = []
result = check_method_return(fake_input)
self.assertFalse(result)
def test_mock_len(self):
fake_input = mock.MagicMock()
fake_input().ops.list.return_value = []
result = check_method_len(fake_input)
self.assertFalse(result)
if __name__ == '__main__':
test_empty = []
if not test_empty:
print("empty list equals to False")
unittest.main()
The run result output is :
empty list equals to False
.F
======================================================================
FAIL: test_mock_return (__main__.TestMockReturnValue)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_mock.py", line 31, in test_mock_return
self.assertFalse(result)
AssertionError: <MagicMock name='mock.ops.list()' id='140459969939728'> is not false
----------------------------------------------------------------------
Ran 2 tests in 0.005s
FAILED (failures=1)
Because when the list is empty, its return value for if is False. So, method "check_method_return" should work exactly the same as "check_method_len" in the real world.
So, my question is :
Is there a way to make the unit test pass for "check_method_return" ?

If this is the case, Here is the solution, I cannot explain the exact difference, but it makes sense:
# this mock away input.ops.list()
fake_input.ops.list.return_value = []
# this did not mock away input.ops.list()
fake_input().ops.list.return_value = []
To show the difference between 2 ways to set mock_input return value
This could help to understand better
[gliang#www ~]$ ipython
Python 2.6.6 (r266:84292, Jul 23 2015, 15:22:56)
IPython 0.13.2 -- An enhanced Interactive Python.
In [1]: import unittest
In [2]: import mock
In [3]: fake_input mock.Mag
mock.MagicMixin mock.MagicMock mock.MagicProxy
In [4]: fake_input = mock.MagicMock()
In [5]: fake_input().ops.list.return_value= []
In [6]: print fake_input().ops.list.return_value
[]
In [7]: print fake_input.ops.list.return_value
<MagicMock name='mock.ops.list()' id='15160848'>
In [8]: fake_input2 = mock.MagicMock()
In [9]: fake_input2.ops.list.return_value = []
In [10]: print fake_input2.ops.list.return_value
[]
In [11]: quit()

Related

In pytest returns magicMock instance instead of return value

i have a sample method and a pytest method for it.
import pytest
from datetime import datetime
# this is the method to test
def get_time_of_day():
"""return string Night/Morning/Afternoon/Evening depending on the hours range"""
time = datetime.now()
print(time.hour)
if 0 <= time.hour <6:
return "Night"
if 6 <= time.hour < 12:
return "Morning"
if 12 <= time.hour <18:
return "Afternoon"
return "Evening"
#pytest method, where I am trying to mock datetime.now
def test_get_time_of_day(mocker):
mock_now = mocker.patch("tests.test_sample.datetime")
mock_now.now.return_value = datetime(2016, 5, 20, 14, 10, 0)
assert get_time_of_day() == "Afternoon"
But when I try to run the pytest as follow
python3 -m pytest test_sample.py
I get TypeError: '<=' not supported between instances of 'int' and 'MagicMock'
I have a directory tests inside that I have the python file test_sample.py. In this file, I have written this code.
I have referred this page:
So do anyone Know to solve this issue. Thank you
This is because when you are assigning a value to mock_now.now.return_value, datetime is already patched and returns a MagicMock instead of the datetime object as you expect.
This would work:
def test_get_time_of_day(mocker):
dt = datetime(2016, 5, 20, 14, 10, 0)
mock_now = mocker.patch("tests.test_sample.datetime")
mock_now.now.return_value = dt
assert get_time_of_day() == "Afternoon"
As you would typically patch objects in modules that you test (instead of within the test module itself), you would not run into this issue.

How I can invoke importing class in other class Python

#!/usr/bin/env python
from __future__ import print_function
import sys
import time
import getopt
import alsaaudio
import numpy
from time import sleep
class A_weight():
def __init__(self):
skaler = 2.361E-14
fix_cur = 0.20565360419770495
A = []
hPa = 4e-11
card = 'default'
array_float = numpy.dtype(float)
stream = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, card)
stream.setchannels(1)
stream.setrate(48000)
stream.setformat(alsaaudio.PCM_FORMAT_S16_LE)
stream.setperiodsize(128)
def A(f):
return (12200**2*f**4/((f**2+20.6**2)*(f**2+12200**2)*numpy.sqrt(f**2+107.7**2)*numpy.sqrt(f**2+737.9**2)))+fix_cur
def listen(self):
glob_leq = 0
liczba_ramek = 0
index_ramek = 0
while True:
try:
l, data = stream.read()
except IOError, e:
error_count += 1
print(" (%d) Error recording: %s" % (error_count, e))
else:
if l==128:
decoded_block = numpy.frombuffer(data, dtype='int16' )
else:
continue
Y = numpy.fft.fft(decoded_block) # fft computing and normalization
Aw = A(numpy.arange(20.,20000,(19980./len(Y))))
Na = Aw*Y
inverse = numpy.fft.ifft(Y)
maks = 32768
array_float = numpy.divide(inverse.real ,float( maks))
array_float = array_float**2
sum_array = numpy.sum(array_float, dtype=float)
glob_leq = glob_leq + sum_array
liczba_ramek += 1
index_ramek += 1
if index_ramek == 375:
index_ramek=0
cis_chwil = numpy.divide(glob_leq, liczba_ramek * 128)
leq =10*numpy.log10(numpy.divide(cis_chwil, hPa))
print (leq)
#A.append(leq)
#print(max(A))
A_weight().listen()
So i trying writing program compute sound pressure level with weighting A.
All work correct but when i want close may code in class I have problem. Because something wrong with invoke to importing class in this case is it alsaaudio.
I get this feedback:
Traceback (most recent call last):
File "rec_A.py", line 64, in <module>
A_weight().listen()
File "rec_A.py", line 37, in listen
l, data = stream.read()
NameError: global name 'stream' is not defined
Do you have any idea
Change each occurrence of stream to self.stream:
class A_weight():
def __init__(self):
skaler = 2.361E-14
fix_cur = 0.20565360419770495
A = []
hPa = 4e-11
card = 'default'
array_float = numpy.dtype(float)
self.stream = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NORMAL, card)
self.stream.setchannels(1)
self.stream.setrate(48000)
self.stream.setformat(alsaaudio.PCM_FORMAT_S16_LE)
self.stream.setperiodsize(128)
...
def listen(self):
glob_leq = 0
liczba_ramek = 0
index_ramek = 0
while True:
try:
l, data = self.stream.read()
...
This will make it an instance variable, and all other methods of that class (as long as they are passed the self argument) will have access to it through self.stream. See this bit of documentation for more details on instance variables.
Also, this is merely an aesthetic point, but the convention in Python is to use upper camel case for class names, i.e., AWeight instead of A_weight - but this will not affect how your code runs.

multiprocessing - pyodbc IOError: bad message length

I am unexpectedly getting IOError: bad message length error when trying to share pyodbc connection across multiple processes, especially when N is more than 4 (no. of cores). Sometimes I also get cPickle.UnpicklingError: invalid load key, '#'., pyodbc.ProgrammingError: ('24000', '[24000] [FreeTDS][SQL Server]Invalid cursor state (0) (SQLExecDirectW)') as errors.
# Import custom python packages
import multiprocessing
import multiprocessing.managers as mm
import pathos.multiprocessing as mp
import pyodbc, datetime, time
class MyConn(object):
def __init__(self):
self.conn = None
self.cursor = None
def connect_to_db(self):
self.conn = pyodbc.connect("DSN=cpmeast;UID=dntcore;PWD=dntcorevs2")
self.cursor = self.conn.cursor()
def run_qry(self, data):
print 'Running query', data
self.cursor.execute("WAITFOR DELAY '00:00:01';select GETDATE(), '"+str(data)+"';")
l = self.cursor.fetchall()
_l = []
for i in l:
_l.append(list(i))
print 'Result for query', data, _l
return _l
class MyManagerClass(object):
def __init__(self):
self.result = multiprocessing.Manager().list()
def read_data(self, *args):
conn = args[0][0]
data = args[0][1]
l = conn.run_qry(data)
self.result.append(l)
class MyManager(mm.BaseManager):
pass # Pass is really enough. Nothing needs to be done here.
def main():
time_start = time.time()
MyManager.register("MyConn", MyConn)
manager = MyManager()
manager.start()
a = manager.MyConn()
a.connect_to_db()
dbm = MyManagerClass()
pool = mp.ProcessingPool(4)
jobs = []
N = 5
for i in range(N):
jobs.append((a, str(i)))
for i in pool.imap(dbm.read_data, jobs):
print 'result'
pool.close()
pool.join()
print 'Result', dbm.result
print 'Closed'
time_stop = time.time()
msg = 'runtime: {0}'.format(str(datetime.timedelta
(seconds=time_stop-time_start)))
print msg
if __name__ == '__main__':
main()

Assert that two lists of objects are equal in django testing

Is there a way to check that two lists of objects are equal in django tests.
lets say I have some model:
class Tag(models.Model):
slug = models.SlugField(max_length=50, unique=True)
def __unicode__(self):
return self.slug
and I run this simple test:
def test_equal_list_fail(self):
tag_list = []
for tag in ['a', 'b', 'c']:
tag_list.append(Tag.objects.create(slug=tag))
tags = Tag.objects.all()
self.assertEqual(tag_list, tags)
this fails with:
======================================================================
FAIL: test_equal_list_fail (userAccount.tests.ProfileTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "path/to/tests.py", line 155, in test_equal_list_fail
self.assertEqual(tag_list, tags)
AssertionError: [<Tag: a>, <Tag: b>, <Tag: c>] != [<Tag: a>, <Tag: b>, <Tag: c>]
----------------------------------------------------------------------
this will work:
def test_equal_list_passes(self):
tag_list = []
for tag in ['a', 'b', 'c']:
tag_list.append(Tag.objects.create(slug=tag))
tags = Tag.objects.all()
for tag_set in zip(tags, tag_list):
self.assertEqual(*tag_set)
However, This fails:
def test_equal_list_fail(self):
tag_list = []
for tag in ['a', 'b', 'c']:
tag_list.append(Tag.objects.create(slug=tag))
tags = Tag.objects.all()
for tag_set in zip(tags, tag_list):
print "\n"
print tag_set[0].slug + "'s pk is %s" % tag_set[0].pk
print tag_set[1].slug + "'s pk is %s" % tag_set[1].pk
print "\n"
self.assertIs(*tag_set)
with:
Creating test database for alias 'default'...
.......
a's pk is 1
a's pk is 1
F.
======================================================================
FAIL: test_equal_list_fail (userAccount.tests.ProfileTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "path/to/tests.py", line 160, in test_equal_list_fail
self.assertIs(*tag_set)
AssertionError: <Tag: a> is not <Tag: a>
Is this expected behavior?
Edit in response to comment
This type of comparison works:
class Obj:
def __init__(self, x):
self.x = x
>>> one = Obj(1)
>>> two = Obj(2)
>>> a = [one, two]
>>> b = [one, two]
>>> a == b
True
Why is the test failing for the other arrays?
To test two lists
use: assertSequenceEqual
Because, in this case, tags = Tag.objects.all() generates a django.db.models.query.QuerySet where as tag_list.append(...) creates a list.
Other options in different situations are:
assertListEqual(a, b)
assertTupleEqual(a, b)
assertSetEqual(a, b)
assertDictEqual(a, b)
Why <Tag: a> is not <Tag: a>
The tags are the same model, but they've been loaded into different places in memory
for tag_set in zip(tags, tag_list):
print "\n"
print tag_set[0].slug + "'s pk is %s" % tag_set[0].pk + ' id is: ' + id(tag_set[0])
print tag_set[1].slug + "'s pk is %s" % tag_set[1].pk + ' id is: ' + id(tag_set[1])
print "\n"
self.assertIs(*tag_set)
returns
.......
a's pk is 1 id is: 4522000208
a's pk is 1 id is: 4522228112
F.
Therefore, is will retrun False
I think what you want to test is if the tags created have the same slugs as those in your test list.
For that, fetch only the slug as a list with values_list, and then compare that:
assertEqual(Tag.objects.values_list('slug', flat=True), ['a','b','c'])
I have to say, this isn't quite a useful test because you are checking django orm functionality, which has already been tested quite well.
Your tests should check for specifics of your own application.

Cannot Pool.map() function because of UnpickleableError?

So I am trying to multi process function F. Which is accessed by a button press with tkinter.
def f(x):
global doom,results,info
doom = doom + 1
if check(x) == True:
results.add(x)
info.append(get_column_number(x))
j.step(1)
texx = "1/"+doom
s.configure(text=texx)
root.update()
The function is called within a function like so:
def dojob():
index = ['URLS'...]
pool = Pool(processes=4)
s.configure(text="Shifting Workload to cores..")
root.update()
pool.map(f, index)
The button is inside root window.
I get the following error:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 808, in __bootstrap_inner
self.run()
File "C:\Python27\lib\threading.py", line 761, in run
self.__target(*self.__args, **self.__kwargs)
File "C:\Python27\lib\multiprocessing\pool.py", line 342, in _handle_tasks
put(task)
UnpickleableError: Cannot pickle <type 'tkapp'> objects
I do not even know what a pickle does? Help?
Here is the complete code:
from Tkinter import *
from ttk import *
from tkMessageBox import showinfo
from multiprocessing import Pool
import random
emails = set()
import urllib2
import urllib2 as urllib
########
CONSTANT_PAGECOUNT = 20
######
def f(x):
global doom,emails,info
doom = doom + 1
if check(x) == True:
print "",
emails.add(x)
info.append(get_column_number(x))
j.step(1)
texx = "Sk1nn1n "+str(doom)+'/'+str(CONSTANT_PAGECOUNT)+""
s.configure(text=texx)
root.update()
return 0
def f(x):
print ""
def showFile(site,info):
top = Toplevel()
top.title('Sites')
x = Text(top)
x.pack()
i=0
for site_url in site:
x.insert(END,site_url)
i=i+1
def get_column_number(url):
return True
def check(url):
return True
def getgoogleurl(search,siteurl=False,startr=0):
if siteurl==False:
return 'http://www.google.com/search?q='+urllib2.quote(search)+'&start='+str(startr)+'&oq='+urllib2.quote(search)
else:
return 'http://www.google.com/search?q=site:'+urllib2.quote(siteurl)+'%20'+urllib2.quote(search)+'&oq=site:'+urllib2.quote(siteurl)+'%20'+urllib2.quote(search)
def getgooglelinks(search,siteurl=False,startr=0):
#google returns 403 without user agent
headers = {'User-agent':'Mozilla/11.0'}
req = urllib2.Request(getgoogleurl(search,siteurl,startr),None,headers)
site = urllib2.urlopen(req)
data = site.read()
site.close()
#no beatifulsoup because google html is generated with javascript
start = data.find('<div id="res">')
end = data.find('<div id="foot">')
if data[start:end]=='':
#error, no links to find
return False
else:
links =[]
data = data[start:end]
start = 0
end = 0
while start>-1 and end>-1:
#get only results of the provided site
if siteurl==False:
start = data.find('<a href="/url?q=')
else:
start = data.find('<a href="/url?q='+str(siteurl))
data = data[start+len('<a href="/url?q='):]
end = data.find('&sa=U&ei=')
if start>-1 and end>-1:
link = urllib2.unquote(data[0:end])
data = data[end:len(data)]
if link.find('http')==0:
links.append(link)
return links
def rip(results=15,accuracy=16):
global e
keyword = ''+str(e.get())
if keyword.strip()=="":
s.configure(text="Please enter a keyword")
root.update()
return 0
linklist = []
counter = 0
doom = 0
while counter < results:
links = getgooglelinks(keyword,startr=counter)
for link in links:
if len(linklist) > CONSTANT_PAGECOUNT:
s.configure(text="Proccessing..")
root.update()
return linklist
else:
doom = doom + 1
linklist.append(link)
texx = str(doom)+"/"+str(CONSTANT_PAGECOUNT)
s.configure(text=texx)
root.update()
root.update()
counter = counter+accuracy
return linklist
def flip():
global e
emails = set()
info = []
keyword = ''+str(e.get())
if keyword.strip()=="":
s.configure(text="Please enter a keyword")
root.update()
return 0
s.configure(text="Generating index..")
root.update()
doom = -1
index = rip(CONSTANT_PAGECOUNT,10)
if 1:
try:
pool = Pool(processes=4)
#s.configure(text="Shifting Workload to cores..")
#root.update()
pool.map(f, index)
pool.close()
except:
print "The errors there.."
j.config(value=CONSTANT_PAGECOUNT)
if len(emails) > 0:
filepath='relavant_list_'+str(random.randint(1,9999))+'.emList.txt'
#print len(emails),
#print "emails found."
ggg = open(filepath,'a+')
for x in emails:
ggg.write(x+"\n")
showinfo(
str(len(emails))+" key word related sites found!",
" sites are saved in "+str(filepath)
)
showFile(emails,info)
s.configure(text=filepath)
else:
s.configure(text='No related sites found : (')
if __name__ == '__main__':
### CONSTANTS
version = '1.0'
### END CONSTANTS
root = Tk()
root.title('Program v'+version)
s = Style()
s.theme_use('default')
#print s.theme_names()
s.configure("black.Horizontal.TProgressbar", foreground='blue', background='blue')
j = Progressbar(root, style="black.Horizontal.TProgressbar", orient="vertical", length=200, mode="determinate", maximum=CONSTANT_PAGECOUNT, value=0)
j.pack(side='right',fill='y')
f = Frame(root)
x = Frame(f)
e = Entry(x,width=51)
s = Label(x,width=50,anchor='center',text='Waiting for task..')
Button(f,text='Generate List!',width=50,command=flip).pack(fill='both',expand=True)
s.pack(side='bottom',fill='y',expand=True)
e.pack(side='top',fill='both',expand=True)
x.pack(side='top',fill='y',expand=True)
f.pack(side='left',expand=True,fill="both")
root.mainloop()
You are leaking a tkinter object. Most likely because you are trying to update the interface from another process with the last line of f()
Update based on code
You have a name collision between your function f() and a variable f in your __main__ which gets assigned to your main window and causes the tkapp pickle error. Rename the function to def myfunc() or something. Also need to call pool.join() after pool.close()