From cf0b1eb1a89bcb37cc8da6b7a13e7316f2af60d3 Mon Sep 17 00:00:00 2001 From: Klus3kk Date: Mon, 16 Dec 2024 22:58:42 +0100 Subject: [PATCH] Made TestSuite component. --- .gitignore | 2 +- core/ImageProcessor.py | 14 ++++++++++---- tests/test_api.py | 19 +++++++++++++++++++ tests/test_artify.py | 18 ------------------ tests/test_core.py | 26 ++++++++++++++++++++++++++ tests/test_fastapi_handler.py | 21 --------------------- tests/test_image_processor.py | 13 ------------- tests/test_interface.py | 16 ++++++++++++++++ tests/test_style_transfer_model.py | 5 ----- tests/test_utilities.py | 22 ++++++++++++++++++++++ 10 files changed, 94 insertions(+), 62 deletions(-) create mode 100644 tests/test_api.py delete mode 100644 tests/test_artify.py create mode 100644 tests/test_core.py delete mode 100644 tests/test_fastapi_handler.py delete mode 100644 tests/test_image_processor.py create mode 100644 tests/test_interface.py delete mode 100644 tests/test_style_transfer_model.py create mode 100644 tests/test_utilities.py diff --git a/.gitignore b/.gitignore index 2b84a0f..7a17cc2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ artify.egg-info -src/__pycache__ +__pycache__/ test .pytest_cache images/content/ diff --git a/core/ImageProcessor.py b/core/ImageProcessor.py index f50ab57..4ffc09b 100644 --- a/core/ImageProcessor.py +++ b/core/ImageProcessor.py @@ -1,12 +1,18 @@ from PIL import Image +from io import BytesIO class ImageProcessor: @staticmethod - def preprocess_image(image_path, size=512): - '''Resize and normalize image for input.''' - image = Image.open(image_path).convert("RGB") + def preprocess_image(image_path_or_data, size=512): + if isinstance(image_path_or_data, (str, bytes)): + image = Image.open(image_path_or_data).convert("RGB") + elif isinstance(image_path_or_data, BytesIO): + image = Image.open(image_path_or_data).convert("RGB") + else: + raise ValueError("Invalid input type for image") image = image.resize((size, size)) - return image + return image + @staticmethod def save_image(image, output_path): diff --git a/tests/test_api.py b/tests/test_api.py new file mode 100644 index 0000000..e97852d --- /dev/null +++ b/tests/test_api.py @@ -0,0 +1,19 @@ +from fastapi.testclient import TestClient +from api.FastAPIHandler import app + +client = TestClient(app) + +def test_root_endpoint(): + response = client.get("/") + assert response.status_code == 200 + assert response.json() == {"message": "Welcome to Artify! Use /apply_style to stylize your images."} + +def test_apply_style_endpoint(): + with open("images/content/sample_content.jpg", "rb") as content, open("images/style/impressionism/sample_style.jpg", "rb") as style: + response = client.post( + "/apply_style/", + files={"content": ("sample_content.jpg", content, "image/jpeg")}, + data={"style_category": "impressionism"} + ) + assert response.status_code == 200 + assert "output_path" in response.json(), "API should return the output path." diff --git a/tests/test_artify.py b/tests/test_artify.py deleted file mode 100644 index a431030..0000000 --- a/tests/test_artify.py +++ /dev/null @@ -1,18 +0,0 @@ -from core.StyleTransferModel import StyleTransferModel -from core.ImageProcessor import ImageProcessor - -if __name__ == "__main__": - content_image_path = "path/to/content.jpg" - style_image_path = "path/to/style.jpg" - output_image_path = "path/to/output.jpg" - - processor = ImageProcessor() - model = StyleTransferModel() - - content_image = processor.preprocess_image(content_image_path) - style_image = processor.preprocess_image(style_image_path) - - styled_image = model.apply_style(content_image, style_image) - processor.save_image(styled_image, output_image_path) - - print("Style transfer completed!") diff --git a/tests/test_core.py b/tests/test_core.py new file mode 100644 index 0000000..e792264 --- /dev/null +++ b/tests/test_core.py @@ -0,0 +1,26 @@ +import pytest +from core.StyleTransferModel import StyleTransferModel +from core.ImageProcessor import ImageProcessor +from PIL import Image +from io import BytesIO + +def test_style_transfer_model_load(): + model = StyleTransferModel() + assert model.model is not None, "Model should be loaded successfully." + +def test_image_preprocessor_preprocess(): + processor = ImageProcessor() + image = Image.new("RGB", (800, 600), color="red") + image_data = BytesIO() + image.save(image_data, format="JPEG") + image_data.seek(0) + + processed_image = processor.preprocess_image(image_data) + assert processed_image.size == (512, 512), "Image should be resized to 512x512." + +def test_image_preprocessor_save(tmp_path): + processor = ImageProcessor() + image = Image.new("RGB", (512, 512), color="blue") + save_path = tmp_path / "test_image.jpg" + processor.save_image(image, save_path) + assert save_path.exists(), "Image should be saved successfully." diff --git a/tests/test_fastapi_handler.py b/tests/test_fastapi_handler.py deleted file mode 100644 index e2ee3b0..0000000 --- a/tests/test_fastapi_handler.py +++ /dev/null @@ -1,21 +0,0 @@ -from fastapi.testclient import TestClient -from api.FastAPIHandler import app - -client = TestClient(app) - -def test_root(): - response = client.get("/") - assert response.status_code == 200 - assert response.json() == { - "message": "Welcome to Artify! Use /apply_style to stylize your images." - } - -def test_apply_style(): - with open("path/to/content_image.jpg", "rb") as content, open("path/to/style_image.jpg", "rb") as style: - response = client.post( - "/apply_style/", - files={"content": ("content_image.jpg", content, "image/jpeg"), - "style": ("style_image.jpg", style, "image/jpeg")}, - ) - assert response.status_code == 200 - assert "output_path" in response.json() diff --git a/tests/test_image_processor.py b/tests/test_image_processor.py deleted file mode 100644 index e3fcbf5..0000000 --- a/tests/test_image_processor.py +++ /dev/null @@ -1,13 +0,0 @@ -import os -from core.ImageProcessor import ImageProcessor - -def test_preprocess_image(): - processor = ImageProcessor() - processed_image = processor.preprocess_image("") - assert processed_image.size == (512, 512) - -def test_save_image(): - processor = ImageProcessor() - sample_image = processor.preprocess_image("") - processor.save_image(sample_image, "") - assert os.path.exists("") diff --git a/tests/test_interface.py b/tests/test_interface.py new file mode 100644 index 0000000..042f9b2 --- /dev/null +++ b/tests/test_interface.py @@ -0,0 +1,16 @@ +import subprocess + +def test_cli_handler(): + command = [ + "python", + "interface/CLIHandler.py", + "--content", + "images/content/sample_content.jpg", + "--style_category", + "impressionism", + "--output", + "images/output/test_output.jpg" + ] + result = subprocess.run(command, capture_output=True, text=True) + assert result.returncode == 0, "CLI should execute without errors." + assert "Styled image saved to:" in result.stdout, "CLI should output success message." diff --git a/tests/test_style_transfer_model.py b/tests/test_style_transfer_model.py deleted file mode 100644 index 8834a3b..0000000 --- a/tests/test_style_transfer_model.py +++ /dev/null @@ -1,5 +0,0 @@ -from core.StyleTransferModel import StyleTransferModel - -def test_model_initialization(): - model = StyleTransferModel() - assert model.model is not None diff --git a/tests/test_utilities.py b/tests/test_utilities.py new file mode 100644 index 0000000..6d21eeb --- /dev/null +++ b/tests/test_utilities.py @@ -0,0 +1,22 @@ +import pytest +from utilities.StyleRegistry import StyleRegistry +from utilities.ConfigManager import ConfigManager +from utilities.Logger import Logger +import os + +def test_style_registry_random_selection(): + registry = StyleRegistry() + random_style = registry.get_random_style_image("impressionism") + assert random_style is not None, "Random style image should be selected." + assert "impressionism" in random_style, "Selected image should belong to the requested category." + +def test_config_manager(tmp_path): + config_path = tmp_path / "config.json" + config_data = {"output_dir": "images/output", "default_style": "impressionism"} + ConfigManager.save_config(config_data, config_path) + loaded_config = ConfigManager.load_config(config_path) + assert loaded_config == config_data, "Config should be saved and loaded correctly." + +def test_logger(): + logger = Logger.setup_logger() + assert logger is not None, "Logger should be set up successfully."