diff --git a/my_tiny_service/api/routers/maths.py b/my_tiny_service/api/routers/maths.py index 58098d9..c919913 100644 --- a/my_tiny_service/api/routers/maths.py +++ b/my_tiny_service/api/routers/maths.py @@ -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, + ) + + diff --git a/tests/test_api.py b/tests/test_api.py index faf1b23..07cddbe 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -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 + +