Skip to content

Commit

Permalink
Corrige testes do subsídio
Browse files Browse the repository at this point in the history
  • Loading branch information
vtr363 committed Nov 1, 2024
1 parent 2250adc commit 7654705
Show file tree
Hide file tree
Showing 18 changed files with 257 additions and 417 deletions.
2 changes: 1 addition & 1 deletion queries/macros/custom_get_where_subquery.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% set where = where | replace("__date_range_end__", date_range_end) %}
{% endif %}
{%- set filtered -%}
(select * from {{ relation }} where {{ where }}) dbt_subquery
(select * from {{ relation }} where {{ where }})
{%- endset -%}
{% do return(filtered) %}
{%- else -%}
Expand Down
18 changes: 9 additions & 9 deletions queries/macros/test_accepted_values_valor_penalidade.sql
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{% test accepted_values_valor_penalidade(model) -%}
{% test accepted_values_in_table(model, column_name, ref_table, ref_column) %}
WITH
all_values AS (
SELECT
DISTINCT valor_penalidade AS value_field,
DISTINCT {{ column_name }} AS value_field,
COUNT(*) AS n_records
FROM
{{ model }}
WHERE
DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")
DATA BETWEEN DATE("{{ var('date_range_start') }}")
AND DATE("{{ var('date_range_end') }}")
GROUP BY
valor_penalidade )
{{ column_name }}
)
SELECT
*
FROM
all_values
WHERE
value_field NOT IN (
SELECT
valor
{{ ref_column }}
FROM
-- `rj-smtr`.`dashboard_subsidio_sppo`.`valor_tipo_penalidade`
{{ ref('valor_tipo_penalidade') }}
)
{{ ref(ref_table) }}
)
{%- endtest %}
14 changes: 7 additions & 7 deletions queries/macros/test_check_gps_capture.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ WITH
SELECT
DATETIME(timestamp_array) AS timestamp_array
FROM
UNNEST( GENERATE_TIMESTAMP_ARRAY( TIMESTAMP("{{ var('start_date') }}"), TIMESTAMP("{{ var('end_date') }}"), INTERVAL {{ interval }} minute) ) AS timestamp_array
UNNEST( GENERATE_TIMESTAMP_ARRAY( TIMESTAMP("{{ var('date_range_start') }}"), TIMESTAMP("{{ var('date_range_end') }}"), INTERVAL {{ interval }} minute) ) AS timestamp_array
WHERE
timestamp_array < TIMESTAMP("{{ var('end_date') }}") ),
timestamp_array < TIMESTAMP("{{ var('date_range_end') }}") ),
logs_table AS (
SELECT
SAFE_CAST(DATETIME(TIMESTAMP(timestamp_captura), "America/Sao_Paulo") AS DATETIME) timestamp_captura,
SAFE_CAST(sucesso AS BOOLEAN) sucesso,
SAFE_CAST(erro AS STRING) erro,
SAFE_CAST(DATA AS DATE) DATA
FROM
rj-smtr-staging.{{model.schema}}_staging.{{ table_id }}_logs AS t ),
rj-smtr-staging.br_rj_riodejaneiro_onibus_gps_staging.{{ table_id }}_logs AS t ),
logs AS (
SELECT
*,
TIMESTAMP_TRUNC(timestamp_captura, minute) AS timestamp_array
FROM
logs_table
WHERE
DATA BETWEEN DATE(TIMESTAMP("{{ var('start_date') }}"))
AND DATE(TIMESTAMP("{{ var('end_date') }}"))
AND timestamp_captura BETWEEN "{{ var('start_date') }}"
AND "{{ var('end_date') }}" )
DATA BETWEEN DATE(TIMESTAMP("{{ var('date_range_start') }}"))
AND DATE(TIMESTAMP("{{ var('date_range_end') }}"))
AND timestamp_captura BETWEEN "{{ var('date_range_start') }}"
AND "{{ var('date_range_end') }}" )
SELECT
COALESCE(logs.timestamp_captura, t.timestamp_array) AS timestamp_captura,
logs.erro
Expand Down
16 changes: 8 additions & 8 deletions queries/macros/test_check_gps_treatment.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
start_date{% test check_gps_treatment(model) -%}
{% test check_gps_treatment(model) -%}
WITH
data_hora AS (
SELECT
Expand All @@ -9,7 +9,7 @@ WITH
FROM
timestamp_array) AS hora,
FROM
UNNEST(GENERATE_TIMESTAMP_ARRAY("{{ var('start_date') }}", "{{ var('end_date') }}", INTERVAL 1 hour)) AS timestamp_array ),
UNNEST(GENERATE_TIMESTAMP_ARRAY("{{ var('date_range_start') }}", "{{ var('date_range_end') }}", INTERVAL 1 hour)) AS timestamp_array ),
gps_raw AS (
SELECT
EXTRACT(date
Expand All @@ -23,8 +23,8 @@ WITH
-- `rj-smtr.br_rj_riodejaneiro_onibus_gps.sppo_registros`
{{ ref('sppo_registros') }}
WHERE
DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")
DATA BETWEEN DATE("{{ var('date_range_start') }}")
AND DATE("{{ var('date_range_end') }}")
GROUP BY
1,
2 ),
Expand All @@ -41,8 +41,8 @@ WITH
-- `rj-smtr.br_rj_riodejaneiro_onibus_gps.sppo_aux_registros_filtrada`
{{ ref('sppo_aux_registros_filtrada') }}
WHERE
DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")
DATA BETWEEN DATE("{{ var('date_range_start') }}")
AND DATE("{{ var('date_range_end') }}")
GROUP BY
1,
2 ),
Expand All @@ -57,8 +57,8 @@ WITH
-- `rj-smtr.br_rj_riodejaneiro_veiculos.gps_sppo`
{{ ref('gps_sppo') }}
WHERE
DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")
DATA BETWEEN DATE("{{ var('date_range_start') }}")
AND DATE("{{ var('date_range_end') }}")
GROUP BY
1,
2),
Expand Down
92 changes: 17 additions & 75 deletions queries/macros/test_check_sppo_veiculo_dia.sql
Original file line number Diff line number Diff line change
@@ -1,79 +1,21 @@
{% test check_sppo_veiculo_dia(model) %}
WITH
count_dist_status AS (
WITH
count_dist_status AS (
SELECT
DATA,
COUNT(DISTINCT status) AS q_dist_status,
FROM
-- rj-smtr.veiculo.sppo_veiculo_dia
{{ ref('sppo_veiculo_dia')}}
WHERE
DATA BETWEEN DATE("{{ var('date_range_start') }}")
AND DATE("{{ var('end_date') }}")
GROUP BY
1
HAVING
COUNT(DISTINCT status) = 1 )
SELECT
DATA,
COUNT(DISTINCT status) AS q_dist_status,
NULL AS q_duplicated_status,
NULL AS q_null_status
*
FROM
-- rj-smtr.veiculo.sppo_veiculo_dia
{{ ref('sppo_veiculo_dia')}}
WHERE
DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")
GROUP BY
1
HAVING
COUNT(DISTINCT status) = 1 ),
count_duplicated_status AS (
SELECT
DATA,
id_veiculo,
COUNT(*) AS q_status,
FROM
-- rj-smtr.veiculo.sppo_veiculo_dia
{{ ref('sppo_veiculo_dia')}}
WHERE
DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")
GROUP BY
1,
2
HAVING
COUNT(*) > 1 ),
count_duplicated_status_agg AS (
SELECT
DATA,
NULL AS q_dist_status,
SUM(q_status) AS q_duplicated_status,
NULL AS q_null_status
FROM
count_duplicated_status
GROUP BY
1),
count_null_status AS (
SELECT
DATA,
NULL AS q_dist_status,
NULL AS q_duplicated_status,
COUNT(*) AS q_null_status
FROM
-- rj-smtr.veiculo.sppo_veiculo_dia
{{ ref('sppo_veiculo_dia')}}
WHERE
DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")
AND status IS NULL
GROUP BY
1 )
SELECT
*
FROM
count_dist_status

UNION ALL

SELECT
*
FROM
count_duplicated_status_agg

UNION ALL

SELECT
*
FROM
count_null_status

count_dist_status
{% endtest %}
4 changes: 2 additions & 2 deletions queries/macros/test_check_trips_processing.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
start_date{% test check_trips_processing(model) -%}
{% test check_trips_processing(model) -%}
SELECT
s.data,
s.tipo_dia,
Expand All @@ -13,7 +13,7 @@ FROM (
FROM
rj-smtr.projeto_subsidio_sppo.subsidio_data_versao_efetiva
WHERE
DATA >= "2024-04-01" -- DATA_SUBSIDIO_V6_INICIO (Feature trajetos alternativos)
DATA >= "{{ var('DATA_SUBSIDIO_V6_INICIO') }}"
AND DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")
) AS s
Expand Down
4 changes: 2 additions & 2 deletions queries/macros/test_check_viagem_completa.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ WITH
-- rj-smtr.projeto_subsidio_sppo.subsidio_data_versao_efetiva
{{ ref('subsidio_data_versao_efetiva') }}
WHERE
DATA >= "2024-04-01"
DATA >= "{{ var('DATA_SUBSIDIO_V6_INICIO') }}"
AND DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")),
viagem_completa AS (
Expand All @@ -17,7 +17,7 @@ WITH
-- rj-smtr.projeto_subsidio_sppo.viagem_completa
{{ ref('viagem_completa') }}
WHERE
DATA >= "2024-04-01"
DATA >= "{{ var('DATA_SUBSIDIO_V6_INICIO') }}"
AND DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")),
feed_info AS (
Expand Down
8 changes: 4 additions & 4 deletions queries/macros/test_completude.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
{% test completude(model) -%}
{% test completude(model, table_id) -%}
WITH
time_array AS (
SELECT
*
FROM
UNNEST(GENERATE_DATE_ARRAY(DATE("{{ var('start_date') }}"), DATE("{{ var('end_date') }}"))) AS DATA ),
UNNEST(GENERATE_DATE_ARRAY(DATE("{{ var('date_range_start') }}"), DATE("{{ var('date_range_end') }}"))) AS DATA ),
{{ model.name }} AS (
SELECT
DATA,
COUNT(*) AS q_registros
FROM
{{ model }}
WHERE
DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")
DATA BETWEEN DATE("{{ var('date_range_start') }}")
AND DATE("{{ var('date_range_end') }}")
GROUP BY
1 )
SELECT
Expand Down
10 changes: 0 additions & 10 deletions queries/macros/test_expression_is_true.sql

This file was deleted.

2 changes: 1 addition & 1 deletion queries/macros/test_subsido_viagens_atualizadas.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ WITH
-- rj-smtr.projeto_subsidio_sppo.viagem_completa
{{ ref('viagem_completa') }}
WHERE
DATA >= "2024-04-01"
DATA >= "{{ var('DATA_SUBSIDIO_V6_INICIO') }}"
AND DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")),
sumario_servico_dia_historico AS (
Expand Down
16 changes: 8 additions & 8 deletions queries/macros/test_teto_pagamento_valor_subsidio_pago.sql
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
{% test teto_pagamento_valor_subsidio_pago(model, expression) -%}
{% test teto_pagamento_valor_subsidio_pago(model, table_id, schema, expression) -%}
WITH
{{ model.name }} AS (
{{ table_id }} AS (
SELECT
*
*,
FROM
{{ model }}
WHERE
DATA BETWEEN DATE("{{ var('start_date') }}")
AND DATE("{{ var('end_date') }}")),
DATA BETWEEN DATE("{{ var('date_range_start') }}")
AND DATE("{{ var('date_range_end') }}")),
subsidio_valor_km_tipo_viagem AS (
SELECT
data_inicio,
data_fim,
MAX(subsidio_km) AS subsidio_km_teto
FROM
`rj-smtr`.`dashboard_subsidio_sppo_staging`.`subsidio_valor_km_tipo_viagem`
-- {{ ref('subsidio_valor_km_tipo_viagem') }}
-- `rj-smtr`.`dashboard_subsidio_sppo_staging`.`subsidio_valor_km_tipo_viagem`
{{ ref('subsidio_valor_km_tipo_viagem') }}
WHERE
subsidio_km > 0
GROUP BY
Expand All @@ -24,7 +24,7 @@ subsidio_valor_km_tipo_viagem AS (
SELECT
*
FROM
{{ model.name }} AS s
{{ table_id }} AS s
LEFT JOIN
subsidio_valor_km_tipo_viagem AS p
ON
Expand Down
13 changes: 0 additions & 13 deletions queries/macros/test_unique_combination.sql

This file was deleted.

2 changes: 2 additions & 0 deletions queries/models/br_rj_riodejaneiro_onibus_gps/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ models:
- check_gps_capture:
table_id: realocacao
interval: 10
name: check_gps_capture__sppo_realocacao
columns:
- name: id_veiculo
description: "Código identificador do veículo (número de ordem)"
Expand Down Expand Up @@ -292,6 +293,7 @@ models:
- check_gps_capture:
table_id: registros
interval: 1
name: check_gps_capture__sppo_registros
columns:
- name: ordem
description: "Código identificador do veículo (número de ordem)"
Expand Down
3 changes: 2 additions & 1 deletion queries/models/br_rj_riodejaneiro_veiculos/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ models:
- name: gps_sppo
description: "Tabela com os dados tratados de registros de GPS do SPPO, incluindo velocidade estimada, estado de movimento, parada em terminal ou garagem e interseção com o traçado da linha informada."
tests:
- check_gps_treatment
- check_gps_treatment:
name: check_gps_treatment__gps_sppo
- dbt_utils.unique_combination_of_columns:
name: unique_columns__gps_sppo
combination_of_columns:
Expand Down
Loading

0 comments on commit 7654705

Please sign in to comment.