234 lines
5.3 KiB
Python
234 lines
5.3 KiB
Python
from __future__ import annotations
|
|
|
|
import pandas as pd
|
|
|
|
from application.csv_cleaning import (
|
|
apply_rules,
|
|
homologate_doc_type,
|
|
split_doc_tipo_y_numero_texto,
|
|
)
|
|
|
|
|
|
def test_homologate_doc_type_aliases() -> None:
|
|
cases = {
|
|
"Cedula": "CC",
|
|
"C.C": "CC",
|
|
"C C": "CC",
|
|
"Registro Civil": "RC",
|
|
"Salvoconducto": "SC",
|
|
"TC": "RC",
|
|
"C\u00e9dula de Extranjeria": "CE",
|
|
}
|
|
|
|
for raw, expected in cases.items():
|
|
assert homologate_doc_type(raw) == expected
|
|
|
|
|
|
def test_split_doc_type_with_long_names() -> None:
|
|
cases = {
|
|
"Cedula 123.456": ("CC", "123456"),
|
|
"Registro Civil: 001-23": ("RC", "00123"),
|
|
"Permiso especial de permanencia 999": ("PE", "999"),
|
|
"Permiso por proteccion Temporal / 555": ("PT", "555"),
|
|
}
|
|
|
|
for raw, expected in cases.items():
|
|
assert split_doc_tipo_y_numero_texto(raw) == expected
|
|
|
|
|
|
def test_split_doc_type_with_abbreviations_and_noise() -> None:
|
|
cases = {
|
|
"C.C-123": ("CC", "123"),
|
|
"TL_987": ("TI", "987"),
|
|
"CE/1.234.567": ("CE", "1234567"),
|
|
"T1 # 44": ("TI", "44"),
|
|
}
|
|
|
|
for raw, expected in cases.items():
|
|
assert split_doc_tipo_y_numero_texto(raw) == expected
|
|
|
|
|
|
def test_split_unknown_type_preserves_clean_uppercase() -> None:
|
|
assert split_doc_tipo_y_numero_texto("ABC Especial: 123-45") == (
|
|
"ABC ESPECIAL",
|
|
"12345",
|
|
)
|
|
|
|
|
|
def test_split_without_homologation_preserves_clean_type() -> None:
|
|
assert split_doc_tipo_y_numero_texto(
|
|
"Cedula 123.456",
|
|
homologate=False,
|
|
) == ("CEDULA", "123456")
|
|
|
|
|
|
def test_apply_rules_homologates_type_column_only() -> None:
|
|
df = pd.DataFrame(
|
|
{
|
|
"tipo": [
|
|
"Cedula",
|
|
"C.C",
|
|
"Registro Civil",
|
|
"Salvoconducto",
|
|
"TC",
|
|
"Extranjera",
|
|
]
|
|
}
|
|
)
|
|
|
|
result, log = apply_rules(df, {"tipo": ["doc_type_homologate"]})
|
|
|
|
assert result["tipo"].tolist() == ["CC", "CC", "RC", "SC", "RC", "CE"]
|
|
assert any("Homologar tipo de documento" in line for line in log)
|
|
|
|
|
|
def test_apply_rules_split_uses_homologation_when_selected() -> None:
|
|
df = pd.DataFrame(
|
|
{
|
|
"documento": [
|
|
"Cedula 123.456",
|
|
"Registro Civil: 001-23",
|
|
"ABC Especial: 123-45",
|
|
]
|
|
}
|
|
)
|
|
|
|
result, _log = apply_rules(
|
|
df,
|
|
{"documento": ["doc_id_split_tipo_numero", "doc_type_homologate"]},
|
|
)
|
|
|
|
assert result["documento_doc_tipo"].tolist() == ["CC", "RC", "ABC ESPECIAL"]
|
|
assert result["documento_doc_numeros"].tolist() == ["123456", "00123", "12345"]
|
|
assert result["documento"].tolist() == df["documento"].tolist()
|
|
|
|
|
|
def test_apply_rules_split_can_skip_homologation() -> None:
|
|
df = pd.DataFrame({"documento": ["Cedula 123.456"]})
|
|
|
|
result, _log = apply_rules(df, {"documento": ["doc_id_split_tipo_numero"]})
|
|
|
|
assert result["documento_doc_tipo"].tolist() == ["CEDULA"]
|
|
assert result["documento_doc_numeros"].tolist() == ["123456"]
|
|
|
|
|
|
def test_apply_rules_unified_date_rule_numeric_and_separated() -> None:
|
|
df = pd.DataFrame(
|
|
{
|
|
"fecha": [
|
|
"20260426",
|
|
"26042026",
|
|
"260426",
|
|
"26/04/2026",
|
|
"04/26/2026",
|
|
"2026-04-26 10:30",
|
|
]
|
|
}
|
|
)
|
|
|
|
result, log = apply_rules(df, {"fecha": ["date_to_ddmmyyyy"]})
|
|
|
|
assert result["fecha"].tolist() == [
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
]
|
|
assert any("Normalizar fecha" in line for line in log)
|
|
|
|
|
|
def test_apply_rules_unified_date_rule_text_months() -> None:
|
|
df = pd.DataFrame(
|
|
{
|
|
"fecha": [
|
|
"26-abril-2026",
|
|
"26 abril 2026",
|
|
"26/abr/2026",
|
|
"abril 26 2026",
|
|
"2026 abril 26",
|
|
"15-sept-25",
|
|
"07 setiembre 2024",
|
|
]
|
|
}
|
|
)
|
|
|
|
result, _log = apply_rules(df, {"fecha": ["date_to_ddmmyyyy"]})
|
|
|
|
assert result["fecha"].tolist() == [
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
"15/09/2025",
|
|
"07/09/2024",
|
|
]
|
|
|
|
|
|
def test_apply_rules_unified_date_spanish_abbr_month_with_time() -> None:
|
|
df = pd.DataFrame(
|
|
{
|
|
"fecha": [
|
|
"15/may./2024 13:44",
|
|
"22/may./2024 11:09",
|
|
"30/04/2024",
|
|
]
|
|
}
|
|
)
|
|
|
|
result, _log = apply_rules(df, {"fecha": ["date_to_ddmmyyyy"]})
|
|
|
|
assert result["fecha"].tolist() == [
|
|
"15/05/2024",
|
|
"22/05/2024",
|
|
"30/04/2024",
|
|
]
|
|
|
|
|
|
def test_apply_rules_date_after_strip_special_spaces_glued_year_time() -> None:
|
|
df = pd.DataFrame({"fecha": ["15/may./2024 13:44"]})
|
|
|
|
result, _log = apply_rules(
|
|
df,
|
|
{"fecha": ["strip_special_spaces", "date_to_ddmmyyyy"]},
|
|
)
|
|
|
|
assert result["fecha"].tolist() == ["15/05/2024"]
|
|
|
|
|
|
def test_apply_rules_keep_date_seps_preserves_time_then_normalizes() -> None:
|
|
df = pd.DataFrame({"fecha": ["30/04/2024 13:44"]})
|
|
|
|
result, _log = apply_rules(
|
|
df,
|
|
{"fecha": ["strip_special_keep_date_seps", "date_to_ddmmyyyy"]},
|
|
)
|
|
|
|
assert result["fecha"].tolist() == ["30/04/2024"]
|
|
|
|
|
|
def test_apply_rules_unified_date_rule_noise_and_unparseable_values() -> None:
|
|
df = pd.DataFrame(
|
|
{
|
|
"fecha": [
|
|
"**26_abril_2026++",
|
|
"Fecha: 26-abril-2026",
|
|
"xx26/04/2026yy",
|
|
"no es fecha",
|
|
"31-febrero-2026",
|
|
]
|
|
}
|
|
)
|
|
|
|
result, _log = apply_rules(df, {"fecha": ["date_to_ddmmyyyy"]})
|
|
|
|
assert result["fecha"].tolist() == [
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
"26/04/2026",
|
|
"no es fecha",
|
|
"31-febrero-2026",
|
|
]
|