|
| 1 | +import copy |
| 2 | +import asyncio |
| 3 | + |
| 4 | +import aiomysql.cursors |
| 5 | +from tests import base |
| 6 | +from tests._testutils import run_until_complete |
| 7 | + |
| 8 | + |
| 9 | +class TestDeserializeCursor(base.AIOPyMySQLTestCase): |
| 10 | + bob = ("bob", 21, {"k1": "pretty", "k2": [18, 25]}) |
| 11 | + jim = ("jim", 56, {"k1": "rich", "k2": [20, 60]}) |
| 12 | + fred = ("fred", 100, {"k1": "longevity", "k2": [100, 160]}) |
| 13 | + havejson = True |
| 14 | + |
| 15 | + cursor_type = aiomysql.cursors.DeserializationCursor |
| 16 | + |
| 17 | + def setUp(self): |
| 18 | + super(TestDeserializeCursor, self).setUp() |
| 19 | + self.conn = conn = self.connections[0] |
| 20 | + |
| 21 | + @asyncio.coroutine |
| 22 | + def prepare(): |
| 23 | + c = yield from conn.cursor(self.cursor_type) |
| 24 | + |
| 25 | + # create a table ane some data to query |
| 26 | + yield from c.execute("drop table if exists deserialize_cursor") |
| 27 | + yield from c.execute("select VERSION()") |
| 28 | + v = yield from c.fetchone() |
| 29 | + version, *db_type = v[0].split('-', 1) |
| 30 | + version = float(".".join(version.split('.', 2)[:2])) |
| 31 | + ismariadb = db_type and 'mariadb' in db_type[0].lower() |
| 32 | + if ismariadb or version < 5.7: |
| 33 | + yield from c.execute( |
| 34 | + """CREATE TABLE deserialize_cursor |
| 35 | + (name char(20), age int , claim text)""") |
| 36 | + self.havejson = False |
| 37 | + else: |
| 38 | + yield from c.execute( |
| 39 | + """CREATE TABLE deserialize_cursor |
| 40 | + (name char(20), age int , claim json)""") |
| 41 | + data = [("bob", 21, '{"k1": "pretty", "k2": [18, 25]}'), |
| 42 | + ("jim", 56, '{"k1": "rich", "k2": [20, 60]}'), |
| 43 | + ("fred", 100, '{"k1": "longevity", "k2": [100, 160]}')] |
| 44 | + yield from c.executemany("insert into deserialize_cursor values " |
| 45 | + "(%s,%s,%s)", |
| 46 | + data) |
| 47 | + |
| 48 | + self.loop.run_until_complete(prepare()) |
| 49 | + |
| 50 | + def tearDown(self): |
| 51 | + @asyncio.coroutine |
| 52 | + def shutdown(): |
| 53 | + c = yield from self.conn.cursor() |
| 54 | + yield from c.execute("drop table deserialize_cursor;") |
| 55 | + |
| 56 | + self.loop.run_until_complete(shutdown()) |
| 57 | + super(TestDeserializeCursor, self).tearDown() |
| 58 | + |
| 59 | + @run_until_complete |
| 60 | + def test_deserialize_cursor(self): |
| 61 | + if not self.havejson: |
| 62 | + return |
| 63 | + bob, jim, fred = copy.deepcopy(self.bob), copy.deepcopy( |
| 64 | + self.jim), copy.deepcopy(self.fred) |
| 65 | + # all assert test compare to the structure as would come |
| 66 | + # out from MySQLdb |
| 67 | + conn = self.conn |
| 68 | + c = yield from conn.cursor(self.cursor_type) |
| 69 | + |
| 70 | + # pull back the single row dict for bob and check |
| 71 | + yield from c.execute("SELECT * from deserialize_cursor " |
| 72 | + "where name='bob'") |
| 73 | + r = yield from c.fetchone() |
| 74 | + self.assertEqual(bob, r, "fetchone via DeserializeCursor failed") |
| 75 | + # same again, but via fetchall => tuple) |
| 76 | + yield from c.execute("SELECT * from deserialize_cursor " |
| 77 | + "where name='bob'") |
| 78 | + r = yield from c.fetchall() |
| 79 | + self.assertEqual([bob], r, |
| 80 | + "fetch a 1 row result via fetchall failed via " |
| 81 | + "DeserializeCursor") |
| 82 | + # get all 3 row via fetchall |
| 83 | + yield from c.execute("SELECT * from deserialize_cursor") |
| 84 | + r = yield from c.fetchall() |
| 85 | + self.assertEqual([bob, jim, fred], r, |
| 86 | + "fetchall failed via DictCursor") |
| 87 | + |
| 88 | + # get all 2 row via fetchmany |
| 89 | + yield from c.execute("SELECT * from deserialize_cursor") |
| 90 | + r = yield from c.fetchmany(2) |
| 91 | + self.assertEqual([bob, jim], r, "fetchmany failed via DictCursor") |
| 92 | + yield from c.execute('commit') |
| 93 | + |
| 94 | + @run_until_complete |
| 95 | + def test_deserialize_cursor_low_version(self): |
| 96 | + if self.havejson: |
| 97 | + return |
| 98 | + bob = ("bob", 21, '{"k1": "pretty", "k2": [18, 25]}') |
| 99 | + jim = ("jim", 56, '{"k1": "rich", "k2": [20, 60]}') |
| 100 | + fred = ("fred", 100, '{"k1": "longevity", "k2": [100, 160]}') |
| 101 | + # all assert test compare to the structure as would come |
| 102 | + # out from MySQLdb |
| 103 | + conn = self.conn |
| 104 | + c = yield from conn.cursor(self.cursor_type) |
| 105 | + |
| 106 | + # pull back the single row dict for bob and check |
| 107 | + yield from c.execute("SELECT * from deserialize_cursor " |
| 108 | + "where name='bob'") |
| 109 | + r = yield from c.fetchone() |
| 110 | + self.assertEqual(bob, r, "fetchone via DeserializeCursor failed") |
| 111 | + # same again, but via fetchall => tuple) |
| 112 | + yield from c.execute("SELECT * from deserialize_cursor " |
| 113 | + "where name='bob'") |
| 114 | + r = yield from c.fetchall() |
| 115 | + self.assertEqual([bob], r, |
| 116 | + "fetch a 1 row result via fetchall failed via " |
| 117 | + "DeserializeCursor") |
| 118 | + # get all 3 row via fetchall |
| 119 | + yield from c.execute("SELECT * from deserialize_cursor") |
| 120 | + r = yield from c.fetchall() |
| 121 | + self.assertEqual([bob, jim, fred], r, |
| 122 | + "fetchall failed via DictCursor") |
| 123 | + |
| 124 | + # get all 2 row via fetchmany |
| 125 | + yield from c.execute("SELECT * from deserialize_cursor") |
| 126 | + r = yield from c.fetchmany(2) |
| 127 | + self.assertEqual([bob, jim], r, "fetchmany failed via DictCursor") |
| 128 | + yield from c.execute('commit') |
| 129 | + |
| 130 | + @run_until_complete |
| 131 | + def test_deserializedictcursor(self): |
| 132 | + if not self.havejson: |
| 133 | + return |
| 134 | + bob = {'name': 'bob', 'age': 21, |
| 135 | + 'claim': {"k1": "pretty", "k2": [18, 25]}} |
| 136 | + conn = self.conn |
| 137 | + c = yield from conn.cursor(aiomysql.cursors.DeserializationCursor, |
| 138 | + aiomysql.cursors.DictCursor) |
| 139 | + yield from c.execute("SELECT * from deserialize_cursor " |
| 140 | + "where name='bob'") |
| 141 | + r = yield from c.fetchall() |
| 142 | + self.assertEqual([bob], r, |
| 143 | + "fetch a 1 row result via fetchall failed via " |
| 144 | + "DeserializationCursor") |
| 145 | + |
| 146 | + @run_until_complete |
| 147 | + def test_ssdeserializecursor(self): |
| 148 | + if not self.havejson: |
| 149 | + return |
| 150 | + conn = self.conn |
| 151 | + c = yield from conn.cursor(aiomysql.cursors.SSCursor, |
| 152 | + aiomysql.cursors.DeserializationCursor) |
| 153 | + yield from c.execute("SELECT * from deserialize_cursor " |
| 154 | + "where name='bob'") |
| 155 | + r = yield from c.fetchall() |
| 156 | + self.assertEqual([self.bob], r, |
| 157 | + "fetch a 1 row result via fetchall failed via " |
| 158 | + "DeserializationCursor") |
| 159 | + |
| 160 | + @run_until_complete |
| 161 | + def test_ssdeserializedictcursor(self): |
| 162 | + if not self.havejson: |
| 163 | + return |
| 164 | + bob = {'name': 'bob', 'age': 21, |
| 165 | + 'claim': {"k1": "pretty", "k2": [18, 25]}} |
| 166 | + conn = self.conn |
| 167 | + c = yield from conn.cursor(aiomysql.cursors.SSCursor, |
| 168 | + aiomysql.cursors.DeserializationCursor, |
| 169 | + aiomysql.cursors.DictCursor) |
| 170 | + yield from c.execute("SELECT * from deserialize_cursor " |
| 171 | + "where name='bob'") |
| 172 | + r = yield from c.fetchall() |
| 173 | + self.assertEqual([bob], r, |
| 174 | + "fetch a 1 row result via fetchall failed via " |
| 175 | + "DeserializationCursor") |
0 commit comments