"""FUENTES-A-LA-CADENA · banco sintético de construir_ue.py (pre-registro UE 4066af10… §3.1 y enmienda 1): parquets con las columnas y los TIPOS de los cargadores reales (fts_pj.parquet, cordis_organizaciones_pj.parquet) y el resultado esperado escrito a mano ANTES de correr. Incluye el caso de cero filas. Uso (contenedor de la ETL): python3 banco_ue.py """ import json import os import sys import tempfile import duckdb sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import construir_ue as CU # noqa: E402 EST = "An estimated repartition for each beneficiary was available in our central accounting system." ENT = "The repartition for each beneficiary was not available ... 100 % of the amount is displayed with the coordinator, which is usually the invoicing party." UNO = "The total commitment amount was attributed to a single beneficiary or in cases involving a consortium ..." PAR = "The total amount was divided by the number of beneficiaries in order to display a proportional repartition." # FTS-REPARTO (14-nov): la frase sola (sin decir cómo se muestra el importe) y el importe sin divulgar del art. 38.3.d, literales de la copia real NOD = "The repartition for each beneficiary was not available in our central accounting system at the time of publication." ART38 = ("In compliance with Article 38(3)(d) of the Financial Regulation, the contract amount will remain undisclosed for at least the duration of the " "framework contract under which it was awarded.") # (nif, ejercicio, programa, imp_contratado_benef, fuente_reparto, coordinador) FTS = [("A00000001", 2020, "LIFE", 100.0, EST, True), ("A00000001", 2020, "H2020", 200.0, None, False), ("B00000002", 2021, "CEF", 1000.0, ENT, True), ("B00000002", 2021, "CEF", 50.0, PAR, False), ("B00000002", 2021, "Prog: A", 10.0, EST, False), ("B00000002", 2022, "CEF", 7.0, UNO, False), ("C00000003", 2019, None, None, EST, False), ("Q2801660H", 2020, "CEF", 9e6, EST, False), # sector público: fuera ("E00000006", 2020, "LIFE", 5.0, EST, False), # entidad de personas: fuera ("1234567" + "8Z", 2020, "LIFE", 5.0, EST, False), # forma de persona física (partida para que el filtro de publicación no la tome por real): fuera (None, 2020, "LIFE", 5.0, EST, False), ("F00000005", 2023, "CEF", 36400.0, NOD, False), ("F00000005", 2023, "CEF", 100.0, EST, False), # FTS-REPARTO: la frase sola, sin reparto ("G00000007", 2024, "FWC", 0.0, ART38, False), ("G00000007", 2024, "FWC", None, ART38, False)] # FTS-REPARTO: sin divulgar, vacío # (nif, projectID, role, imp_ec, anio_firma, programa) CORDIS = [("A00000001", "P1", "coordinator", 500.0, 2021, "HORIZON"), ("A00000001", "P2", "participant", None, 2021, "H2020"), ("B00000002", "P3", "participant", 100.0, 2022, "H2020"), ("B00000002", "P3", "thirdParty", 50.0, 2022, "H2020"), ("D00000004", "P4", "associatedPartner", None, 2023, "HORIZON"), ("S2800000A", "P5", "coordinator", 70.0, 2023, "HORIZON")] # sector público: fuera # Esperado (nif, f, anio): (n, coord, importe, sin_reparto, importe_sin_reparto, programas) ESPERADO = {("A00000001", 0, 2020): (2, 1, 300.0, 0, None, "H2020:1|LIFE:1"), ("B00000002", 0, 2021): (3, 1, 10.0, 2, 1050.0, "CEF:2|Prog: A:1"), ("B00000002", 0, 2022): (1, 0, None, 1, 7.0, "CEF:1"), ("C00000003", 0, 2019): (1, 0, None, 0, None, "sin programa:1"), ("A00000001", 1, 2021): (2, 1, 500.0, None, None, "H2020:1|HORIZON:1"), ("B00000002", 1, 2022): (1, 0, 150.0, None, None, "H2020:1"), ("D00000004", 1, 2023): (1, 0, None, None, None, "HORIZON:1"), ("F00000005", 0, 2023): (2, 0, 100.0, 1, 36400.0, "CEF:2"), # FTS-REPARTO: los 36.400 € no son suyos ("G00000007", 0, 2024): (2, 0, None, 0, None, "FWC:2")} # FTS-REPARTO: ni «0 €» ni suma def main(): d = tempfile.mkdtemp(prefix="banco_ue_") c = duckdb.connect() c.execute("CREATE TABLE f (nif VARCHAR, ejercicio INTEGER, programa VARCHAR, imp_contratado_benef DOUBLE, fuente_reparto VARCHAR, coordinador BOOLEAN)") c.executemany("INSERT INTO f VALUES (?, ?, ?, ?, ?, ?)", FTS) c.execute(f"COPY f TO '{d}/fts.parquet' (FORMAT parquet)") c.execute("CREATE TABLE o (nif VARCHAR, projectID VARCHAR, role VARCHAR, imp_ec DOUBLE, anio_firma BIGINT, programa VARCHAR, programa_marco VARCHAR)") c.executemany("INSERT INTO o VALUES (?, ?, ?, ?, ?, ?, NULL)", CORDIS) c.execute(f"COPY o TO '{d}/cordis.parquet' (FORMAT parquet)") out = {} con = CU.construir(f"{d}/fts.parquet", f"{d}/cordis.parquet", None) got = {(r[0], int(r[1]), int(r[2])): tuple(r[3:]) for r in con.execute( "SELECT nif, f, anio, n, coord, importe, sin_reparto, importe_sin_reparto, programas FROM ue").fetchall()} casos = [] for k, e in ESPERADO.items(): g = got.get(k) ok = g is not None and g[0] == e[0] and g[1] == e[1] and ((g[2] is None) == (e[2] is None)) and (e[2] is None or abs(g[2] - e[2]) < 0.005) \ and g[3] == e[3] and ((g[4] is None) == (e[4] is None)) and (e[4] is None or abs(g[4] - e[4]) < 0.005) and g[5] == e[5] casos.append({"caso": "|".join(map(str, k)), "ok": bool(ok), "esperado": e, "obtenido": g}) sobran = sorted("|".join(map(str, k)) for k in set(got) - set(ESPERADO)) casos.append({"caso": "nada más (sector público, entidades de personas, personas físicas y NIF vacío fuera)", "ok": not sobran, "sobran": sobran}) # cero filas: las dos fuentes sin ninguna sociedad c.execute(f"COPY (SELECT * FROM f WHERE false) TO '{d}/fts0.parquet' (FORMAT parquet)") c.execute(f"COPY (SELECT * FROM o WHERE false) TO '{d}/cordis0.parquet' (FORMAT parquet)") n0 = CU.construir(f"{d}/fts0.parquet", f"{d}/cordis0.parquet", None).execute("SELECT count(*) FROM ue").fetchone()[0] casos.append({"caso": "cero filas", "ok": n0 == 0, "obtenido": n0}) out = {"casos": casos, "ok": sum(x["ok"] for x in casos), "de": len(casos)} json.dump(out, open(sys.argv[1], "w"), ensure_ascii=False, indent=1, default=str) print(json.dumps({"ok": out["ok"], "de": out["de"], "fallan": [x for x in casos if not x["ok"]]}, ensure_ascii=False, default=str)) if __name__ == "__main__": main()