|
12 | 12 |
|
13 | 13 | __all__ = ["TestConversion", "TestCursor", "TestBulkInserts"]
|
14 | 14 |
|
15 |
| - |
16 | 15 | class TestConversion(base.PyCovenantSQLTestCase):
|
17 | 16 | def test_datatypes(self):
|
18 | 17 | """ test every data type """
|
19 | 18 | conn = self.connections[0]
|
20 | 19 | c = conn.cursor()
|
21 |
| - c.execute("create table test_datatypes (b bit, i int, l bigint, f real, s varchar(32), u varchar(32), bb blob, d date, dt datetime, ts timestamp, td time, t time, st datetime)") |
22 |
| - try: |
23 |
| - # insert values |
24 |
| - |
25 |
| - v = (True, -3, 123456789012, 5.7, "hello'\" world", u"Espa\xc3\xb1ol", "binary\x00data".encode(conn.encoding), datetime.date(1988,2,2), datetime.datetime(2014, 5, 15, 7, 45, 57), datetime.timedelta(5,6), datetime.time(16,32), time.localtime()) |
26 |
| - c.execute("insert into test_datatypes (b,i,l,f,s,u,bb,d,dt,td,t,st) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", v) |
27 |
| - c.execute("select b,i,l,f,s,u,bb,d,dt,td,t,st from test_datatypes") |
28 |
| - r = c.fetchone() |
29 |
| - self.assertEqual(0, r[0]) |
30 |
| - self.assertEqual(v[1:10], r[1:10]) |
31 |
| - self.assertEqual(datetime.timedelta(0, 60 * (v[10].hour * 60 + v[10].minute)), r[10]) |
32 |
| - self.assertEqual(datetime.datetime(*v[-1][:6]), r[-1]) |
33 |
| - |
34 |
| - c.execute("delete from test_datatypes") |
35 |
| - |
36 |
| - # check nulls |
37 |
| - c.execute("insert into test_datatypes (b,i,l,f,s,u,bb,d,dt,td,t,st) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", [None] * 12) |
38 |
| - c.execute("select b,i,l,f,s,u,bb,d,dt,td,t,st from test_datatypes") |
39 |
| - r = c.fetchone() |
40 |
| - self.assertEqual(tuple([None] * 12), r) |
41 | 20 |
|
| 21 | + self.safe_create_table( |
| 22 | + conn, "test_datatypes","create table test_datatypes (b bit, i int, l bigint, f real, s varchar(32), u varchar(32), bb blob, d date, dt datetime, ts timestamp, td time, t time, st datetime)") |
| 23 | + |
| 24 | + # insert values |
| 25 | + v = (True, -3, 123456789012, 5.7, "hello'\" world", u"Espa\xc3\xb1ol", "binary\x00data".encode(conn.encoding), datetime.date(1988,2,2), datetime.datetime(2014, 5, 15, 7, 45, 57), datetime.timedelta(5,6), datetime.time(16,32), time.localtime()) |
| 26 | + c.execute("insert into test_datatypes (b,i,l,f,s,u,bb,d,dt,td,t,st) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", v) |
| 27 | + c.execute("select b,i,l,f,s,u,bb,d,dt,td,t,st from test_datatypes") |
| 28 | + r = c.fetchone() |
| 29 | + self.assertEqual(0, r[0]) |
| 30 | + self.assertEqual(v[1:10], r[1:10]) |
| 31 | + self.assertEqual(datetime.timedelta(0, 60 * (v[10].hour * 60 + v[10].minute)), r[10]) |
| 32 | + self.assertEqual(datetime.datetime(*v[-1][:6]), r[-1]) |
| 33 | + |
| 34 | + c.execute("delete from test_datatypes") |
| 35 | + |
| 36 | + # check nulls |
| 37 | + c.execute("insert into test_datatypes (b,i,l,f,s,u,bb,d,dt,td,t,st) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", [None] * 12) |
| 38 | + c.execute("select b,i,l,f,s,u,bb,d,dt,td,t,st from test_datatypes") |
| 39 | + r = c.fetchone() |
| 40 | + self.assertEqual(tuple([None] * 12), r) |
| 41 | + |
| 42 | + c.execute("delete from test_datatypes") |
| 43 | + |
| 44 | + # check sequences type |
| 45 | + for seq_type in (tuple, list, set, frozenset): |
| 46 | + c.execute("insert into test_datatypes (i, l) values (2,4), (6,8), (10,12)") |
| 47 | + seq = seq_type([2,6]) |
| 48 | + c.execute("select l from test_datatypes where i in %s order by i", (seq,)) |
| 49 | + r = c.fetchall() |
| 50 | + self.assertEqual(((4,),(8,)), r) |
42 | 51 | c.execute("delete from test_datatypes")
|
43 | 52 |
|
44 |
| - # check sequences type |
45 |
| - for seq_type in (tuple, list, set, frozenset): |
46 |
| - c.execute("insert into test_datatypes (i, l) values (2,4), (6,8), (10,12)") |
47 |
| - seq = seq_type([2,6]) |
48 |
| - c.execute("select l from test_datatypes where i in %s order by i", (seq,)) |
49 |
| - r = c.fetchall() |
50 |
| - self.assertEqual(((4,),(8,)), r) |
51 |
| - c.execute("delete from test_datatypes") |
52 |
| - |
53 |
| - finally: |
54 |
| - c.execute("drop table test_datatypes") |
| 53 | + c.close() |
55 | 54 |
|
56 | 55 | def test_dict(self):
|
57 | 56 | """ test dict escaping """
|
58 | 57 | conn = self.connections[0]
|
59 | 58 | c = conn.cursor()
|
60 |
| - c.execute("create table test_dict (a integer, b integer, c integer)") |
61 |
| - try: |
62 |
| - c.execute("insert into test_dict (a,b,c) values (%(a)s, %(b)s, %(c)s)", {"a":1,"b":2,"c":3}) |
63 |
| - c.execute("select a,b,c from test_dict") |
64 |
| - self.assertEqual((1,2,3), c.fetchone()) |
65 |
| - finally: |
66 |
| - c.execute("drop table test_dict") |
| 59 | + self.safe_create_table( |
| 60 | + conn, "test_dict", "create table test_dict (a integer, b integer, c integer)") |
| 61 | + c.execute("insert into test_dict (a,b,c) values (%(a)s, %(b)s, %(c)s)", {"a":1,"b":2,"c":3}) |
| 62 | + c.execute("select a,b,c from test_dict") |
| 63 | + self.assertEqual((1,2,3), c.fetchone()) |
| 64 | + c.close() |
67 | 65 |
|
68 | 66 | def test_string(self):
|
69 | 67 | conn = self.connections[0]
|
70 | 68 | c = conn.cursor()
|
71 |
| - c.execute("create table test_dict (a text)") |
| 69 | + self.safe_create_table( |
| 70 | + conn, "test_string", "create table test_string (a text)") |
72 | 71 | test_value = "I am a test string"
|
73 |
| - try: |
74 |
| - c.execute("insert into test_dict (a) values (%s)", test_value) |
75 |
| - c.execute("select a from test_dict") |
76 |
| - self.assertEqual((test_value,), c.fetchone()) |
77 |
| - finally: |
78 |
| - c.execute("drop table test_dict") |
| 72 | + |
| 73 | + c.execute("insert into test_string (a) values (%s)", test_value) |
| 74 | + c.execute("select a from test_string") |
| 75 | + self.assertEqual((test_value,), c.fetchone()) |
| 76 | + |
| 77 | + c.close() |
79 | 78 |
|
80 | 79 | def test_integer(self):
|
81 | 80 | conn = self.connections[0]
|
82 | 81 | c = conn.cursor()
|
83 |
| - c.execute("create table test_dict (a integer)") |
| 82 | + |
| 83 | + self.safe_create_table( |
| 84 | + conn, "test_integer", "create table test_integer (a integer)") |
84 | 85 | test_value = 12345
|
85 |
| - try: |
86 |
| - c.execute("insert into test_dict (a) values (%s)", test_value) |
87 |
| - c.execute("select a from test_dict") |
88 |
| - self.assertEqual((test_value,), c.fetchone()) |
89 |
| - finally: |
90 |
| - c.execute("drop table test_dict") |
| 86 | + c.execute("insert into test_integer (a) values (%s)", test_value) |
| 87 | + c.execute("select a from test_integer") |
| 88 | + self.assertEqual((test_value,), c.fetchone()) |
| 89 | + c.close() |
91 | 90 |
|
92 | 91 | # TODO support binary
|
93 | 92 | # def test_binary(self):
|
@@ -143,16 +142,16 @@ def test_datetime_microseconds(self):
|
143 | 142 | conn = self.connections[0]
|
144 | 143 | c = conn.cursor()
|
145 | 144 | dt = datetime.datetime(2013, 11, 12, 9, 9, 9, 123450)
|
146 |
| - c.execute("create table test_datetime (id int, ts datetime(6))") |
147 |
| - try: |
148 |
| - c.execute( |
149 |
| - "insert into test_datetime values (%s, %s)", |
150 |
| - (1, dt) |
151 |
| - ) |
152 |
| - c.execute("select ts from test_datetime") |
153 |
| - self.assertEqual((dt,), c.fetchone()) |
154 |
| - finally: |
155 |
| - c.execute("drop table test_datetime") |
| 145 | + self.safe_create_table( |
| 146 | + conn, "test_datetime", "create table test_datetime (id int, ts datetime(6))") |
| 147 | + c.execute( |
| 148 | + "insert into test_datetime values (%s, %s)", |
| 149 | + (1, dt) |
| 150 | + ) |
| 151 | + c.execute("select ts from test_datetime") |
| 152 | + self.assertEqual((dt,), c.fetchone()) |
| 153 | + |
| 154 | + c.close() |
156 | 155 |
|
157 | 156 |
|
158 | 157 | class TestCursor(base.PyCovenantSQLTestCase):
|
@@ -213,27 +212,25 @@ def test_fetch_no_result(self):
|
213 | 212 | """ test a fetchone() with no rows """
|
214 | 213 | conn = self.connections[0]
|
215 | 214 | c = conn.cursor()
|
216 |
| - c.execute("create table test_nr (b varchar(32))") |
217 |
| - try: |
218 |
| - data = "pycovenantsql" |
219 |
| - c.execute("insert into test_nr (b) values (%s)", (data,)) |
220 |
| - self.assertEqual(None, c.fetchone()) |
221 |
| - finally: |
222 |
| - c.execute("drop table test_nr") |
| 215 | + self.safe_create_table( |
| 216 | + conn, "test_nr", "create table test_nr (b varchar(32))") |
| 217 | + data = "pycovenantsql" |
| 218 | + c.execute("insert into test_nr (b) values (%s)", (data,)) |
| 219 | + self.assertEqual(None, c.fetchone()) |
| 220 | + c.close() |
223 | 221 |
|
224 | 222 | def test_aggregates(self):
|
225 | 223 | """ test aggregate functions """
|
226 | 224 | conn = self.connections[0]
|
227 | 225 | c = conn.cursor()
|
228 |
| - try: |
229 |
| - c.execute('create table test_aggregates (i integer)') |
230 |
| - for i in range(0, 10): |
231 |
| - c.execute('insert into test_aggregates (i) values (%s)', (i,)) |
232 |
| - c.execute('select sum(i) from test_aggregates') |
233 |
| - r, = c.fetchone() |
234 |
| - self.assertEqual(sum(range(0,10)), r) |
235 |
| - finally: |
236 |
| - c.execute('drop table test_aggregates') |
| 226 | + self.safe_create_table( |
| 227 | + conn, "test_aggregates", "create table test_aggregates (i integer)") |
| 228 | + for i in range(0, 10): |
| 229 | + c.execute('insert into test_aggregates (i) values (%s)', (i,)) |
| 230 | + c.execute('select sum(i) from test_aggregates') |
| 231 | + r, = c.fetchone() |
| 232 | + self.assertEqual(sum(range(0,10)), r) |
| 233 | + c.close() |
237 | 234 |
|
238 | 235 | def test_single_tuple(self):
|
239 | 236 | """ test a single tuple """
|
|
0 commit comments