unit.sql 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. -- Copyright Materialize, Inc. and contributors. All rights reserved.
  2. --
  3. -- Licensed under the Apache License, Version 2.0 (the "License");
  4. -- you may not use this file except in compliance with the License.
  5. -- You may obtain a copy of the License in the LICENSE file at the
  6. -- root of this repository, or online at
  7. --
  8. -- http://www.apache.org/licenses/LICENSE-2.0
  9. --
  10. -- Unless required by applicable law or agreed to in writing, software
  11. -- distributed under the License is distributed on an "AS IS" BASIS,
  12. -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. -- See the License for the specific language governing permissions and
  14. -- limitations under the License.
  15. {%- materialization unit, adapter='materialize' -%}
  16. {% set relations = [] %}
  17. {% set expected_rows = config.get('expected_rows') %}
  18. {% set expected_sql = config.get('expected_sql') %}
  19. {% set tested_expected_column_names = expected_rows[0].keys() if (expected_rows | length) > 0 else get_columns_in_query(sql) %}
  20. {%- set target_relation = this.incorporate(type='view') -%}
  21. {%- set temp_relation = make_temp_relation(target_relation) -%}
  22. {%- call statement(auto_begin=True) -%}
  23. {{ materialize__create_view_as(temp_relation, get_empty_subquery_sql(sql)) }}
  24. {%- endcall -%}
  25. {%- set columns_in_relation = adapter.get_columns_in_relation(temp_relation) -%}
  26. {%- set column_name_to_data_types = {} -%}
  27. {%- for column in columns_in_relation -%}
  28. {%- do column_name_to_data_types.update({column.name|lower: column.data_type}) -%}
  29. {%- endfor -%}
  30. {% if not expected_sql %}
  31. {% set expected_sql = get_expected_sql(expected_rows, column_name_to_data_types) %}
  32. {% endif %}
  33. {% set unit_test_sql = get_unit_test_sql(sql, expected_sql, tested_expected_column_names) %}
  34. {% call statement('main', fetch_result=True) -%}
  35. {{ unit_test_sql }}
  36. {%- endcall %}
  37. {% do adapter.drop_relation(temp_relation) %}
  38. {{ return({'relations': relations}) }}
  39. {%- endmaterialization -%}