Skip to content

Commit

Permalink
Added tests for handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlota de la Vega committed Jun 2, 2024
1 parent ad39961 commit 66e3c87
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ pylint:
pylint --rcfile=.pylintrc src

coverage: ## Run tests with coverage
pytest --cov=src --cov-report=term-missing
pytest --cov=. --cov-report html:cov_html -s
73 changes: 40 additions & 33 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
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"}')

0 comments on commit 66e3c87

Please sign in to comment.