70 lines
2.1 KiB
Python
70 lines
2.1 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
from bot_descarga.config.settings import Settings
|
|
from bot_descarga.infrastructure.file_manager import FileManager
|
|
from bot_descarga.infrastructure.pdf_validator import PdfValidator
|
|
|
|
|
|
class AlwaysValid:
|
|
def validate(self, ruta: str):
|
|
return True, "OK"
|
|
|
|
|
|
class AlwaysInvalid:
|
|
def validate(self, ruta: str):
|
|
return False, "corrupto"
|
|
|
|
|
|
def test_ruta_final_crea_carpeta_nit():
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
fm = FileManager(tmp, AlwaysValid())
|
|
ruta = fm.ruta_final("900111", "12345")
|
|
assert Path(ruta).parent.exists()
|
|
assert Path(ruta).name == "12345.pdf"
|
|
assert "900111" in ruta
|
|
|
|
|
|
def test_exists_valid_cuando_no_existe():
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
fm = FileManager(tmp, AlwaysValid())
|
|
ok, ruta, motivo = fm.exists_valid("900", "1")
|
|
assert ok is False
|
|
assert motivo == "no existe"
|
|
assert ruta.endswith("1.pdf")
|
|
|
|
|
|
def test_exists_valid_cuando_valido():
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
fm = FileManager(tmp, AlwaysValid())
|
|
ruta = fm.ruta_final("900", "1")
|
|
Path(ruta).write_bytes(b"%PDF-dummy")
|
|
ok, _, motivo = fm.exists_valid("900", "1")
|
|
assert ok is True
|
|
assert motivo == "OK"
|
|
assert Path(ruta).exists()
|
|
|
|
|
|
def test_exists_valid_elimina_corrupto():
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
fm = FileManager(tmp, AlwaysInvalid())
|
|
ruta = fm.ruta_final("900", "1")
|
|
Path(ruta).write_bytes(b"basura")
|
|
ok, _, motivo = fm.exists_valid("900", "1")
|
|
assert ok is False
|
|
assert motivo == "corrupto"
|
|
assert not Path(ruta).exists()
|
|
|
|
|
|
def test_move_to_final():
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
fm = FileManager(tmp, PdfValidator(Settings(peso_minimo_bytes=1)))
|
|
src = Path(tmp) / "temp.pdf"
|
|
src.write_bytes(b"%PDF-1.4")
|
|
dest = fm.ruta_final("NIT1", "R1")
|
|
fm.move_to_final(str(src), dest)
|
|
assert Path(dest).exists()
|
|
assert not src.exists()
|