Skip to content

Commit

Permalink
100% cover "reporter_utils"
Browse files Browse the repository at this point in the history
  • Loading branch information
wwakabobik committed Aug 20, 2024
1 parent 6e87fe5 commit fd0a0f2
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 18 deletions.
26 changes: 26 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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

48 changes: 48 additions & 0 deletions tests/utils/test_reporter_utils_cqptions.py
Original file line number Diff line number Diff line change
@@ -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
29 changes: 11 additions & 18 deletions tests/utils/test_reporter_utils_delete_file.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
15 changes: 15 additions & 0 deletions tests/utils/test_reporter_utils_init_get_cases.py
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions tests/utils/test_reporter_utils_zip_file.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit fd0a0f2

Please sign in to comment.