| 6 | |
| 7 | |
| 8 | class TestDictCursor(base.PyMySQLTestCase): |
| 9 | bob = {"name": "bob", "age": 21, "DOB": datetime.datetime(1990, 2, 6, 23, 4, 56)} |
| 10 | jim = {"name": "jim", "age": 56, "DOB": datetime.datetime(1955, 5, 9, 13, 12, 45)} |
| 11 | fred = {"name": "fred", "age": 100, "DOB": datetime.datetime(1911, 9, 12, 1, 1, 1)} |
| 12 | |
| 13 | cursor_type = pymysql.cursors.DictCursor |
| 14 | |
| 15 | def setUp(self): |
| 16 | super().setUp() |
| 17 | self.conn = conn = self.connect() |
| 18 | c = conn.cursor(self.cursor_type) |
| 19 | |
| 20 | # create a table and some data to query |
| 21 | with warnings.catch_warnings(): |
| 22 | warnings.filterwarnings("ignore") |
| 23 | c.execute("drop table if exists dictcursor") |
| 24 | # include in filterwarnings since for unbuffered dict cursor warning for lack of table |
| 25 | # will only be propagated at start of next execute() call |
| 26 | c.execute( |
| 27 | """CREATE TABLE dictcursor (name char(20), age int , DOB datetime)""" |
| 28 | ) |
| 29 | data = [ |
| 30 | ("bob", 21, "1990-02-06 23:04:56"), |
| 31 | ("jim", 56, "1955-05-09 13:12:45"), |
| 32 | ("fred", 100, "1911-09-12 01:01:01"), |
| 33 | ] |
| 34 | c.executemany("insert into dictcursor values (%s,%s,%s)", data) |
| 35 | |
| 36 | def tearDown(self): |
| 37 | c = self.conn.cursor() |
| 38 | c.execute("drop table dictcursor") |
| 39 | super().tearDown() |
| 40 | |
| 41 | def _ensure_cursor_expired(self, cursor): |
| 42 | pass |
| 43 | |
| 44 | def test_DictCursor(self): |
| 45 | bob, jim, fred = self.bob.copy(), self.jim.copy(), self.fred.copy() |
| 46 | # all assert test compare to the structure as would come out from MySQLdb |
| 47 | conn = self.conn |
| 48 | c = conn.cursor(self.cursor_type) |
| 49 | |
| 50 | # try an update which should return no rows |
| 51 | c.execute("update dictcursor set age=20 where name='bob'") |
| 52 | bob["age"] = 20 |
| 53 | # pull back the single row dict for bob and check |
| 54 | c.execute("SELECT * from dictcursor where name='bob'") |
| 55 | r = c.fetchone() |
| 56 | self.assertEqual(bob, r, "fetchone via DictCursor failed") |
| 57 | self._ensure_cursor_expired(c) |
| 58 | |
| 59 | # same again, but via fetchall => tuple) |
| 60 | c.execute("SELECT * from dictcursor where name='bob'") |
| 61 | r = c.fetchall() |
| 62 | self.assertEqual( |
| 63 | [bob], r, "fetch a 1 row result via fetchall failed via DictCursor" |
| 64 | ) |
| 65 | # same test again but iterate over the |
nothing calls this directly
no outgoing calls
no test coverage detected