-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generic test for exposure schema validation
- Loading branch information
erikzaadi
committed
Sep 10, 2023
1 parent
9098208
commit b94d093
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |