test_basic.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. import pytest
  16. from dbt.tests.adapter.basic.expected_catalog import (
  17. base_expected_catalog,
  18. expected_references_catalog,
  19. no_stats,
  20. )
  21. from dbt.tests.adapter.basic.test_adapter_methods import BaseAdapterMethod
  22. from dbt.tests.adapter.basic.test_base import BaseSimpleMaterializations
  23. from dbt.tests.adapter.basic.test_docs_generate import (
  24. BaseDocsGenerate,
  25. BaseDocsGenReferences,
  26. )
  27. from dbt.tests.adapter.basic.test_empty import BaseEmpty
  28. from dbt.tests.adapter.basic.test_ephemeral import BaseEphemeral
  29. from dbt.tests.adapter.basic.test_generic_tests import BaseGenericTests
  30. from dbt.tests.adapter.basic.test_incremental import (
  31. BaseIncremental,
  32. BaseIncrementalNotSchemaChange,
  33. )
  34. from dbt.tests.adapter.basic.test_singular_tests import BaseSingularTests
  35. from dbt.tests.adapter.basic.test_singular_tests_ephemeral import (
  36. BaseSingularTestsEphemeral,
  37. )
  38. from dbt.tests.adapter.basic.test_snapshot_check_cols import BaseSnapshotCheckCols
  39. from dbt.tests.adapter.basic.test_snapshot_timestamp import BaseSnapshotTimestamp
  40. from dbt.tests.util import (
  41. check_relation_types,
  42. check_relations_equal,
  43. check_result_nodes_by_name,
  44. relation_from_name,
  45. run_dbt,
  46. )
  47. class TestSimpleMaterializationsMaterialize(BaseSimpleMaterializations):
  48. # Custom base test that removes the incremental portion and overrides the expected relations
  49. def test_base(self, project):
  50. # seed command
  51. results = run_dbt(["seed"])
  52. # seed result length
  53. assert len(results) == 1
  54. # run command
  55. results = run_dbt()
  56. # run result length
  57. assert len(results) == 3
  58. # names exist in result nodes
  59. check_result_nodes_by_name(results, ["view_model", "table_model", "swappable"])
  60. # check relation types
  61. expected = {
  62. "base": "table",
  63. "view_model": "view",
  64. "table_model": "materialized_view",
  65. "swappable": "materialized_view",
  66. }
  67. check_relation_types(project.adapter, expected)
  68. # base table rowcount
  69. relation = relation_from_name(project.adapter, "base")
  70. result = project.run_sql(
  71. f"select count(*) as num_rows from {relation}", fetch="one"
  72. )
  73. assert result[0] == 10
  74. # relations_equal
  75. check_relations_equal(
  76. project.adapter, ["base", "view_model", "table_model", "swappable"]
  77. )
  78. # check relations in catalog
  79. catalog = run_dbt(["docs", "generate"])
  80. assert len(catalog.nodes) == 4
  81. assert len(catalog.sources) == 1
  82. # run_dbt changing materialized_var to view
  83. results = run_dbt(
  84. ["run", "-m", "swappable", "--vars", "materialized_var: view"]
  85. )
  86. assert len(results) == 1
  87. # check relation types, swappable is view
  88. expected["swappable"] = "view"
  89. check_relation_types(project.adapter, expected)
  90. class TestSingularTestsMaterialize(BaseSingularTests):
  91. pass
  92. class TestSingularTestsEphemeralMaterialize(BaseSingularTestsEphemeral):
  93. pass
  94. class TestEmptyMaterialize(BaseEmpty):
  95. pass
  96. class TestEphemeral(BaseEphemeral):
  97. pass
  98. @pytest.mark.skip(reason="dbt-materialize does not support incremental models")
  99. class TestIncrementalMaterialize(BaseIncremental):
  100. pass
  101. class TestGenericTestsMaterialize(BaseGenericTests):
  102. pass
  103. @pytest.mark.skip(reason="dbt-materialize does not support snapshots")
  104. class TestSnapshotCheckColsMaterialize(BaseSnapshotCheckCols):
  105. pass
  106. @pytest.mark.skip(reason="dbt-materialize does not support snapshots")
  107. class TestSnapshotTimestampMaterialize(BaseSnapshotTimestamp):
  108. pass
  109. # This additional model sql was needed to ensure we use the materialize__create_view_as sql.
  110. class TestBaseAdapterMethodMaterialize(BaseAdapterMethod):
  111. models__model_sql = """
  112. {% set upstream = ref('upstream') %}
  113. {% if execute %}
  114. {# don't ever do any of this #}
  115. {%- do adapter.drop_schema(upstream) -%}
  116. {% set existing = adapter.get_relation(upstream.database, upstream.schema, upstream.identifier) %}
  117. {% if existing is not none %}
  118. {% do exceptions.CompilationError('expected ' ~ ' to not exist, but it did') %}
  119. {% endif %}
  120. {%- do adapter.create_schema(upstream) -%}
  121. {% set sql = materialize__create_view_as(upstream, 'select 2 as id') %}
  122. {% do run_query(sql) %}
  123. {% endif %}
  124. select * from {{ upstream }}
  125. """
  126. pass
  127. @pytest.mark.skip(reason="dbt-materialize does not support incremental models")
  128. class TestBaseIncrementalNotSchemaChangeMaterialize(BaseIncrementalNotSchemaChange):
  129. pass
  130. class TestDocsGenerateMaterialize(BaseDocsGenerate):
  131. @pytest.fixture(scope="class")
  132. def expected_catalog(self, project, profile_user):
  133. return base_expected_catalog(
  134. project,
  135. role="materialize",
  136. id_type="integer",
  137. text_type="text",
  138. time_type="timestamp without time zone",
  139. view_type="view",
  140. table_type="table",
  141. model_stats=no_stats(),
  142. )
  143. class TestDocsGenReferencesMaterialize(BaseDocsGenReferences):
  144. @pytest.fixture(scope="class")
  145. def expected_catalog(self, project, profile_user):
  146. catalog = expected_references_catalog(
  147. project,
  148. role="materialize",
  149. id_type="integer",
  150. text_type="text",
  151. time_type="timestamp without time zone",
  152. bigint_type="bigint",
  153. view_type="view",
  154. table_type="table",
  155. model_stats=no_stats(),
  156. )
  157. # We set `table_type="table"` above because seeds use tables, but table
  158. # materializations still use materialized views.
  159. catalog["nodes"]["model.test.ephemeral_summary"]["metadata"][
  160. "type"
  161. ] = "materialized_view"
  162. return catalog