test_utils.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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.utils.fixture_cast_bool_to_text import (
  17. models__test_cast_bool_to_text_yml,
  18. )
  19. from dbt.tests.adapter.utils.fixture_date_spine import (
  20. models__test_date_spine_yml,
  21. )
  22. from dbt.tests.adapter.utils.fixture_get_intervals_between import (
  23. models__test_get_intervals_between_yml,
  24. )
  25. from dbt.tests.adapter.utils.test_cast import BaseCast
  26. from dbt.tests.adapter.utils.test_cast_bool_to_text import BaseCastBoolToText
  27. from dbt.tests.adapter.utils.test_current_timestamp import BaseCurrentTimestampAware
  28. from dbt.tests.adapter.utils.test_date_spine import BaseDateSpine
  29. from dbt.tests.adapter.utils.test_generate_series import BaseGenerateSeries
  30. from dbt.tests.adapter.utils.test_get_intervals_between import BaseGetIntervalsBetween
  31. from dbt.tests.adapter.utils.test_get_powers_of_two import BaseGetPowersOfTwo
  32. from dbt.tests.adapter.utils.test_listagg import BaseListagg
  33. from dbt.tests.adapter.utils.test_timestamps import BaseCurrentTimestamps
  34. models__test_cast_bool_to_text_sql = """
  35. with data_bool as (
  36. select 0=1 as input, 'false' as expected
  37. UNION ALL
  38. select 1=1 as input, 'true' as expected
  39. ),
  40. data_null as (
  41. select null as input, null as expected
  42. )
  43. select
  44. {{ cast_bool_to_text("input") }} as actual,
  45. expected
  46. from data_bool
  47. UNION ALL
  48. select
  49. {{ cast_bool_to_text("input") }} as actual,
  50. expected
  51. from data_null
  52. """
  53. # The upstream test_date_spine has PostgreSQL-specific logic that doesn't
  54. # get picked up because it conditions on `target == "postgres"`. So we just
  55. # hardcode that PostgreSQL-specific logic here.
  56. models__test_date_spine_sql = """
  57. with generated_dates as (
  58. {{ date_spine("day", "'2023-09-01'::date", "'2023-09-10'::date") }}
  59. ), expected_dates as (
  60. select '2023-09-01'::date as expected
  61. union all
  62. select '2023-09-02'::date as expected
  63. union all
  64. select '2023-09-03'::date as expected
  65. union all
  66. select '2023-09-04'::date as expected
  67. union all
  68. select '2023-09-05'::date as expected
  69. union all
  70. select '2023-09-06'::date as expected
  71. union all
  72. select '2023-09-07'::date as expected
  73. union all
  74. select '2023-09-08'::date as expected
  75. union all
  76. select '2023-09-09'::date as expected
  77. ), joined as (
  78. select
  79. generated_dates.date_day,
  80. expected_dates.expected
  81. from generated_dates
  82. left join expected_dates on generated_dates.date_day = expected_dates.expected
  83. )
  84. SELECT * from joined
  85. """
  86. # The upstream get_intervals_between has PostgreSQL-specific logic that doesn't
  87. # get picked up because it conditions on `target == "postgres"`. So we just
  88. # hardcode that PostgreSQL-specific logic here.
  89. models__test_get_intervals_between_sql = """
  90. SELECT
  91. {{ get_intervals_between("'2023-09-01'::date", "'2023-09-12'::date", "day") }} as intervals,
  92. 11 as expected
  93. """
  94. class TestCast(BaseCast):
  95. pass
  96. # The `cast_bool_to_text` macro works as expected, but we must alter the test case
  97. # because set operation type conversions do not work properly.
  98. # See https://github.com/MaterializeInc/database-issues/issues/1065
  99. class TestCastBoolToText(BaseCastBoolToText):
  100. @pytest.fixture(scope="class")
  101. def models(self):
  102. return {
  103. "test_cast_bool_to_text.yml": models__test_cast_bool_to_text_yml,
  104. "test_cast_bool_to_text.sql": self.interpolate_macro_namespace(
  105. models__test_cast_bool_to_text_sql, "cast_bool_to_text"
  106. ),
  107. }
  108. pass
  109. @pytest.mark.skip(reason="Materialize supports the list_agg() function")
  110. class TestListagg(BaseListagg):
  111. pass
  112. # Materialize implements `timestamp`, which is an equivalent abbreviation for
  113. # `timestamp without timezone` (as required by the SQL standard)
  114. # See https://www.postgresql.org/docs/current/datatype-datetime.html
  115. class TestCurrentTimestamps(BaseCurrentTimestamps):
  116. @pytest.fixture(scope="class")
  117. def expected_schema(self):
  118. return {
  119. "current_timestamp": "timestamp with time zone",
  120. "current_timestamp_in_utc_backcompat": "timestamp without time zone",
  121. "current_timestamp_backcompat": "timestamp without time zone",
  122. }
  123. pass
  124. class TestCurrentTimestamp(BaseCurrentTimestampAware):
  125. pass
  126. class TestDateSpine(BaseDateSpine):
  127. @pytest.fixture(scope="class")
  128. def models(self):
  129. return {
  130. "test_date_spine.yml": models__test_date_spine_yml,
  131. "test_date_spine.sql": self.interpolate_macro_namespace(
  132. models__test_date_spine_sql, "date_spine"
  133. ),
  134. }
  135. class TestGenerateSeries(BaseGenerateSeries):
  136. pass
  137. class TestGetIntervalsBeteween(BaseGetIntervalsBetween):
  138. @pytest.fixture(scope="class")
  139. def models(self):
  140. return {
  141. "test_get_intervals_between.yml": models__test_get_intervals_between_yml,
  142. "test_get_intervals_between.sql": self.interpolate_macro_namespace(
  143. models__test_get_intervals_between_sql, "get_intervals_between"
  144. ),
  145. }
  146. class TestGetPowersOfTwo(BaseGetPowersOfTwo):
  147. pass