How to mock sqlite3.connect in Python - unit-testing

I am using Python 3.3, under the Anaconda environment.
I would like to mock sqlite3.connect. For example in MyTests (see below), I would like test_sqlite3_connect to return the string connection rather than an actual sqlite3.Connection object.
I have tried patching it, but that does not work.
from unittest.mock import patch
import unittest
import sqlite3
#patch('sqlite3.connect')
def sqlite3_connect(self,connection_string):
print('connect with : {0}'.format(connection_string))
return 'connection '
class MyTests(unittest.TestCase):
def test_sqlite3_connect(self):
print('testing connection')
dbc = DataBaseClass()
class DataBaseClass():
def __init__(self):
print('initialising database class')
self.connection = sqlite3.connect('test database')

I managed to solve the question using information from the Quick Guide section of
http://www.voidspace.org.uk/python/mock/
The following code shows two ways of mocking sqlite3.connect.
''' An example of how to mock the sqlite3.connection method '''
from unittest.mock import MagicMock,Mock
import unittest
import sqlite3
class MyTests(unittest.TestCase):
def test_sqlite3_connect_success(self):
sqlite3.connect = MagicMock(return_value='connection succeeded')
dbc = DataBaseClass()
sqlite3.connect.assert_called_with('test_database')
self.assertEqual(dbc.connection,'connection succeeded')
def test_sqlite3_connect_fail(self):
sqlite3.connect = MagicMock(return_value='connection failed')
dbc = DataBaseClass()
sqlite3.connect.assert_called_with('test_database')
self.assertEqual(dbc.connection, 'connection failed')
def test_sqlite3_connect_with_sideaffect(self):
self._setup_mock_sqlite3_connect()
dbc = DataBaseClass('good_connection_string')
self.assertTrue(dbc.connection)
sqlite3.connect.assert_called_with('good_connection_string')
dbc = DataBaseClass('bad_connection_string')
self.assertFalse(dbc.connection)
sqlite3.connect.assert_called_with('bad_connection_string')
def _setup_mock_sqlite3_connect(self):
values = {'good_connection_string':True,
'bad_connection_string':False}
def side_effect(arg):
return values[arg]
sqlite3.connect = Mock(side_effect=side_effect)
class DataBaseClass():
def __init__(self,connection_string='test_database'):
self.connection = sqlite3.connect(connection_string)

Related

How to test a non-view function in a Flask app factory?

I'm using the factory pattern in Flask. This is a simplified version of my code:
def create_app():
app = Flask(__name__)
#app.before_request
def update_last_seen():
if current_user.is_authenticated:
current_user.update(last_seen=arrow.utcnow().datetime)
#app.route('/', methods=['GET'])
def home():
return render_template("home.html")
return app
I'm using pytest-flask and I would like to be able to write a test for the update_last_seen function.
How can I access that function? I can't find it in client.application (client being a fixture auto-used through pytest-flask), nor in my app fixture that I set through conftest.py like so:
#pytest.fixture
def app():
os.environ["FLASK_ENV"] = "test"
os.environ["MONGO_DB"] = "test"
os.environ["MONGO_URI"] = 'mongomock://localhost'
app = create_app()
app.config['ENV'] = 'test'
app.config['DEBUG'] = True
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
return app
So when I run this test:
def test_previous_visit_is_stored_in_session(app, client):
app.update_last_seen()
The error I get is:
def test_previous_visit_is_stored_in_session(app, client):
> app.update_last_seen()
E AttributeError: 'Flask' object has no attribute 'update_last_seen'
I've been looking through app.before_request_funcs too, but to no avail unfortunately.
Referring to the docs, you can manually run the pre-processing of a request.
initial_last_seen = current_user.last_seen
with app.test_request_context('/'):
app.preprocess_request()
assert current_user.last_seen != initial_last_seen # ...for example

Proxy my Elastic Search connection through Django for filtering

I believe I need to proxy my ElasticSearch connection via a Django URL in order to do filtering by user token.
So instead of going via localhost:9200/_search, I want to use localhost:8000/myapi/elastic/_search.
I am unsure how to connect them. I've tried using a serializers/views setup
myapp/search.py
class TaskIndex(DocType):
title = String()
class Meta:
index = 'task-index'
# Bulk indexing function, run in shell
def bulk_indexing():
TaskIndex.init()
es = Elasticsearch()
bulk(client=es, actions=(b.indexing() for b in models.Task.objects.all().iterator()))
# Simple search function
def _search(title):
s = Search().filter('term', title=title.text)
response = s.execute()
return response
api/serializers.py
from myapp.search import TaskIndex
class ElasticSerializer(serializers.ModelSerializer):
class Meta:
model = TaskIndex
api/views.py
class ElasticViewSet(viewsets.ModelViewSet):
queryset = TaskIndex.objects.none()
serializer_class = ElasticSerializer
api/urls.py
router.register(r'elastic', ElasticViewSet)
So this is how I was able to achieve it. Really simple method:
views.py
from services import elastic_result
class ElasticView(APIView):
permission_classes=[]
def post(self, title):
_search = elastic_result(id, title)
return _search
pass
services.py
import requests
import json
def elastic_result(id, request):
requestdata = json.loads(request.body)
r = requests.post('http://localhost:9200/_search', json=requestdata)
items = r.json()
return Response(items)
urls.py
import ElasticView
url(r'^elastic/_search', ElasticView.as_view()

How to perform the function, after all crawling is done in scrapy?

spider_closed() function is not performing. If i give just print statement it is printing but if i perform any function call and return the value it is not working.
import scrapy
import re
from pydispatch import dispatcher
from scrapy import signals
from SouthShore.items import Product
from SouthShore.internalData import internalApi
from scrapy.http import Request
class bestbuycaspider(scrapy.Spider):
name = "bestbuy_dca"
allowed_domains = ["bestbuy.ca"]
start_urls = ["http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+beds",
"http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+night+stand",
"http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+headboard",
"http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+desk",
"http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+bookcase",
"http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+dresser",
"http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+tv+stand",
"http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+armoire",
"http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+kids",
"http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+changing+table",
"http://www.bestbuy.ca/Search/SearchResults.aspx?type=product&page=1&sortBy=relevance&sortDir=desc&pageSize=96&query=south+shore+furniture+baby"]
def __init__(self,jsondetails="",serverdetails="", *args,**kwargs):
super(bestbuycaspider, self).__init__(*args, **kwargs)
dispatcher.connect(self.spider_closed, signal=signals.spider_closed)
self.jsondetails = jsondetails
self.serverdetails=serverdetails
self.data = []
def parse(self,response):
#my stuff here
def spider_closed(self,spider):
print "returning values"
self.results['extractedData']=self.data
print self.results=internalApi(self.jsondetails,self.serverdetails)
yield self.results
1) I want to call some function and return the scraped values
You can create an Item Pipeline with close_spider() method:
class MyPipeline(object):
def close_spider(self, spider):
do_something_here()
Just don't forget to activate it in settings.py as described in the docummentation link above.

Python 3 urlopen context manager mocking

I am new to testing and need some help here.
Assuming having this method:
from urllib.request import urlopen
def get_posts():
with urlopen('some url here') as data:
return json.loads(data.read().decode('utf-8'))
The question is how to test this method (using mock.patch decorator if possible)?
What I have now:
#mock.patch('mymodule.urlopen')
def test_get_post(self, mocked_urlopen):
mocked_urlopen.__enter__ = Mock(return_value=self.test_data)
mocked_urlopen.__exit__ = Mock(return_value=False)
...
But it does not seem to be working.
P.S. Is there any convenient way to work with data variable (which type is HTTPResponse) in test so it could just be simple string?
I was fighting with this as well, and finally figured it out. (Python 3 syntax):
import urllib.request
import unittest
from unittest.mock import patch, MagicMock
class TestUrlopen(unittest.TestCase):
#patch('urllib.request.urlopen')
def test_cm(self, mock_urlopen):
cm = MagicMock()
cm.getcode.return_value = 200
cm.read.return_value = 'contents'
cm.__enter__.return_value = cm
mock_urlopen.return_value = cm
with urllib.request.urlopen('http://foo') as response:
self.assertEqual(response.getcode(), 200)
self.assertEqual(response.read(), 'contents')
#patch('urllib.request.urlopen')
def test_no_cm(self, mock_urlopen):
cm = MagicMock()
cm.getcode.return_value = 200
cm.read.return_value = 'contents'
mock_urlopen.return_value = cm
response = urllib.request.urlopen('http://foo')
self.assertEqual(response.getcode(), 200)
self.assertEqual(response.read(), 'contents')
response.close()
here is my take on this
from urllib.request import urlopen
from unittest.mock import patch
class Mock():
def __init__(self, request, context):
return None
def read(self):
return self
def decode(self, arg):
return ''
def __iter__(self):
return self
def __next__(self):
raise StopIteration
with patch('urllib.request.urlopen', Mock):
# do whatever over here
with urlopen('some url here') as data is a context manager
Also, a file can be used as a context manager, so a better approach here is to use io.StringIO
import io
import json
import urllib.request
from unittest.mock import patch
def get_posts():
with urllib.request.urlopen('some url here') as data:
return json.load(data)
def test_get_posts():
data = io.StringIO('{"id": 123}')
with patch.object(urllib.request, 'urlopen', return_value=data):
assert get_posts() == {"id": 123}
Ok, so I have written simple class to simulate context manager.
class PatchContextManager:
def __init__(self, method, enter_return, exit_return=False):
self._patched = patch(method)
self._enter_return = enter_return
self._exit_return = exit_return
def __enter__(self):
res = self._patched.__enter__()
res.context = MagicMock()
res.context.__enter__.return_value = self._enter_return
res.context.__exit__.return_value = self._exit_return
res.return_value = res.context
return res
def __exit__(self, type, value, tb):
return self._patched.__exit__()
Usage:
with PatchContextManager('mymodule.method', 'return_string') as mocked:
a = mymodule.method(47) # a == 'return_string'
mocked.assert_called_with(47)
...

Is it possible to use setup_method with fixtures?

I have the following code:
import pytest
#pytest.fixture
def example_smtp():
return "example"
class TestClass(object):
def test_function(self, example_smtp):
# 1
obj = NewObject(example_smtp)
obj.initialize()
print example_smtp
# here may rise exception
some_action()
# 2
# but we have to cleanup
obj.cleanup()
some_action() may raise a exception, so I want to move 1 and 2 to setup_method and teardown_method, but I don't know how to do it.
setup_method allows only two arguments, so I can't use example_smtp in it.
A better approach is to just write a fixture that creates NewObject for you and cleans up afterwards:
import pytest
#pytest.fixture
def example_smtp():
return "example"
class TestClass(object):
#pytest.yield_fixture(autouse=True)
def obj(self):
obj = NewObject(example_smtp)
obj.initialize()
yield obj
obj.cleanup()
def test_function(self, obj, example_smtp):
# use obj here
some_action(obj)
But if you really prefer to have a "setup_method"-like function (perhaps you are initializing several objects which don't appear in your snippet), you can declare an autouse fixture instead:
import pytest
#pytest.fixture
def example_smtp():
return "example"
class TestClass(object):
#pytest.yield_fixture(autouse=True)
def some_setup(self):
self.obj = ...
# ... setup other objects, perhaps
yield
# ... cleanup everything
self.obj.cleanup()
def test_function(self, example_smtp):
some_action(self.obj)
IMO, there's no compelling reason not to use fixtures when using pytest style test classes (IOW, not subclassing unittest.TestCase) because if you want a single method that does all the setup/cleanup for you you can use an autouse fixture.
I have solved that problem with addfinalizer() function from request object.
import pytest
#pytest.fixture
def example_smtp():
return "example"
class TestClass(object):
#pytest.fixture
def obj(self, request, example_smtp):
print 'initialize', example_smtp
def fin():
print 'finalize'
request.addfinalizer(fin)
def test(self, obj):
some_action_raise_error()
Thank you jonrsharpe for information about yield fixtures.