testDB.py
4.25 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
130
131
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
import traceback
import MySQLdb, json
from common.testException import TestDBException
logger = logging.getLogger("db_logger")
class TestDB():
db = None
def __init__(self):
try:
self.db = MySQLdb.connect(host='localhost', user='root', passwd='PASSWORD', db='mydb', port=3306, charset='utf8')
except Exception as e:
logger.error(traceback.format_exc())
raise TestDBException(-1, "TestDBException: cannot connect TestDB(%s)"%(e[1]))
def commitDB(self):
try:
self.db.commit()
except Exception as e:
logger.error(traceback.format_exc())
raise TestDBException(-1, "TestDBException: cannot commit db(%s)"%(e[1]))
def rollbackDB(self):
try:
self.db.rollback()
except Exception as e:
logger.error(traceback.format_exc())
raise TestDBException(-1, "TestDBException: cannot rollback db(%s)"%(e[1]))
def closeDB(self):
try:
self.db.close()
except Exception as e:
logger.error(traceback.format_exc())
raise TestDBException(-1, "TestDBException: cannot close db connection(%s)"%(e[1]))
def getDictCursor(self):
try:
return self.db.cursor(MySQLdb.cursors.DictCursor)
except Exception as e:
logger.error(traceback.format_exc())
raise TestDBException(-1, "TestDBException: cannot get mysql dict cursor(%s)"%(e[1]))
def getCommonCursor(self):
try:
return self.db.cursor()
except Exception as e:
logger.error(traceback.format_exc())
raise TestDBException(-1, "TestDBException: cannot get mysql common cursor(%s)"%(e[1]))
def executeQuery(self, cursor, query):
try:
logger.info(query)
row = cursor.execute(query)
return row
except Exception as e:
logger.error(traceback.format_exc())
logger.error(query)
raise TestDBException(-1, "TestDBException: cannot execute query(%s)" %(query))
def executeSelectQuery(self, query, one=False):
try:
cursor = self.getDictCursor()
self.executeQuery(cursor, query)
if one:
res = cursor.fetchone()
else:
res = cursor.fetchall()
self.closeDbCursor(cursor)
return res
except TestDBException as e:
raise e
except Exception as e:
print(traceback.format_exc())
raise TestDBException(-1, "TestDBException: executeSelectQuery()")
def closeDbCursor(self, cursor):
try:
cursor.close()
except Exception as e:
logger.error(traceback.format_exc())
raise TestDBException(-1, "TestDBException: cannot close db cursor(%s)"%(e[1]))
# insert api call log function
def insertIntoApiCallLog(self, url, ip, method, response_time, result):
try:
_id = None
cursor = self.getDictCursor()
query = ("""INSERT INTO api_call_log (url, ip, method, ret_code, ret_message, response_time) VALUES ('%s', '%s', '%s', '%s', '%s', '%s');"""
% (url, ip, method, result.get('code'), result.get('message'), response_time))
row = self.executeQuery(cursor, query)
if row > 0:
_id = cursor.lastrowid
self.commitDB()
self.closeDbCursor(cursor)
return _id
except Exception as e:
print(traceback.format_exc())
logger.error(traceback.format_exc())
raise TestDBException(-1, "TestDBException: insertIntoApiCallLog()")
def selectApiCallLog(self):
try:
_id = None
cursor = self.getDictCursor()
query = """SELECT * FROM api_call_log;"""
self.executeQuery(cursor, query)
logList = cursor.fetchall()
self.closeDbCursor(cursor)
return logList
except Exception as e:
print(traceback.format_exc())
logger.error(traceback.format_exc())
raise TestDBException(-1, "TestDBException: selectApiCallLog()")