Repository navigation
Expand file tree
/
Copy pathcsv_parse.py
More file actions
129 lines (120 loc) · 5.07 KB
/
Copy pathcsv_parse.py
File metadata and controls
129 lines (120 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@Project :Http_API_Automation_PY3
@File :csv_parse.py
@Author :Daqiao Wang
@Date :2021/3/5
"""
import os
import sys
import pandas as pd
from loguru import logger
from ast import literal_eval
cur_dir = os.path.dirname(__file__)
project_root_dir = os.path.dirname(cur_dir)
sys.path.append(project_root_dir)
from config import base_config
def cases_from_csvfile(csvfile, username=None) -> list:
"""
Convert a CSV file into a list of dictionary objects representing test cases.
Args:
csvfile (str): The path to the input CSV file.
username (str): The test account name.
Returns:
list: A list of dictionary objects representing test cases. Each dictionary contains the following keys:
- 'request_url' (str): The URL for the test case.
- 'http_method' (str): The HTTP method for the test case.
- 'data_type' (str): The type of data for the test case.
- 'priority' (str): The priority of the test case.
- 'status_code' (list): The expected status code(s) for the test case.
......
"""
df = pd.read_csv(csvfile, index_col=False, keep_default_na=False, on_bad_lines='skip', encoding='utf-8')
if 'page_url' in df.columns:
df.drop(['page_url', ], axis=1, inplace=True)
df.reset_index(drop=True, inplace=True)
priorities = base_config.priorities
skip_urls = base_config.skip_urls
data_total = list()
for index, row in df.iterrows():
request_url = row['request_url']
if request_url.startswith('#'):
continue
http_method = row['method'].lower()
row['http_method'] = http_method
row['case_id'] = f"{index}_{request_url}_{http_method}"
# filter priority
priority_str = row.get('priority', 'p1')
if priority_str not in priorities:
# logger.debug(f'only run priority: {priorities}. Current case priority is:{priority_str}')
continue
# filter run_env
run_env_from_sys_argv = os.environ.get('run_env')
run_env_from_csv = row.get('run_env', 'all')
if run_env_from_csv.lower() != 'all' and run_env_from_sys_argv != run_env_from_csv:
logger.debug(f'{run_env_from_csv=}, {run_env_from_sys_argv=}')
continue
# filter test account
test_account = row.get('test_account', 'all')
if test_account.lower() != 'all' and test_account != username:
logger.debug(f'{test_account=}, {username=}')
continue
# status code
status_code = row.get('status_code')
# logger.debug(f'{status_code=} {type(status_code)}')
if not status_code:
row['status_code'] = [200, ]
elif isinstance(status_code, int):
row['status_code'] = [status_code, ]
elif isinstance(status_code, str):
try:
tmp = literal_eval(status_code.strip())
except (ValueError, SyntaxError) as err:
err_msg = (f'Invalid status_code "{status_code}" in {csvfile} '
f'row {index + 2} (url: {request_url}): {err}')
logger.error(err_msg)
raise ValueError(err_msg) from err
if isinstance(tmp, int):
row['status_code'] = [tmp, ]
elif isinstance(tmp, (list, tuple)):
row['status_code'] = list(tmp)
else:
err_msg = (f'Unsupported status_code "{status_code}" (parsed as {type(tmp).__name__}) '
f'in {csvfile} row {index + 2} (url: {request_url}), expected int or list of ints.')
logger.error(err_msg)
raise ValueError(err_msg)
else:
err_msg = f'unknown status code in {csvfile} row {index + 2} (url: {request_url}): {status_code!r}'
logger.error(err_msg)
raise ValueError(err_msg)
# logger.debug(f'{row["status_code"]}=')
data_type = row.get('data_type')
if not data_type:
if http_method in ('get', 'delete'):
row['data_type'] = 'params'
elif http_method in ('post', 'put', 'patch'):
row['data_type'] = 'json'
else:
row['data_type'] = 'data'
if skip_urls and any(x in request_url for x in skip_urls):
continue
row.drop('method', inplace=True)
# logger.debug(f'{row=}')
data_total.append(dict(row))
return data_total
def cases_from_csv_dir(csv_dir=None, username=None):
csv_lst = sorted(
[os.path.join(csv_dir, x) for x in os.listdir(csv_dir) if x.endswith('.csv')],
key=lambda path: os.path.basename(path),
)
logger.debug(f'{csv_lst=}')
data_total = list()
for csv_file in csv_lst:
case_lst = cases_from_csvfile(csvfile=csv_file, username=username)
data_total.extend(case_lst)
return data_total
if __name__ == '__main__':
case_data_lst = cases_from_csvfile(csvfile='demo_cases.csv')
logger.debug(f'{case_data_lst=}')
logger.debug(f'{len(case_data_lst)=}')