refactoring dos testes para o modulo 'scanner.py'
This commit is contained in:
@ -1,31 +1,55 @@
|
||||
# import unittest
|
||||
from pathlib import Path
|
||||
from py import path
|
||||
import pytest
|
||||
|
||||
# import tempfile
|
||||
from modules.scanner import listar_ficheiros
|
||||
|
||||
|
||||
# class TestScanner(unittest.TestCase):
|
||||
# def setUp(self) -> None:
|
||||
# # criar derectorio temporario
|
||||
# self.temp_dir = tempfile.TemporaryDirectory()
|
||||
# (Path(self.temp_dir.name) / "teste.txt").touch()
|
||||
# (Path(self.temp_dir.name) / "imagem.jpg").touch()
|
||||
#
|
||||
# def tearDown(self) -> None:
|
||||
# self.temp_dir.cleanup()
|
||||
#
|
||||
# def test_listar_ficheiros(self):
|
||||
# ficheiros = listar_ficheiros(self.temp_dir.name)
|
||||
# self.assertEqual(len(ficheiros), 2)
|
||||
# for f in ficheiros:
|
||||
# self.assertTrue(Path(f).exists())
|
||||
#
|
||||
#
|
||||
# if __name__ == "__main__":
|
||||
# unittest.main()
|
||||
def criar_estrutura_testes(base_dir):
|
||||
"""Cria uma estrutura de pastas e ficheiros para os testes"""
|
||||
(base_dir / "imagem.jpg").write_text("conteudo imagem")
|
||||
(base_dir / "documento.").write_text("conteudo documento")
|
||||
(base_dir / ".ficheiro_oculto.txt").write_text("conteudo ficheiro oculto")
|
||||
(base_dir / "subpasta").mkdir()
|
||||
(base_dir / "subpasta" / "video.mp4").write_text("conteudo video")
|
||||
|
||||
|
||||
def test_listar_ficheiros_todos(tmp_path):
|
||||
criar_estrutura_testes(tmp_path)
|
||||
ficheiros = listar_ficheiros(str(tmp_path))
|
||||
assert len(ficheiros) == 4 # 4 ficheiros no total (3 normais + 1 oculto)
|
||||
|
||||
|
||||
def test_listar_ficheiros_excluir_ocultos(tmp_path):
|
||||
criar_estrutura_testes(tmp_path)
|
||||
ficheiros = listar_ficheiros(str(tmp_path), incluir_ocultos=False)
|
||||
assert len(ficheiros) == 3
|
||||
assert not any(Path(f).name.startswith(".") for f in ficheiros)
|
||||
|
||||
|
||||
def test_listar_ficheiros_filtrar_por_extensao(tmp_path):
|
||||
criar_estrutura_testes(tmp_path)
|
||||
ficheiros = listar_ficheiros(str(tmp_path), extensoes=[".jpg", "mp4"])
|
||||
assert len(ficheiros) == 2
|
||||
assert all(Path(f).suffix.lower() in [".jpg", ".mp4"] for f in ficheiros)
|
||||
|
||||
|
||||
def test_listar_ficheiros_extensoes_sem_ponto(tmp_path):
|
||||
criar_estrutura_testes(tmp_path)
|
||||
ficheiros = listar_ficheiros(str(tmp_path), extensoes=["jpg", "mp4"])
|
||||
assert len(ficheiros) == 2
|
||||
assert all(Path(f).suffix.lower() in [".jpg", ".mp4"] for f in ficheiros)
|
||||
|
||||
|
||||
def test_listar_ficheiros_directorio_invalido():
|
||||
with pytest.raises(FileNotFoundError):
|
||||
listar_ficheiros("direntório_inexistente")
|
||||
|
||||
|
||||
def test_listar_ficheiros_nao_e_directorio(tmp_path):
|
||||
ficheiro = tmp_path / "arquivo.txt"
|
||||
ficheiro.write_text("conteudo")
|
||||
with pytest.raises(NotADirectoryError):
|
||||
listar_ficheiros(str(ficheiro))
|
||||
|
||||
|
||||
def test_listar_ficheiros_vazio(tmp_path):
|
||||
@ -61,3 +85,23 @@ def test_listar_ficheiros_em_subpastas(tmp_path):
|
||||
|
||||
assert len(ficheiros) == 1
|
||||
assert "ficheiro_sub.txt" in nome
|
||||
|
||||
|
||||
def test_listar_ficheiros_ocultos(tmp_path):
|
||||
ficheiro_oculto = tmp_path / ".oculto.txt"
|
||||
ficheiro_oculto.write_text("conteudo")
|
||||
|
||||
ficheiros = listar_ficheiros(str(tmp_path))
|
||||
nomes = [Path(f).name for f in ficheiros]
|
||||
|
||||
assert ".oculto.txt" in nomes
|
||||
|
||||
|
||||
def test_listar_ficheiros_nomes_especiais(tmp_path):
|
||||
ficheiro_especial = tmp_path / "ficheiro_éxamplo-çãõ.txt"
|
||||
ficheiro_especial.write_text("conteudo")
|
||||
|
||||
ficheiros = listar_ficheiros(str(tmp_path))
|
||||
nomes = [Path(f).name for f in ficheiros]
|
||||
|
||||
assert "ficheiro_éxamplo-çãõ.txt" in nomes
|
||||
|
||||
Reference in New Issue
Block a user