@@ -332,6 +332,18 @@ def testProgrammaticStaticInsert(self):
332
332
333
333
name = self .databaseTemplate .query_for_object ("SELECT name FROM animal WHERE category = 'kill_bill_viper'" , required_type = types .StringType )
334
334
self .assertEquals (name , "black mamba" )
335
+
336
+ def testProgrammaticStaticInsertWithInsertApi (self ):
337
+ self .mock .expects (once ()).method ("execute" ).id ("#1" )
338
+ self .mock .expects (once ()).method ("execute" ).id ("#2" ).after ("#1" )
339
+ self .mock .expects (once ()).method ("fetchall" ).will (return_value ([("black mamba" ,)])).id ("#3" ).after ("#2" )
340
+ self .mock .lastrowid = 42
341
+
342
+ id = self .databaseTemplate .insert_and_return_id ("INSERT INTO animal (name, category, population) VALUES ('black mamba', 'kill_bill_viper', 1)" )
343
+ self .assertEquals (id , 42 )
344
+
345
+ name = self .databaseTemplate .query_for_object ("SELECT name FROM animal WHERE category = 'kill_bill_viper'" , required_type = types .StringType )
346
+ self .assertEquals (name , "black mamba" )
335
347
336
348
def testProgrammaticInsertWithBoundVariables (self ):
337
349
self .mock .expects (once ()).method ("execute" ).id ("#1" )
@@ -354,6 +366,27 @@ def testProgrammaticInsertWithBoundVariables(self):
354
366
name = self .databaseTemplate .query_for_object ("select name from animal where name = 'cottonmouth'" , required_type = types .StringType )
355
367
self .assertEquals (name , "cottonmouth" )
356
368
369
+ def testProgrammaticInsertWithBoundVariablesWithInsertApi (self ):
370
+ self .mock .expects (once ()).method ("execute" ).id ("#1" )
371
+ self .mock .expects (once ()).method ("execute" ).id ("#2" ).after ("#1" )
372
+ self .mock .expects (once ()).method ("fetchall" ).will (return_value ([("black mamba" ,)])).id ("#3" ).after ("#2" )
373
+ self .mock .expects (once ()).method ("execute" ).id ("#4" ).after ("#3" )
374
+ self .mock .expects (once ()).method ("execute" ).id ("#5" ).after ("#4" )
375
+ self .mock .expects (once ()).method ("fetchall" ).will (return_value ([("cottonmouth" ,)])).id ("#6" ).after ("#5" )
376
+ self .mock .lastrowid = 42
377
+
378
+ id = self .databaseTemplate .insert_and_return_id ("INSERT INTO animal (name, category, population) VALUES (?, ?, ?)" , ('black mamba' , 'kill_bill_viper' , 1 ))
379
+ self .assertEquals (id , 42 )
380
+
381
+ name = self .databaseTemplate .query_for_object ("SELECT name FROM animal WHERE category = 'kill_bill_viper'" , required_type = types .StringType )
382
+ self .assertEquals (name , "black mamba" )
383
+
384
+ id = self .databaseTemplate .insert_and_return_id ("INSERT INTO animal (name, category, population) VALUES (%s, %s, %s)" , ('cottonmouth' , 'kill_bill_viper' , 1 ))
385
+ self .assertEquals (id , 42 )
386
+
387
+ name = self .databaseTemplate .query_for_object ("select name from animal where name = 'cottonmouth'" , required_type = types .StringType )
388
+ self .assertEquals (name , "cottonmouth" )
389
+
357
390
class AbstractDatabaseTemplateTestCase (unittest .TestCase ):
358
391
def __init__ (self , methodName = 'runTest' ):
359
392
unittest .TestCase .__init__ (self , methodName )
@@ -522,6 +555,14 @@ def testProgrammaticStaticInsert(self):
522
555
523
556
name = self .databaseTemplate .query_for_object ("SELECT name FROM animal WHERE category = 'kill_bill_viper'" , required_type = types .StringType )
524
557
self .assertEquals (name , "black mamba" )
558
+
559
+ def testProgrammaticStaticInsertWithInsertApi (self ):
560
+ self .databaseTemplate .execute ("DELETE FROM animal" )
561
+ id = self .databaseTemplate .insert_and_return_id ("INSERT INTO animal (name, category, population) VALUES ('black mamba', 'kill_bill_viper', 1)" )
562
+ self .assertEquals (id , 1 )
563
+
564
+ name = self .databaseTemplate .query_for_object ("SELECT name FROM animal WHERE category = 'kill_bill_viper'" , required_type = types .StringType )
565
+ self .assertEquals (name , "black mamba" )
525
566
526
567
def testProgrammaticInsertWithBoundVariables (self ):
527
568
self .databaseTemplate .execute ("DELETE FROM animal" )
@@ -537,6 +578,20 @@ def testProgrammaticInsertWithBoundVariables(self):
537
578
name = self .databaseTemplate .query_for_object ("select name from animal where name = 'cottonmouth'" , required_type = types .StringType )
538
579
self .assertEquals (name , "cottonmouth" )
539
580
581
+ def testProgrammaticInsertWithBoundVariablesWithInsertApi (self ):
582
+ self .databaseTemplate .execute ("DELETE FROM animal" )
583
+ id = self .databaseTemplate .insert_and_return_id ("INSERT INTO animal (name, category, population) VALUES (?, ?, ?)" , ('black mamba' , 'kill_bill_viper' , 1 ))
584
+ self .assertEquals (id , 1 )
585
+
586
+ name = self .databaseTemplate .query_for_object ("SELECT name FROM animal WHERE category = 'kill_bill_viper'" , required_type = types .StringType )
587
+ self .assertEquals (name , "black mamba" )
588
+
589
+ id = self .databaseTemplate .insert_and_return_id ("INSERT INTO animal (name, category, population) VALUES (%s, %s, %s)" , ('cottonmouth' , 'kill_bill_viper' , 1 ))
590
+ self .assertEquals (id , 2 )
591
+
592
+ name = self .databaseTemplate .query_for_object ("select name from animal where name = 'cottonmouth'" , required_type = types .StringType )
593
+ self .assertEquals (name , "cottonmouth" )
594
+
540
595
class MySQLDatabaseTemplateTestCase (AbstractDatabaseTemplateTestCase ):
541
596
def __init__ (self , methodName = 'runTest' ):
542
597
AbstractDatabaseTemplateTestCase .__init__ (self , methodName )
0 commit comments