alessandro trinca tornidor
commited on
Commit
·
7024f75
1
Parent(s):
0c528ca
test: update test cases
Browse files- tests/test_app.py +20 -18
tests/test_app.py
CHANGED
|
@@ -12,6 +12,10 @@ class TestAppEndpoints(unittest.TestCase):
|
|
| 12 |
def setUp(self):
|
| 13 |
self.client = TestClient(app)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def test_health(self):
|
| 16 |
response = self.client.get("/health")
|
| 17 |
self.assertEqual(response.status_code, 200)
|
|
@@ -79,18 +83,30 @@ class TestAppEndpoints(unittest.TestCase):
|
|
| 79 |
self.assertEqual(response.status_code, 200)
|
| 80 |
self.assertEqual(response.json()["source"], "wordsapi")
|
| 81 |
|
| 82 |
-
@patch("my_ghost_writer.app.pymongo_operations_rw.get_document_by_word", side_effect=AssertionError("fail"))
|
| 83 |
@patch("my_ghost_writer.app.requests.get")
|
| 84 |
-
def
|
| 85 |
mock_response = MagicMock()
|
| 86 |
-
mock_response.status_code =
|
| 87 |
mock_response.json.return_value = {"error": "not found"}
|
| 88 |
mock_requests_get.return_value = mock_response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
with patch("my_ghost_writer.app.db_ok", {"mongo_ok": True}):
|
| 90 |
body = '{"query": "test"}'
|
| 91 |
response = self.client.post("/thesaurus-wordsapi", json=body)
|
| 92 |
self.assertEqual(response.status_code, 500)
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
@patch("my_ghost_writer.app.WORDSAPI_URL", "http://mocked-url.com")
|
| 96 |
@patch("my_ghost_writer.app.RAPIDAPI_HOST", "mocked-rapidapi-host.com")
|
|
@@ -122,20 +138,6 @@ class TestAppEndpoints(unittest.TestCase):
|
|
| 122 |
self.assertEqual(response.status_code, 500)
|
| 123 |
self.assertEqual(response.text, "")
|
| 124 |
|
| 125 |
-
|
| 126 |
-
@patch("my_ghost_writer.app.pymongo_operations_rw.get_document_by_word", side_effect=AssertionError("fail"))
|
| 127 |
-
@patch("my_ghost_writer.app.requests.get")
|
| 128 |
-
def test_thesaurus_wordsapi_remote_non_200(self, mock_requests_get, mock_get_doc):
|
| 129 |
-
mock_response = MagicMock()
|
| 130 |
-
mock_response.status_code = 400
|
| 131 |
-
mock_response.json.return_value = {"error": "not found"}
|
| 132 |
-
mock_requests_get.return_value = mock_response
|
| 133 |
-
with patch("my_ghost_writer.app.db_ok", {"mongo_ok": True}):
|
| 134 |
-
body = '{"query": "test"}'
|
| 135 |
-
response = self.client.post("/thesaurus-wordsapi", json=body)
|
| 136 |
-
self.assertEqual(response.status_code, 500)
|
| 137 |
-
self.assertIn("Error - Internal Server Error", response.text)
|
| 138 |
-
|
| 139 |
def test_lifespan(self):
|
| 140 |
# Test that lifespan yields and cancels the task
|
| 141 |
async def run_lifespan():
|
|
|
|
| 12 |
def setUp(self):
|
| 13 |
self.client = TestClient(app)
|
| 14 |
|
| 15 |
+
def tearDown(self) -> None:
|
| 16 |
+
self.client.close()
|
| 17 |
+
return super().tearDown()
|
| 18 |
+
|
| 19 |
def test_health(self):
|
| 20 |
response = self.client.get("/health")
|
| 21 |
self.assertEqual(response.status_code, 200)
|
|
|
|
| 83 |
self.assertEqual(response.status_code, 200)
|
| 84 |
self.assertEqual(response.json()["source"], "wordsapi")
|
| 85 |
|
|
|
|
| 86 |
@patch("my_ghost_writer.app.requests.get")
|
| 87 |
+
def test_thesaurus_wordsapi_remote_404(self, mock_requests_get):
|
| 88 |
mock_response = MagicMock()
|
| 89 |
+
mock_response.status_code = 404
|
| 90 |
mock_response.json.return_value = {"error": "not found"}
|
| 91 |
mock_requests_get.return_value = mock_response
|
| 92 |
+
with patch("my_ghost_writer.app.db_ok", {"mongo_ok": True}):
|
| 93 |
+
body = '{"query": "test"}'
|
| 94 |
+
response = self.client.post("/thesaurus-wordsapi", json=body)
|
| 95 |
+
self.assertEqual(response.status_code, 404)
|
| 96 |
+
response_json = response.json()
|
| 97 |
+
self.assertDictEqual({'msg': {'error': 'not found'}}, response_json)
|
| 98 |
+
|
| 99 |
+
@patch("my_ghost_writer.app.requests.get")
|
| 100 |
+
def test_thesaurus_wordsapi_remote_500(self, mock_requests_get):
|
| 101 |
+
mock_response = MagicMock()
|
| 102 |
+
mock_response.status_code = 500
|
| 103 |
+
mock_requests_get.side_effect = Exception("test exception")
|
| 104 |
with patch("my_ghost_writer.app.db_ok", {"mongo_ok": True}):
|
| 105 |
body = '{"query": "test"}'
|
| 106 |
response = self.client.post("/thesaurus-wordsapi", json=body)
|
| 107 |
self.assertEqual(response.status_code, 500)
|
| 108 |
+
response_text = response.text
|
| 109 |
+
self.assertEqual("", response_text)
|
| 110 |
|
| 111 |
@patch("my_ghost_writer.app.WORDSAPI_URL", "http://mocked-url.com")
|
| 112 |
@patch("my_ghost_writer.app.RAPIDAPI_HOST", "mocked-rapidapi-host.com")
|
|
|
|
| 138 |
self.assertEqual(response.status_code, 500)
|
| 139 |
self.assertEqual(response.text, "")
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
def test_lifespan(self):
|
| 142 |
# Test that lifespan yields and cancels the task
|
| 143 |
async def run_lifespan():
|