@@ -27,6 +27,7 @@ def test_delete_returns_affected_rows(self):
27
27
def test_insert_returns_last_row_id (self ):
28
28
self .assertEqual (self .db .execute ("INSERT INTO cs50(val) VALUES('foo')" ), 1 )
29
29
self .assertEqual (self .db .execute ("INSERT INTO cs50(val) VALUES('bar')" ), 2 )
30
+ self .assertEqual (self .db .execute ("INSERT INTO cs50(val) VALUES('qux')" ), 3 )
30
31
31
32
def test_select_all (self ):
32
33
self .assertEqual (self .db .execute ("SELECT * FROM cs50" ), [])
@@ -131,55 +132,13 @@ def test_rollback(self):
131
132
def test_identifier_case (self ):
132
133
self .assertIn ("count" , self .db .execute ("SELECT 1 AS count" )[0 ])
133
134
134
- def tearDown (self ):
135
- self .db .execute ("DROP TABLE IF EXISTS cs50" )
136
- self .db .execute ("DROP TABLE IF EXISTS foo" )
137
- self .db .execute ("DROP TABLE IF EXISTS bar" )
138
-
139
- class MySQLTests (SQLTests ):
140
- @classmethod
141
- def setUpClass (self ):
142
- self .
db = SQL (
"mysql://[email protected] /test" )
143
-
144
- def setUp (self ):
145
- self .db .execute ("CREATE TABLE IF NOT EXISTS cs50 (id INTEGER NOT NULL AUTO_INCREMENT, val VARCHAR(16), bin BLOB, PRIMARY KEY (id))" )
146
- self .db .execute ("DELETE FROM cs50" )
147
-
148
- class PostgresTests (SQLTests ):
149
- @classmethod
150
- def setUpClass (self ):
151
- self .
db = SQL (
"postgresql://postgres:[email protected] /test" )
152
-
153
- def setUp (self ):
154
- self .db .execute ("CREATE TABLE IF NOT EXISTS cs50 (id SERIAL PRIMARY KEY, val VARCHAR(16), bin BYTEA)" )
155
- self .db .execute ("DELETE FROM cs50" )
156
-
157
- def test_cte (self ):
158
- self .assertEqual (self .db .execute ("WITH foo AS ( SELECT 1 AS bar ) SELECT bar FROM foo" ), [{"bar" : 1 }])
159
-
160
- def test_postgres_scheme (self ):
161
- db = SQL (
"postgres://postgres:[email protected] /test" )
162
- db .execute ("SELECT 1" )
163
-
164
- class SQLiteTests (SQLTests ):
165
- @classmethod
166
- def setUpClass (self ):
167
- open ("test.db" , "w" ).close ()
168
- self .db = SQL ("sqlite:///test.db" )
169
-
170
- def setUp (self ):
171
- self .db .execute ("CREATE TABLE IF NOT EXISTS cs50 (id INTEGER PRIMARY KEY, val TEXT, bin BLOB)" )
172
- self .db .execute ("DELETE FROM cs50" )
173
-
174
- def test_lastrowid (self ):
175
- self .db .execute ("CREATE TABLE foo(id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)" )
176
- self .assertEqual (self .db .execute ("INSERT INTO foo (firstname, lastname) VALUES('firstname', 'lastname')" ), 1 )
177
- self .assertRaises (ValueError , self .db .execute , "INSERT INTO foo (id, firstname, lastname) VALUES(1, 'firstname', 'lastname')" )
178
- self .assertEqual (self .db .execute ("INSERT OR IGNORE INTO foo (id, firstname, lastname) VALUES(1, 'firstname', 'lastname')" ), None )
135
+ def test_none (self ):
136
+ self .db .execute ("CREATE TABLE foo (val INTEGER)" )
137
+ self .db .execute ("SELECT * FROM foo WHERE val = ?" , None )
179
138
180
139
def test_integrity_constraints (self ):
181
140
self .db .execute ("CREATE TABLE foo(id INTEGER PRIMARY KEY)" )
182
- self .assertEqual ( self . db .execute ("INSERT INTO foo VALUES(1)" ), 1 )
141
+ self .db .execute ("INSERT INTO foo VALUES(1)" )
183
142
self .assertRaises (ValueError , self .db .execute , "INSERT INTO foo VALUES(1)" )
184
143
185
144
def test_foreign_key_support (self ):
@@ -188,7 +147,7 @@ def test_foreign_key_support(self):
188
147
self .assertRaises (ValueError , self .db .execute , "INSERT INTO bar VALUES(50)" )
189
148
190
149
def test_qmark (self ):
191
- self .db .execute ("CREATE TABLE foo (firstname STRING , lastname STRING )" )
150
+ self .db .execute ("CREATE TABLE foo (firstname VARCHAR(255) , lastname VARCHAR(255) )" )
192
151
193
152
self .db .execute ("INSERT INTO foo VALUES (?, 'bar')" , "baz" )
194
153
self .assertEqual (self .db .execute ("SELECT * FROM foo" ), [{"firstname" : "baz" , "lastname" : "bar" }])
@@ -218,7 +177,7 @@ def test_qmark(self):
218
177
self .assertEqual (self .db .execute ("SELECT * FROM foo" ), [{"firstname" : "bar" , "lastname" : "baz" }])
219
178
self .db .execute ("DELETE FROM foo" )
220
179
221
- self .db .execute ("CREATE TABLE bar (firstname STRING )" )
180
+ self .db .execute ("CREATE TABLE bar (firstname VARCHAR(255) )" )
222
181
223
182
self .db .execute ("INSERT INTO bar VALUES (?)" , "baz" )
224
183
self .assertEqual (self .db .execute ("SELECT * FROM bar" ), [{"firstname" : "baz" }])
@@ -242,7 +201,7 @@ def test_qmark(self):
242
201
self .assertRaises (RuntimeError , self .db .execute , "INSERT INTO foo VALUES (?, ?)" , 'bar' , baz = 'baz' )
243
202
244
203
def test_named (self ):
245
- self .db .execute ("CREATE TABLE foo (firstname STRING , lastname STRING )" )
204
+ self .db .execute ("CREATE TABLE foo (firstname VARCHAR(255) , lastname VARCHAR(255) )" )
246
205
247
206
self .db .execute ("INSERT INTO foo VALUES (:baz, 'bar')" , baz = "baz" )
248
207
self .assertEqual (self .db .execute ("SELECT * FROM foo" ), [{"firstname" : "baz" , "lastname" : "bar" }])
@@ -264,7 +223,7 @@ def test_named(self):
264
223
self .assertEqual (self .db .execute ("SELECT * FROM foo" ), [{"firstname" : "bar" , "lastname" : "baz" }])
265
224
self .db .execute ("DELETE FROM foo" )
266
225
267
- self .db .execute ("CREATE TABLE bar (firstname STRING )" )
226
+ self .db .execute ("CREATE TABLE bar (firstname VARCHAR(255) )" )
268
227
self .db .execute ("INSERT INTO bar VALUES (:baz)" , baz = "baz" )
269
228
self .assertEqual (self .db .execute ("SELECT * FROM bar" ), [{"firstname" : "baz" }])
270
229
@@ -274,7 +233,7 @@ def test_named(self):
274
233
self .assertRaises (RuntimeError , self .db .execute , "INSERT INTO foo VALUES (:bar, :baz)" , 'baz' , bar = 'bar' )
275
234
276
235
def test_numeric (self ):
277
- self .db .execute ("CREATE TABLE foo (firstname STRING , lastname STRING )" )
236
+ self .db .execute ("CREATE TABLE foo (firstname VARCHAR(255) , lastname VARCHAR(255) )" )
278
237
279
238
self .db .execute ("INSERT INTO foo VALUES (:1, 'bar')" , "baz" )
280
239
self .assertEqual (self .db .execute ("SELECT * FROM foo" ), [{"firstname" : "baz" , "lastname" : "bar" }])
@@ -296,7 +255,7 @@ def test_numeric(self):
296
255
self .assertEqual (self .db .execute ("SELECT * FROM foo" ), [{"firstname" : "bar" , "lastname" : "baz" }])
297
256
self .db .execute ("DELETE FROM foo" )
298
257
299
- self .db .execute ("CREATE TABLE bar (firstname STRING )" )
258
+ self .db .execute ("CREATE TABLE bar (firstname VARCHAR(255) )" )
300
259
self .db .execute ("INSERT INTO bar VALUES (:1)" , "baz" )
301
260
self .assertEqual (self .db .execute ("SELECT * FROM bar" ), [{"firstname" : "baz" }])
302
261
@@ -308,9 +267,51 @@ def test_numeric(self):
308
267
def test_cte (self ):
309
268
self .assertEqual (self .db .execute ("WITH foo AS ( SELECT 1 AS bar ) SELECT bar FROM foo" ), [{"bar" : 1 }])
310
269
311
- def test_none (self ):
312
- self .db .execute ("CREATE TABLE foo (val INTEGER)" )
313
- self .db .execute ("SELECT * FROM foo WHERE val = ?" , None )
270
+ def tearDown (self ):
271
+ self .db .execute ("DROP TABLE IF EXISTS cs50" )
272
+ self .db .execute ("DROP TABLE IF EXISTS bar" )
273
+ self .db .execute ("DROP TABLE IF EXISTS foo" )
274
+
275
+ class MySQLTests (SQLTests ):
276
+ @classmethod
277
+ def setUpClass (self ):
278
+ self .
db = SQL (
"mysql://[email protected] /test" )
279
+
280
+ def setUp (self ):
281
+ self .db .execute ("CREATE TABLE IF NOT EXISTS cs50 (id INTEGER NOT NULL AUTO_INCREMENT, val VARCHAR(16), bin BLOB, PRIMARY KEY (id))" )
282
+ self .db .execute ("DELETE FROM cs50" )
283
+
284
+ class PostgresTests (SQLTests ):
285
+ @classmethod
286
+ def setUpClass (self ):
287
+ self .
db = SQL (
"postgresql://postgres:[email protected] /test" )
288
+
289
+ def setUp (self ):
290
+ self .db .execute ("CREATE TABLE IF NOT EXISTS cs50 (id SERIAL PRIMARY KEY, val VARCHAR(16), bin BYTEA)" )
291
+ self .db .execute ("DELETE FROM cs50" )
292
+
293
+ def test_cte (self ):
294
+ self .assertEqual (self .db .execute ("WITH foo AS ( SELECT 1 AS bar ) SELECT bar FROM foo" ), [{"bar" : 1 }])
295
+
296
+ def test_postgres_scheme (self ):
297
+ db = SQL (
"postgres://postgres:[email protected] /test" )
298
+ db .execute ("SELECT 1" )
299
+
300
+ class SQLiteTests (SQLTests ):
301
+ @classmethod
302
+ def setUpClass (self ):
303
+ open ("test.db" , "w" ).close ()
304
+ self .db = SQL ("sqlite:///test.db" )
305
+
306
+ def setUp (self ):
307
+ self .db .execute ("CREATE TABLE IF NOT EXISTS cs50 (id INTEGER PRIMARY KEY, val TEXT, bin BLOB)" )
308
+ self .db .execute ("DELETE FROM cs50" )
309
+
310
+ def test_lastrowid (self ):
311
+ self .db .execute ("CREATE TABLE foo(id INTEGER PRIMARY KEY AUTOINCREMENT, firstname TEXT, lastname TEXT)" )
312
+ self .assertEqual (self .db .execute ("INSERT INTO foo (firstname, lastname) VALUES('firstname', 'lastname')" ), 1 )
313
+ self .assertRaises (ValueError , self .db .execute , "INSERT INTO foo (id, firstname, lastname) VALUES(1, 'firstname', 'lastname')" )
314
+ self .assertEqual (self .db .execute ("INSERT OR IGNORE INTO foo (id, firstname, lastname) VALUES(1, 'firstname', 'lastname')" ), None )
314
315
315
316
if __name__ == "__main__" :
316
317
suite = unittest .TestSuite ([
0 commit comments