|
| 1 | +import logging |
| 2 | +import csv |
| 3 | +from pathlib import Path |
| 4 | +from test2text.db import DbClient |
| 5 | + |
| 6 | +logging.basicConfig(level=logging.INFO) |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | +EMPTY = "" |
| 10 | + |
| 11 | + |
| 12 | +def is_empty(value): |
| 13 | + return True if value == EMPTY else False |
| 14 | + |
| 15 | + |
| 16 | +def trace_test_cases_to_annos(db_path: Path, trace_file_path: Path): |
| 17 | + db = DbClient(db_path) |
| 18 | + |
| 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(test_script=test_script, test_case=current_tc) |
| 38 | + annotation_id = db.annotations.get_or_insert(summary=concat_summary) |
| 39 | + insertions.append(db.cases_to_annos.insert(case_id=case_id, annotation_id=annotation_id)) |
| 40 | + else: |
| 41 | + if not is_empty(row[global_columns.index("TestCase")]): |
| 42 | + if current_tc != row[global_columns.index("TestCase")]: |
| 43 | + current_tc = row[global_columns.index("TestCase")] |
| 44 | + if is_empty(test_script) and not is_empty(row[global_columns.index("TestScript")]): |
| 45 | + test_script = row[global_columns.index("TestScript")] |
| 46 | + concat_summary += row[0] |
| 47 | + |
| 48 | + db.conn.commit() |
| 49 | + logger.info(f"Inserted {len(insertions)} testcase-annotations pairs to database. Successful: {sum(insertions)}") |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + db_path = Path('./private/requirements.db') |
| 54 | + trace_file_path = Path('./private/annotations/stp_0006.Trace.csv') |
| 55 | + trace_test_cases_to_annos(db_path, trace_file_path) |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
0 commit comments