Skip to content

Commit

Permalink
解决查询数据返回值有浮点型时的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
Chras-fu committed Jan 16, 2024
1 parent 81c4d77 commit 60a5c88
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
9 changes: 4 additions & 5 deletions core/api/teststep.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,13 @@ def exec_sql(self, sql, case):
else:
results = conn.query(sql["sqlText"])
names = sql["names"].split(",") # name数量可以比结果数量段,但不能长,不能会indexError
values = list(zip(*list(results)))
for j, n in enumerate(names):
if len(values) == 0:
self.context[n] = [] # 如果查询结果为空 则变量保存为空数组
if len(results) == 0:
self.context[n] = [] # 如果查询结果为空 则变量保存为空数组
continue
if j >= len(values):
if j >= len(results):
raise IndexError("变量数错误, 请检查变量数配置是否与查询语句一致,当前查询结果: <br>{}".format(results))
self.context[n] = list(values[j]) # 保存变量到变量空间
self.context[n] = results[j] # 保存变量到变量空间

def save_response(self, res):
"""保存响应结果"""
Expand Down
15 changes: 14 additions & 1 deletion tools/utils/sql.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import decimal
import pymssql as mssql
import pymysql as mysql
import psycopg2 as pgsql
Expand Down Expand Up @@ -46,7 +47,19 @@ def query(self, sql):
cur.execute(sql)
resList = cur.fetchall()
self.conn.close()
return resList
results = []
for res in resList:
for index, value in enumerate(res):
if len(results) < index + 1:
results.append([])
if isinstance(value, decimal.Decimal):
if len(str(value).split(".")[1]) > 16:
results[index].append(str(value))
else:
results[index].append(float(value))
else:
results[index].append(value)
return results

def exec(self, sql):
"""执行非查询语句"""
Expand Down

0 comments on commit 60a5c88

Please sign in to comment.