-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
299 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import unittest | ||
import logging | ||
from pathlib import Path | ||
|
||
# Relative imports of test modules | ||
from .tests.test_image_converter import ImageConverterTests | ||
from .tests.test_text_converter import TextConverterTests | ||
from .tests.test_main import MainModuleTests | ||
|
||
def setup_logging(): | ||
""" | ||
Sets up logging for the test results. | ||
""" | ||
log_file_path = Path(__file__).parent / 'test_results.log' | ||
logging.basicConfig(filename=log_file_path, | ||
level=logging.INFO, | ||
format='%(asctime)s:%(levelname)s:%(message)s') | ||
|
||
def create_test_suite(): | ||
""" | ||
Creates a unified test suite combining all tests from the three modules. | ||
""" | ||
test_suite = unittest.TestSuite() | ||
test_suite.addTest(unittest.makeSuite(ImageConverterTests)) | ||
test_suite.addTest(unittest.makeSuite(TextConverterTests)) | ||
test_suite.addTest(unittest.makeSuite(MainModuleTests)) | ||
|
||
return test_suite | ||
|
||
def configure_test_runner(): | ||
""" | ||
Configures a test runner that executes the test suite. | ||
""" | ||
return unittest.TextTestRunner(verbosity=2) | ||
|
||
def main(): | ||
""" | ||
Main function to run the test suite. | ||
""" | ||
setup_logging() | ||
suite = create_test_suite() | ||
runner = configure_test_runner() | ||
test_result = runner.run(suite) | ||
|
||
if not test_result.wasSuccessful(): | ||
logging.error(f"Number of failed tests: {len(test_result.failures)}") | ||
for test, traceback in test_result.failures: | ||
logging.error(f"{test.id()}: {traceback}") | ||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import pytest | ||
import os | ||
from PIL import Image | ||
from ..src.image_converter import ImageConverter, convert_image | ||
|
||
# Test data setup | ||
@pytest.fixture | ||
def setup_images(tmp_path): | ||
""" | ||
Setup fixture to create dummy images in different formats. | ||
""" | ||
bmp_path = tmp_path / "test.bmp" | ||
jpg_path = tmp_path / "test.jpg" | ||
png_path = tmp_path / "test.png" | ||
|
||
# Create a simple image in BMP, JPG, PNG formats | ||
img = Image.new("RGB", (100, 100), color="red") | ||
img.save(bmp_path, "BMP") | ||
img.save(jpg_path, "JPEG") | ||
img.save(png_path, "PNG") | ||
|
||
return bmp_path, jpg_path, png_path | ||
|
||
# Test Cases for Successful Conversions | ||
def test_convert_bmp_to_jpg(setup_images, tmp_path): | ||
bmp_path, _, _ = setup_images | ||
output_path = tmp_path / "output.jpg" | ||
convert_image(str(bmp_path), str(output_path)) | ||
assert os.path.exists(output_path) | ||
|
||
def test_convert_bmp_to_png(setup_images, tmp_path): | ||
bmp_path, _, _ = setup_images | ||
output_path = tmp_path / "output.png" | ||
convert_image(str(bmp_path), str(output_path)) | ||
assert os.path.exists(output_path) | ||
|
||
def test_copy_jpg(setup_images, tmp_path): | ||
_, jpg_path, _ = setup_images | ||
output_path = tmp_path / "copy.jpg" | ||
convert_image(str(jpg_path), str(output_path)) | ||
assert os.path.exists(output_path) | ||
|
||
def test_copy_png(setup_images, tmp_path): | ||
_, _, png_path = setup_images | ||
output_path = tmp_path / "copy.png" | ||
convert_image(str(png_path), str(output_path)) | ||
assert os.path.exists(output_path) | ||
|
||
# Test Cases for Failure Cases | ||
def test_unsupported_input_format(setup_images, tmp_path): | ||
_, _, _ = setup_images | ||
output_path = tmp_path / "output.unknown" | ||
with pytest.raises(ValueError): | ||
convert_image("unsupported.format", str(output_path)) | ||
|
||
def test_unsupported_output_format(setup_images, tmp_path): | ||
bmp_path, _, _ = setup_images | ||
output_path = tmp_path / "output.unknown" | ||
with pytest.raises(ValueError): | ||
convert_image(str(bmp_path), str(output_path)) | ||
|
||
def test_same_input_output_path(setup_images): | ||
bmp_path, _, _ = setup_images | ||
with pytest.raises(ValueError): | ||
convert_image(str(bmp_path), str(bmp_path)) | ||
|
||
# Test Cases for Edge Cases | ||
def test_nonexistent_input_file(tmp_path): | ||
nonexistent_path = tmp_path / "nonexistent.bmp" | ||
output_path = tmp_path / "output.jpg" | ||
with pytest.raises(FileNotFoundError): | ||
convert_image(str(nonexistent_path), str(output_path)) | ||
|
||
def test_invalid_file_path(): | ||
invalid_path = "/invalid/path/to/file.bmp" | ||
with pytest.raises(ValueError): | ||
convert_image(invalid_path, "output.jpg") | ||
|
||
# Utility Function Tests | ||
def test_supported_conversions(): | ||
expected_output = "Supported file formats and conversions:\n" \ | ||
"JPEG: can be converted to JPEG (no conversion needed)\n" \ | ||
"PNG: can be converted to PNG (no conversion needed)\n" \ | ||
"BMP: can be converted to JPEG or PNG" | ||
assert ImageConverter.supported_conversions() == expected_output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import pytest | ||
from unittest.mock import patch, MagicMock | ||
from ..main import * | ||
import os | ||
|
||
# Test Menu Display | ||
def test_print_menu(capsys): | ||
main.print_menu() | ||
captured = capsys.readouterr() | ||
assert "File Converter" in captured.out | ||
|
||
# File Conversion Tests | ||
@patch('main.input', side_effect=['1', '/path/to/pngfile.png', 'n']) | ||
@patch('main.convert_files') | ||
def test_convert_single_file(mock_convert_files, mock_input): | ||
with patch('os.path.exists', return_value=True), \ | ||
patch('os.path.isfile', return_value=True): | ||
main.main() | ||
mock_convert_files.assert_called_once() | ||
|
||
@patch('main.input', side_effect=['1', '/path/to/directory', 'n']) | ||
@patch('main.convert_files') | ||
def test_convert_batch_files(mock_convert_files, mock_input): | ||
with patch('os.listdir', return_value=['file1.png', 'file2.png']), \ | ||
patch('os.path.exists', return_value=True), \ | ||
patch('os.path.isdir', return_value=True): | ||
main.main() | ||
assert mock_convert_files.call_count == 1 | ||
|
||
# User Input Handling | ||
@patch('main.input', side_effect=['7', 'q']) | ||
def test_invalid_choice(mock_input, capsys): | ||
main.main() | ||
captured = capsys.readouterr() | ||
assert "Invalid choice!" in captured.out | ||
|
||
@patch('main.input', side_effect=['1', '/invalid/path.png', 'n']) | ||
def test_invalid_file_path(mock_input, capsys): | ||
with patch('os.path.exists', return_value=False): | ||
main.main() | ||
captured = capsys.readouterr() | ||
assert "Invalid path!" in captured.out | ||
|
||
# Error Handling Tests | ||
def test_unsupported_conversion_type(): | ||
with pytest.raises(ValueError): | ||
main.convert_extension("unsupported_type") | ||
|
||
@patch('main.input', side_effect=['1', '/nonexistent/path.png', 'n']) | ||
def test_nonexistent_path_handling(mock_input, capsys): | ||
with patch('os.path.exists', return_value=False): | ||
main.main() | ||
captured = capsys.readouterr() | ||
assert "Invalid path!" in captured.out |
Oops, something went wrong.