Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_pact_files(file_location):
if not file_location:
return []
for filename in glob.glob(file_location):
yield BrokerPact.load_file(filename, result_factory=PytestResult)
def pytest_generate_tests(metafunc):
if "pact_verifier" in metafunc.fixturenames:
broker_url = get_broker_url(metafunc.config)
if not broker_url:
pact_files = get_pact_files(metafunc.config.getoption("pact_files"))
if not pact_files:
raise ValueError("need a --pact-broker-url or --pact-files option")
metafunc.parametrize(
"pact_verifier", flatten_pacts(pact_files), ids=test_id, indirect=True
)
else:
provider_name = metafunc.config.getoption("pact_provider_name")
if not provider_name:
raise ValueError("--pact-broker-url requires the --pact-provider-name option")
broker = PactBrokerConfig(
broker_url,
metafunc.config.getoption("pact_broker_token"),
metafunc.config.getoption("pact_consumer_version_tag", []),
)
broker_pacts = BrokerPacts(
provider_name, pact_broker=broker, result_factory=PytestResult
)
pacts = broker_pacts.consumers()
filter_consumer_name = metafunc.config.getoption("pact_verify_consumer")
if not filter_consumer_name:
filter_consumer_name = metafunc.config.getoption("pact_consumer_name")
if filter_consumer_name:
warnings.warn(
"The --pact-consumer-name command-line option is deprecated "
"and will be removed in the 3.0.0 release.",
DeprecationWarning,
pact_files = get_pact_files(metafunc.config.getoption("pact_files"))
if not pact_files:
raise ValueError("need a --pact-broker-url or --pact-files option")
metafunc.parametrize(
"pact_verifier", flatten_pacts(pact_files), ids=test_id, indirect=True
)
else:
provider_name = metafunc.config.getoption("pact_provider_name")
if not provider_name:
raise ValueError("--pact-broker-url requires the --pact-provider-name option")
broker = PactBrokerConfig(
broker_url,
metafunc.config.getoption("pact_broker_token"),
metafunc.config.getoption("pact_consumer_version_tag", []),
)
broker_pacts = BrokerPacts(
provider_name, pact_broker=broker, result_factory=PytestResult
)
pacts = broker_pacts.consumers()
filter_consumer_name = metafunc.config.getoption("pact_verify_consumer")
if not filter_consumer_name:
filter_consumer_name = metafunc.config.getoption("pact_consumer_name")
if filter_consumer_name:
warnings.warn(
"The --pact-consumer-name command-line option is deprecated "
"and will be removed in the 3.0.0 release.",
DeprecationWarning,
)
if filter_consumer_name:
pacts = [pact for pact in pacts if pact.consumer == filter_consumer_name]
metafunc.parametrize("pact_verifier", flatten_pacts(pacts), ids=test_id, indirect=True)
def compare_dict(self, data, spec, path):
if fold_type(data) is not dict:
return self.result.fail(
f"{self.interaction_name} element is not an object (is {nice_type(data)})", path
)
for key in spec:
if key not in data:
return self.result.fail(f"{self.interaction_name} element {key!r} is missing", path)
p = path + [key]
if not self.compare(data[key], spec[key], p):
return self.result.fail(
f"{self.interaction_name} element {key} ({nice_type(data[key])}) "
f"does not match spec ({nice_type(spec[key])})",
path,
)
return True
def compare_list(self, data, path, spec):
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 len(data) != len(spec):
return self.result.fail(
f"{self.interaction_name} array is incorrect length (is {len(data)} elements)", path
)
for i, (data_elem, spec_elem) in enumerate(zip(data, spec)):
p = path + [i]
if not self.compare(data_elem, spec_elem, p):
return self.result.fail(
f"{self.interaction_name} element {i} ({nice_type(data_elem)}) "
f"does not match spec ({nice_type(spec_elem)})",
path,
)
return True
def compare_list(self, data, path, spec):
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 len(data) != len(spec):
return self.result.fail(
f"{self.interaction_name} array is incorrect length (is {len(data)} elements)", path
)
for i, (data_elem, spec_elem) in enumerate(zip(data, spec)):
p = path + [i]
if not self.compare(data_elem, spec_elem, p):
return self.result.fail(
f"{self.interaction_name} element {i} ({nice_type(data_elem)}) "
f"does not match spec ({nice_type(spec_elem)})",
path,
)
return True
def apply_rules_dict(self, data, spec, path):
log.debug(f"apply_rules_dict data={data!r} spec={spec!r} path={format_path(path)}")
if fold_type(data) is not dict:
return self.result.fail(
f"{self.interaction_name} element is not an object (is {nice_type(data)})", path
)
for k in spec:
p = path + [k]
if k not in data:
# we always flag a failure if a given key is not in the response
return self.result.fail(f"{self.interaction_name} element {k!r} is missing", path)
if not self.apply_rules(data[k], spec[k], p):
return False
return True
def apply_rules_dict(self, data, spec, path):
log.debug(f"apply_rules_dict data={data!r} spec={spec!r} path={format_path(path)}")
if fold_type(data) is not dict:
return self.result.fail(
f"{self.interaction_name} element is not an object (is {nice_type(data)})", path
)
for k in spec:
p = path + [k]
if k not in data:
# we always flag a failure if a given key is not in the response
return self.result.fail(f"{self.interaction_name} element {k!r} is missing", path)
if not self.apply_rules(data[k], spec[k], p):
return False
return True
def compare_list(self, data, path, spec):
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 len(data) != len(spec):
return self.result.fail(
f"{self.interaction_name} array is incorrect length (is {len(data)} elements)", path
)
for i, (data_elem, spec_elem) in enumerate(zip(data, spec)):
p = path + [i]
if not self.compare(data_elem, spec_elem, p):
return self.result.fail(
f"{self.interaction_name} element {i} ({nice_type(data_elem)}) "
f"does not match spec ({nice_type(spec_elem)})",
path,
)
return True
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