Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: tests/unit/test_utils: Parametrize and increase test coverage #1768

Merged
merged 6 commits into from
Aug 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import sys
import uuid

import pytest
from vorta.keyring.abc import VortaKeyring
from vorta.utils import (
find_best_unit_for_sizes,
get_path_datasize,
is_system_tray_available,
normalize_path,
pretty_bytes,
)

Expand Down Expand Up @@ -55,7 +59,9 @@ def test_best_unit_for_sizes_nonmetric(sizes, expected_unit):
],
)
def test_pretty_bytes_fixed_units(size, metric, precision, fixed_unit, expected_output):
# test pretty bytes when specifying a fixed unit of measurement
"""
test pretty bytes when specifying a fixed unit of measurement
"""
output = pretty_bytes(size, metric=metric, precision=precision, fixed_unit=fixed_unit)
assert output == expected_output

Expand All @@ -74,3 +80,60 @@ def test_pretty_bytes_nonfixed_units(size, metric, expected_output):
# test pretty bytes when NOT specifying a fixed unit of measurement
output = pretty_bytes(size, metric=metric, precision=1)
assert output == expected_output


def test_normalize_path():
"""
Test that path is normalized for macOS, but does nothing for other platforms.
"""
input_path = '/Users/username/caf\u00e9/file.txt'
expected_output = '/Users/username/café/file.txt'

actual_output = normalize_path(input_path)

if sys.platform == 'darwin':
assert actual_output == expected_output
else:
assert actual_output == input_path


def test_get_path_datasize(tmpdir):
"""
Test that get_path_datasize() works correctly when passed excluded patterns.
"""
# Create a temporary directory for testing
test_dir = tmpdir.mkdir("test_dir")
test_file = test_dir.join("test_file.txt")
test_file.write("Hello, World!")

# Create a subdirectory with a file to exclude
excluded_dir = test_dir.mkdir("excluded_dir")
excluded_file = excluded_dir.join("excluded_file.txt")
excluded_file.write("Excluded file, should not be checked.")

exclude_patterns = [f"{excluded_dir}"]

# Test when the path is a directory
data_size, files_count = get_path_datasize(str(test_dir), exclude_patterns)
assert data_size == len("Hello, World!")
assert files_count == 1

# Test when the path is a file
data_size, files_count = get_path_datasize(str(test_file), exclude_patterns)
assert data_size == len("Hello, World!")
assert files_count == 1

# Test when the path is a directory with an excluded file
data_size, files_count = get_path_datasize(str(excluded_dir), exclude_patterns)
assert data_size == 0
assert files_count == 0


def test_is_system_tray_available(mocker):
"""
sanity check to ensure proper behavior
"""
mocker.patch('PyQt6.QtWidgets.QSystemTrayIcon.isSystemTrayAvailable', return_value=False)
assert is_system_tray_available() is False
mocker.patch('PyQt6.QtWidgets.QSystemTrayIcon.isSystemTrayAvailable', return_value=True)
assert is_system_tray_available() is True