Skip to content

Commit 1e291ca

Browse files
committed
Added unit test cases for materialized view
1 parent 6c6d66d commit 1e291ca

File tree

4 files changed

+140
-1
lines changed

4 files changed

+140
-1
lines changed

dbt/include/oracle/macros/materializations/materialized_view/materialized_view.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
{%- endmacro %}
1010

1111
{% macro oracle__get_replace_materialized_view_as_sql(relation, sql, existing_relation, backup_relation, intermediate_relation) %}
12-
{{ oracle__drop_relation(relation) }}
12+
{{ oracle__drop_relation(existing_relation) }}
1313
{{ oracle__get_create_materialized_view_as_sql(relation, sql) }}
1414
{% endmacro %}
1515

tests/functional/adapter/materialized_view/__init__.py

Whitespace-only changes.
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""
2+
Copyright (c) 2022, Oracle and/or its affiliates.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
https://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+
"""
16+
from typing import Optional, Tuple
17+
18+
import pytest
19+
20+
from dbt.adapters.base.relation import BaseRelation
21+
22+
from dbt.tests.adapter.materialized_view.basic import MaterializedViewBasic
23+
from dbt.tests.util import (
24+
assert_message_in_logs,
25+
get_model_file,
26+
run_dbt,
27+
run_dbt_and_capture,
28+
set_model_file,
29+
)
30+
from tests.functional.adapter.materialized_view.utils import query_relation_type
31+
32+
33+
class TestOracleMaterializedViewBasic(MaterializedViewBasic):
34+
35+
@staticmethod
36+
def insert_record(project, table: BaseRelation, record: Tuple[int, int]):
37+
my_id, value = record
38+
project.run_sql(f"insert into {table} (id, value) values ({my_id}, {value})")
39+
40+
@staticmethod
41+
def refresh_materialized_view(project, materialized_view: BaseRelation):
42+
sql = f"""
43+
BEGIN
44+
DBMS_MVIEW.REFRESH('{materialized_view}');
45+
END;
46+
"""
47+
project.run_sql(sql)
48+
49+
@staticmethod
50+
def query_row_count(project, relation: BaseRelation) -> int:
51+
sql = f"select count(*) from {relation}"
52+
return project.run_sql(sql, fetch="one")[0]
53+
54+
@staticmethod
55+
def query_relation_type(project, relation: BaseRelation) -> Optional[str]:
56+
return query_relation_type(project, relation)
57+
58+
@pytest.fixture(scope="function", autouse=True)
59+
def setup(self, project, my_materialized_view):
60+
61+
run_dbt(["seed"])
62+
run_dbt(["run", "--models", my_materialized_view.identifier, "--full-refresh"])
63+
64+
# the tests touch these files, store their contents in memory
65+
initial_model = get_model_file(project, my_materialized_view)
66+
67+
yield
68+
69+
# and then reset them after the test runs
70+
set_model_file(project, my_materialized_view, initial_model)
71+
run_dbt(["run-operation", "drop_schema", "--args", f"relation: {my_materialized_view.schema}"])
72+
73+
@pytest.mark.skip(
74+
"The current implementation does not support overwriting materialized views with tables."
75+
)
76+
def test_table_replaces_materialized_view(self, project, my_materialized_view):
77+
super().test_table_replaces_materialized_view(project, my_materialized_view)
78+
79+
@pytest.mark.skip(
80+
"The current implementation does not support overwriting materialized views with views."
81+
)
82+
def test_view_replaces_materialized_view(self, project, my_materialized_view):
83+
super().test_view_replaces_materialized_view(project, my_materialized_view)
84+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from typing import List, Optional
2+
3+
from dbt.adapters.base.relation import BaseRelation
4+
from dbt.adapters.oracle.relation import OracleRelation
5+
6+
7+
def query_relation_type(project, relation: BaseRelation) -> Optional[str]:
8+
assert isinstance(relation, OracleRelation)
9+
10+
sql = f"""
11+
with tables as
12+
(select SYS_CONTEXT('userenv', 'DB_NAME') table_catalog,
13+
owner table_schema,
14+
table_name,
15+
case
16+
when iot_type = 'Y'
17+
then 'IOT'
18+
when temporary = 'Y'
19+
then 'TEMP'
20+
else 'BASE TABLE'
21+
end table_type
22+
from sys.all_tables
23+
where upper(table_name) not in (select upper(mview_name) from sys.all_mviews)
24+
union all
25+
select SYS_CONTEXT('userenv', 'DB_NAME'),
26+
owner,
27+
view_name,
28+
'VIEW'
29+
from sys.all_views
30+
union all
31+
select SYS_CONTEXT('userenv', 'DB_NAME'),
32+
owner,
33+
mview_name,
34+
'MATERIALIZED VIEW'
35+
from sys.all_mviews
36+
)
37+
select case table_type
38+
when 'BASE TABLE' then 'table'
39+
when 'VIEW' then 'view'
40+
when 'MATERIALIZED VIEW' then 'materialized_view'
41+
end as "relation_type"
42+
from tables
43+
where table_type in ('BASE TABLE', 'VIEW', 'MATERIALIZED VIEW')
44+
and upper(table_schema) = upper('{relation.schema}')
45+
and upper(table_name) = upper('{relation.identifier}')
46+
"""
47+
48+
results = project.run_sql(sql, fetch="all")
49+
if len(results) == 0:
50+
return None
51+
elif len(results) > 1:
52+
raise ValueError(f"More than one instance of {relation.identifier} found!")
53+
else:
54+
return results[0][0]
55+

0 commit comments

Comments
 (0)