179 lines
6.3 KiB
Python
179 lines
6.3 KiB
Python
|
|
import pytest
|
||
|
|
from pathlib import Path
|
||
|
|
from src.core.utils import list_files
|
||
|
|
|
||
|
|
|
||
|
|
class TestUtils:
|
||
|
|
"""Testy pro utils funkce"""
|
||
|
|
|
||
|
|
@pytest.fixture
|
||
|
|
def temp_dir(self, tmp_path):
|
||
|
|
"""Fixture pro dočasný adresář s testovací strukturou"""
|
||
|
|
# Vytvoření souborů v root
|
||
|
|
(tmp_path / "file1.txt").write_text("content1")
|
||
|
|
(tmp_path / "file2.jpg").write_text("image")
|
||
|
|
|
||
|
|
# Podsložka
|
||
|
|
subdir1 = tmp_path / "subdir1"
|
||
|
|
subdir1.mkdir()
|
||
|
|
(subdir1 / "file3.txt").write_text("content3")
|
||
|
|
(subdir1 / "file4.png").write_text("image2")
|
||
|
|
|
||
|
|
# Vnořená podsložka
|
||
|
|
subdir2 = subdir1 / "subdir2"
|
||
|
|
subdir2.mkdir()
|
||
|
|
(subdir2 / "file5.txt").write_text("content5")
|
||
|
|
|
||
|
|
# Prázdná složka
|
||
|
|
empty_dir = tmp_path / "empty"
|
||
|
|
empty_dir.mkdir()
|
||
|
|
|
||
|
|
return tmp_path
|
||
|
|
|
||
|
|
def test_list_files_basic(self, temp_dir):
|
||
|
|
"""Test základního listování souborů"""
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
assert isinstance(files, list)
|
||
|
|
assert len(files) > 0
|
||
|
|
assert all(isinstance(f, Path) for f in files)
|
||
|
|
|
||
|
|
def test_list_files_finds_all_files(self, temp_dir):
|
||
|
|
"""Test že najde všechny soubory včetně vnořených"""
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
filenames = {f.name for f in files}
|
||
|
|
|
||
|
|
assert "file1.txt" in filenames
|
||
|
|
assert "file2.jpg" in filenames
|
||
|
|
assert "file3.txt" in filenames
|
||
|
|
assert "file4.png" in filenames
|
||
|
|
assert "file5.txt" in filenames
|
||
|
|
assert len(filenames) == 5
|
||
|
|
|
||
|
|
def test_list_files_recursive(self, temp_dir):
|
||
|
|
"""Test rekurzivního procházení složek"""
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
|
||
|
|
# Kontrola cest - měly by obsahovat subdir1 a subdir2
|
||
|
|
file_paths = [str(f) for f in files]
|
||
|
|
assert any("subdir1" in path for path in file_paths)
|
||
|
|
assert any("subdir2" in path for path in file_paths)
|
||
|
|
|
||
|
|
def test_list_files_only_files_no_directories(self, temp_dir):
|
||
|
|
"""Test že vrací pouze soubory, ne složky"""
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
|
||
|
|
# Všechny výsledky by měly být soubory
|
||
|
|
assert all(f.is_file() for f in files)
|
||
|
|
|
||
|
|
# Složky by neměly být ve výsledcích
|
||
|
|
filenames = {f.name for f in files}
|
||
|
|
assert "subdir1" not in filenames
|
||
|
|
assert "subdir2" not in filenames
|
||
|
|
assert "empty" not in filenames
|
||
|
|
|
||
|
|
def test_list_files_with_string_path(self, temp_dir):
|
||
|
|
"""Test s cestou jako string"""
|
||
|
|
files = list_files(str(temp_dir))
|
||
|
|
assert len(files) == 5
|
||
|
|
|
||
|
|
def test_list_files_with_path_object(self, temp_dir):
|
||
|
|
"""Test s cestou jako Path objekt"""
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
assert len(files) == 5
|
||
|
|
|
||
|
|
def test_list_files_empty_directory(self, temp_dir):
|
||
|
|
"""Test prázdné složky"""
|
||
|
|
empty_dir = temp_dir / "empty"
|
||
|
|
files = list_files(empty_dir)
|
||
|
|
assert files == []
|
||
|
|
|
||
|
|
def test_list_files_nonexistent_directory(self):
|
||
|
|
"""Test neexistující složky"""
|
||
|
|
with pytest.raises(NotADirectoryError) as exc_info:
|
||
|
|
list_files("/nonexistent/path")
|
||
|
|
assert "není platná složka" in str(exc_info.value)
|
||
|
|
|
||
|
|
def test_list_files_file_not_directory(self, temp_dir):
|
||
|
|
"""Test když je zadán soubor místo složky"""
|
||
|
|
file_path = temp_dir / "file1.txt"
|
||
|
|
with pytest.raises(NotADirectoryError) as exc_info:
|
||
|
|
list_files(file_path)
|
||
|
|
assert "není platná složka" in str(exc_info.value)
|
||
|
|
|
||
|
|
def test_list_files_returns_absolute_paths(self, temp_dir):
|
||
|
|
"""Test že vrací absolutní cesty"""
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
assert all(f.is_absolute() for f in files)
|
||
|
|
|
||
|
|
def test_list_files_different_extensions(self, temp_dir):
|
||
|
|
"""Test s různými příponami"""
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
extensions = {f.suffix for f in files}
|
||
|
|
|
||
|
|
assert ".txt" in extensions
|
||
|
|
assert ".jpg" in extensions
|
||
|
|
assert ".png" in extensions
|
||
|
|
|
||
|
|
def test_list_files_hidden_files(self, temp_dir):
|
||
|
|
"""Test se skrytými soubory (začínající tečkou)"""
|
||
|
|
# Vytvoření skrytého souboru
|
||
|
|
(temp_dir / ".hidden").write_text("hidden content")
|
||
|
|
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
filenames = {f.name for f in files}
|
||
|
|
|
||
|
|
# Skryté soubory by měly být také nalezeny
|
||
|
|
assert ".hidden" in filenames
|
||
|
|
|
||
|
|
def test_list_files_special_characters_in_names(self, temp_dir):
|
||
|
|
"""Test se speciálními znaky v názvech"""
|
||
|
|
# Vytvoření souborů se spec. znaky
|
||
|
|
(temp_dir / "soubor s mezerami.txt").write_text("content")
|
||
|
|
(temp_dir / "český_název.txt").write_text("content")
|
||
|
|
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
filenames = {f.name for f in files}
|
||
|
|
|
||
|
|
assert "soubor s mezerami.txt" in filenames
|
||
|
|
assert "český_název.txt" in filenames
|
||
|
|
|
||
|
|
def test_list_files_symlinks(self, temp_dir):
|
||
|
|
"""Test se symbolickými linky (pokud OS podporuje)"""
|
||
|
|
try:
|
||
|
|
# Vytvoření symlinku
|
||
|
|
target = temp_dir / "file1.txt"
|
||
|
|
link = temp_dir / "link_to_file1.txt"
|
||
|
|
link.symlink_to(target)
|
||
|
|
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
# Symlink by měl být také nalezen a považován za soubor
|
||
|
|
filenames = {f.name for f in files}
|
||
|
|
assert "link_to_file1.txt" in filenames or "file1.txt" in filenames
|
||
|
|
except OSError:
|
||
|
|
# Pokud OS nepodporuje symlinky, přeskočíme
|
||
|
|
pytest.skip("OS does not support symlinks")
|
||
|
|
|
||
|
|
def test_list_files_large_directory_structure(self, tmp_path):
|
||
|
|
"""Test s větší strukturou složek"""
|
||
|
|
# Vytvoření více vnořených úrovní
|
||
|
|
for i in range(3):
|
||
|
|
level_dir = tmp_path / f"level{i}"
|
||
|
|
level_dir.mkdir()
|
||
|
|
for j in range(5):
|
||
|
|
(level_dir / f"file_{i}_{j}.txt").write_text(f"content {i} {j}")
|
||
|
|
|
||
|
|
files = list_files(tmp_path)
|
||
|
|
# Měli bychom najít 3 * 5 = 15 souborů
|
||
|
|
assert len(files) == 15
|
||
|
|
|
||
|
|
def test_list_files_preserves_path_structure(self, temp_dir):
|
||
|
|
"""Test že zachovává strukturu cest"""
|
||
|
|
files = list_files(temp_dir)
|
||
|
|
|
||
|
|
# Najdeme soubor v subdir2
|
||
|
|
file5 = [f for f in files if f.name == "file5.txt"][0]
|
||
|
|
|
||
|
|
# Cesta by měla obsahovat obě složky
|
||
|
|
assert "subdir1" in str(file5)
|
||
|
|
assert "subdir2" in str(file5)
|