@@ -166,7 +166,7 @@ def setUp(self):
166166 super ().setUp ()
167167 self .events = []
168168 def mycoll (x , y ):
169- if x == "boom" :
169+ if "boom" in ( x , y ) :
170170 self .events .append ("collation raised" )
171171 raise KeyError ("collation raised" )
172172 return (x > y ) - (x < y )
@@ -178,6 +178,9 @@ def mycoll(x, y):
178178 def assertCollationRaises (self ):
179179 return self .assertRaisesRegex (KeyError , "collation raised" )
180180
181+ def events_after_exception (self ):
182+ return self .events [self .events .index ("collation raised" ) + 1 :]
183+
181184 def test_execute (self ):
182185 for sql in self .step_fails , self .step_succeeds :
183186 with self .subTest (sql = sql ), self .assertCollationRaises ():
@@ -221,55 +224,118 @@ def test_executescript(self):
221224 "no such table" ):
222225 self .con .execute ("select * from u" )
223226
224- def test_converter_not_used (self ):
227+ def test_converter_not_called (self ):
228+ calls = []
229+ sqlite .register_converter ("COLLTEST" , calls .append )
230+ self .addCleanup (sqlite .converters .pop , "COLLTEST" , None )
225231 with memory_database (detect_types = sqlite .PARSE_COLNAMES ) as con :
226232 con .create_collation ("mycoll" , lambda x , y : 1 / 0 )
227233 with self .assertRaises (ZeroDivisionError ):
228- con .execute ("select 'a' = 'b' collate mycoll as \" x [conv]\" " )
234+ con .execute ("""
235+ select 'a' = 'b' collate mycoll as "x [colltest]"
236+ """ )
237+ self .assertEqual (calls , [])
229238
230239 def test_function_not_called (self ):
231- calls = []
232- self .con .create_function ("f" , 1 , calls .append )
240+ def f (value ):
241+ self .events .append ("function" )
242+ self .con .create_function ("f" , 1 , f )
233243 with self .assertCollationRaises ():
234244 self .con .execute ("select f(y = 'q' collate mycoll) from t "
235245 "order by rowid desc" )
236- self .assertEqual (calls , [])
246+ self .assertEqual (self . events , ["collation raised" ])
237247
238- def test_aggregate_not_called (self ):
239- calls = []
248+ def test_aggregate_step_not_called (self ):
249+ events = self . events
240250 class Agg :
241251 def step (self , value ):
242- calls .append (value )
252+ events .append ("step" )
243253 def finalize (self ):
244- return len ( calls )
254+ events . append ( "finalize" )
245255 self .con .create_aggregate ("agg" , 1 , Agg )
246256 with self .assertCollationRaises ():
247- self .con .execute ("select agg(y = 'q' collate mycoll) from t "
248- "where rowid = 2" )
249- self .assertEqual (calls , [])
257+ self .con .execute ("select agg(y = 'q' collate mycoll) from t" )
258+ # SQLite calls finalize() to release the aggregate.
259+ self .assertEqual (self . events_after_exception () , ["finalize" ])
250260
251- def test_window_function_not_called (self ):
252- calls = []
261+ @with_tracebacks (ZeroDivisionError )
262+ def test_aggregate_finalize_error (self ):
263+ class Agg :
264+ def step (self , value ):
265+ pass
266+ def finalize (self ):
267+ 1 / 0
268+ self .con .create_aggregate ("agg" , 1 , Agg )
269+ with self .assertCollationRaises ():
270+ self .con .execute ("select agg(y = 'q' collate mycoll) from t" )
271+
272+ def test_window_function_value_not_called (self ):
273+ events = self .events
253274 class Window :
254275 def step (self , value ):
255- calls .append ("step" )
276+ events .append ("step" )
256277 def inverse (self , value ):
257- calls .append ("inverse" )
278+ events .append ("inverse" )
258279 def value (self ):
259- calls .append ("value" )
280+ events .append ("value" )
260281 return 0
261282 def finalize (self ):
262283 return 0
263284 self .con .create_window_function ("win" , 1 , Window )
264- # SQLite evaluates the filter for all rows before it calls the
265- # window function. The filter is false for the first row, so the
266- # first call after the exception is value().
285+ # For this query, SQLite evaluates the filter for all rows before it
286+ # calls the window function. The filter is false for the first row,
287+ # so the first call after the exception is value().
267288 with self .assertCollationRaises ():
268289 self .con .execute ("""
269290 select win(x) filter (where y = 'q' collate mycoll)
270291 over (order by rowid) from t
271- """ ).fetchall ()
272- self .assertEqual (calls , [])
292+ """ )
293+ self .assertEqual (self .events_after_exception (), [])
294+
295+ def test_window_function_inverse_not_called (self ):
296+ events = self .events
297+ class Window :
298+ def step (self , value ):
299+ events .append ("step" )
300+ def inverse (self , value ):
301+ events .append ("inverse" )
302+ def value (self ):
303+ events .append ("value" )
304+ return 0
305+ def finalize (self ):
306+ return 0
307+ self .con .create_window_function ("win" , 1 , Window )
308+ self .con .executemany ("insert into t values (?, ?)" ,
309+ [(3 , "c" ), (4 , "d" )])
310+ self .con .execute ("create table u(w, y)" )
311+ # SQLite uses the collation when it adds a row to the index. For the
312+ # next row, the first call is inverse().
313+ self .con .execute ("create index u_y on u(y collate mycoll)" )
314+ with self .assertCollationRaises ():
315+ self .con .execute ("""
316+ insert into u
317+ select win(x) over (order by rowid
318+ rows between 1 preceding and current row),
319+ y
320+ from t order by rowid
321+ """ )
322+ self .assertEqual (self .events_after_exception (), [])
323+
324+ def test_authorizer_not_called (self ):
325+ def authorizer (* args ):
326+ self .events .append ("authorizer" )
327+ return sqlite .SQLITE_OK
328+ # SQLite prepares a statement for pragma_table_info() during the
329+ # step, and it calls the authorizer for that statement.
330+ self .con .executemany ("insert into t values (?, ?)" , [(3 , "c" )])
331+ self .con .set_authorizer (authorizer )
332+ with self .assertCollationRaises ():
333+ self .con .execute ("""
334+ select t.x, p.name from t join pragma_table_info('t') p
335+ where t.y = 'q' collate mycoll
336+ """ )
337+ self .con .set_authorizer (None )
338+ self .assertEqual (self .events_after_exception (), [])
273339
274340 def test_progress_handler_not_called (self ):
275341 def progress ():
@@ -279,20 +345,22 @@ def progress():
279345 with self .assertCollationRaises ():
280346 self .con .execute (self .step_succeeds + " desc" )
281347 self .con .set_progress_handler (None , 1 )
282- self .assertEqual (self .events [- 1 ], "collation raised" )
283348 self .assertIn ("progress" , self .events )
349+ self .assertEqual (self .events_after_exception (), [])
284350
285351 def test_trace_callback_not_called (self ):
286352 self .con .execute ("create table u(x)" )
287- self .con .execute ("create trigger tr after insert on u "
353+ # SQLite calls the trace callback again for each trigger program.
354+ self .con .execute ("create trigger u_tr after insert on u "
288355 "begin select 1; end" )
289356 traced = []
357+ sql = ("insert into u select y = 'q' collate mycoll from t "
358+ "order by rowid desc" )
290359 self .con .set_trace_callback (traced .append )
291360 with self .assertCollationRaises ():
292- self .con .execute ("insert into u select y = 'q' collate mycoll "
293- "from t order by rowid desc" )
361+ self .con .execute (sql )
294362 self .con .set_trace_callback (None )
295- self .assertNotIn ( "-- TRIGGER tr" , traced )
363+ self .assertEqual ( traced , [ sql ] )
296364
297365
298366class AuthorizerTests (MemoryDatabaseMixin , unittest .TestCase ):
0 commit comments