4
4
import pandas as pd
5
5
6
6
from lib .testrail_conn import APIClient
7
- '''
7
+
8
8
from database import (
9
9
Database ,
10
10
Projects ,
11
11
TestSuites ,
12
12
ReportTestCaseCoverage ,
13
+ # ReportMilestones,
13
14
# ReportTestRunCounts
14
15
)
15
- '''
16
+
16
17
from utils .datetime_utils import DatetimeUtils as dt
17
18
18
19
@@ -22,8 +23,9 @@ def __init__(self):
22
23
try :
23
24
TESTRAIL_HOST = os .environ ['TESTRAIL_HOST' ]
24
25
self .client = APIClient (TESTRAIL_HOST )
25
- self .client .user = os .environ ['TESTRAIL_USERNAME' ]
26
+ self .client .user = os .environ ['TESTRAIL_USERNAME' ]
26
27
self .client .password = os .environ ['TESTRAIL_PASSWORD' ]
28
+
27
29
except KeyError :
28
30
print ("ERROR: Missing testrail env var" )
29
31
sys .exit (1 )
@@ -87,8 +89,8 @@ class TestRailClient(TestRail):
87
89
88
90
def __init__ (self ):
89
91
super ().__init__ ()
90
- # self.db = DatabaseTestRail()
91
- '''
92
+ self .db = DatabaseTestRail ()
93
+
92
94
def data_pump (self , project = 'all' , suite = 'all' ):
93
95
# call database for 'all' values
94
96
# convert inputs to a list so we can easily
@@ -119,7 +121,7 @@ def data_pump(self, project='all', suite='all'):
119
121
self .testrail_coverage_update (projects_id ,
120
122
testrail_project_id , suite ['id' ])
121
123
122
- def testrail_project_ids(self, project):
124
+ def testrail_project_ids (self , project = 'all' ):
123
125
""" Return the ids needed to be able to query the TestRail API for
124
126
a specific test suite from a specific project
125
127
@@ -163,7 +165,7 @@ def testrail_coverage_update(self, projects_id,
163
165
164
166
# Insert data in 'totals' array into DB
165
167
self .db .report_test_coverage_insert (projects_id , payload )
166
-
168
+
167
169
def testrail_run_counts_update (self , project , num_days ):
168
170
start_date = dt .start_date (num_days )
169
171
@@ -187,11 +189,16 @@ def testrail_run_counts_update(self, project, num_days):
187
189
# Insert data in 'totals' array into DB
188
190
self .db .report_test_runs_insert (projects_id , totals )
189
191
190
- '''
191
- def test_rail_milestones (self , project_id ):
192
- payload = self .milestones ('59' )
193
-
194
- df = pd .json_normalize (payload )
192
+ def test_rail_milestones (self , project = 'all' ):
193
+ project_ids_list = self .testrail_project_ids (project )
194
+ print (project_ids_list )
195
+ project_id = [value [1 ] for value in project_ids_list ]
196
+ milestones_all = pd .DataFrame ()
197
+
198
+ for project in project_id :
199
+ payload = self .milestones (project )
200
+ df = pd .json_normalize (payload )
201
+ milestones_all = pd .concat ([milestones_all , df ], ignore_index = True )
195
202
196
203
selected_columns = {
197
204
"id" : "id" ,
@@ -203,14 +210,39 @@ def test_rail_milestones(self, project_id):
203
210
"completed_on" : "completed_on" ,
204
211
"url" : "url"
205
212
}
213
+
206
214
# Select specific columns
207
- df_selected = df [selected_columns .keys ()]
208
- df_selected ['started_on' ] = df_selected ['started_on' ].apply (dt .convert_epoch_to_datetime ) # noqa
215
+ df_selected = milestones_all [selected_columns .keys ()]
216
+ print ("---------------" )
217
+ print (df_selected ['completed_on' ].dtype ) # Float
218
+ print (df_selected ['started_on' ].dtype ) # Int
219
+
220
+ df_selected ['started_on' ] = pd .to_numeric (df_selected ['started_on' ], errors = 'coerce' ) # noqa Ensure numeric for epoch
221
+ df_selected ['started_on' ] = pd .to_datetime (df_selected ['started_on' ], unit = 's' , errors = 'coerce' ). # noqa
222
+
223
+ df_selected ['completed_on' ] = pd .to_numeric (df_selected ['completed_on' ], errors = 'coerce' ) # noqa Ensure numeric for epoch
224
+ df_selected ['completed_on' ] = pd .to_datetime (df_selected ['completed_on' ], unit = 's' , errors = 'coerce' ). # noqa
209
225
210
- df_selected .to_csv ('output.csv' , index = False )
226
+ '''
227
+ These work with datatypes warnings
228
+ # Step 1: Replace invalid values with NaN and cast to numeric (epoch seconds)
229
+ df_selected.loc[:,'started_on'] = pd.to_numeric(df_selected['started_on'], errors='coerce')
230
+
231
+ # Step 2: Convert epoch timestamps to datetime64
232
+ df_selected.loc[:,'started_on'] = pd.to_datetime(df_selected['started_on'], unit='s', errors='coerce')
233
+
234
+ # Step 1: Replace invalid values with NaN and cast to numeric (epoch seconds)
235
+ df_selected.loc[:,'completed_on'] = pd.to_numeric(df_selected['completed_on'], errors='coerce')
236
+
237
+ # Step 2: Convert epoch timestamps to datetime64
238
+ df_selected.loc[:,'completed_on'] = pd.to_datetime(df_selected['completed_on'], unit='s', errors='coerce')
239
+
240
+ df_selected['completed_on'] = df_selected['completed_on'].dt.strftime('%Y-%m-%dT%H')
241
+ '''
211
242
print (df_selected )
243
+ self .db .report_milestones_insert (df_selected )
244
+
212
245
213
- '''
214
246
class DatabaseTestRail (Database ):
215
247
216
248
def __init__ (self ):
@@ -231,6 +263,22 @@ def test_suites_update(self, testrail_project_id,
231
263
self .session .add (suites )
232
264
self .session .commit ()
233
265
266
+ def report_milestones_insert (sefl , payload ):
267
+ for index , row in payload .iterrows ():
268
+ print (row )
269
+ '''
270
+ report = ReportMilestones(milestone_id=row['id'],
271
+ project_id=row['project_id'], # noqa
272
+ milestone_name=row['name'], # noqa
273
+ milestone_start_date=row['start_on'], # noqa
274
+ milestone_complete=row['is_completed'],
275
+ milestone_end_date=row['completed_on'],
276
+ milestone_description=row['description'],
277
+ milestone_url=row['url'])
278
+ #self.session.add(report)
279
+ #self.session.commit()
280
+ '''
281
+
234
282
def report_test_coverage_payload (self , cases ):
235
283
"""given testrail data (cases), calculate test case counts by type"""
236
284
@@ -323,7 +371,7 @@ def report_test_runs_insert(self, project_id, payload):
323
371
if t ['testrail_completed_on' ]:
324
372
created_on = dt .convert_epoch_to_datetime (t ['testrail_created_on' ]) # noqa
325
373
completed_on = dt .convert_epoch_to_datetime (t ['testrail_completed_on' ]) # noqa
326
-
374
+
327
375
report = ReportTestRunCounts (
328
376
projects_id = project_id ,
329
377
testrail_run_id = t ['testrail_run_id' ],
@@ -333,8 +381,6 @@ def report_test_runs_insert(self, project_id, payload):
333
381
test_case_blocked_count = t ['blocked_count' ],
334
382
testrail_created_on = created_on ,
335
383
testrail_completed_on = completed_on )
336
-
337
-
384
+
338
385
# self.session.add(report)
339
386
self .session .commit ()
340
- '''
0 commit comments