You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.9 KiB
62 lines
1.9 KiB
import pymysql |
|
import random |
|
from pymysql.cursors import DictCursor |
|
from config import db_config |
|
|
|
|
|
class Result: |
|
def __init__(self): |
|
self.db_conn = pymysql.connect(**db_config) |
|
self.cursor = self.db_conn.cursor(cursor=DictCursor) |
|
|
|
def save(self, data): |
|
try: |
|
self.cursor.execute( |
|
"INSERT INTO result (api, test_type, case_id, xml_req_params, xml_resp_xml, xml_resp_json, xml_resp_format, json_req_method, json_req_params, json_req_data, json_resp_json, compare_result) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", |
|
data) |
|
self.db_conn.commit() |
|
except Exception as e: |
|
print('save_result', e) |
|
|
|
def close_db(self): |
|
self.cursor.close() |
|
self.db_conn.close() |
|
|
|
|
|
class Case: |
|
def __init__(self): |
|
self.db_conn = pymysql.connect(**db_config) |
|
self.cursor = self.db_conn.cursor(cursor=DictCursor) |
|
|
|
def get(self, case_id=None): |
|
try: |
|
self.cursor.execute("SELECT COUNT(*) as case_count FROM `case`;;") |
|
count = int(self.cursor.fetchone()['case_count']) |
|
if case_id is None: |
|
case_id = random.randint(1, count + 1) |
|
self.cursor.execute( |
|
"SELECT * FROM `case` WHERE id = %s;", (case_id,)) |
|
case_data = self.cursor.fetchone() |
|
print(case_data) |
|
return case_data |
|
except Exception as e: |
|
print('save_result', e) |
|
|
|
def get_all(self): |
|
try: |
|
self.cursor.execute("SELECT * FROM `case`;") |
|
case_list = self.cursor.fetchall() |
|
# print(case_list) |
|
return case_list |
|
except Exception as e: |
|
print('save_result', e) |
|
|
|
def close_db(self): |
|
self.cursor.close() |
|
self.db_conn.close() |
|
|
|
|
|
if __name__ == '__main__': |
|
case = Case() |
|
case.get(1) |
|
case.get_all()
|
|
|