Skip to content

Commit

Permalink
tests - v3x
Browse files Browse the repository at this point in the history
  • Loading branch information
commonism committed Dec 7, 2023
1 parent e9e419b commit 7147e85
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
7 changes: 7 additions & 0 deletions aiopenapi3/v30/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ class ServerVariable(ObjectExtended):
default: Optional[str] = Field(...)
description: Optional[str] = Field(default=None)

@model_validator(mode="after")
def validate_ServerVariable(cls, s: "ServerVariable"):
assert isinstance(s.enum, (list, None.__class__))
# default value must be in enum
assert s.default in (s.enum or [s.default])
return s


class Server(ObjectExtended):
"""
Expand Down
2 changes: 1 addition & 1 deletion aiopenapi3/v31/servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ServerVariable(ObjectExtended):
def validate_ServerVariable(cls, s: "ServerVariable"):
assert isinstance(s.enum, (list, None.__class__))
# default value must be in enum
assert s.default in (s.enum or [None])
assert s.default in (s.enum or [s.default])
return s


Expand Down
14 changes: 8 additions & 6 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def schema(self):
return getattr(getattr(aiopenapi3, f"v{self.major}{self.minor}"), "Schema")


@pytest.fixture(scope="session", params=[_Version(3, 0, 3), _Version(3, 1, 0)])
@pytest.fixture(scope="session", params=[_Version(3, 0, 3), _Version(3, 1, 0)], ids=("v30", "v31"))
def openapi_version(request):
return request.param

Expand All @@ -74,7 +74,9 @@ def __str__(self):
return f'swagger: "{self.major}.{self.minor}"'


@pytest.fixture(scope="session", params=[_VersionS(2, 0), _VersionS(3, 0, 3), _VersionS(3, 1, 0)])
@pytest.fixture(
scope="session", params=[_VersionS(2, 0), _VersionS(3, 0, 3), _VersionS(3, 1, 0)], ids=("v20", "v30", "v31")
)
def api_version(request):
return request.param

Expand Down Expand Up @@ -474,10 +476,10 @@ def with_schema_baseurl_v20():


@pytest.fixture
def with_paths_servers():
yield _get_parsed_yaml("paths-servers.yaml")
def with_paths_servers(openapi_version):
yield _get_parsed_yaml("paths-servers.yaml", openapi_version)


@pytest.fixture
def with_paths_server_variables():
yield _get_parsed_yaml("paths-server-variables.yaml")
def with_paths_server_variables(openapi_version):
yield _get_parsed_yaml("paths-server-variables.yaml", openapi_version)
8 changes: 8 additions & 0 deletions tests/path_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
This file tests that paths are parsed and populated correctly
"""
import base64
import copy
import uuid
import pathlib

Expand Down Expand Up @@ -522,3 +523,10 @@ def test_paths_server_variables(httpx_mock, with_paths_server_variables):
r = api._.operation()
request = httpx_mock.get_requests()[-1]
assert request.url.host == "operation" and request.url.path == "/v3/defined"


def test_paths_server_variables_missing(with_paths_server_variables):
dd = copy.deepcopy(with_paths_server_variables)
dd["servers"][0]["url"] = "https://{missing}/test"
with pytest.raises(ValueError, match=r"Missing Server Variables \[\'missing\'\]"):
OpenAPI("http://example/openapi.yaml", dd, session_factory=httpx.Client)

0 comments on commit 7147e85

Please sign in to comment.