I want to make unit tests for some django views which use a custom pyodbc database connection
views.py
from django.http import JsonResponse, HttpResponseNotFound, HttpResponseBadRequest, HttpResponseServerError, HttpResponseForbidden
from django.core.exceptions import SuspiciousOperation
from django.utils.datastructures import MultiValueDictKeyError
import os
import pyodbc
# Create your views here.
db_credentials = os.environ.get('DATABASE_CREDENTIALS')
dbh = pyodbc.connect(db_credentials)
def get_domains(request):
if request.method == 'GET':
args = request.GET
elif request.method == 'POST':
args = request.POST
try:
cursor = dbh.cursor()
if 'owner' in args:
owner = args['owner']
cursor.execute('{call GET_DOMAINS_FOR_OWNER(?)}', owner)
else:
cursor.execute('{call GET_DOMAINS()}')
result = cursor.fetchall()
if(result):
return JsonResponse([row[0] for row in result], safe=False)
else:
return JsonResponse([], safe=False)
except pyodbc.Error as e:
return HttpResponseServerError(e)
except SuspiciousOperation as e:
return HttpResponseForbidden(e)
Since I don't want the unit tests to be hitting the database, how can I mock the behaviour given that:
- The mock library won't work since pyodbc is a Python C extension
- Using sys.modules doesn't seem to work, probably because the module is being used in views.py and not on tests.py
Here is my test driver
tests.py
from django.test import SimpleTestCase
from sms_admin import *
# Create your tests here.
HTTP_OK = 200
HTTP_NOTFOUND = 404
class AdminTestCase(SimpleTestCase):
"""docstring for AdminTestCase"""
def test_get_pool_for_lds(self):
response = self.client.get('/sms_admin/get_pool_for_lds', {'domain': 'sqlconnect', 'stage': 'dev', 'lds': 'reader'})
self.assertEqual(response.content, b'pdss_reader')
self.assertEqual(response.status_code, HTTP_OK)
Aucun commentaire:
Enregistrer un commentaire