From 66e3c8757df0ea0df42b2c0e4e400a9abb3c7d02 Mon Sep 17 00:00:00 2001 From: Carlota de la Vega Date: Sun, 2 Jun 2024 16:13:30 +0200 Subject: [PATCH] Added tests for handler --- Makefile | 2 +- tests/test_handler.py | 73 ++++++++++++++++++++++++------------------- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index cd9cdf3..b057415 100644 --- a/Makefile +++ b/Makefile @@ -14,4 +14,4 @@ pylint: pylint --rcfile=.pylintrc src coverage: ## Run tests with coverage - pytest --cov=src --cov-report=term-missing \ No newline at end of file + pytest --cov=. --cov-report html:cov_html -s \ No newline at end of file diff --git a/tests/test_handler.py b/tests/test_handler.py index a2bd56f..c60b7ae 100644 --- a/tests/test_handler.py +++ b/tests/test_handler.py @@ -6,60 +6,67 @@ class TestHandler(unittest.TestCase): + def test_handle_receive_data(self): + socket_mock = MagicMock() + socket_mock.recv.return_value = b'{"command": "generate_number", "text": "5"}' + handler = Handler(socket_mock) + + data = handler._receive_data() + + self.assertEqual(data, '{"command": "generate_number", "text": "5"}') + + def test_process_request_valid_json(self): + handler = Handler(MagicMock()) + data = '{"command": "generate_number", "text": "5"}' + + request = handler._process_request(data) + + self.assertEqual(request, {"command": "generate_number", "text": "5"}) + + def test_process_request_invalid_json(self): + handler = Handler(MagicMock()) + data = '{"command": "generate_number", "text": "5"' + + request = handler._process_request(data) + + self.assertEqual(request["status"], "error") + self.assertIn("Error decoding JSON", request["message"]) + def test_execute_command_generate_number_success(self): - # Arrange - request = { - "command": "generate_number", - "text": "5" - } + request = {"command": "generate_number", "text": "5"} + expected_file_path = "data/nums/drawn_number_5.png" os.makedirs(os.path.dirname(expected_file_path), exist_ok=True) handler = Handler(MagicMock()) - # Act handler._execute_command(request) - - # Assert self.assertTrue(os.path.exists(expected_file_path)) - - # Cleanup os.remove(expected_file_path) def test_execute_command_generate_number_error(self): - # Arrange - request = { - "command": "generate_number", - "text": "abc" - } + request = {"command": "generate_number", "text": "abc"} expected_response = { "status": "error", - "message": "Error generating the image: invalid literal for int() with base 10: 'abc'" + "message": "Error generating the image: invalid literal for int() with base 10: 'abc'", } handler = Handler(MagicMock()) - # Act response = handler._execute_command(request) - - # Assert self.assertEqual(response, expected_response) def test_execute_command_unknown_command(self): - # Arrange - request = { - "command": "unknown_command", - "text": "5" - } - expected_response = { - "status": "error", - "message": "Unknown command: unknown_command" - } + request = {"command": "unknown_command", "text": "5"} + expected_response = {"status": "error", "message": "Unknown command: unknown_command"} handler = Handler(MagicMock()) - # Act response = handler._execute_command(request) - - # Assert self.assertEqual(response, expected_response) -if __name__ == '__main__': - unittest.main() \ No newline at end of file + def test_send_response(self): + socket_mock = MagicMock() + handler = Handler(socket_mock) + response = {"status": "success", "message": "Image generated successfully"} + + handler._send_response(response) + + socket_mock.sendall.assert_called_once_with(b'{"status": "success", "message": "Image generated successfully"}')