Skip to content

Commit 4c99fac

Browse files
committed
added with operator for DBClient
1 parent 8c9032e commit 4c99fac

File tree

10 files changed

+784
-793
lines changed

10 files changed

+784
-793
lines changed

convert_trace_annos.py

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -13,50 +13,49 @@ def is_empty(value):
1313
return True if value == EMPTY else False
1414

1515

16-
def trace_test_cases_to_annos(db_path: Path, trace_file_path: Path):
17-
db = get_db_client()
16+
def trace_test_cases_to_annos(trace_file_path: Path):
17+
with get_db_client() as db:
1818

19-
insertions = list()
20-
logger.info("Reading trace file and inserting annotations into table...")
21-
with open(trace_file_path, mode="r", newline="", encoding="utf-8") as trace_file:
22-
reader = csv.reader(trace_file)
23-
current_tc = EMPTY
24-
concat_summary = EMPTY
25-
test_script = EMPTY
26-
global_columns = next(reader)
27-
for row in reader:
28-
if row[0] == "TestCaseStart":
29-
current_tc = row[1]
30-
test_script = EMPTY
31-
concat_summary = EMPTY
32-
next(reader)
33-
elif row[0] == "Summary":
34-
continue
35-
elif row[0] == "TestCaseEnd":
36-
if not is_empty(current_tc) and not is_empty(concat_summary):
37-
case_id = db.test_cases.get_or_insert(
38-
test_script=test_script, test_case=current_tc
39-
)
40-
annotation_id = db.annotations.get_or_insert(summary=concat_summary)
41-
insertions.append(
42-
db.cases_to_annos.insert(
43-
case_id=case_id, annotation_id=annotation_id
19+
insertions = list()
20+
logger.info("Reading trace file and inserting annotations into table...")
21+
with open(trace_file_path, mode="r", newline="", encoding="utf-8") as trace_file:
22+
reader = csv.reader(trace_file)
23+
current_tc = EMPTY
24+
concat_summary = EMPTY
25+
test_script = EMPTY
26+
global_columns = next(reader)
27+
for row in reader:
28+
if row[0] == "TestCaseStart":
29+
current_tc = row[1]
30+
test_script = EMPTY
31+
concat_summary = EMPTY
32+
next(reader)
33+
elif row[0] == "Summary":
34+
continue
35+
elif row[0] == "TestCaseEnd":
36+
if not is_empty(current_tc) and not is_empty(concat_summary):
37+
case_id = db.test_cases.get_or_insert(
38+
test_script=test_script, test_case=current_tc
4439
)
45-
)
46-
else:
47-
if not is_empty(row[global_columns.index("TestCase")]):
48-
if current_tc != row[global_columns.index("TestCase")]:
49-
current_tc = row[global_columns.index("TestCase")]
50-
if is_empty(test_script) and not is_empty(
51-
row[global_columns.index("TestScript")]
52-
):
53-
test_script = row[global_columns.index("TestScript")]
54-
concat_summary += row[0]
40+
annotation_id = db.annotations.get_or_insert(summary=concat_summary)
41+
insertions.append(
42+
db.cases_to_annos.insert(
43+
case_id=case_id, annotation_id=annotation_id
44+
)
45+
)
46+
else:
47+
if not is_empty(row[global_columns.index("TestCase")]):
48+
if current_tc != row[global_columns.index("TestCase")]:
49+
current_tc = row[global_columns.index("TestCase")]
50+
if is_empty(test_script) and not is_empty(
51+
row[global_columns.index("TestScript")]
52+
):
53+
test_script = row[global_columns.index("TestScript")]
54+
concat_summary += row[0]
5555

56-
db.conn.commit()
57-
logger.info(
58-
f"Inserted {len(insertions)} testcase-annotations pairs to database. Successful: {sum(insertions)}"
59-
)
56+
logger.info(
57+
f"Inserted {len(insertions)} testcase-annotations pairs to database. Successful: {sum(insertions)}"
58+
)
6059

6160

6261
if __name__ == "__main__":

0 commit comments

Comments
 (0)