Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
singlestore-labs
GitHub Repository: singlestore-labs/singlestoredb-python
Path: blob/main/singlestoredb/mysql/tests/test_DictCursor.py
469 views
1
# type: ignore
2
import datetime
3
import warnings
4
5
import pytest
6
7
import singlestoredb.mysql.cursors as cursors
8
from singlestoredb.mysql.tests import base
9
10
11
class TestDictCursor(base.PyMySQLTestCase):
12
13
bob = {'name': 'bob', 'age': 21, 'DOB': datetime.datetime(1990, 2, 6, 23, 4, 56)}
14
jim = {'name': 'jim', 'age': 56, 'DOB': datetime.datetime(1955, 5, 9, 13, 12, 45)}
15
fred = {'name': 'fred', 'age': 100, 'DOB': datetime.datetime(1911, 9, 12, 1, 1, 1)}
16
17
cursor_type = cursors.DictCursor
18
19
def setUp(self):
20
super(TestDictCursor, self).setUp()
21
self.conn = conn = self.connect(cursorclass=self.cursor_type)
22
c = conn.cursor()
23
24
# create a table ane some data to query
25
with warnings.catch_warnings():
26
warnings.filterwarnings('ignore')
27
c.execute('drop table if exists dictcursor')
28
# include in filterwarnings since for unbuffered dict cursor warning
29
# for lack of table will only be propagated at start of next execute() call
30
c.execute(
31
"""CREATE TABLE dictcursor (name char(20), age int , DOB datetime)""",
32
)
33
data = [
34
('bob', 21, '1990-02-06 23:04:56'),
35
('jim', 56, '1955-05-09 13:12:45'),
36
('fred', 100, '1911-09-12 01:01:01'),
37
]
38
c.executemany('insert into dictcursor values (%s,%s,%s)', data)
39
40
def tearDown(self):
41
c = self.conn.cursor()
42
c.execute('drop table dictcursor')
43
super(TestDictCursor, self).tearDown()
44
45
def _ensure_cursor_expired(self, cursor):
46
pass
47
48
def test_DictCursor(self):
49
bob, jim, fred = self.bob.copy(), self.jim.copy(), self.fred.copy()
50
# all assert test compare to the structure as would come out from MySQLdb
51
conn = self.conn
52
c = conn.cursor()
53
54
# try an update which should return no rows
55
c.execute("update dictcursor set age=20 where name='bob'")
56
bob['age'] = 20
57
# pull back the single row dict for bob and check
58
c.execute("SELECT * from dictcursor where name='bob'")
59
r = c.fetchone()
60
self.assertEqual(bob, r, 'fetchone via DictCursor failed')
61
self._ensure_cursor_expired(c)
62
63
# same again, but via fetchall => tuple)
64
c.execute("SELECT * from dictcursor where name='bob'")
65
r = c.fetchall()
66
self.assertEqual(
67
[bob], r, 'fetch a 1 row result via fetchall failed via DictCursor',
68
)
69
# same test again but iterate over the
70
c.execute("SELECT * from dictcursor where name='bob'")
71
for r in c:
72
self.assertEqual(
73
bob, r, 'fetch a 1 row result via iteration failed via DictCursor',
74
)
75
# get all 3 row via fetchall
76
c.execute('SELECT * from dictcursor ORDER BY name')
77
r = c.fetchall()
78
self.assertEqual([bob, fred, jim], r, 'fetchall failed via DictCursor')
79
# same test again but do a list comprehension
80
c.execute('SELECT * from dictcursor ORDER BY name')
81
r = list(c)
82
self.assertEqual([bob, fred, jim], r, 'DictCursor should be iterable')
83
# get all 2 row via fetchmany
84
c.execute('SELECT * from dictcursor ORDER BY name')
85
r = c.fetchmany(2)
86
self.assertEqual([bob, fred], r, 'fetchmany failed via DictCursor')
87
self._ensure_cursor_expired(c)
88
89
@pytest.mark.skip('Custom cursors are only available when creating a connection')
90
def test_custom_dict(self):
91
class MyDict(dict):
92
pass
93
94
class MyDictCursor(self.cursor_type):
95
dict_type = MyDict
96
97
keys = ['name', 'age', 'DOB']
98
bob = MyDict([(k, self.bob[k]) for k in keys])
99
jim = MyDict([(k, self.jim[k]) for k in keys])
100
fred = MyDict([(k, self.fred[k]) for k in keys])
101
102
cur = self.conn.cursor(MyDictCursor)
103
cur.execute("SELECT * FROM dictcursor WHERE name='bob'")
104
r = cur.fetchone()
105
self.assertEqual(bob, r, 'fetchone() returns MyDictCursor')
106
self._ensure_cursor_expired(cur)
107
108
cur.execute('SELECT * FROM dictcursor ORDER BY name')
109
r = cur.fetchall()
110
self.assertEqual([bob, fred, jim], r, 'fetchall failed via MyDictCursor')
111
112
cur.execute('SELECT * FROM dictcursor ORDER BY name')
113
r = list(cur)
114
self.assertEqual([bob, fred, jim], r, 'list failed via MyDictCursor')
115
116
cur.execute('SELECT * FROM dictcursor ORDER BY name')
117
r = cur.fetchmany(2)
118
self.assertEqual([bob, fred], r, 'list failed via MyDictCursor')
119
self._ensure_cursor_expired(cur)
120
121
122
class TestSSDictCursor(TestDictCursor):
123
cursor_type = cursors.SSDictCursor
124
125
def _ensure_cursor_expired(self, cursor):
126
list(cursor.fetchall_unbuffered())
127
128
129
if __name__ == '__main__':
130
import unittest
131
132
unittest.main()
133
134