test_store_test_failures.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.contracts.results import TestStatus
  17. from dbt.tests.adapter.store_test_failures_tests.basic import (
  18. StoreTestFailuresAsBase,
  19. StoreTestFailuresAsExceptions,
  20. StoreTestFailuresAsGeneric,
  21. StoreTestFailuresAsInteractions,
  22. StoreTestFailuresAsProjectLevelEphemeral,
  23. StoreTestFailuresAsProjectLevelOff,
  24. StoreTestFailuresAsProjectLevelView,
  25. TestResult,
  26. )
  27. from dbt.tests.adapter.store_test_failures_tests.fixtures import (
  28. models__file_model_but_with_a_no_good_very_long_name,
  29. models__fine_model,
  30. )
  31. from dbt.tests.adapter.store_test_failures_tests.test_store_test_failures import (
  32. StoreTestFailuresBase,
  33. )
  34. from dbt.tests.util import run_dbt
  35. TEST__MATERIALIZED_VIEW_TRUE = """
  36. {{ config(store_failures_as="materialized_view", store_failures=True) }}
  37. select *
  38. from {{ ref('chipmunks') }}
  39. where shirt = 'green'
  40. """
  41. TEST__MATERIALIZED_VIEW_FALSE = """
  42. {{ config(store_failures_as="materialized_view", store_failures=False) }}
  43. select *
  44. from {{ ref('chipmunks') }}
  45. where shirt = 'green'
  46. """
  47. TEST__MATERIALIZED_VIEW_UNSET = """
  48. {{ config(store_failures_as="materialized_view") }}
  49. select *
  50. from {{ ref('chipmunks') }}
  51. where shirt = 'green'
  52. """
  53. class TestStoreTestFailures(StoreTestFailuresBase):
  54. @pytest.fixture(scope="class")
  55. def models(self):
  56. return {
  57. "fine_model.sql": models__fine_model,
  58. "fine_model_but_with_a_no_good_very_long_name.sql": models__file_model_but_with_a_no_good_very_long_name,
  59. }
  60. class TestMaterializeStoreTestFailures(TestStoreTestFailures):
  61. pass
  62. class TestStoreTestFailuresAsInteractions(StoreTestFailuresAsInteractions):
  63. pass
  64. class TestStoreTestFailuresAsProjectLevelOff(StoreTestFailuresAsProjectLevelOff):
  65. pass
  66. class TestStoreTestFailuresAsProjectLevelView(StoreTestFailuresAsProjectLevelView):
  67. pass
  68. class TestStoreTestFailuresAsGeneric(StoreTestFailuresAsGeneric):
  69. pass
  70. class TestStoreTestFailuresAsProjectLevelEphemeral(
  71. StoreTestFailuresAsProjectLevelEphemeral
  72. ):
  73. pass
  74. class TestStoreTestFailuresAsExceptions(StoreTestFailuresAsExceptions):
  75. def test_tests_run_unsuccessfully_and_raise_appropriate_exception(self, project):
  76. results = run_dbt(["test"], expect_pass=False)
  77. assert len(results) == 1
  78. result = results[0]
  79. assert "Compilation Error" in result.message
  80. assert "'error' is not a valid value" in result.message
  81. assert (
  82. "Accepted values are: ['ephemeral', 'table', 'view', 'materialized_view']"
  83. in result.message
  84. )
  85. class TestStoreTestFailuresAsProjectLevelMaterializeView(StoreTestFailuresAsBase):
  86. """
  87. These scenarios test that `store_failures_as` at the project level takes precedence over `store_failures`
  88. at the model level.
  89. Test Scenarios:
  90. - If `store_failures_as = "materialized_view"` in the project and `store_failures = False` in the model,
  91. then store the failures in a materialized view.
  92. - If `store_failures_as = "materialized_view"` in the project and `store_failures = True` in the model,
  93. then store the failures in a materialized view.
  94. - If `store_failures_as = "materialized_view"` in the project and `store_failures` is not set,
  95. then store the failures in a materialized view.
  96. """
  97. @pytest.fixture(scope="class")
  98. def tests(self):
  99. return {
  100. "results_true.sql": TEST__MATERIALIZED_VIEW_TRUE,
  101. "results_false.sql": TEST__MATERIALIZED_VIEW_FALSE,
  102. "results_unset.sql": TEST__MATERIALIZED_VIEW_UNSET,
  103. }
  104. @pytest.fixture(scope="class")
  105. def project_config_update(self):
  106. return {"tests": {"store_failures_as": "materialized_view"}}
  107. def test_tests_run_successfully_and_are_stored_as_expected(self, project):
  108. expected_results = {
  109. TestResult("results_true", TestStatus.Fail, "materialized_view"),
  110. TestResult("results_false", TestStatus.Fail, "materialized_view"),
  111. TestResult("results_unset", TestStatus.Fail, "materialized_view"),
  112. }
  113. self.run_and_assert(project, expected_results)