Skip to content

Commit

Permalink
Made TestSuite component.
Browse files Browse the repository at this point in the history
  • Loading branch information
Klus3kk committed Dec 16, 2024
1 parent 0be977e commit cf0b1eb
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
artify.egg-info
src/__pycache__
__pycache__/
test
.pytest_cache
images/content/
Expand Down
14 changes: 10 additions & 4 deletions core/ImageProcessor.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
19 changes: 19 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
@@ -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."
18 changes: 0 additions & 18 deletions tests/test_artify.py

This file was deleted.

26 changes: 26 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -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."
21 changes: 0 additions & 21 deletions tests/test_fastapi_handler.py

This file was deleted.

13 changes: 0 additions & 13 deletions tests/test_image_processor.py

This file was deleted.

16 changes: 16 additions & 0 deletions tests/test_interface.py
Original file line number Diff line number Diff line change
@@ -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."
5 changes: 0 additions & 5 deletions tests/test_style_transfer_model.py

This file was deleted.

22 changes: 22 additions & 0 deletions tests/test_utilities.py
Original file line number Diff line number Diff line change
@@ -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."

0 comments on commit cf0b1eb

Please sign in to comment.