Skip to content

Commit 76e7783

Browse files
committed
Updated tests to mock the request
1 parent 7cb32e5 commit 76e7783

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

evaluation_function/evaluation_test.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from unittest.mock import Mock, patch
23

34
from .evaluation import Params, evaluation_function
45

@@ -21,10 +22,27 @@ class TestEvaluationFunction(unittest.TestCase):
2122
as it should.
2223
"""
2324

24-
def test_evaluation(self):
25-
response, answer, params = "Hello, World", "Hello, World", Params()
25+
@patch('requests.post')
26+
def test_evaluation(self, mock_post):
27+
mock_api_response = {
28+
"is_correct": True,
29+
"feedback": "",
30+
"error": None
31+
}
2632

27-
result = evaluation_function(response, answer, params).to_dict()
33+
mock_response_obj = Mock()
34+
mock_response_obj.status_code = 200
35+
mock_response_obj.json.return_value = mock_api_response
2836

37+
mock_post.return_value = mock_response_obj
38+
39+
# 4. Run your actual test logic
40+
response_input, answer, params = "x+y", "x+y", {"type": "structure"}
41+
42+
result = evaluation_function(response_input, answer, params).to_dict()
43+
44+
# 5. Assertions
2945
self.assertEqual(result.get("is_correct"), True)
3046
self.assertFalse(result.get("feedback", False))
47+
48+
mock_post.assert_called_once()

evaluation_function/preview_test.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import unittest
2+
from unittest.mock import patch, Mock
23

34
from .preview import Params, preview_function
45

@@ -21,7 +22,19 @@ class TestPreviewFunction(unittest.TestCase):
2122
as it should.
2223
"""
2324

24-
def test_preview(self):
25+
@patch('requests.post')
26+
def test_preview(self, mock_post):
27+
mock_api_response = {
28+
"latex_string": "A",
29+
"sympy_string": "A"
30+
}
31+
32+
mock_response_obj = Mock()
33+
mock_response_obj.status_code = 200
34+
mock_response_obj.json.return_value = mock_api_response
35+
36+
mock_post.return_value = mock_response_obj
37+
2538
response, params = "A", Params()
2639
result = preview_function(response, params)
2740

0 commit comments

Comments
 (0)