Files
ISSSTELEON/Transformation/tests/generic/vigencias_sin_traslape.sql
T
rockdrigo d6e2c68dbf dbt: silver's source mappings declared, and a test that no form maps to two targets while both are in force
How: `cat_sistema_fuente` and `cat_mapeo` are silver sources in every domain; the generic test
`vigencias_sin_traslape` self-joins the rows of the same form (compared null-safe) and fails on each pair
with a different target and overlapping validity, in portable SQL (`date_literal` for the open end). Its
arguments stay at the test's top level, which dbt-core 1.7 (the MySQL group) reads.

Implications: a mapping written past the application's check (by hand, by a script, by a restore) shows up
in `dbt test`. Checked on the three domains' seeded databases (pass) and with a planted clash (1 failure).
2026-11-13 15:35:01 -06:00

22 lines
1.0 KiB
SQL

{#
Rows that describe the same thing (`forma`, compared null-safe) with a different target (`destino`) and
validities that overlap: each pair is a failure. It is the non-overlap rule of a table with validity dates
(`vigente_desde`, `vigente_hasta`, NULL while current), which no constraint expresses on every engine.
#}
{% test vigencias_sin_traslape(model, forma, destino, llave, desde='vigente_desde', hasta='vigente_hasta') %}
select a.{{ llave }} as primera, b.{{ llave }} as segunda
from {{ model }} a
join {{ model }} b
on a.{{ llave }} < b.{{ llave }}
{%- for columna in forma %}
and (a.{{ columna }} = b.{{ columna }} or (a.{{ columna }} is null and b.{{ columna }} is null))
{%- endfor %}
where (
{%- for columna in destino %}
coalesce(a.{{ columna }}, '') <> coalesce(b.{{ columna }}, ''){% if not loop.last %} or{% endif %}
{%- endfor %}
)
and a.{{ desde }} <= coalesce(b.{{ hasta }}, {{ date_literal('9999-12-31') }})
and b.{{ desde }} <= coalesce(a.{{ hasta }}, {{ date_literal('9999-12-31') }})
{% endtest %}