@@ -332,6 +332,18 @@ def testProgrammaticStaticInsert(self):
332332
333333 name = self .databaseTemplate .query_for_object ("SELECT name FROM animal WHERE category = 'kill_bill_viper'" , required_type = types .StringType )
334334 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" )
335347
336348 def testProgrammaticInsertWithBoundVariables (self ):
337349 self .mock .expects (once ()).method ("execute" ).id ("#1" )
@@ -354,6 +366,27 @@ def testProgrammaticInsertWithBoundVariables(self):
354366 name = self .databaseTemplate .query_for_object ("select name from animal where name = 'cottonmouth'" , required_type = types .StringType )
355367 self .assertEquals (name , "cottonmouth" )
356368
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+
357390class AbstractDatabaseTemplateTestCase (unittest .TestCase ):
358391 def __init__ (self , methodName = 'runTest' ):
359392 unittest .TestCase .__init__ (self , methodName )
@@ -522,6 +555,14 @@ def testProgrammaticStaticInsert(self):
522555
523556 name = self .databaseTemplate .query_for_object ("SELECT name FROM animal WHERE category = 'kill_bill_viper'" , required_type = types .StringType )
524557 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" )
525566
526567 def testProgrammaticInsertWithBoundVariables (self ):
527568 self .databaseTemplate .execute ("DELETE FROM animal" )
@@ -537,6 +578,20 @@ def testProgrammaticInsertWithBoundVariables(self):
537578 name = self .databaseTemplate .query_for_object ("select name from animal where name = 'cottonmouth'" , required_type = types .StringType )
538579 self .assertEquals (name , "cottonmouth" )
539580
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+
540595class MySQLDatabaseTemplateTestCase (AbstractDatabaseTemplateTestCase ):
541596 def __init__ (self , methodName = 'runTest' ):
542597 AbstractDatabaseTemplateTestCase .__init__ (self , methodName )
0 commit comments