|
| 1 | +import unittest |
| 2 | +from unittest.mock import mock_open, patch, MagicMock |
| 3 | + |
| 4 | +from hackingBuddyGPT.usecases.web_api_testing.testing.test_handler import TestHandler |
| 5 | +from hackingBuddyGPT.usecases.web_api_testing.utils import LLMHandler |
| 6 | + |
| 7 | + |
| 8 | +class TestTestHandler(unittest.TestCase): |
| 9 | + def setUp(self): |
| 10 | + self.llm_handler = MagicMock(spec=LLMHandler) |
| 11 | + self.handler = TestHandler(self.llm_handler) |
| 12 | + |
| 13 | + def test_parse_test_case(self): |
| 14 | + note = "Test case for GET /users:\nDescription: Get all users\nInput Data: {}\nExpected Output: 200" |
| 15 | + parsed = self.handler.parse_test_case(note) |
| 16 | + self.assertEqual(parsed["description"], "Test case for GET /users") |
| 17 | + self.assertEqual(parsed["expected_output"], "200") |
| 18 | + |
| 19 | + @patch("builtins.open", new_callable=mock_open) |
| 20 | + def test_write_test_case_to_file(self, mock_open): |
| 21 | + test_case = {"input": {}, "expected_output": {}} |
| 22 | + self.handler.file = "mock_test_case.txt" # override to avoid real file writes |
| 23 | + self.handler.write_test_case_to_file("desc", test_case) |
| 24 | + mock_open().write.assert_called() |
| 25 | + |
| 26 | + @patch("builtins.open", new_callable=mock_open) |
| 27 | + def test_generate_test_case_and_write_output(self, mock_file): |
| 28 | + """ |
| 29 | + Advanced integration test for generating and writing a test case. |
| 30 | +
|
| 31 | + It verifies that: |
| 32 | + - the LLM handler is called correctly, |
| 33 | + - the generated test case contains expected data, |
| 34 | + - the output is written to file in the correct format. |
| 35 | + """ |
| 36 | + # Inputs |
| 37 | + analysis = "GET /status returns server status" |
| 38 | + endpoint = "/status" |
| 39 | + method = "GET" |
| 40 | + body = "{}" |
| 41 | + status_code = 200 |
| 42 | + prompt_history = [] |
| 43 | + |
| 44 | + # Call generate_test_case directly |
| 45 | + description, test_case, updated_history = self.handler.generate_test_case( |
| 46 | + analysis, endpoint, method, body, status_code, prompt_history |
| 47 | + ) |
| 48 | + |
| 49 | + # Assertions on the generated test case |
| 50 | + self.assertEqual(description, "Test case for GET /status") |
| 51 | + self.assertEqual(test_case["endpoint"], "/status") |
| 52 | + self.assertEqual(test_case["method"], "GET") |
| 53 | + self.assertEqual(test_case["expected_output"]["expected_status_code"], 200) |
| 54 | + self.assertEqual(test_case["expected_output"]["expected_body"], {"status": "ok"}) |
| 55 | + |
| 56 | + # Call write_test_case_to_file and check what was written |
| 57 | + self.handler.write_test_case_to_file(description, test_case) |
| 58 | + handle = mock_file() |
| 59 | + written_data = "".join(call.args[0] for call in handle.write.call_args_list) |
| 60 | + |
| 61 | + self.assertIn("Test case for GET /status", written_data) |
| 62 | + self.assertIn('"expected_status_code": 200', written_data) |
| 63 | + self.assertIn('"expected_body": {"status": "ok"}', written_data) |
| 64 | + |
0 commit comments