-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathop.py
More file actions
193 lines (144 loc) · 4.22 KB
/
op.py
File metadata and controls
193 lines (144 loc) · 4.22 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -*- coding: utf-8 -*-
import re
from ucloud.testing.exc import CompareError
def eq(value, expected):
""" value is equal to expected
"""
assert value == expected
def ne(value, expected):
""" value is equal to expected
"""
assert value != expected
def gt(value, expected):
""" value is greater than expected
"""
assert float(value) > float(expected)
def ge(value, expected):
""" value is greater than or equal to expected
"""
assert float(value) >= float(expected)
def abs_eq(value, expected):
""" value is approx equal to expected
"""
assert round(float(value), 2) == round(float(expected), 2)
def lt(value, expected):
""" value is less than excepted
"""
assert float(value) < float(expected)
def le(value, expected):
""" value is less than or equal to excepted
"""
assert float(value) <= float(expected)
def str_eq(value, expected):
""" value is equal to excepted as string
"""
assert str(value) == str(expected)
def float_eq(value, expected):
""" value is equal to excepted as float
"""
assert round(float(value), 2) == round(float(expected), 2)
def len_eq(value, expected):
""" length of value is equal to excepted
"""
assert isinstance(expected, int)
assert len(value) == expected
def len_gt(value, expected):
""" length of value is greater than excepted
"""
assert isinstance(expected, int)
assert len(value) > expected
def len_ge(value, expected):
""" length of value is greater than or equal to excepted
"""
assert isinstance(expected, int)
assert len(value) >= expected
def len_lt(value, expected):
""" length of value is less than excepted
"""
assert isinstance(expected, int)
assert len(value) < expected
def len_le(value, expected):
""" length of value is less than or equal to excepted
"""
assert isinstance(expected, int)
assert len(value) <= expected
def contains(value, expected):
""" value is contains expected
"""
assert expected in value
def contained_by(value, expected):
""" value is contained by expected
"""
assert value in expected
def type_eq(value, expected):
assert isinstance(value, expected)
def regex(value, expected):
assert isinstance(expected, str)
assert isinstance(value, str)
assert re.match(expected, value)
def startswith(value, expected):
assert str(value).startswith(expected)
def endswith(value, expected):
assert str(value).endswith(expected)
def object_contains(value, expected):
assert str(expected) in str(value)
def object_not_contains(value, expected):
assert str(expected) not in str(value)
mapper = {
"eq": eq,
"equals": eq,
"==": eq,
"abs_eq": abs_eq,
"abs_equals": abs_eq,
"lt": lt,
"less_than": lt,
"le": le,
"less_than_or_equals": le,
"gt": gt,
"greater_than": gt,
"ge": ge,
"greater_than_or_equals": ge,
"ne": ne,
"not_equals": ne,
"str_eq": str_eq,
"string_equals": str_eq,
"float_eq": float_eq,
"float_equals": float_eq,
"len_eq": len_eq,
"length_equals": len_eq,
"count_eq": len_eq,
"len_gt": len_gt,
"count_gt": len_gt,
"length_greater_than": len_gt,
"count_greater_than": len_gt,
"len_ge": len_ge,
"count_ge": len_ge,
"length_greater_than_or_equals": len_ge,
"count_greater_than_or_equals": len_ge,
"len_lt": len_lt,
"count_lt": len_lt,
"length_less_than": len_lt,
"count_less_than": len_lt,
"len_le": len_le,
"count_le": len_le,
"length_less_than_or_equals": len_le,
"count_less_than_or_equals": len_le,
"contains": contains,
"contained_by": contained_by,
"type": type_eq,
"regex": regex,
"startswith": startswith,
"endswith": endswith,
"object_contains": object_contains,
"object_not_contains": object_not_contains,
}
def check(name, value, expected):
if name not in mapper:
raise CompareError("comparator {} is not found".format(name))
try:
return mapper.get(name)(value, expected)
except AssertionError as e:
msg = "assert error, expect {} {} {}, got error {}".format(
value, name, expected, e
)
raise CompareError(msg)