-
Notifications
You must be signed in to change notification settings - Fork 3k
/
checkers.py
148 lines (120 loc) · 6.75 KB
/
checkers.py
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
# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
# --------------------------------------------------------------------------------------------
import re
import collections
import jmespath
from .exceptions import JMESPathCheckAssertionError
class JMESPathCheck(object): # pylint: disable=too-few-public-methods
def __init__(self, query, expected_result, case_sensitive=True):
self._query = query
self._expected_result = expected_result
self._case_sensitive = case_sensitive
def __call__(self, execution_result):
json_value = execution_result.get_output_in_json()
actual_result = None
try:
actual_result = jmespath.search(self._query, json_value,
jmespath.Options(collections.OrderedDict))
except jmespath.exceptions.JMESPathTypeError:
raise JMESPathCheckAssertionError(self._query, self._expected_result, actual_result,
execution_result.output)
if self._case_sensitive:
equals = actual_result == self._expected_result or str(actual_result) == str(self._expected_result)
else:
equals = actual_result == self._expected_result \
or str(actual_result).lower() == str(self._expected_result).lower()
if not equals:
if actual_result:
raise JMESPathCheckAssertionError(self._query, self._expected_result, actual_result,
execution_result.output)
raise JMESPathCheckAssertionError(self._query, self._expected_result, 'None',
execution_result.output)
class JMESPathCheckExists(object): # pylint: disable=too-few-public-methods
def __init__(self, query):
self._query = query
def __call__(self, execution_result):
json_value = execution_result.get_output_in_json()
actual_result = jmespath.search(self._query, json_value,
jmespath.Options(collections.OrderedDict))
if not actual_result:
raise JMESPathCheckAssertionError(self._query, 'some value', actual_result,
execution_result.output)
class JMESPathCheckNotExists(object): # pylint: disable=too-few-public-methods
def __init__(self, query):
self._query = query
def __call__(self, execution_result):
json_value = execution_result.get_output_in_json()
actual_result = jmespath.search(self._query, json_value,
jmespath.Options(collections.OrderedDict))
if actual_result:
raise JMESPathCheckAssertionError(self._query, 'some value', actual_result,
execution_result.output)
class JMESPathCheckGreaterThan(object): # pylint: disable=too-few-public-methods
def __init__(self, query, expected_result):
self._query = query
self._expected_result = expected_result
def __call__(self, execution_result):
json_value = execution_result.get_output_in_json()
actual_result = jmespath.search(self._query, json_value,
jmespath.Options(collections.OrderedDict))
if not actual_result > self._expected_result:
expected_result_format = "> {}".format(self._expected_result)
if actual_result:
raise JMESPathCheckAssertionError(self._query, expected_result_format, actual_result,
execution_result.output)
raise JMESPathCheckAssertionError(self._query, expected_result_format, 'None',
execution_result.output)
class JMESPathPatternCheck(object): # pylint: disable=too-few-public-methods
def __init__(self, query, expected_result):
self._query = query
self._expected_result = expected_result
def __call__(self, execution_result):
json_value = execution_result.get_output_in_json()
actual_result = jmespath.search(self._query, json_value,
jmespath.Options(collections.OrderedDict))
if not re.match(self._expected_result, str(actual_result), re.IGNORECASE):
raise JMESPathCheckAssertionError(self._query, self._expected_result, actual_result,
execution_result.output)
class NoneCheck(object): # pylint: disable=too-few-public-methods
def __call__(self, execution_result): # pylint: disable=no-self-use
none_strings = ['[]', '{}', 'false']
try:
data = execution_result.output.strip()
assert not data or data in none_strings
except AssertionError:
raise AssertionError("Actual value '{}' != Expected value falsy (None, '', []) or "
"string in {}".format(data, none_strings))
class StringCheck(object): # pylint: disable=too-few-public-methods
def __init__(self, expected_result):
self.expected_result = expected_result
def __call__(self, execution_result):
try:
result = execution_result.output.strip().strip('"')
assert result == self.expected_result
except AssertionError:
raise AssertionError(
"Actual value '{}' != Expected value {}".format(result, self.expected_result))
class StringContainCheck(object): # pylint: disable=too-few-public-methods
def __init__(self, expected_result):
self.expected_result = expected_result
def __call__(self, execution_result):
try:
result = execution_result.output.strip('"')
assert self.expected_result in result
except AssertionError:
raise AssertionError(
"Actual value '{}' doesn't contain Expected value {}".format(result,
self.expected_result))
class StringContainCheckIgnoreCase(object): # pylint: disable=too-few-public-methods
def __init__(self, expected_result):
self.expected_result = expected_result.lower()
def __call__(self, execution_result):
try:
result = execution_result.output.strip('"').lower()
assert self.expected_result in result
except AssertionError:
raise AssertionError(
"Actual value '{}' doesn't contain Expected value {}".format(result,
self.expected_result))