Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions my_tiny_service/api/routers/maths.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,18 @@ def division(maths_input: MathsIn) -> MathsResult:
status_code=starlette.status.HTTP_400_BAD_REQUEST,
detail="Division by zero is not allowed",
) from e

@router.post(
"/exponentiation",
summary="Calculate the exponentiation of two numbers",
response_model=MathsResult,
)
def exponentiation(maths_input: MathsIn) -> MathsResult:
"""Calculates the exponentiation of two whole numbers."""
return MathsResult(
**maths_input.dict(),
operation="exponentiation",
result=maths_input.number1 ** maths_input.number2,
)


15 changes: 15 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,18 @@ def test_divide_by_zero(client: starlette.testclient.TestClient) -> None:

# THEN the status code should be 400 (Bad request)
assert response.status_code == 400

def test_exponentiation(client: starlette.testclient.TestClient) -> None:
"""Test that the exponentiation endpoint correctly calculates A^B."""

# GIVEN the exponentiation path and two numbers for exponentiation
path = "/v1/maths/exponentiation"
body = {"number1": 2, "number2": 3}

# WHEN calling the api
response = client.post(path, json=body)

# THEN the result should be 8
assert response.json().get("result") == 8