From fd0a0f2713dab8d1df45aebdd8560609ce615a02 Mon Sep 17 00:00:00 2001 From: wwakabobik Date: Tue, 20 Aug 2024 22:03:14 +0200 Subject: [PATCH] 100% cover "reporter_utils" --- tests/conftest.py | 26 ++++++++++ tests/utils/test_reporter_utils_cqptions.py | 48 +++++++++++++++++++ .../utils/test_reporter_utils_delete_file.py | 29 +++++------ .../test_reporter_utils_init_get_cases.py | 15 ++++++ tests/utils/test_reporter_utils_zip_file.py | 30 ++++++++++++ 5 files changed, 130 insertions(+), 18 deletions(-) create mode 100644 tests/utils/test_reporter_utils_cqptions.py create mode 100644 tests/utils/test_reporter_utils_init_get_cases.py create mode 100644 tests/utils/test_reporter_utils_zip_file.py diff --git a/tests/conftest.py b/tests/conftest.py index 72e18db..1be4f13 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1 +1,27 @@ """Conftest for testsuite""" + +from os import path, remove + +import pytest +from faker import Faker + + +@pytest.fixture +def create_test_file(): + """ + Fixture to create random test file + + :return: filename + :rtype: str + """ + test_file = f"not_existing_{Faker().file_name()}" + with open(test_file, "w", encoding="utf-8") as file: + file.write("Test") + assert path.exists(test_file) is True + yield test_file + # Cleanup if not removed by tests + try: + remove(test_file) + except FileNotFoundError: + pass + diff --git a/tests/utils/test_reporter_utils_cqptions.py b/tests/utils/test_reporter_utils_cqptions.py new file mode 100644 index 0000000..a153e67 --- /dev/null +++ b/tests/utils/test_reporter_utils_cqptions.py @@ -0,0 +1,48 @@ +"""Tests for the reporter_utils module, function 'check_captions_and_files'""" + +from logging import getLogger +from random import randint +from unittest.mock import MagicMock + +from faker import Faker + +from testrail_api_reporter.utils.reporter_utils import check_captions_and_files # pylint: disable=import-error,no-name-in-module + +faker = Faker() + +def test_check_captions_and_files_not_list(): + """Test check_captions_and_files when captions is not a list""" + captions = faker.sentence() + files = [faker.file_name() for _ in range(randint(1, 10))] + + logger = getLogger(__name__) + logger.debug = MagicMock() + + result = check_captions_and_files(captions, files, debug=True, logger=logger) + assert result is None + logger.debug.assert_called_once_with("Captions are not a list, thus no legend will be displayed") + +def test_check_captions_and_files_different_length(): + """Test check_captions_and_files when captions and files have different lengths""" + captions = [faker.sentence() for _ in range(randint(1, 10))] + files = [faker.file_name() for _ in range(randint(1, 10))] + + logger = getLogger(__name__) + logger.debug = MagicMock() + + result = check_captions_and_files(captions, files, debug=True, logger=logger) + assert result is None + logger.debug.assert_called_once_with( + "Caption and file lists are not the same length %s != %s thus no legend will be displayed", + len(captions), + len(files), + ) + +def test_check_captions_and_files_same_length(): + """Test check_captions_and_files when captions and files have the same length""" + length = randint(1, 10) + captions = [faker.sentence() for _ in range(length)] + files = [faker.file_name() for _ in range(length)] + + result = check_captions_and_files(captions, files, debug=False) + assert result == captions diff --git a/tests/utils/test_reporter_utils_delete_file.py b/tests/utils/test_reporter_utils_delete_file.py index c4fb83e..8f0a553 100644 --- a/tests/utils/test_reporter_utils_delete_file.py +++ b/tests/utils/test_reporter_utils_delete_file.py @@ -1,35 +1,28 @@ """Tests for the reporter_utils module, function 'delete_file'""" -import os +from os import path from unittest.mock import MagicMock +from faker import Faker + from testrail_api_reporter.utils.reporter_utils import delete_file # pylint: disable=import-error,no-name-in-module -def test_delete_file(): +def test_delete_file(create_test_file): """Test delete file""" - test_file = "test_file.txt" - with open(test_file, "w", encoding="utf-8") as file: - file.write("Test") - assert os.path.exists(test_file) is True - delete_file(test_file, debug=False) + delete_file(create_test_file, debug=False) - assert os.path.exists(test_file) is False + assert path.exists(create_test_file) is False -def test_delete_file_with_debug(): +def test_delete_file_with_debug(create_test_file): """Test delete file with debug output""" - test_file = "test_file.txt" - with open(test_file, "w", encoding="utf-8") as file: - file.write("Test") - - assert os.path.exists(test_file) is True mock_logger = MagicMock() - delete_file(test_file, debug=True, logger=mock_logger) + delete_file(create_test_file, debug=True, logger=mock_logger) - assert os.path.exists(test_file) is False - mock_logger.debug.assert_called_once_with(f"Removed {test_file}") + assert path.exists(create_test_file) is False + mock_logger.debug.assert_called_once_with(f"Removed {create_test_file}") def test_delete_file_non_existent(capfd): @@ -38,7 +31,7 @@ def test_delete_file_non_existent(capfd): :param capfd - fixture of cap failure logger """ - delete_file("non_existent_file.txt", debug=True) + delete_file(Faker().file_name(), debug=True) _, err = capfd.readouterr() assert "No such file or directory" in err diff --git a/tests/utils/test_reporter_utils_init_get_cases.py b/tests/utils/test_reporter_utils_init_get_cases.py new file mode 100644 index 0000000..e95fef9 --- /dev/null +++ b/tests/utils/test_reporter_utils_init_get_cases.py @@ -0,0 +1,15 @@ +"""Tests for the reporter_utils module, function 'init_get_cases_process'""" + +from testrail_api_reporter.utils.reporter_utils import init_get_cases_process # pylint: disable=import-error,no-name-in-module + + +def test_init_get_cases_process(): + """Test init_get_cases_process""" + cases_list, first_run, criteria, response, retry = init_get_cases_process() + + assert isinstance(cases_list, list) + assert cases_list == [] + assert first_run is True + assert criteria is None + assert response is None + assert retry == 0 diff --git a/tests/utils/test_reporter_utils_zip_file.py b/tests/utils/test_reporter_utils_zip_file.py new file mode 100644 index 0000000..a18a142 --- /dev/null +++ b/tests/utils/test_reporter_utils_zip_file.py @@ -0,0 +1,30 @@ +"""Tests for the reporter_utils module, function 'zip_file'""" + +import os +import logging +from unittest.mock import MagicMock +from testrail_api_reporter.utils.reporter_utils import zip_file # pylint: disable=import-error,no-name-in-module + +def test_zip_file_default(create_test_file): + """Test zip file with default parameters""" + zipped_file = zip_file(create_test_file, debug=False) + assert os.path.exists(zipped_file) is True + os.remove(zipped_file) + os.remove(create_test_file) + +def test_zip_file_suffix(create_test_file): + """Test zip file with custom suffix""" + zipped_file = zip_file(create_test_file, suffix="_suffix", debug=False) + assert os.path.exists(zipped_file) is True + os.remove(zipped_file) + os.remove(create_test_file) + +def test_zip_file_logger(create_test_file): + """Test zip file with logger""" + logger = logging.getLogger(__name__) + logger.debug = MagicMock() + zipped_file = zip_file(create_test_file, debug=True, logger=logger) + assert os.path.exists(zipped_file) is True + logger.debug.assert_called_once_with(f"ZIPped {create_test_file} to {zipped_file}") + os.remove(zipped_file) + os.remove(create_test_file)