Python(33):数据断言(查询数据库数据和插入数据对比)
前言:
需求:需要针对查询数据库数据和插入的数据进行对比,用Python语言进行编写
数据库查询的结果可参考:https://blog.csdn.net/fen_fen/article/details/135462484
1、查询结果数据示例
#查询结果 result_list = [{'ID': 181, 'AES': 'HMF', 'SM4': '北京XXX技术有限公司', 'SM4_A': '北京市朝阳区明天第一城6-901', 'EMAIL': 'hmf@yahoo.com', 'PHONE': '15652996964', 'SSN': '110103201206200910', 'MILITARY': '武水电字第3632734号', 'PASSPORT': 'BWP018930705', 'INTELSSN': '110103201206200910', 'INTELPASSPORT': 'BWP018930705', 'INTELMILITARY': '武水电字第3632734号', 'INTELGANGHUI': 'H21157232', 'INTELTAITONEI': '9839487602', 'CREDIT_CARD_SHORT': '117', 'CREDIT_CARD_LONG': '6212262502009182455', 'JOB': '测试开发工程师', 'SM4_CBC': '北京XXX技术有限公司', 'SM4_A_CBC': '产品研发部'}]#插入的数据转换成字典,例子
dict_input ={'AES': 'HMF', 'SM4': '北京XXX技术有限公司', 'SM4_A': '北京市朝阳区明天第一城6-901', 'EMAIL': 'hmf@yahoo.com', 'PHONE': '15652996964', 'SSN': '110103201206200910', 'MILITARY': '武水电字第3632734号', 'PASSPORT': 'BWP018930705', 'INTELSSN': '110103201206200910', 'INTELPASSPORT': 'BWP018930705', 'INTELMILITARY': '武水电字第3632734号', 'INTELGANGHUI': 'H21157232', 'INTELTAITONEI': '9839487602', 'CREDIT_CARD_SHORT': '117', 'CREDIT_CARD_LONG': '6212262502009182455', 'JOB': '测试开发工程师', 'SM4_CBC': '北京XXX技术有限公司', 'SM4_A_CBC': '产品研发部'}
2、python编写的对比代码示例
2.1、dbassert.py
#_*_coding:utf-8_*_
def assert_plain_data(result_list, dict_input):
count = 0
for item in result_list:
for key in item:
if key != "ID":
for key_input in dict_input:
# print(key)
# print(key_input)
if key == key_input:
# print(mapInput[key_input])
if item[key] == dict_input[key_input]:
count += 1
return count
def assert_cipher_data(result_list,cipher):
count = 0
for item in result_list:
for key in item:
if key != "ID":
if item[key].endswith(cipher):
count += 1
return count
2.2、调用
result_list = [{'ID': 181, 'AES': 'HMF', 'SM4': '北京XXX技术有限公司', 'SM4_A': '北京市朝阳区明天第一城6-901', 'EMAIL': 'hmf@yahoo.com', 'PHONE': '15652996964', 'SSN': '110103201206200910', 'MILITARY': '武水电字第3632734号', 'PASSPORT': 'BWP018930705', 'INTELSSN': '110103201206200910', 'INTELPASSPORT': 'BWP018930705', 'INTELMILITARY': '武水电字第3632734号', 'INTELGANGHUI': 'H21157232', 'INTELTAITONEI': '9839487602', 'CREDIT_CARD_SHORT': '117', 'CREDIT_CARD_LONG': '6212262502009182455', 'JOB': '测试开发工程师', 'SM4_CBC': '北京XXX技术有限公司', 'SM4_A_CBC': '产品研发部'}]
dict_input ={'AES': 'HMF', 'SM4': '北京XXX技术有限公司', 'SM4_A': '北京市朝阳区明天第一城6-901', 'EMAIL': 'hmf@yahoo.com', 'PHONE': '15652996964', 'SSN': '110103201206200910', 'MILITARY': '武水电字第3632734号', 'PASSPORT': 'BWP018930705', 'INTELSSN': '110103201206200910', 'INTELPASSPORT': 'BWP018930705', 'INTELMILITARY': '武水电字第3632734号', 'INTELGANGHUI': 'H21157232', 'INTELTAITONEI': '9839487602', 'CREDIT_CARD_SHORT': '117', 'CREDIT_CARD_LONG': '6212262502009182455', 'JOB': '测试开发工程师', 'SM4_CBC': '北京XXX技术有限公司', 'SM4_A_CBC': '产品研发部'}
num = assert_plain_data(result_list,dict_input)
print(num)
result_list2 = [{'ID': 181, 'AES': 'HMF|^|', 'SM4': '北京XXX技术有限公司', 'SM4_A': '北京市朝阳区明天第一城6-901', 'EMAIL': 'hmf@yahoo.com', 'PHONE': '15652996964', 'SSN': '110103201206200910', 'MILITARY': '武水电字第3632734号', 'PASSPORT': 'BWP018930705', 'INTELSSN': '110103201206200910', 'INTELPASSPORT': 'BWP018930705', 'INTELMILITARY': '武水电字第3632734号', 'INTELGANGHUI': 'H21157232', 'INTELTAITONEI': '9839487602', 'CREDIT_CARD_SHORT': '117', 'CREDIT_CARD_LONG': '6212262502009182455', 'JOB': '测试开发工程师', 'SM4_CBC': '北京XXX技术有限公司', 'SM4_A_CBC': '产品研发部'}]
count =assert_cipher_data(result_list2,"|^|")
print(count)
2.3、执行结果
最后再用unittest的断言进行判断数量是否一致
assertEqual(self, first, second, msg=None)