Path: blob/main/singlestoredb/mysql/tests/test_DictCursor.py
469 views
# type: ignore1import datetime2import warnings34import pytest56import singlestoredb.mysql.cursors as cursors7from singlestoredb.mysql.tests import base8910class TestDictCursor(base.PyMySQLTestCase):1112bob = {'name': 'bob', 'age': 21, 'DOB': datetime.datetime(1990, 2, 6, 23, 4, 56)}13jim = {'name': 'jim', 'age': 56, 'DOB': datetime.datetime(1955, 5, 9, 13, 12, 45)}14fred = {'name': 'fred', 'age': 100, 'DOB': datetime.datetime(1911, 9, 12, 1, 1, 1)}1516cursor_type = cursors.DictCursor1718def setUp(self):19super(TestDictCursor, self).setUp()20self.conn = conn = self.connect(cursorclass=self.cursor_type)21c = conn.cursor()2223# create a table ane some data to query24with warnings.catch_warnings():25warnings.filterwarnings('ignore')26c.execute('drop table if exists dictcursor')27# include in filterwarnings since for unbuffered dict cursor warning28# for lack of table will only be propagated at start of next execute() call29c.execute(30"""CREATE TABLE dictcursor (name char(20), age int , DOB datetime)""",31)32data = [33('bob', 21, '1990-02-06 23:04:56'),34('jim', 56, '1955-05-09 13:12:45'),35('fred', 100, '1911-09-12 01:01:01'),36]37c.executemany('insert into dictcursor values (%s,%s,%s)', data)3839def tearDown(self):40c = self.conn.cursor()41c.execute('drop table dictcursor')42super(TestDictCursor, self).tearDown()4344def _ensure_cursor_expired(self, cursor):45pass4647def test_DictCursor(self):48bob, jim, fred = self.bob.copy(), self.jim.copy(), self.fred.copy()49# all assert test compare to the structure as would come out from MySQLdb50conn = self.conn51c = conn.cursor()5253# try an update which should return no rows54c.execute("update dictcursor set age=20 where name='bob'")55bob['age'] = 2056# pull back the single row dict for bob and check57c.execute("SELECT * from dictcursor where name='bob'")58r = c.fetchone()59self.assertEqual(bob, r, 'fetchone via DictCursor failed')60self._ensure_cursor_expired(c)6162# same again, but via fetchall => tuple)63c.execute("SELECT * from dictcursor where name='bob'")64r = c.fetchall()65self.assertEqual(66[bob], r, 'fetch a 1 row result via fetchall failed via DictCursor',67)68# same test again but iterate over the69c.execute("SELECT * from dictcursor where name='bob'")70for r in c:71self.assertEqual(72bob, r, 'fetch a 1 row result via iteration failed via DictCursor',73)74# get all 3 row via fetchall75c.execute('SELECT * from dictcursor ORDER BY name')76r = c.fetchall()77self.assertEqual([bob, fred, jim], r, 'fetchall failed via DictCursor')78# same test again but do a list comprehension79c.execute('SELECT * from dictcursor ORDER BY name')80r = list(c)81self.assertEqual([bob, fred, jim], r, 'DictCursor should be iterable')82# get all 2 row via fetchmany83c.execute('SELECT * from dictcursor ORDER BY name')84r = c.fetchmany(2)85self.assertEqual([bob, fred], r, 'fetchmany failed via DictCursor')86self._ensure_cursor_expired(c)8788@pytest.mark.skip('Custom cursors are only available when creating a connection')89def test_custom_dict(self):90class MyDict(dict):91pass9293class MyDictCursor(self.cursor_type):94dict_type = MyDict9596keys = ['name', 'age', 'DOB']97bob = MyDict([(k, self.bob[k]) for k in keys])98jim = MyDict([(k, self.jim[k]) for k in keys])99fred = MyDict([(k, self.fred[k]) for k in keys])100101cur = self.conn.cursor(MyDictCursor)102cur.execute("SELECT * FROM dictcursor WHERE name='bob'")103r = cur.fetchone()104self.assertEqual(bob, r, 'fetchone() returns MyDictCursor')105self._ensure_cursor_expired(cur)106107cur.execute('SELECT * FROM dictcursor ORDER BY name')108r = cur.fetchall()109self.assertEqual([bob, fred, jim], r, 'fetchall failed via MyDictCursor')110111cur.execute('SELECT * FROM dictcursor ORDER BY name')112r = list(cur)113self.assertEqual([bob, fred, jim], r, 'list failed via MyDictCursor')114115cur.execute('SELECT * FROM dictcursor ORDER BY name')116r = cur.fetchmany(2)117self.assertEqual([bob, fred], r, 'list failed via MyDictCursor')118self._ensure_cursor_expired(cur)119120121class TestSSDictCursor(TestDictCursor):122cursor_type = cursors.SSDictCursor123124def _ensure_cursor_expired(self, cursor):125list(cursor.fetchall_unbuffered())126127128if __name__ == '__main__':129import unittest130131unittest.main()132133134