Files
IPSExtractor/tests/test_utils_patterns.py
T

44 lines
1.5 KiB
Python

from core.utils import _matches_pattern, find_first_matching_file
def test_matches_pattern_simple_contains():
assert _matches_pattern("Factura_123.pdf", "factura")
assert _matches_pattern("otro_factura_1.tif", "FACTURA")
assert not _matches_pattern("recibo_123.txt", "factura")
def test_matches_pattern_wildcard_without_extension():
# Patrón sin extensión se aplica sobre el nombre sin extensión
assert _matches_pattern("Factura_1.pdf", "*_1")
assert _matches_pattern("doc_OK1.tif", "doc_*1")
assert not _matches_pattern("Factura_10.pdf", "*_1") # termina en _10
def test_matches_pattern_wildcard_with_extension():
# Patrón con extensión válida se aplica al nombre completo
assert _matches_pattern("Factura_OK1.pdf", "*_OK?.pdf")
assert not _matches_pattern("Factura_OK1.tif", "*_OK?.pdf")
assert not _matches_pattern("Factura_OK12.pdf", "*_OK?.pdf") # demasiados caracteres
def test_matches_pattern_only_allowed_exts():
# Extensiones que no sean pdf/tif/tiff se ignoran
assert not _matches_pattern("Factura_1.png", "*_1")
def test_find_first_matching_file_uses_helper_and_sorts():
files = [
"Z_Factura_1.pdf",
"A_Factura_1.pdf",
"B_Factura_2.tif",
"nota.txt",
]
# Sin comodines: contiene
assert find_first_matching_file(files, "Factura_") == "A_Factura_1.pdf"
# Con comodines sin extensión
assert find_first_matching_file(files, "*_2") == "B_Factura_2.tif"
# Con comodines + extensión explícita
assert find_first_matching_file(files, "*_1.pdf") == "A_Factura_1.pdf"