Skip to content

Commit

Permalink
Added Handler - execute command - test
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlota de la Vega committed Jun 1, 2024
1 parent 54649b8 commit fab854c
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from src import server

server = server.Server("localhost", 12345)
server.start()
if __name__ == "__main__":
server = server.Server("localhost", 12345)
server.start()
8 changes: 6 additions & 2 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,23 @@ def build_conditional_gan(generator, discriminator):
Returns:
conditionalGAN: Compiled cGAN model.
"""
cond_gan = cgan.ConditionalGAN(
config = cgan.GANConfig(
discriminator=discriminator,
generator=generator,
latent_dim=latent_dim,
image_size=image_size,
num_classes=num_classes,
)
cond_gan.compile(

cond_gan = cgan.ConditionalGAN(
config=config,
d_optimizer=keras.optimizers.Adam(learning_rate=0.0003),
g_optimizer=keras.optimizers.Adam(learning_rate=0.0003),
loss_fn=keras.losses.BinaryCrossentropy(from_logits=True),
)

cond_gan.compile()

return cond_gan


Expand Down
Empty file added tests/__init__.py
Empty file.
65 changes: 65 additions & 0 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import unittest
from unittest.mock import MagicMock

from src.handler import Handler


class TestHandler(unittest.TestCase):
def test_execute_command_generate_number_success(self):
# Arrange
request = {
"command": "generate_number",
"text": "5"
}
expected_file_path = "./data/nums/drawn_number_5.png"
handler = Handler(MagicMock())

# Act
handler._execute_command(request)

# Assert
self.assertTrue(os.path.exists(expected_file_path))

# Clean up
os.remove(expected_file_path)


def test_execute_command_generate_number_error(self):
# Arrange
request = {
"command": "generate_number",
"text": "abc"
}
expected_response = {
"status": "error",
"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"
}
handler = Handler(MagicMock())

# Act
response = handler._execute_command(request)

# Assert
self.assertEqual(response, expected_response)

if __name__ == '__main__':
unittest.main()

0 comments on commit fab854c

Please sign in to comment.