diff --git a/macros/edr/tests/test_exposure_schema_validity.sql b/macros/edr/tests/test_exposure_schema_validity.sql new file mode 100644 index 000000000..f704c9b9d --- /dev/null +++ b/macros/edr/tests/test_exposure_schema_validity.sql @@ -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 %}