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