Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __init__(self, path, message):
if isinstance(path, list):
path = format_path(path)
message = path + " " + message
super().__init__(message)
# given some actual data and a pact spec at a certain path, check any rules for that path
log.debug(f"apply_rules data={data!r} spec={spec!r} path={format_path(path)}")
weighted_rule = self.find_rule(path)
log.debug(f"... rule lookup got {weighted_rule}")
if weighted_rule:
try:
weighted_rule.rule.apply(data, spec, path)
except RuleFailed as e:
log.debug(f"... failed: {e}")
return self.result.fail(str(e), path)
log.debug("... passed")
# we passed the matchingRule but we also need to check the contents of arrays/dicts
if fold_type(spec) is list:
r = self.apply_rules_array(data, spec, path)
log.debug(f"apply_rules {format_path(path)} success={r!r}")
return r
elif fold_type(spec) is dict:
r = self.apply_rules_dict(data, spec, path)
log.debug(f"apply_rules {format_path(path)} success={r!r}")
return r
elif not weighted_rule:
if path[0] in ("header", "headers"):
log.debug("... falling back on header matching")
return self.compare_header(data, spec, path)
else:
# in the absence of a rule, and we're at a leaf, we fall back on equality
log.debug("... falling back on equality matching {data} vs {spec}")
if data != spec:
return self.result.fail(
f"Element mismatch {data!r} is not expected {spec!r}", path
)
def apply_rules(self, data, spec, path):
# given some actual data and a pact spec at a certain path, check any rules for that path
log.debug(f"apply_rules data={data!r} spec={spec!r} path={format_path(path)}")
weighted_rule = self.find_rule(path)
log.debug(f"... rule lookup got {weighted_rule}")
if weighted_rule:
try:
weighted_rule.rule.apply(data, spec, path)
except RuleFailed as e:
log.debug(f"... failed: {e}")
return self.result.fail(str(e), path)
log.debug("... passed")
# we passed the matchingRule but we also need to check the contents of arrays/dicts
if fold_type(spec) is list:
r = self.apply_rules_array(data, spec, path)
log.debug(f"apply_rules {format_path(path)} success={r!r}")
return r
elif fold_type(spec) is dict:
def fail(self, message, path=None):
self.success = self.FAIL
if path:
message += " at " + format_path(path)
log.error(message)
return not message
def compare(self, data, spec, path):
log.debug(f"compare data={data!r} spec={spec!r} path={format_path(path)}")
if fold_type(spec) is list:
return self.compare_list(data, path, spec)
if fold_type(spec) is dict:
return self.compare_dict(data, spec, path)
if data != spec:
return self.result.fail(f"Element mismatch {data!r} is not expected {spec!r}", path)
return True
def check_rules(self, data, spec, path):
log.debug(f"check_rules data={data!r} spec={spec!r} path={format_path(path)}")
if self.matching_rules:
# if we have matchingRules then just look at those things
r = self.apply_rules(data, spec, path)
else:
# otherwise the actual must equal the expected (excepting dict elements in actual that are unexpected)
if path[0] in ("header", "headers"):
r = self.compare_header(data, spec, path)
else:
r = self.compare(data, spec, ["body"])
log.debug(f"check_rules success={r!r}")
return r
def apply_rules_array(self, data, spec, path):
log.debug(f"apply_rules_array data={data!r} spec={spec!r} path={format_path(path)}")
if fold_type(data) is not list:
return self.result.fail(
f"{self.interaction_name} element is not an array (is {nice_type(data)})", path
)
if not data and not spec:
# both arrays are empty, there's no further rules to apply
return True
if not spec and data:
return self.result.fail(
f"{self.interaction_name} spec requires empty array but data has contents", path
)
if spec and not data:
return self.result.fail(