Commit f425796d0d6fd567e179dd4ec4494678b36f9d4d
Committed by
liujiqiang
1 parent
8c43a9dc
删除公共类的 jsonclass和数据驱动的myrequest
Showing
2 changed files
with
0 additions
and
213 deletions
src/commons/JsonClass.py deleted
100644 → 0
1 | -#!/usr/bin/python | |
2 | -# -*- coding: UTF-8 -*- | |
3 | -import json | |
4 | -import operator | |
5 | - | |
6 | - | |
7 | -def dict_diff(a1,b1): | |
8 | - #提取出dict与str或list的异常对比场景 | |
9 | - if type(a1)!= type(b1): | |
10 | - print("实际响应json的元素{}的类型{}和期望响应json元素{}的类型{}不一致".format(a1,type(a1),b1,type(b1))) | |
11 | - return False | |
12 | - d1=list(set(a1.keys()).difference(set(b1.keys()))) | |
13 | - d2=list(set(b1.keys()).difference(set(a1.keys()))) | |
14 | - if len(d1)==0 and len(d2)==0: | |
15 | - return True | |
16 | - else: | |
17 | - print("实际json比期望json多了键值",d1) | |
18 | - print("实际json比期望json少了键值",d2) | |
19 | - return False | |
20 | - | |
21 | -def list_diff(a1,b1): | |
22 | - #提取出list与str或dict的异常对比场景 | |
23 | - if type(a1)!= type(b1): | |
24 | - print("实际响应json的元素{}的类型{}和期望响应json元素{}的类型{}不一致".format(a1,type(a1),b1,type(b1))) | |
25 | - return False | |
26 | - elif len(a1)!=len(b1): | |
27 | - print("实际响应json列表长度{}和期望响应json列表长度{}不一致".format(a1,b1)) | |
28 | - return False | |
29 | - else: | |
30 | - return True | |
31 | - | |
32 | -#无法判断[[1],[2]]和[[1],[1]]的区别,只能判断列表长度和字典键值是否一致 | |
33 | -#a为实际对比的json数据,b为期望的json数据 | |
34 | -def compare_json(a,b): | |
35 | - #判断检查数据是否为列表格式的数据 | |
36 | - if isinstance(a,list): | |
37 | - if list_diff(a,b)==True: | |
38 | - #为列表时必须进行排序,否则可能出现列表对比的位置不一致 | |
39 | - a.sort() | |
40 | - b.sort() | |
41 | - for i in range(len(a)): | |
42 | - #判断列表中的指定的元素是否为字典格式 | |
43 | - if compare_json(a[i],b[i]) == False: | |
44 | - return False | |
45 | - else: | |
46 | - return False | |
47 | - #判断检查数据是否为字典格式的数据 | |
48 | - elif isinstance(a,dict): | |
49 | - if dict_diff(a,b)==True: | |
50 | - #如果第一层键值相同时,遍历检查内层所有键值 | |
51 | - for key in a.keys(): | |
52 | - #内层键值为列表数据类型时 | |
53 | - if compare_json(a[key],b[key]) == False: | |
54 | - return False | |
55 | - else: | |
56 | - return False | |
57 | - #期望数据非dict和list类型时,直接进行对比 | |
58 | - elif operator.eq(a, b)==False: | |
59 | - print("实际数据的{}和期望数据的{}不一致".format(a,b)) | |
60 | - return False | |
61 | - | |
62 | -#字典嵌套字典,完全相同 | |
63 | -a1={"key1": 0,"key2": {"key11": 0,"key22": {"key111": 0,"key222": 0,"key333":0}}} | |
64 | -b1={"key1": 0,"key2": {"key11": 0,"key22": {"key111": 0,"key222": 0,"key333":0}}} | |
65 | -#字典嵌套字典,实际数据多了键值key33,实际少了键值key333 | |
66 | -a2={"key1": 0,"key2": {"key11": 0,"key22": {"key111": 0,"key222": 0,"key33":0}}} | |
67 | -b2={"key1": 0,"key2": {"key11": 0,"key22": {"key111": 0,"key222": 0,"key333":0}}} | |
68 | -#列表嵌套列,完全相同但顺序不一致 | |
69 | -a3=["key11","key22",["key11","key22",["key111",{"key1111":1}]]] | |
70 | -b3=[["key11","key22",["key111",{"key1111":1}]],"key11","key22"] | |
71 | -#列表嵌套列,顺序不一致,实际数据多了键值key111,实际少了键值key1111 | |
72 | -a4=["key11","key22",["key11","key22",["key111",{"key111":1}]]] | |
73 | -b4=[["key11","key22",["key111",{"key1111":1}]],"key11","key22"] | |
74 | -#列表嵌套列表,顺序不一致,列表值不一致 | |
75 | -a5=["key","key22",["key11","key22",["key111"]]] | |
76 | -b5=["key11","key22",["key11","key22",["key111"]]] | |
77 | -#列表嵌套列表,顺序一致,但是列表长度不一致 | |
78 | -a6=["key","key22",["key11","key22",["key111"]]] | |
79 | -b6=["key11","key22",["key11","key22",["key111",{"key1111":1}]]] | |
80 | -#字典和列表相互嵌套,实际数据多了键值key2,实际少了键值key22 | |
81 | -a7={"key1":0,"key2": ["key11",{"key2": {"key111":0,"key222": ["key111","key222"]}}]} | |
82 | -b7={"key1":0,"key2": ["key11",{"key22": {"key111":0,"key222": ["key111","key222"]}}]} | |
83 | -#字典和列表相互嵌套,实际数据某一个列表长度大于期望数据 | |
84 | -a8={"key1":0,"key2": ["key11",{"key22": {"key111":0,"key222": ["key111"]}}]} | |
85 | -b8={"key1":0,"key2": ["key11",{"key22": {"key111":0,"key222": ["key111","key222"]}}]} | |
86 | -#字典和列表相互嵌套,实际数据某一个列表的值和期望数据的值不一致 | |
87 | -a9={"key1":0,"key2": ["key11",{"key22": {"key111":0,"key222": ["key111","key22211"]}}]} | |
88 | -b9={"key1":0,"key2": ["key11",{"key22": {"key111":0,"key222": ["key111","key222"]}}]} | |
89 | -"""exception""" | |
90 | -#字典和字符串对比 | |
91 | -a10=[{"key11": 0},{"key11": 0}] | |
92 | -b10=["key11","key33"] | |
93 | -#字典和list对比 | |
94 | -a11=[{"key11": 0},{"key11": 0}] | |
95 | -b11=[["key11"],["key33"]] | |
96 | -#列表与字典对比 | |
97 | -a12=[["key11"],["key33"]] | |
98 | -b12=[{"key11": 0},{"key11": 0}] | |
99 | -#列表与字符串对比 | |
100 | -a13=[["key11"],["key33"]] | |
101 | -b13=["key11","key11"] | |
102 | -#字符串与列表对比 | |
103 | -a14=["key11","key11"] | |
104 | -b14=[["key11"],["key33"]] | |
105 | -#字符串与字典对比 | |
106 | -a15=["key11","key33"] | |
107 | -b15=[{"key11": 0},{"key11": 0}] | |
108 | - | |
109 | -print(compare_json(a9,b9)) | |
110 | 0 | \ No newline at end of file |
src/commons/MyRequest.py deleted
100644 → 0
1 | -#!/usr/bin/python | |
2 | -# -*- coding: UTF-8 -*- | |
3 | -import requests | |
4 | -import json | |
5 | -from commons import common as com | |
6 | -from commons.Logging import Logger | |
7 | -# from requests.adapters import HTTPAdapter HTTPAdapter(max_retries=3) | |
8 | -import urllib3 | |
9 | -urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) | |
10 | -log=Logger() | |
11 | - | |
12 | -class myrequest(object): | |
13 | - | |
14 | - def __init__(self): | |
15 | - self.timeout = 60 | |
16 | - self.max_retries = 3 | |
17 | - self.keep_alive = False | |
18 | - self.ssl_verify=False | |
19 | - self.proxies=None | |
20 | - self.allow_redirects=False | |
21 | - | |
22 | - def get(self,url,**kwargs): | |
23 | - log.info('{0:=^80}'.format('进行GET请求')) | |
24 | - log.info('请求地址为{};\n'.format(url)) | |
25 | - res = self.se.get(url, proxies=self.proxies,**kwargs) | |
26 | - log.info('响应body为{};'.format(res.json())) | |
27 | - return res | |
28 | - | |
29 | - def post(self,url, data=None, json=None, **kwargs): | |
30 | - '''处理动态数据的方法''' | |
31 | - log.info('{0:=^80}'.format('进行POST请求')) | |
32 | - log.info('请求地址为{};\n 请求body为{};\n'.format(url,data)) | |
33 | - res = self.se.post(url, data=data, json=json,**kwargs) | |
34 | - log.info('响应body为{};'.format(res.json())) | |
35 | - return res | |
36 | - | |
37 | - def post2(self,session,api,**kwargs): | |
38 | - '''处理静态数据的方法''' | |
39 | - self.se=session | |
40 | - self.api=api.split("_")[2] | |
41 | - self.api_num=(api.split("_")[2]+api.split("_")[1]) | |
42 | - #获取接口的请求方法,url,header,body_format,body等数据 | |
43 | - self.method=com.get_api_config(self.api,self.api,'method') | |
44 | - self.body_format=com.get_api_config(self.api,self.api,'body_format') | |
45 | - self.url=com.get_api_config(self.api,self.api, 'url') | |
46 | - self.headers=json.loads(com.get_api_config(self.api,self.api,'header')) | |
47 | - #判断请求数据类型 | |
48 | - self.data=None if self.body_format=="JSON" else com.get_api_config(self.api,self.api_num,'body') | |
49 | - self.json=None if self.body_format=="FORM" else json.loads(com.get_api_config(self.api,self.api_num,'body')) | |
50 | - log.info('{0:=^80}'.format('请求接口为{}'.format(api))) | |
51 | - log.info('请求地址为{}; 请求方式为{}; 请求body格式为{};\n 请求body为{}{};'.format( | |
52 | - self.url, | |
53 | - self.method, | |
54 | - self.body_format, | |
55 | - self.data, | |
56 | - self.json)) | |
57 | - | |
58 | - if self.method.upper()=="POST": | |
59 | - res = self.se.post(self.url, data=self.data, json=self.json,headers=self.headers,**kwargs) | |
60 | - log.info('响应body为{};'.format(res.json())) | |
61 | - return res | |
62 | - elif self.method.upper()=="GET": | |
63 | - res = self.se.GET(self.url,proxies=self.proxies,**kwargs) | |
64 | - log.info('响应body为{};'.format(res.json())) | |
65 | - return res | |
66 | - elif self.method.upper()=="PUT": | |
67 | - res = self.se.put(self.self.url, data=self.data, json=self.json,proxies=self.proxies,**kwargs) | |
68 | - log.info('响应body为{};'.format(res.json())) | |
69 | - return res | |
70 | - elif self.method.upper()=="DELETE": | |
71 | - res = self.se.delete(self.self.url,proxies=self.proxies,**kwargs) | |
72 | - log.info('响应body为{};'.format(res.json())) | |
73 | - return res | |
74 | - elif self.method.upper()=="HEAD": | |
75 | - res = self.se.head(self.url,proxies=self.proxies,**kwargs) | |
76 | - log.info('响应body为{};'.format(res.json())) | |
77 | - return res | |
78 | - else : | |
79 | - raise Exception("Request method error") | |
80 | - | |
81 | - def close_session(self): | |
82 | - self.se.close() | |
83 | - | |
84 | -# | |
85 | -# test=myrequest() | |
86 | -# # 获取session | |
87 | -# re=test.getSession(True) | |
88 | -# print(re.headers) | |
89 | -# # 请求 | |
90 | -# # re2=test.post(com.get_api_config('upStream', 'upStream01', 'url'), | |
91 | -# # json=json.loads(d), | |
92 | -# # headers=json.loads(com.get_api_config('upStream', 'upStream01', 'header'))) | |
93 | -# # print(re2.json()) | |
94 | -# | |
95 | -# d=com.get_api_config('upStream', 'upStream01', 'body') | |
96 | -# print(type(json.loads(d))) | |
97 | -# print(d) | |
98 | -# print(d.encode().decode('unicode-escape')) | |
99 | -# | |
100 | -# re3=test.post2("test_01_upStream",body="json") | |
101 | -# print(re3.json()) | |
102 | -# a="test_04_upStream" | |
103 | -# print(a.split("_")[2]) | |
104 | -# print(a.split("_")[2]+a.split("_")[1]) | |
105 | 0 | \ No newline at end of file |