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
Empty file added tests/__init__.py
Empty file.
Binary file added tests/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file not shown.
55 changes: 55 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -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
Comment on lines +4 to +10

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"})
Binary file added web/__pycache__/app.cpython-312.pyc
Binary file not shown.
Loading