Skip to content

Commit ac08fa0

Browse files
committed
fix: type-hinting compliance
1 parent 078f9a9 commit ac08fa0

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

tests/features/environment.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1+
from behave.model import Scenario
12
from fastapi.testclient import TestClient
23

34
from api.service import app
5+
from tests.features.stubs import TestContext
46

57

6-
def before_all(context):
7-
print("\nSetting up resources for the entire test run...")
8+
def before_all(context: TestContext):
9+
print("Setting up resources for the entire test run...")
810
context.mock_server = TestClient(app)
911
context.setup_complete = True
1012
print("Loaded mock server, ready for feature tests!\n")
1113

1214

13-
def after_all(context):
15+
def after_all(context: TestContext):
1416
if context.setup_complete:
1517
print("Tearing down resources for the entire test run...")
16-
# Perform teardown activities here
1718
context.teardown_complete = True
1819

1920

20-
def before_scenario(context, scenario):
21+
def before_scenario(context: TestContext, scenario: Scenario):
2122
context.response = None

tests/features/steps/api_steps.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Dict
22

3-
from behave import given, then, when
3+
from behave import given, then, when # type: ignore
44

55
from tests.features.stubs import TestContext
66

@@ -15,7 +15,7 @@
1515
def __parse_keyword(keyword: str):
1616
keyword = keyword.lower()
1717
if keyword not in ("fizz", "buzz", "fizzbuzz"):
18-
raise ValueError(f"Invalid keyword")
18+
raise AssertionError(f"Invalid keyword")
1919
return keyword
2020

2121

@@ -38,24 +38,27 @@ def step_get_request(context: TestContext, endpoint_name: str):
3838
assert (
3939
endpoint_name in ENDPOINT_MAP.keys()
4040
), f"Endpoint '{endpoint_name}' is not recognized."
41-
endpoint = ENDPOINT_MAP.get(endpoint_name)
41+
endpoint = ENDPOINT_MAP[endpoint_name]
4242
context.response = context.mock_server.get(endpoint)
4343

4444

4545
@then("the response is returned with status code {status_code:d}")
4646
def step_evaluate_request_status(context: TestContext, status_code: int):
47+
assert context.response is not None, "Last response was null"
4748
assert (
4849
context.response.status_code == status_code
4950
), f"Expected status code {status_code} but got {context.response.status_code}"
5051

5152

5253
@then('the response JSON contains "{message:S}" in keys')
5354
def step_evaluate_response_message(context: TestContext, message: str):
55+
assert context.response is not None, "Last response was null"
5456
assert message in context.response.json()
5557

5658

5759
@then('the sequence contains {count:d} instances of "{word:S}"')
5860
def step_check_count(context: TestContext, count: int, word: str):
61+
assert context.response is not None, "Last response was null"
5962
key = __parse_keyword(word)
6063
instance_count = context.response.json()[key]
6164
assert (
@@ -65,6 +68,7 @@ def step_check_count(context: TestContext, count: int, word: str):
6568

6669
@then('an error is raised with "{message}" in "{key:S}"')
6770
def step_check_error_output(context: TestContext, message: str, key: str):
71+
assert context.response is not None, "Last response was null"
6872
response = context.response.json()["message"][key]
6973
assert (
7074
message in response

tests/features/stubs.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
from typing import Optional
1+
from typing import Optional, Protocol
22

33
from behave.runner import Context
4-
from fastapi import Response
54
from fastapi.testclient import TestClient
65

76

7+
class ResponseData(Protocol):
8+
status_code: int
9+
10+
def json(self) -> dict:
11+
...
12+
13+
814
class TestContext(Context):
915
"""Custom Fizzbuzz API context type stub.
1016
@@ -23,4 +29,4 @@ class TestContext(Context):
2329
mock_server: TestClient
2430
setup_complete: bool
2531
teardown_complete: bool
26-
response: Optional[Response]
32+
response: Optional[ResponseData]

0 commit comments

Comments
 (0)