5
5
from databricks .sdk import WorkspaceClient
6
6
7
7
8
- def make_getrun_path_pattern (run_id : int , page_token : str ) -> Pattern [str ]:
9
- return re .compile (
10
- rf'{ re .escape ("http://localhost/api/" )} 2.\d{ re .escape (f"/jobs/runs/get?page_token={ page_token } &run_id={ run_id } " )} '
11
- )
8
+ def make_getrun_path_pattern (run_id : int , page_token : Optional [str ] = None ) -> Pattern [str ]:
9
+ if page_token :
10
+ return re .compile (
11
+ rf'{ re .escape ("http://localhost/api/" )} 2.\d{ re .escape (f"/jobs/runs/get?page_token={ page_token } &run_id={ run_id } " )} '
12
+ )
13
+ else :
14
+ return re .compile (
15
+ rf'{ re .escape ("http://localhost/api/" )} 2.\d{ re .escape (f"/jobs/runs/get?run_id={ run_id } " )} ' )
12
16
13
17
14
18
def make_getjob_path_pattern (job_id : int , page_token : Optional [str ] = None ) -> Pattern [str ]:
@@ -27,6 +31,12 @@ def make_listjobs_path_pattern(page_token: str) -> Pattern[str]:
27
31
)
28
32
29
33
34
+ def make_listruns_path_pattern (page_token : str ) -> Pattern [str ]:
35
+ return re .compile (
36
+ rf'{ re .escape ("http://localhost/api/" )} 2.\d{ re .escape (f"/jobs/runs/list" )} \?(?:expand_tasks=(?:true|false)&)?page_token={ re .escape (page_token )} '
37
+ )
38
+
39
+
30
40
def test_get_run_with_no_pagination (config , requests_mock ):
31
41
run1 = {"tasks" : [{"run_id" : 0 }, {"run_id" : 1 }], }
32
42
requests_mock .get (make_getrun_path_pattern (1337 , "initialToken" ), text = json .dumps (run1 ))
@@ -617,3 +627,190 @@ def test_list_jobs_with_many_tasks(config, requests_mock):
617
627
# check that job_id 300 was never used in jobs/get call
618
628
history = requests_mock .request_history
619
629
assert all ('300' not in request .qs .get ("job_id" , ['' ]) for request in history )
630
+
631
+
632
+ def test_list_runs_without_task_expansion (config , requests_mock ):
633
+ listruns_page1 = {
634
+ "runs" : [{
635
+ "run_id" : 100 ,
636
+ "run_name" : "run100" ,
637
+ }, {
638
+ "run_id" :
639
+ 200 ,
640
+ "run_name" :
641
+ "run200" ,
642
+ "job_parameters" : [{
643
+ "name" : "param1" ,
644
+ "default" : "default1"
645
+ }, {
646
+ "name" : "param2" ,
647
+ "default" : "default2"
648
+ }]
649
+ }, {
650
+ "run_id" : 300 ,
651
+ "run_name" : "run300" ,
652
+ }],
653
+ "next_page_token" :
654
+ "tokenToSecondPage"
655
+ }
656
+ listruns_page2 = {
657
+ "runs" : [{
658
+ "run_id" : 400 ,
659
+ "run_name" : "run400" ,
660
+ "repair_history" : [{
661
+ "id" : "repair400_1" ,
662
+ }, {
663
+ "id" : "repair400_2" ,
664
+ }]
665
+ }]
666
+ }
667
+
668
+ requests_mock .get (make_listruns_path_pattern ("initialToken" ), text = json .dumps (listruns_page1 ))
669
+ requests_mock .get (make_listruns_path_pattern ("tokenToSecondPage" ), text = json .dumps (listruns_page2 ))
670
+ w = WorkspaceClient (config = config )
671
+
672
+ runs_list = list (w .jobs .list_runs (expand_tasks = False , page_token = "initialToken" ))
673
+ runs_dict = [run .as_dict () for run in runs_list ]
674
+
675
+ assert runs_dict == [{
676
+ "run_id" : 100 ,
677
+ "run_name" : "run100" ,
678
+ }, {
679
+ "run_id" :
680
+ 200 ,
681
+ "run_name" :
682
+ "run200" ,
683
+ "job_parameters" : [{
684
+ "name" : "param1" ,
685
+ "default" : "default1"
686
+ }, {
687
+ "name" : "param2" ,
688
+ "default" : "default2"
689
+ }]
690
+ }, {
691
+ "run_id" : 300 ,
692
+ "run_name" : "run300" ,
693
+ }, {
694
+ "run_id" : 400 ,
695
+ "run_name" : "run400" ,
696
+ "repair_history" : [{
697
+ "id" : "repair400_1" ,
698
+ }, {
699
+ "id" : "repair400_2" ,
700
+ }]
701
+ }]
702
+
703
+ # only two requests should be made which are jobs/list requests
704
+ assert requests_mock .call_count == 2
705
+
706
+
707
+ def test_list_runs (config , requests_mock ):
708
+ listruns_page1 = {
709
+ "runs" : [{
710
+ "run_id" : 100 ,
711
+ "tasks" : [{
712
+ "task_key" : "taskkey101"
713
+ }, {
714
+ "task_key" : "taskkey102"
715
+ }],
716
+ "has_more" : True
717
+ }, {
718
+ "run_id" : 200 ,
719
+ "tasks" : [{
720
+ "task_key" : "taskkey201"
721
+ }]
722
+ }, {
723
+ "run_id" : 300 ,
724
+ "tasks" : [{
725
+ "task_key" : "taskkey301"
726
+ }]
727
+ }],
728
+ "next_page_token" :
729
+ "tokenToSecondPage"
730
+ }
731
+ listruns_page2 = {
732
+ "runs" : [{
733
+ "run_id" : 400 ,
734
+ "tasks" : [{
735
+ "task_key" : "taskkey401"
736
+ }, {
737
+ "task_key" : "taskkey402"
738
+ }],
739
+ "has_more" : True
740
+ }]
741
+ }
742
+
743
+ getrun_100_page1 = {
744
+ "run_id" : 100 ,
745
+ "tasks" : [{
746
+ "task_key" : "taskkey101"
747
+ }, {
748
+ "task_key" : "taskkey102"
749
+ }],
750
+ "next_page_token" : "tokenToSecondPage_100"
751
+ }
752
+ getrun_100_page2 = {"run_id" : 100 , "tasks" : [{"task_key" : "taskkey103" }]}
753
+ getrun_400_page1 = {
754
+ "run_id" : 400 ,
755
+ "tasks" : [{
756
+ "task_key" : "taskkey401"
757
+ }, {
758
+ "task_key" : "taskkey403"
759
+ }],
760
+ "next_page_token" : "tokenToSecondPage_400"
761
+ }
762
+ getrun_400_page2 = {"run_id" : 400 , "tasks" : [{"task_key" : "taskkey402" }, {"task_key" : "taskkey404" }]}
763
+
764
+ requests_mock .get (make_listruns_path_pattern ("initialToken" ), text = json .dumps (listruns_page1 ))
765
+ requests_mock .get (make_listruns_path_pattern ("tokenToSecondPage" ), text = json .dumps (listruns_page2 ))
766
+
767
+ requests_mock .get (make_getrun_path_pattern (100 ), text = json .dumps (getrun_100_page1 ))
768
+ requests_mock .get (make_getrun_path_pattern (100 , "tokenToSecondPage_100" ),
769
+ text = json .dumps (getrun_100_page2 ))
770
+
771
+ requests_mock .get (make_getrun_path_pattern (400 ), text = json .dumps (getrun_400_page1 ))
772
+ requests_mock .get (make_getrun_path_pattern (400 , "tokenToSecondPage_400" ),
773
+ text = json .dumps (getrun_400_page2 ))
774
+ w = WorkspaceClient (config = config )
775
+
776
+ runs_list = list (w .jobs .list_runs (expand_tasks = True , page_token = "initialToken" ))
777
+ runs_dict = [run .as_dict () for run in runs_list ]
778
+
779
+ assert runs_dict == [{
780
+ "run_id" :
781
+ 100 ,
782
+ "tasks" : [{
783
+ "task_key" : "taskkey101" ,
784
+ }, {
785
+ "task_key" : "taskkey102" ,
786
+ }, {
787
+ "task_key" : "taskkey103" ,
788
+ }],
789
+ }, {
790
+ "run_id" : 200 ,
791
+ "tasks" : [{
792
+ "task_key" : "taskkey201" ,
793
+ }],
794
+ }, {
795
+ "run_id" : 300 ,
796
+ "tasks" : [{
797
+ "task_key" : "taskkey301" ,
798
+ }],
799
+ }, {
800
+ "run_id" :
801
+ 400 ,
802
+ "tasks" : [{
803
+ "task_key" : "taskkey401" ,
804
+ }, {
805
+ "task_key" : "taskkey403" ,
806
+ }, {
807
+ "task_key" : "taskkey402" ,
808
+ }, {
809
+ "task_key" : "taskkey404" ,
810
+ }],
811
+ }]
812
+
813
+ # check that job_id 200 and 300 was never used in runs/get call
814
+ history = requests_mock .request_history
815
+ assert all ('300' not in request .qs .get ("run_id" , ['' ]) for request in history )
816
+ assert all ('200' not in request .qs .get ("run_id" , ['' ]) for request in history )
0 commit comments