2121from api .testrail .client import TestRail
2222from api .testrail .helpers import testrail_project_ids
2323
24-
2524_DB = None
2625_TR = None
2726
@@ -93,21 +92,18 @@ def update_testrail_test_health_row(payload, update_list):
9392
9493 def increment_average (starting_count , starting_average , new_value ) -> float :
9594 return (
96- float (starting_average )
97- * ( float ( starting_count ) / float (starting_count + 1 ) )
98- ) + ( float ( new_value ) * ( 1.0 / float ( starting_count + 1 )))
95+ (( float (starting_average ) * float ( starting_count )) + float ( new_value ) )
96+ / ( float (starting_count ) + 1 )
97+ )
9998
10099 new_row ["testrail_case_id" ] = payload .get ("case_id" )
101100 new_row ["testrail_project_id" ] = payload .get ("project_id" )
102101 new_row ["testrail_case_name" ] = payload .get ("case_name" )
103102 new_row ["testrail_suite_name" ] = payload .get ("suite_name" )
104103
105104 try :
106- matching_update = [
107- u for u in update_list if u ["testrail_case_id" ] == payload ["case_id" ]
108- ]
109- if matching_update :
110- row = Update (** matching_update [0 ])
105+ if payload ["case_id" ] in update_list :
106+ row = Update (** (update_list .get (payload ["case_id" ])))
111107 else :
112108 row = db .session .scalars (
113109 select (ReportTestRailTestHealth ).where (
@@ -153,7 +149,9 @@ def testrail_test_health(project, num_days=1):
153149
154150 # Dictionary of project ids and their respective service acct user id
155151 AUTOUSERS = {17 : 976 }
156- updates = []
152+ suite_cache = {}
153+ test_cache = {}
154+ updates = {}
157155 project_ids_list = testrail_project_ids (project )[0 ]
158156 start_date = datetime .now () - timedelta (days = int (num_days ))
159157 for project_id in project_ids_list :
@@ -171,12 +169,22 @@ def testrail_test_health(project, num_days=1):
171169 plan_details = tr .get_test_plan (plan .get ("id" ))
172170 for entry in plan_details .get ("entries" ):
173171 for run in entry .get ("runs" ):
174- for result in tr .test_results_for_run (run .get ("id" )).get ("results" ):
172+ run_id = run .get ("id" )
173+ suite_id = run .get ("suite_id" )
174+ if suite_id not in suite_cache :
175+ suite_cache [suite_id ] = tr .test_suite (
176+ suite_id ).get ("name" )
177+ print (f"Processing Run: { run .get ('name' )} ..." )
178+ run_results = tr .test_results_for_run (run_id ).get ("results" , [])
179+ print (f"Processing { len (run_results )} test results..." )
180+ for result in run_results :
175181 if not result .get ("elapsed" ):
176182 continue
177- test = tr .get_test (result .get ("test_id" ))
178- suite_name = tr .test_suite (
179- run .get ("suite_id" )).get ("name" )
183+ test_id = result .get ("test_id" )
184+ if test_id not in test_cache :
185+ test_cache [test_id ] = tr .get_test (test_id )
186+ test = test_cache .get (test_id )
187+ suite_name = suite_cache .get (suite_id )
180188 update_payload = {
181189 "case_id" : test .get ("case_id" ),
182190 "project_id" : project_id ,
@@ -191,23 +199,16 @@ def testrail_test_health(project, num_days=1):
191199 new_row = update_testrail_test_health_row (
192200 update_payload , updates
193201 )
194- matching_row = [
195- u
196- for u in updates
197- if u .get ("testrail_case_id" ) == new_row ["testrail_case_id" ]
198- ]
199- if matching_row :
200- updates .remove (matching_row [0 ])
201- updates .append (new_row )
202+ updates [new_row .get ("testrail_case_id" )] = new_row
202203
203204 report_test_health_update (updates )
204205
205206
206207def report_test_health_update (payload ):
207208 db = _db ()
208- for row in payload :
209+ for testrail_case_id , row in payload . items () :
209210 matching_row = db .session .query (ReportTestRailTestHealth ).filter (
210- ReportTestRailTestHealth .testrail_case_id == row [ " testrail_case_id" ]
211+ ReportTestRailTestHealth .testrail_case_id == testrail_case_id
211212 )
212213 if matching_row .all ():
213214 matching_row .update (row , synchronize_session = "fetch" )
0 commit comments