Mocking Only One Function of a Class - python

I would like to write a unit test that can ensure a SQL statement in a function call is schematically correct. It should test the execution of this call. I would then like to mock the call to commit, so that no insertions to the database take place. I'm using psycopg2 for my tests.
I have a function like:
def test_insert(a, b, c):
con = psycopg2.connect(os.environ['PGDB'])
cur = con.cursor()
cur.execute('insert into test_table values ({a}, {b}, {c})'.format(a=a, b=b, c=c))
con.commit()
con.close()
when calling test_insert(1,2,3) I see the row inserted into the table. Now I try to mock the call. I've taken a few approaches so far:
#mock.patch('psycopg2.connect')
def test(mock_connect, a, b, c):
mock_con = mock_connect.return_value
mock_con.commit.return_value = None
insert_row(a, b, c)
This seems to work but does not actually call the execution statement. test_insert(1,4,'xyz') fails for instance while test(1,4,'xyz') does not. Next I tried to mock just the commit method of the connection class in psycopg2:
#mock.patch('psycopg2.extensions.connection.commit')
def test_insert(mock_commit, a, b, c):
mock_commit.return_value = None
insert_row(a,b,c)
but this gives me a syntax error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/a/.virtualenv/test/lib/python2.7/site-packages/mock/mock.py", line 1318, in patched
patching.__exit__(*exc_info)
File "/home/a/.virtualenv/test/lib/python2.7/site-packages/mock/mock.py", line 1480, in __exit__
setattr(self.target, self.attribute, self.temp_original)
TypeError: can't set attributes of built-in/extension type 'psycopg2.extensions.connection'
Is there a good way to do what I am trying to do?

I assume you are using pytest and it is not a good practice to name your functions starting with test_ if they are not actual tests as this will probably raise problems with the testing framework. Therefore I slightly modified your initial snippet as follows and I named the module psyco.py
import psycopg2
import os
def insert(a, b, c):
con = psycopg2.connect(os.environ['PGDB'])
import ipdb; ipdb.set_trace()
cur = con.cursor()
cur.execute('insert into test_table values ({a}, {b}, {c})'.format(a=a, b=b, c=c))
con.commit()
con.close()
Next, I created the test for your method by taking into account how patch works and where to patch. As you are dealing with os environment variables this question can help you understand why I mocked it that way.
An example implementation of the test could be as follows:
from psyco import insert
from unittest.mock import patch, Mock, MagicMock
import os
#patch.dict(os.environ,{'PGDB':'db_url'})
#patch('psycopg2.connect')
def test_insert_function(psycopg2_mock):
x = 1
y = 4
z = 'xyz'
sql_query = 'insert into test_table values ({0}, {1}, {2})'.format(x,y,z)
insert(x,y,z)
assert psycopg2_mock.return_value.cursor.call_count == 1
psycopg2_mock.return_value.cursor.return_value.execute.assert_called_with(sql_query)
assert psycopg2_mock.return_value.commit.call_count == 1
assert psycopg2_mock.return_value.close.call_count == 1

Related

Executing python function in postgres

I'm trying to run a python function on the cursor.execute parameter but it just throws me this error.
I'm using psycopg2
Traceback (most recent call last):
File "cliente.py", line 55, in <module>
cursorDB.execute(get_datos_animal('falsa'))
psycopg2.errors.UndefinedColumn: column "falsa" does not exist
LINE 1: ...e, clasificacion FROM animales WHERE animales.hierro = falsa
and my python function is this one
def get_datos_animal(hierro_v):
return "SELECT hierro, registro, nombre, fecha_nacimiento, raza, sexo, hierro_madre, hierro_padre, clasificacion FROM animales WHERE animales.hierro = " + str(hierro_v)
any idea what i´m doing wrong?
Have several functions like this with same errors.
Use the automatic parameter quoting provided by your connection to ensure that values in queries are always quoted correctly, and to avoid SQL injection attacks.
stmt = """SELECT hierro, registro, nombre, fecha_nacimiento, raza, sexo, hierro_madre, hierro_padre, clasificacion
FROM animales
WHERE animales.hierro = %s"""
cursor.execute(stmt, (hierro_v,))
In postgres if you pass value without quotes it will treat that as column name.
Try this:
def get_datos_animal(hierro_v):
return "SELECT hierro, registro, nombre, fecha_nacimiento, raza, sexo, hierro_madre, hierro_padre, clasificacion FROM animales WHERE animales.hierro = '"+str(hierro_v)+"'"

Issue in Model-View-Controller Pattern implementation

I am running an example for learning The Model-View-Controller Pattern in python, but the code is giving an error. I tried to debug the code, but I couldn't find the main root/cause. Removing close connection system works, but what is the issue of the code? Can you advise me what is wrong?
# Filename: mvc.py
import sqlite3
import types
class DefectModel:
def getDefectList(self, component):
query = '''select ID from defects where Component = '%s' ''' %component
defectlist = self._dbselect(query)
list = []
for row in defectlist:
list.append(row[0])
return list
def getSummary(self, id):
query = '''select summary from defects where ID = '%d' ''' % id
summary = self._dbselect(query)
for row in summary:
return row[0]
def _dbselect(self, query):
connection = sqlite3.connect('example.db')
cursorObj = connection.cursor()
results = cursorObj.execute(query)
connection.commit()
cursorObj.close()
return results
class DefectView:
def summary(self, summary, defectid):
print("#### Defect Summary for defect# %d ####\n %s" % (defectid,summary) )
def defectList(self, list, category):
print("#### Defect List for %s ####\n" % category )
for defect in list:
print(defect )
class Controller:
def __init__(self): pass
def getDefectSummary(self, defectid):
model = DefectModel()
view = DefectView()
summary_data = model.getSummary(defectid)
return view.summary(summary_data, defectid)
def getDefectList(self, component):
model = DefectModel()
view = DefectView()
defectlist_data = model.getDefectList(component)
return view.defectList(defectlist_data, component)
This is related run.py.
#run.py
import mvc
controller = mvc.Controller()
# Displaying Summary for defect id # 2
print(controller.getDefectSummary(2))
# Displaying defect list for 'ABC' Component print
controller.getDefectList('ABC')
If you need to create the database, it is available here:
# Filename: datbase.py
import sqlite3
import types
# Create a database in RAM
db = sqlite3.connect('example.db')
# Get a cursor object
cursor = db.cursor()
cursor.execute("drop table defects")
cursor.execute("CREATE TABLE defects(id INTEGER PRIMARY KEY, Component TEXT, Summary TEXT)")
cursor.execute("INSERT INTO defects VALUES (1,'XYZ','File doesn‘t get deleted')")
cursor.execute("INSERT INTO defects VALUES (2,'XYZ','Registry doesn‘t get created')")
cursor.execute("INSERT INTO defects VALUES (3,'ABC','Wrong title gets displayed')")
# Save (commit) the changes
db.commit()
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
db.close()
My error is as below:
> Windows PowerShell Copyright (C) Microsoft Corporation. All rights
> reserved.
>
> PS E:\Projects\test> & python e:/Projects/test/mvc.py
> Traceback (most recent call last): File
> "e:/Projects/test/mvc.py", line 56, in <module>
> import mvc File "e:\Projects\test\mvc.py", line 65, in <module>
> cursor.execute("drop table defects") sqlite3.OperationalError: no such table: defects PS E:\Projects\test> & python
> e:/Projects/ramin/mvc.py Traceback (most recent call last):
> File "e:/Projects/test/mvc.py", line 56, in <module>
> import mvc File "e:\Projects\test\mvc.py", line 80, in <module>
> print(controller.getDefectSummary(2)) File "e:\Projects\test\mvc.py", line 44, in getDefectSummary
> summary_data = model.getSummary(defectid) File "e:\Projects\test\mvc.py", line 18, in getSummary
> for row in summary: sqlite3.ProgrammingError: Cannot operate on a closed cursor. PS E:\Projects\test>
I suspect that the problem is this line: cursor.execute("drop table defects")
Maybe you dropped that table in a previous run, and since it's no longer there, sqlite3 raises an OperationalError exception.
In your code there is a comment that says that you are using an in-memory sqlite database, but you are not. This is how you create an in-memory db:
db = sqlite3.connect(:memory:)
If you use an in-memory db you don't need to drop anything, since you are creating the db on the fly when you run your script.
Note: last year I wanted to understand the MVC better, so I wrote a series of articles about it. Here is the one where I use SQLite as a storage backend for my Model.

Patching a method without changing how the method works?

I'm trying to test that a pandas method gets called with some values.
However, just by applying a #patch decorator causes the patched method to throw a ValueError within pandas, when the actual method does not. I'm just trying to test that Stock.calc_sma is calling the underlying pandas.rolling_mean function.
I'm under the assumption that the #patch decorator basically adds some "magic" methods to the thing I'm patching that allow me to check if the function was called. If this is the case, why doesn't the pandas.rolling_mean function behave the same whether it's patched vs. not patched?
app/models.py
import pandas as pd
class Stock: # i've excluded a bunch of class methods, including the one that sets self.data, which is a DataFrame of stock prices.
def calc_sma(self, num_days)
if self.data.shape[0] > num_days: # Stock.data holds a DataFrame of stock prices
column_title = 'sma' + str(num_days)
self.data[column_title] = pd.rolling_mean(self.data['Adj Close'], num_days)
app/tests/TestStockModel.py
def setUp(self):
self.stock = MagicMock(Stock)
self.stock.ticker = "AAPL"
self.stock.data = DataFrame(aapl_test_data.data)
#patch('app.models.pd.rolling_mean')
def test_calc_sma(self, patched_rolling_mean):
Stock.calc_sma(self.stock, 3)
assert(isinstance(self.stock.data['sma3'], Series))
patched_rolling_mean.assert_any_call()
ERROR: test_calc_sma (TestStockModel.TestStockModel)
Traceback (most recent call last):
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/mock.py", line 1201, in patched
return func(*args, **keywargs)
File "/Users/grant/Code/python/chartflux/app/tests/TestStockModel.py", line 26, in test_calc_sma
Stock.calc_sma(self.stock, 3)
File "/Users/grant/Code/python/chartflux/app/models.py", line 27, in calc_sma
self.data[column_title] = pd.rolling_mean(self.data['Adj Close'], num_days)
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/pandas/core/frame.py", line 1887, in __setitem__
self._set_item(key, value)
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/pandas/core/frame.py", line 1967, in _set_item
value = self._sanitize_column(key, value)
File "/Users/grant/Code/python/chartflux/env/lib/python2.7/site-packages/pandas/core/frame.py", line 2017, in _sanitize_column
raise ValueError('Length of values does not match length of '
ValueError: Length of values does not match length of index
>>> import os
>>> os.getcwd()
'/'
>>> from unittest.mock import patch
>>> with patch('os.getcwd'):
... print(os.getcwd)
... print(os.getcwd())
... print(len(os.getcwd()))
...
<MagicMock name='getcwd' id='4472112296'>
<MagicMock name='getcwd()' id='4472136928'>
0
By default patch replaces things with really generic mock objects. As you can see, calling the mock just returns another mock. It has a len of 0 even if the replaced object wouldn't have a len. Its attributes are also generic mocks.
So to simulate behavior requires things extra arguments like:
>>> with patch('os.getcwd', return_value='/a/wonderful/place'):
... os.getcwd()
...
'/a/wonderful/place'
Or to "pass through":
>>> _cwd = os.getcwd
>>> with patch('os.getcwd') as p:
... p.side_effect = lambda: _cwd()
... print(os.getcwd())
...
/
There is a similar example in https://docs.python.org/3.5/library/unittest.mock-examples.html

Python unittesting initiate values

Sorry if this question is stupid. I created an unittest class which needs to take given inputs and outputs from outside. Thus, I guess these values should be initiated. However, I met some errors in the following code:
CODE:
import unittest
from StringIO import StringIO
##########Inputs and outputs from outside#######
a=[1,2]
b=[2,3]
out=[3,4]
####################################
def func1(a,b):
return a+b
class MyTestCase(unittest.TestCase):
def __init__(self,a,b,out):
self.a=a
self.b=b
self.out=out
def testMsed(self):
for i in range(self.tot_iter):
print i
fun = func1(self.a[i],self.b[i])
value = self.out[i]
testFailureMessage = "Test of function name: %s iteration: %i expected: %i != calculated: %i" % ("func1",i,value,fun)
self.assertEqual(round(fun,3),round(value,3),testFailureMessage)
if __name__ == '__main__':
f = MyTestCase(a,b,out)
from pprint import pprint
stream = StringIO()
runner = unittest.TextTestRunner(stream=stream, verbosity=2)
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
print 'Tests run', result.testsRun
However, I got the following error
Traceback (most recent call last):
File "C:testing.py", line 33, in <module>
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
File "C:\Python27\lib\unittest\loader.py", line 310, in makeSuite
return _makeLoader(prefix, sortUsing, suiteClass).loadTestsFromTestCase(testCaseClass)
File "C:\Python27\lib\unittest\loader.py", line 50, in loadTestsFromTestCase
if issubclass(testCaseClass, suite.TestSuite):
TypeError: issubclass() arg 1 must be a class
Can anyone give me some suggestions? Thanks!
The root of the problem is this line,
result = runner.run(unittest.makeSuite(MyTestCase(a,b,out)))
unittest.makeSuite expects a class, not an instance of a class. So just MyTestCase, not MyTestCase(a, b, out). This means that you can't pass parameters to your test case in the manner you are attempting to. You should probably move the code from init to a setUp function. Either access a, b, and out as globals inside setUp or take a look at this link for information regarding passing parameters to a unit test.
By the way, here is the source file within python where the problem originated. Might be informative to read.

how to make a python-mysqldb template?

I've learn some basics about python-mysqldb ,when I want to define anther function for query,I have to write (connect ,cursor...try ..) repeatedly
so I want to design a template like jdbcTemplate (Java EE, Spring)
my code is:
def DBV():
def templateFN(fn):
logging.basicConfig(level=logging.INFO)
log = logging.getLogger('DB')
conn = MySQLdb.connect(user='root',passwd='247326',db='lucky',charset="utf8",cursorclass=MySQLdb.cursors.DictCursor);
cursor = conn.cursor()
def wrap(data=None):
try:
return fn(cursor=cursor,data=data)
#conn.commit()
except Exception ,e:
conn.rollback()
log.error('%s, transaction rollback',e)
finally:
cursor.close()
conn.close()
return wrap
class DB():
#templateFN
def insertTest(self,cursor,data=None):
data = {
'field':'this is a test',
'name':'this is a name'
}
return cursor.execute('insert into test(field,name) values(%(field)s,%(name)s)',data)
return DB()
db = DBV()
print 'return value',db.insertTest(data="ok")
Traceback (most recent call last):
File "D:\WorkSpaces\Aptana Studio 3 Workspace\VLuck\src\com\test.py", line 164, in
print 'return value',db.insertTest(data="ok")
TypeError: wrap() got multiple values for keyword argument 'data'
but failed,how should I do it right
Here's a solution I came up with, inspired by another answer:
def connect_mysql(func):
# Assign value of 'self' to be default func
func.func_defaults = func.func_defaults[:-1] + (func,)
func._cnx = mysql.connector.connect(**CONFIG)
func._cursor = func._cnx.cursor()
return func
#connect_mysql
def test(data, self=None):
self._cursor.execute("SELECT %(c1)s", data)
print(self._cursor.fetchall())
test({'c1': 1})

Categories

Resources