Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# For sake of example to check allOf logic
{"type": "string", "example": datetime.date(2020, 1, 1)},
{"type": "string", "example": datetime.date(2020, 1, 1)},
]
},
}
],
}
}
},
}
schema_path = testdir.makefile(".yaml", schema=yaml.dump(schema))
# Then yaml loader should ignore it
# And data generation should work without errors
schema = schemathesis.from_path(str(schema_path))
@given(case=schema["/teapot"]["GET"].as_strategy())
def test(case):
assert isinstance(case.query["key"], str)
test()
def test_path_loader(simple_schema):
# Each loader method should read the specified schema correctly
assert schemathesis.from_path(SIMPLE_PATH).raw_schema == simple_schema
kwargs = {"endpoint": endpoint}
if override:
case = Case(**kwargs)
response = case.call(base_url)
else:
case = Case(**kwargs)
endpoint.base_url = base_url
response = case.call()
assert response.status_code == 200
assert response.json() == {"success": True}
with pytest.warns(None) as records:
del response
assert not records
schema = schemathesis.from_path(SIMPLE_PATH)
ENDPOINT = Endpoint("/api/success", "GET", {}, base_url="http://example.com", schema=schema)
@pytest.mark.parametrize(
"case, expected",
(
(Case(ENDPOINT, body={"test": 1}), "requests.get('http://example.com/api/success', json={'test': 1})"),
(Case(ENDPOINT), "requests.get('http://example.com/api/success')"),
(Case(ENDPOINT, query={"a": 1}), "requests.get('http://example.com/api/success', params={'a': 1})"),
),
)
def test_get_code_to_reproduce(case, expected):
assert case.get_code_to_reproduce() == expected
def test_validate_response(testdir):
def test_from_path(simple_schema):
assert schemathesis.from_path(SIMPLE_PATH).raw_schema == simple_schema
def test_complex_dereference(testdir):
# This tests includes:
# - references to other files
# - local references in referenced files
# - different directories - relative paths to other files
schema_root = testdir.mkdir("root")
common = testdir.mkdir("common")
paths = schema_root.mkdir("paths")
schemas = schema_root.mkdir("schemas")
teapot_schemas = schemas.mkdir("teapot")
root = schema_root / "root.yaml"
root.write_text(yaml.dump(ROOT_SCHEMA), "utf8")
(paths / "teapot.yaml").write_text(yaml.dump(TEAPOT_PATHS), "utf8")
(teapot_schemas / "create.yaml").write_text(yaml.dump(TEAPOT_CREATE_SCHEMAS), "utf8")
(common / "responses.yaml").write_text(yaml.dump(COMMON_RESPONSES), "utf8")
schema = schemathesis.from_path(str(root))
assert schema.endpoints["/teapot"]["POST"] == Endpoint(
path="/teapot",
method="POST",
definition={
"requestBody": {
"content": {
"application/json": {
"schema": {
"additionalProperties": False,
"description": "Test",
"properties": {
"profile": {
"additionalProperties": False,
"description": "Test",
"properties": {"id": {"type": "integer"}},
"required": ["id"],