Skip to content

Commit

Permalink
Add generic test for exposure schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
erikzaadi committed Sep 10, 2023
1 parent 9098208 commit b94d093
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions macros/edr/tests/test_exposure_schema_validity.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% test exposure_schema_validity(model) %}
{% if execute %}
{% set exposures = [] -%}
{% for exposure in graph.exposures.values() -%}
{%- if model['unique_id'] in exposure.depends_on.nodes -%}
{%- if exposure['meta'] is not none -%}
{%- do exposures.append(exposure) -%}
{%- endif -%}
{%- endif -%}
{%- endfor %}
{%- if exposures.length > 0 -%}
{% set model_relation = elementary.get_model_relation_for_test(model, context["model"]) %}
{%- set full_table_name = elementary.relation_to_full_name(model_relation) %}
{{- elementary.test_log('start', full_table_name, 'exposure validation') }}
{%- set columns_from_model = adapter.get_columns_in_relation(model) -%}
{%- set columns_dict = dict() -%}
{%- for column in columns -%}
{%- do column_dtypes.update({ column['name']: elementary.normalize_data_type(column['dtype']) }) -%}
{%- endfor -%}
{%- set invalid_exposures = [] -%}
{%- for exposure in exposures -%}
{# Depend on meta since column level info is not available on exposures #}
{%- set meta = fromjson(exposure['meta'], '') -%}
{%- if meta != '' -%}
{%- for column in meta.getattr('columns', []) -%}
{%- if column['name'] not in columns_dict.keys() -%}
{%- do invalid_exposures.append('Exposure: ' ~ exposure.name ~ ' at ' ~ exposure.url ~ " has a column that's missing in the model: " ~ column['name']) -%}
{%- elif elementary.normalize_data_type(column.getattr('dtype', '')) != elementary.normalize_data_type(column_dtypes[column['name']]) -%}
{%- do invalid_exposures.append('Exposure: ' ~ exposure.name ~ ' at ' ~ exposure.url ~ " has a different data type for the column '" ~ " column '" ~ column['name'] ~ "': '" ~ column['dtype'] ~ "' vs '" ~ column_dtypes ~ "'" -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endfor %}
{%- if invalid_exposures.length > 0 -%}
{{- return(invalid_exposures) }}
{%- else -%}
{{- elementary.no_results_query() }}
{%- endif -%}
{{- elementary.test_log('end', full_table_name, 'exposure validation') }}
{% endif %}
{% endif %}
{% endtest %}

0 comments on commit b94d093

Please sign in to comment.