diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/__pycache__/__init__.cpython-312.pyc b/tests/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..227e5fd Binary files /dev/null and b/tests/__pycache__/__init__.cpython-312.pyc differ diff --git a/tests/__pycache__/test_app.cpython-312-pytest-9.0.2.pyc b/tests/__pycache__/test_app.cpython-312-pytest-9.0.2.pyc new file mode 100644 index 0000000..7accd6c Binary files /dev/null and b/tests/__pycache__/test_app.cpython-312-pytest-9.0.2.pyc differ diff --git a/tests/test_app.py b/tests/test_app.py new file mode 100644 index 0000000..146c0bf --- /dev/null +++ b/tests/test_app.py @@ -0,0 +1,55 @@ +import sys +from unittest.mock import MagicMock, patch + +# Mock inference module before importing app +sys.modules['inference'] = MagicMock() + +import unittest +from fastapi.testclient import TestClient +from web.app import app +import inference + +class TestApp(unittest.TestCase): + def setUp(self): + self.client = TestClient(app) + # Reset the mock before each test + inference.generate_solution.reset_mock() + inference.generate_solution.side_effect = None + inference.generate_solution.return_value = None + + def test_ui_index(self): + response = self.client.get("/ui") + self.assertEqual(response.status_code, 200) + + def test_root(self): + response = self.client.get("/") + self.assertEqual(response.status_code, 200) + self.assertEqual( + response.json(), + {"message": "OpenMath API. POST /solve with a problem to get a solution."} + ) + + def test_solve_empty_problem_error(self): + response = self.client.post("/solve", json={"problem": " "}) + self.assertEqual(response.status_code, 400) + self.assertEqual(response.json(), {"detail": "`problem` must be a non-empty string"}) + + def test_solve_success(self): + inference.generate_solution.side_effect = None + inference.generate_solution.return_value = "The solution is 2." + + response = self.client.post("/solve", json={"problem": "1 + 1"}) + self.assertEqual(response.status_code, 200) + self.assertEqual(response.json(), {"solution": "The solution is 2."}) + + def test_solve_error_path(self): + # Configure the mock to raise an exception + inference.generate_solution.side_effect = Exception("Mocked inference error") + + response = self.client.post( + "/solve", + json={"problem": "1 + 1"}, + ) + + self.assertEqual(response.status_code, 500) + self.assertEqual(response.json(), {"detail": "Mocked inference error"}) diff --git a/web/__pycache__/app.cpython-312.pyc b/web/__pycache__/app.cpython-312.pyc new file mode 100644 index 0000000..c0980cc Binary files /dev/null and b/web/__pycache__/app.cpython-312.pyc differ