2222
2323
2424def _load_openapi_spec_from_file () -> dict [str , Any ]:
25- """Load OpenAPI specification from configured path."""
25+ """Load OpenAPI specification from configured path.
26+
27+ Load and return the OpenAPI JSON document from the configured file path.
28+
29+ If the configured file is present, its contents are parsed as JSON and
30+ returned as a dictionary.
31+ If the file is missing, the running test is failed via pytest.fail.
32+
33+ Returns:
34+ spec (dict[str, Any]): Parsed OpenAPI specification.
35+
36+ Raises:
37+ AssertionError: Causes the test to fail through pytest.fail when the file is not found.
38+ """
2639 path = Path (OPENAPI_FILE )
2740 if path .is_file ():
2841 with path .open ("r" , encoding = "utf-8" ) as f :
@@ -33,7 +46,13 @@ def _load_openapi_spec_from_file() -> dict[str, Any]:
3346
3447
3548def _load_openapi_spec_from_url () -> dict [str , Any ]:
36- """Load OpenAPI specification from URL."""
49+ """Load OpenAPI specification from URL.
50+
51+ Retrieve the OpenAPI specification by requesting the application's /openapi.json endpoint.
52+
53+ Returns:
54+ dict[str, Any]: The parsed OpenAPI specification as a dictionary.
55+ """
3756 configuration_filename = "tests/configuration/lightspeed-stack-proper-name.yaml"
3857 cfg = configuration
3958 cfg .load_configuration (configuration_filename )
@@ -52,18 +71,42 @@ def _load_openapi_spec_from_url() -> dict[str, Any]:
5271
5372@pytest .fixture (scope = "module" , name = "spec_from_file" )
5473def open_api_spec_from_file () -> dict [str , Any ]:
55- """Fixture containing OpenAPI specification represented as a dictionary."""
74+ """Fixture containing OpenAPI specification represented as a dictionary.
75+
76+ Provides the parsed OpenAPI specification as a dictionary for tests.
77+
78+ Returns:
79+ openapi_spec (dict[str, Any]): The OpenAPI document parsed from docs/openapi.json.
80+ """
5681 return _load_openapi_spec_from_file ()
5782
5883
5984@pytest .fixture (scope = "module" , name = "spec_from_url" )
6085def open_api_spec_from_url () -> dict [str , Any ]:
61- """Fixture containing OpenAPI specification represented as a dictionary."""
86+ """Fixture containing OpenAPI specification represented as a dictionary.
87+
88+ Provides the OpenAPI specification loaded from the running application's
89+ /openapi.json endpoint.
90+
91+ Returns:
92+ dict: The OpenAPI document parsed into a dictionary.
93+ """
6294 return _load_openapi_spec_from_url ()
6395
6496
6597def _check_openapi_top_level_info (spec : dict [str , Any ]) -> None :
66- """Check all top level informations stored in OpenAPI specification."""
98+ """Check all top level informations stored in OpenAPI specification.
99+
100+ Checks that the OpenAPI version, info section (title and version), contact, and license
101+ (name and URL) match the expected values used by the project.
102+
103+ Parameters:
104+ spec (dict): Parsed OpenAPI specification document.
105+
106+ Raises:
107+ AssertionError: If any required top-level field is missing or does not
108+ match the expected value.
109+ """
67110 assert spec .get ("openapi" ) == "3.1.0"
68111
69112 info = spec .get ("info" ) or {}
@@ -79,7 +122,14 @@ def _check_openapi_top_level_info(spec: dict[str, Any]) -> None:
79122
80123
81124def _check_server_section_present (spec : dict [str , Any ]) -> None :
82- """Check if the servers section stored in OpenAPI specification."""
125+ """Check if the servers section stored in OpenAPI specification.
126+
127+ Parameters:
128+ spec (dict[str, Any]): Parsed OpenAPI specification.
129+
130+ Raises:
131+ AssertionError: If the 'servers' field is missing, not a list, or empty.
132+ """
83133 servers = spec .get ("servers" )
84134 assert isinstance (servers , list ) and servers , "servers must be a non-empty list"
85135
@@ -89,14 +139,14 @@ def _check_paths_and_responses_exist(
89139) -> None :
90140 """Checks if the specified paths and responses exist in the API specification.
91141
92- Args :
93- spec (dict): The API specification.
94- path (str): The API endpoint path to check.
95- method (str): The HTTP method to check.
96- expected_codes (set[str]): The set of expected HTTP status codes.
142+ Parameters :
143+ spec (dict): The API specification.
144+ path (str): The API endpoint path to check.
145+ method (str): The HTTP method to check.
146+ expected_codes (set[str]): The set of expected HTTP status codes.
97147
98- Raises:
99- AssertionError: If the path, method, or any of the expected response codes are missing.
148+ Raises:
149+ AssertionError: If the path, method, or any of the expected response codes are missing.
100150 """
101151 paths = spec .get ("paths" ) or {}
102152 assert path in paths , f"Missing path: { path } "
@@ -111,12 +161,29 @@ def _check_paths_and_responses_exist(
111161
112162
113163def test_openapi_top_level_info_from_file (spec_from_file : dict [str , Any ]) -> None :
114- """Test all top level informations stored in OpenAPI specification."""
164+ """Test all top level informations stored in OpenAPI specification.
165+
166+ Asserts that the OpenAPI version, info (title and version), contact, and
167+ license fields meet the repository's expected values.
168+
169+ Parameters:
170+ spec_from_file (dict[str, Any]): OpenAPI specification dictionary
171+ loaded from docs/openapi.json.
172+ """
115173 _check_openapi_top_level_info (spec_from_file )
116174
117175
118176def test_openapi_top_level_info_from_url (spec_from_url : dict [str , Any ]) -> None :
119- """Test all top level informations stored in OpenAPI specification."""
177+ """Test all top level informations stored in OpenAPI specification.
178+
179+ Asserts that the OpenAPI version, info section (including title, version,
180+ and contact), and license (name and URL) meet the project's expectations
181+ used by the tests.
182+
183+ Parameters:
184+ spec_from_url (dict[str, Any]): OpenAPI document parsed from the
185+ application's /openapi.json endpoint.
186+ """
120187 _check_openapi_top_level_info (spec_from_url )
121188
122189
@@ -196,7 +263,19 @@ def test_servers_section_present_from_url(spec_from_url: dict[str, Any]) -> None
196263def test_paths_and_responses_exist_from_file (
197264 spec_from_file : dict , path : str , method : str , expected_codes : set [str ]
198265) -> None :
199- """Tests all paths defined in OpenAPI specification."""
266+ """Tests all paths defined in OpenAPI specification.
267+
268+ Verify that the given path and HTTP method are defined in the provided
269+ OpenAPI specification and that the operation contains all expected response
270+ status codes.
271+
272+ Parameters:
273+ spec_from_file (dict): OpenAPI specification document loaded from the local file.
274+ path (str): API path to check (e.g., "/items/{id}").
275+ method (str): HTTP method to check for the path (e.g., "get", "post").
276+ expected_codes (set[str]): Set of expected HTTP response status codes
277+ as strings (e.g., {"200", "404"}).
278+ """
200279 _check_paths_and_responses_exist (spec_from_file , path , method , expected_codes )
201280
202281
@@ -266,5 +345,17 @@ def test_paths_and_responses_exist_from_file(
266345def test_paths_and_responses_exist_from_url (
267346 spec_from_url : dict , path : str , method : str , expected_codes : set [str ]
268347) -> None :
269- """Tests all paths defined in OpenAPI specification."""
348+ """Tests all paths defined in OpenAPI specification.
349+
350+ Verify that the OpenAPI spec served at /openapi.json contains the given
351+ path and HTTP method and that the operation declares the specified response
352+ status codes.
353+
354+ Parameters:
355+ path (str): OpenAPI path string to check (for example, "/items/{id}").
356+ method (str): HTTP method name for the operation to check (e.g., "get",
357+ "post"); case-insensitive.
358+ expected_codes (set[str]): Set of response status code strings expected
359+ to be present for the operation (for example, {"200", "404"}).
360+ """
270361 _check_paths_and_responses_exist (spec_from_url , path , method , expected_codes )
0 commit comments