@@ -295,6 +295,87 @@ def test_list_filter_created_by_me(self, session_factory, service):
295295 assert len (result .pipeline_runs ) == 1
296296 assert result .pipeline_runs [0 ].created_by == "alice@example.com"
297297
298+ def test_list_include_sql_default_none (self , session_factory , service ):
299+ _create_run (session_factory , service , root_task = _make_task_spec ())
300+
301+ with session_factory () as session :
302+ result = service .list (session = session )
303+ assert result .sql is None
304+
305+ def test_list_include_sql_true (self , session_factory , service ):
306+ _create_run (session_factory , service , root_task = _make_task_spec ())
307+
308+ with session_factory () as session :
309+ result = service .list (session = session , include_sql = True )
310+ expected = (
311+ "SELECT pipeline_run.id, pipeline_run.root_execution_id,"
312+ " pipeline_run.annotations, pipeline_run.created_by,"
313+ " pipeline_run.created_at, pipeline_run.updated_at,"
314+ " pipeline_run.parent_pipeline_id, pipeline_run.extra_data \n "
315+ "FROM pipeline_run"
316+ " ORDER BY pipeline_run.created_at DESC, pipeline_run.id DESC\n "
317+ " LIMIT 10 OFFSET 0"
318+ )
319+ assert result .sql == expected
320+
321+ def test_list_include_sql_with_filter_query (self , session_factory , service ):
322+ run = _create_run (session_factory , service , root_task = _make_task_spec ())
323+ with session_factory () as session :
324+ service .set_annotation (session = session , id = run .id , key = "team" , value = "ml" )
325+
326+ fq = json .dumps ({"and" : [{"key_exists" : {"key" : "team" }}]})
327+ with session_factory () as session :
328+ result = service .list (session = session , filter_query = fq , include_sql = True )
329+ expected = (
330+ "SELECT pipeline_run.id, pipeline_run.root_execution_id,"
331+ " pipeline_run.annotations, pipeline_run.created_by,"
332+ " pipeline_run.created_at, pipeline_run.updated_at,"
333+ " pipeline_run.parent_pipeline_id, pipeline_run.extra_data \n "
334+ "FROM pipeline_run \n "
335+ "WHERE EXISTS (SELECT pipeline_run_annotation.pipeline_run_id \n "
336+ "FROM pipeline_run_annotation \n "
337+ "WHERE pipeline_run_annotation.pipeline_run_id = pipeline_run.id"
338+ " AND pipeline_run_annotation.\" key\" = 'team')"
339+ " ORDER BY pipeline_run.created_at DESC, pipeline_run.id DESC\n "
340+ " LIMIT 10 OFFSET 0"
341+ )
342+ assert result .sql == expected
343+
344+ def test_list_include_sql_with_cursor (self , session_factory , service ):
345+ for i in range (12 ):
346+ _create_run (
347+ session_factory ,
348+ service ,
349+ root_task = _make_task_spec (f"pipeline-{ i } " ),
350+ )
351+
352+ with session_factory () as session :
353+ page1 = service .list (session = session )
354+ assert page1 .next_page_token is not None
355+
356+ with session_factory () as session :
357+ page2 = service .list (
358+ session = session ,
359+ page_token = page1 .next_page_token ,
360+ include_sql = True ,
361+ )
362+
363+ cursor_dt_iso , cursor_id = page1 .next_page_token .split ("~" )
364+ cursor_dt = datetime .datetime .fromisoformat (cursor_dt_iso )
365+ sql_dt = cursor_dt .strftime ("%Y-%m-%d %H:%M:%S.%f" )
366+ expected = (
367+ "SELECT pipeline_run.id, pipeline_run.root_execution_id,"
368+ " pipeline_run.annotations, pipeline_run.created_by,"
369+ " pipeline_run.created_at, pipeline_run.updated_at,"
370+ " pipeline_run.parent_pipeline_id, pipeline_run.extra_data \n "
371+ "FROM pipeline_run \n "
372+ f"WHERE (pipeline_run.created_at, pipeline_run.id)"
373+ f" < ('{ sql_dt } ', '{ cursor_id } ')"
374+ " ORDER BY pipeline_run.created_at DESC, pipeline_run.id DESC\n "
375+ " LIMIT 10 OFFSET 0"
376+ )
377+ assert page2 .sql == expected
378+
298379
299380class TestCreatePipelineRunResponse :
300381 def test_base_response (self , session_factory , service ):
0 commit comments