1
0
mirror of https://github.com/kevin1024/vcrpy.git synced 2025-12-08 16:53:23 +00:00

change all the matchers with an assert statement and refactor the requests_match function

In order to use the new assert mechanism that returns explicit assertion failure message, all the default matchers does not return a boolean, but only do an assert statement with a basic assertion message (value_1 != value_2).
The requests_match function has been refactored to use the 'get_matchers_results' function in order to have explicit failures that are logged if any.
Many unit tests have been changed as the matchers does not return a boolean value anymore.
Note: Only the matchers "body" and "raw_body" does not have an assertion message, the body values might be big and not useful to be display to spot the differences.
This commit is contained in:
Arthur Hamon
2019-06-12 12:20:30 +02:00
parent 46f5b8a187
commit 0a01f0fb51
2 changed files with 78 additions and 45 deletions

View File

@@ -8,35 +8,47 @@ log = logging.getLogger(__name__)
def method(r1, r2):
return r1.method == r2.method
assert r1.method == r2.method, "{} != {}".format(r1.method, r2.method)
def uri(r1, r2):
return r1.uri == r2.uri
assert r1.uri == r2.uri, "{} != {}".format(r1.uri, r2.uri)
def host(r1, r2):
return r1.host == r2.host
assert r1.host == r2.host, "{} != {}".format(r1.host, r2.host)
def scheme(r1, r2):
return r1.scheme == r2.scheme
assert r1.scheme == r2.scheme, "{} != {}".format(r1.scheme, r2.scheme)
def port(r1, r2):
return r1.port == r2.port
assert r1.port == r2.port, "{} != {}".format(r1.port, r2.port)
def path(r1, r2):
return r1.path == r2.path
assert r1.path == r2.path, "{} != {}".format(r1.path, r2.path)
def query(r1, r2):
return r1.query == r2.query
assert r1.query == r2.query, "{} != {}".format(r1.query, r2.query)
def raw_body(r1, r2):
return read_body(r1) == read_body(r2)
assert read_body(r1) == read_body(r2)
def body(r1, r2):
transformer = _get_transformer(r1)
r2_transformer = _get_transformer(r2)
if transformer != r2_transformer:
transformer = _identity
assert transformer(read_body(r1)) == transformer(read_body(r2))
def headers(r1, r2):
assert r1.headers == r2.headers, "{} != {}".format(r1.headers, r2.headers)
def _header_checker(value, header='Content-Type'):
@@ -74,31 +86,15 @@ def _get_transformer(request):
return _identity
def body(r1, r2):
transformer = _get_transformer(r1)
r2_transformer = _get_transformer(r2)
if transformer != r2_transformer:
transformer = _identity
return transformer(read_body(r1)) == transformer(read_body(r2))
def headers(r1, r2):
return r1.headers == r2.headers
def _log_matches(r1, r2, matches):
differences = [m for m in matches if not m[0]]
if differences:
log.debug(
"Requests {} and {} differ according to "
"the following matchers: {}".format(r1, r2, differences)
)
def requests_match(r1, r2, matchers):
matches = [(m(r1, r2), m) for m in matchers]
_log_matches(r1, r2, matches)
return all(m[0] for m in matches)
successes, failures = get_matchers_results(r1, r2, matchers)
if failures:
log.debug(
"Requests {} and {} differ.\n"
"Failure details:\n"
"{}".format(r1, r2, failures)
)
return len(failures) == 0
def _evaluate_matcher(matcher_function, *args):