@@ -51,7 +51,7 @@ def test_django_date_extract(self):
5151@override_settings (DEBUG = True )
5252class LastExecutedQueryTest (TestCase ):
5353
54- def test_last_executed_query (self ):
54+ def test_last_executed_query_without_previous_query (self ):
5555 """
5656 last_executed_query should not raise an exception even if no previous
5757 query has been run.
@@ -73,6 +73,36 @@ def test_query_encoding(self):
7373 last_sql = cursor .db .ops .last_executed_query (cursor , sql , params )
7474 self .assertIsInstance (last_sql , str )
7575
76+ def test_last_executed_query (self ):
77+ # last_executed_query() interpolate all parameters, in most cases it is
78+ # not equal to QuerySet.query.
79+ for qs in (
80+ Article .objects .filter (pk = 1 ),
81+ Article .objects .filter (pk__in = (1 , 2 ), reporter__pk = 3 ),
82+ ):
83+ sql , params = qs .query .sql_with_params ()
84+ cursor = qs .query .get_compiler (DEFAULT_DB_ALIAS ).execute_sql (CURSOR )
85+ self .assertEqual (
86+ cursor .db .ops .last_executed_query (cursor , sql , params ),
87+ str (qs .query ),
88+ )
89+
90+ @skipUnlessDBFeature ('supports_paramstyle_pyformat' )
91+ def test_last_executed_query_dict (self ):
92+ square_opts = Square ._meta
93+ sql = 'INSERT INTO %s (%s, %s) VALUES (%%(root)s, %%(square)s)' % (
94+ connection .introspection .identifier_converter (square_opts .db_table ),
95+ connection .ops .quote_name (square_opts .get_field ('root' ).column ),
96+ connection .ops .quote_name (square_opts .get_field ('square' ).column ),
97+ )
98+ with connection .cursor () as cursor :
99+ params = {'root' : 2 , 'square' : 4 }
100+ cursor .execute (sql , params )
101+ self .assertEqual (
102+ cursor .db .ops .last_executed_query (cursor , sql , params ),
103+ sql % params ,
104+ )
105+
76106
77107class ParameterHandlingTest (TestCase ):
78108
0 commit comments