Exemple #1
0
def test_client_calls_endpoint_using_server_with_path(spec_dict, config):
    spec_dict["servers"].insert(0, {"url": "http://localhost:8001/with/path"})
    app = Application(spec_dict, module=config.endpoint_base)
    client = Client(spec_dict, client=TestClient(app))
    response = client.dummy_test_endpoint()
    assert isinstance(response, OpenAPIResponse)
    assert response.data == b'{"foo":"bar"}'
Exemple #2
0
def test_client_too_few_args_raises_error(spec_dict, config):
    app = Application(spec_dict, module=config.endpoint_base)
    client = Client(spec_dict, client=TestClient(app))
    with pytest.raises(RuntimeError) as error:
        client.dummy_test_endpoint_with_argument()
    assert error.exconly() == (
        "RuntimeError: Incorrect arguments: dummyTestEndpointWithArgument accepts 1 positional argument: test_arg"
    )
Exemple #3
0
def test_client_incorrect_args_raises_error(spec_dict, config):
    app = Application(spec_dict, module=config.endpoint_base)
    client = Client(spec_dict, client=TestClient(app))
    with pytest.raises(RuntimeError) as error:
        client.dummy_test_endpoint("foo")
    assert (
        error.exconly() ==
        "RuntimeError: Incorrect arguments: dummyTestEndpoint accepts no positional arguments"
    )
Exemple #4
0
def test_common_headers_included_in_request(spec_dict, config, monkeypatch):
    app = Application(spec_dict, module=config.endpoint_base)
    client = Client(spec_dict, client=TestClient(app), headers={"foo": "bar"})

    def patch_request(request):
        def wrapper(*args, **kwargs):
            client.request_info = {"args": args, "kwargs": kwargs}
            return request(*args, **kwargs)

        return wrapper

    monkeypatch.setattr(client.client, "request",
                        patch_request(client.client.request))
    client.dummy_test_endpoint(headers_={"baz": "bam"})
    headers = client.request_info["kwargs"]["headers"]
    assert dict(headers) == {"foo": "bar", "baz": "bam"}
Exemple #5
0
from pathlib import Path

from pyotr.client import Client

SPEC_PATH = Path(__file__).parent / "petstore.yaml"

client = Client.from_file(SPEC_PATH)

assert client.find_pets_by_status(status="available").payload == {
    "pets": [{
        "name": "Athena",
        "photoUrls": ["sdfsdfasdf", "asdasdasdasd"]
    }]
}
Exemple #6
0
def test_from_file(config, filename):
    file_path = config.test_dir / filename
    client = Client.from_file(file_path)
    assert client.spec.info.title == "Test Spec"
Exemple #7
0
def test_incorrect_endpoint_raises_error(spec_dict):
    client = Client(spec_dict)
    with pytest.raises(AttributeError):
        client.foo_bar()
Exemple #8
0
def test_use_first_server_url_as_default(spec_dict):
    client = Client(spec_dict)
    assert client.server_url == spec_dict["servers"][0]["url"]
Exemple #9
0
def test_known_server_url_gets_selected(spec_dict):
    client = Client(spec_dict, server_url="foo.bar")
    assert client.server_url == "foo.bar"
    assert client.spec.servers[-1].url == "foo.bar"
Exemple #10
0
def test_unknown_server_url_gets_added_to_spec(spec_dict):
    test_server = spec_dict["servers"][1]["url"]
    client = Client(spec_dict, server_url=test_server)
    assert client.server_url == test_server
Exemple #11
0
def test_client_calls_endpoint_with_body(spec_dict, config):
    app = Application(spec_dict, module=config.endpoint_base)
    client = Client(spec_dict, client=TestClient(app))
    response = client.dummy_post_endpoint(body_={"foo": "bar"})
    assert isinstance(response, OpenAPIResponse)
    assert response.status_code == HTTPStatus.NO_CONTENT
Exemple #12
0
def test_endpoint_docstring_constructed_with_default_values(spec_dict):
    client = Client(spec_dict)
    assert client.dummy_test_endpoint_with_argument.__doc__ == "dummyTestEndpointWithArgument"
Exemple #13
0
def test_endpoint_docstring_constructed_from_spec(spec_dict):
    client = Client(spec_dict)
    assert client.dummy_test_endpoint.__doc__ == (
        "A dummy test endpoint.\n\nA test endpoint that does nothing, so is pretty dummy, but works fine for testing."
    )
Exemple #14
0
def test_from_file_raises_exception_if_unknown_type(config):
    file_path = config.test_dir / "openapi.unknown"
    with pytest.raises(RuntimeError):
        Client.from_file(file_path)
Exemple #15
0
def test_client_calls_endpoint(spec_dict, config):
    app = Application(spec_dict, module=config.endpoint_base)
    client = Client(spec_dict, client=TestClient(app))
    response = client.dummy_test_endpoint()
    assert isinstance(response, OpenAPIResponse)
    assert response.data == b'{"foo":"bar"}'