I'm trying to set up some tests for a script that will pull data from a MySQL database. I found an example here that looks like what I want to do, but it just gives me an object instead of results:
<MagicMock name='pymysql.connect().cursor[38 chars]152'>
Here is the function I am using (simple.py
):
import pymysql
def get_user_data():
connection = pymysql.connect(host='localhost', user='user', password='password',
db='db', charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
sql = "SELECT `id`, `password` FROM `users`"
cursor.execute(sql)
results = cursor.fetchall()
finally:
connection.close()
return results
And the test:
from unittest import TestCase, mock
import simple
class TestSimple(TestCase):
@mock.patch('simple.pymysql', autospec=True)
def test_get-data(self, mock_pymysql):
mock_cursor = mock.MagicMock()
test_data = [{'password': 'secret', 'id': 1}]
mock_cursor.fetchall.return_value = test_data
mock_pymysql.connect.return_value.__enter__.return_value = mock_cursor
self.assertEqual(test_data, simple.get_user_data())
The results:
AssertionError: [{'id': 1, 'password': 'secret'}] != <MagicMock name='pymysql.connect().cursor[38 chars]840'>
I'm using Python 3.51
Aucun commentaire:
Enregistrer un commentaire