"""FICHA360-B · banco de 20 casos (pre-registro §3): filas sintéticas con la forma de los crudos → construir.construir → esperado.""" import gzip, json, os, sys, tempfile sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import construir def nif(c, l="B"): return f"{l}{c:07d}0" SUB = "SUBVENCIÓN y ENTREGA DINERARIA SIN CONTRAPRESTACIÓN " def fila(cod, fecha, ben, instr=SUB, importe=1000, equiv=1000, edp=False): return {"codConcesion": cod, "fechaConcesion": fecha, "beneficiario": ben, "instrumento": instr, "importe": importe, "ayudaEquivalente": equiv, "_entidad_de_personas": edp} # El DNI del caso 3 es sintético y va partido para que la copia pública no lleve nada con forma de DNI def escribir(base): p = f"{base}/barrido_2022_20261111/paginas"; os.makedirs(p) pag0 = [fila("SB1", "2022-03-01", nif(1) + " UNA SL"), fila("SB2", "2022-04-01", nif(2) + " DOS SL"), fila("SB3", "2022-05-01", "1234" + "5678" + "Z PERSONA FISICA"), fila("SB4", "2022-05-02", "***1234** PERSONA")] pag1 = [fila("SB2", "2022-04-01", nif(2) + " DOS SL"), fila("SB5", "2022-06-01", nif(5, "E") + " COMUNIDAD CB"), fila("SB6", "2022-06-02", nif(6, "J") + " SOCIEDAD CIVIL"), fila("SB7", "2022-06-03", nif(7, "P") + " AYUNTAMIENTO"), fila("SB8", "2022-06-04", nif(8, "Q") + " ORGANISMO"), fila("SB9", "2022-06-05", nif(9, "S") + " MINISTERIO")] for i, pg in enumerate((pag0, pag1)): with gzip.open(f"{p}/2022-01-01_2022-01-07_p{i:03d}.json.gz", "wt") as f: json.dump({"content": [{k: v for k, v in x.items() if k != "_entidad_de_personas"} for x in pg], "totalElements": 99}, f) def jl(sub, nombre, filas): d = f"{base}/{sub}/2023"; os.makedirs(d, exist_ok=True) with gzip.open(f"{d}/{nombre}.jsonl.gz", "wt") as f: for x in filas: f.write(json.dumps(x, ensure_ascii=False) + "\n") jl("concesiones", "a", [fila("SB2", "2022-04-01", nif(2) + " DOS SL"), fila("SB10", "2023-01-10", nif(10) + " DIEZ SL", edp=True), fila("GR11", "2023-02-01", nif(11) + " ONCE SL", "GARANTÍA", 100, 5), fila("SB11", "2023-03-01", nif(11) + " ONCE SL", importe=50, equiv=50), fila("PR12", "2024-02-01", nif(12) + " DOCE SL", "PRÉSTAMO", 2000, 300), fila("VF13", "2025-02-01", nif(13) + " TRECE SL", "VENTAJA FISCAL", 70, 70), fila("OT14", "2025-03-01", nif(14) + " CATORCE SL", "APORTACIÓN DE FINANCIACIÓN RIESGO", 900, 90), fila("SB19", "2021-12-30", nif(19) + " DIECINUEVE SL")]) ae = lambda cod, fecha, b: {"codConcesion": cod, "fechaConcesion": fecha, "beneficiario": b + " - X", "instrumento": SUB, "importe": 400, "ayudaEquivalente": 400, "_entidad_de_personas": False} jl("ayudasestado", "a", [ae("SB15", "2019-05-01", nif(15)), ae("SB16", "2023-05-01", nif(16))]) mi = lambda cod, fecha, b: {"codigoConcesion": cod, "fechaConcesion": fecha, "beneficiario": b + " X SL", "instrumento": SUB, "ayudaEquivalente": 300, "_entidad_de_personas": False} jl("minimis", "a", [mi("SB17a", "2020-02-01", nif(17)), mi("SB17b", "2020-03-01", nif(17)), mi("SB18", "2023-02-01", nif(18))]) ESPERADO = { # caso -> filas (via, instrumento, anio, n, importe, equivalente) de su NIF 1: [("concesion", "subvencion", 2022, 1, 1000.0, 1000.0)], 2: [("concesion", "subvencion", 2022, 1, 1000.0, 1000.0)], 3: [], 4: [], 5: [], 6: [], 7: [], 8: [], 9: [], 10: [], 11: [("concesion", "garantia", 2023, 1, 100.0, 5.0), ("concesion", "subvencion", 2023, 1, 50.0, 50.0)], 12: [("concesion", "prestamo", 2024, 1, 2000.0, 300.0)], 13: [("concesion", "ventaja_fiscal", 2025, 1, 70.0, 70.0)], 14: [("concesion", "otros", 2025, 1, 900.0, 90.0)], 15: [("ayuda_estado", "subvencion", 2019, 1, 400.0, 400.0)], 16: [], 17: [("minimis", "subvencion", 2020, 2, None, 600.0)], 18: [], 19: [], 20: []} LETRA = {5: "E", 6: "J", 7: "P", 8: "Q", 9: "S"} def main(): with tempfile.TemporaryDirectory() as base: escribir(base) con = construir.construir(base, None, "500MB") filas = con.execute("SELECT nif, via, instrumento, anio, n, importe, equivalente FROM vias ORDER BY 1,2,3,4").fetchall() por = {} for r in filas: por.setdefault(r[0], []).append(tuple(r[1:])) res = {} for c, esp in ESPERADO.items(): if c in (3, 4): # personas físicas: ninguna fila cuyo NIF no sea de las letras admitidas ok = all(k[0] in "ABCDFGHNRUVW" for k in por) else: got = sorted(por.get(nif(c, LETRA.get(c, "B")), [])) ok = got == sorted(esp) res[c] = ok ok = sum(res.values()) print(json.dumps({"aciertos": ok, "de": len(res), "pasa": ok == 20, "casos": res}, ensure_ascii=False)) return 0 if ok == 20 else 1 if __name__ == "__main__": sys.exit(main())