|
5 | 5 | """
|
6 | 6 | # pylint: disable=wrong-import-position
|
7 | 7 |
|
| 8 | +import csv |
| 9 | +from datetime import datetime, timedelta |
8 | 10 | from unittest.mock import patch
|
9 | 11 |
|
10 | 12 | import DIRAC
|
@@ -186,3 +188,96 @@ def test_PivotedPilotSummaryTable():
|
186 | 188 | assert "Total" in columns
|
187 | 189 |
|
188 | 190 | cleanUpPilots(pilotRef)
|
| 191 | + |
| 192 | + |
| 193 | +# Parse date strings into datetime objects |
| 194 | +def process_data(data): |
| 195 | + converted_data = [] |
| 196 | + |
| 197 | + for row in data: |
| 198 | + # date fields |
| 199 | + date_indices = [10, 11] # Positions of date fields |
| 200 | + for i in date_indices: |
| 201 | + if not row[i]: |
| 202 | + row[i] = None |
| 203 | + else: |
| 204 | + try: |
| 205 | + row[i] = datetime.strptime(row[i], "%Y-%m-%d %H:%M:%S") |
| 206 | + except ValueError: |
| 207 | + # Handle invalid dates |
| 208 | + row[i] = None |
| 209 | + # Convert other fields to appropriate types |
| 210 | + int_indices = [0, 1] # Positions of integer fields |
| 211 | + for i in int_indices: |
| 212 | + if not row[i]: |
| 213 | + row[i] = 0 |
| 214 | + else: |
| 215 | + try: |
| 216 | + row[i] = int(row[i]) |
| 217 | + except ValueError: |
| 218 | + # Handle invalid integers |
| 219 | + row[i] = 0 |
| 220 | + float_indices = [9] # Positions of float fields |
| 221 | + for i in float_indices: |
| 222 | + if not row[i]: |
| 223 | + row[i] = 0 |
| 224 | + else: |
| 225 | + try: |
| 226 | + row[i] = float(row[i]) |
| 227 | + except ValueError: |
| 228 | + # Handle invalid float |
| 229 | + row[i] = 0 |
| 230 | + converted_data.append(tuple(row)) |
| 231 | + return converted_data |
| 232 | + |
| 233 | + |
| 234 | +def test_summarySnapshot(): |
| 235 | + # first delete all pilots |
| 236 | + sql = "DELETE FROM PilotAgents" |
| 237 | + res = paDB._update(sql) |
| 238 | + assert res["OK"], res["Message"] |
| 239 | + sql = "DELETE FROM PilotsHistorySummary" |
| 240 | + res = paDB._update(sql) |
| 241 | + assert res["OK"], res["Message"] |
| 242 | + |
| 243 | + # insert some predefined pilots to test the summary snapshot |
| 244 | + with open("pilots.csv", newline="", encoding="utf-8") as csvfile: |
| 245 | + csvreader = csv.reader(csvfile) |
| 246 | + data = list(csvreader) |
| 247 | + processed_data = process_data(data) |
| 248 | + placeholders = ",".join(["%s"] * len(processed_data[0])) |
| 249 | + sql = f"INSERT INTO PilotAgents (InitialJobID, CurrentJobID, PilotJobReference, PilotStamp, DestinationSite, Queue, GridSite, VO, GridType, BenchMark, SubmissionTime, LastUpdateTime, Status, StatusReason, AccountingSent) VALUES ({placeholders})" |
| 250 | + res = paDB._updatemany(sql, processed_data) |
| 251 | + assert res["OK"], res["Message"] |
| 252 | + sql = "SELECT * FROM PilotsHistorySummary ORDER BY GridSite, GridType, Status, VO;" |
| 253 | + result = PilotAgentsDB()._query(sql) |
| 254 | + assert result["OK"], result["Message"] |
| 255 | + values = result["Value"][1] |
| 256 | + assert len(values) == 5, "Expected 5 record in the summary" |
| 257 | + # Check it corresponds to the basic "GROUP BY" query |
| 258 | + sql = "SELECT GridSite, GridType, Status, VO, COUNT(*) FROM PilotAgents GROUP BY GridSite, GridType, Status, VO ORDER BY GridSite, Status, VO;" |
| 259 | + result_grouped = PilotAgentsDB()._query(sql) |
| 260 | + assert result_grouped["OK"], result_grouped["Message"] |
| 261 | + sql = "SELECT * FROM PilotsHistorySummary ORDER BY GridSite, Status, VO;" |
| 262 | + result_summary = PilotAgentsDB()._query(sql) |
| 263 | + assert result_summary["OK"], result_summary["Message"] |
| 264 | + assert result_grouped["Value"] == result_summary["Value"], "Summary and grouped query results differ" |
| 265 | + |
| 266 | + # deleting now |
| 267 | + with open("pilots.csv", newline="", encoding="utf-8") as csvfile: |
| 268 | + csvreader = csv.reader(csvfile) |
| 269 | + data = list(csvreader) |
| 270 | + processed_data = process_data(data) |
| 271 | + pilotStamps = [row[3] for row in processed_data] |
| 272 | + pilotStampsStr = ",".join("'" + p + "'" for p in pilotStamps) |
| 273 | + sql = f"DELETE FROM PilotAgents WHERE PilotStamp IN (%s)" % pilotStampsStr |
| 274 | + res = paDB._update(sql) |
| 275 | + assert res["OK"], res["Message"] |
| 276 | + # Check it corresponds to the basic "GROUP BY" query |
| 277 | + sql = "SELECT GridSite, GridType, Status, VO, COUNT(*) FROM PilotAgents GROUP BY GridSite, GridType, Status, VO ORDER BY GridSite, Status, VO;" |
| 278 | + result_grouped = PilotAgentsDB()._query(sql) |
| 279 | + assert result_grouped["OK"], result_grouped["Message"] |
| 280 | + sql = "select * FROM PilotsHistorySummary ORDER BY GridSite, Status, VO;" |
| 281 | + result_summary = PilotAgentsDB()._query(sql) |
| 282 | + assert result_summary["OK"], result_summary["Message"] |
| 283 | + assert result_grouped["Value"] == result_summary["Value"], "Summary and grouped query results differ" |
0 commit comments