-
Notifications
You must be signed in to change notification settings - Fork 40
/
test_relation.py
169 lines (149 loc) · 5.82 KB
/
test_relation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from dataclasses import dataclass, replace
from datetime import datetime
import pytest
from dbt.adapters.base import BaseRelation
from dbt.adapters.base.relation import EventTimeFilter
from dbt.adapters.contracts.relation import RelationType
@pytest.mark.parametrize(
"relation_type,result",
[
(RelationType.View, True),
(RelationType.External, False),
],
)
def test_can_be_renamed(relation_type, result):
my_relation = BaseRelation.create(type=relation_type)
my_relation = replace(my_relation, renameable_relations=frozenset({RelationType.View}))
assert my_relation.can_be_renamed is result
def test_can_be_renamed_default():
my_relation = BaseRelation.create(type=RelationType.View)
assert my_relation.can_be_renamed is False
@pytest.mark.parametrize(
"relation_type,result",
[
(RelationType.View, True),
(RelationType.External, False),
],
)
def test_can_be_replaced(relation_type, result):
my_relation = BaseRelation.create(type=relation_type)
my_relation = replace(my_relation, replaceable_relations=frozenset({RelationType.View}))
assert my_relation.can_be_replaced is result
def test_can_be_replaced_default():
my_relation = BaseRelation.create(type=RelationType.View)
assert my_relation.can_be_replaced is False
@pytest.mark.parametrize(
"limit,require_alias,expected_result",
[
(None, False, '"test_database"."test_schema"."test_identifier"'),
(
0,
True,
'(select * from "test_database"."test_schema"."test_identifier" where false limit 0) _dbt_limit_subq_test_identifier',
),
(
1,
True,
'(select * from "test_database"."test_schema"."test_identifier" limit 1) _dbt_limit_subq_test_identifier',
),
(
0,
False,
'(select * from "test_database"."test_schema"."test_identifier" where false limit 0)',
),
(
1,
False,
'(select * from "test_database"."test_schema"."test_identifier" limit 1)',
),
],
)
def test_render_limited(limit, require_alias, expected_result):
my_relation = BaseRelation.create(
database="test_database",
schema="test_schema",
identifier="test_identifier",
limit=limit,
require_alias=require_alias,
)
actual_result = my_relation.render_limited()
assert actual_result == expected_result
assert str(my_relation) == expected_result
@pytest.mark.parametrize(
"event_time_filter,require_alias,expected_result",
[
(None, False, '"test_database"."test_schema"."test_identifier"'),
(
EventTimeFilter(field_name="column"),
False,
'"test_database"."test_schema"."test_identifier"',
),
(None, True, '"test_database"."test_schema"."test_identifier"'),
(
EventTimeFilter(field_name="column"),
True,
'"test_database"."test_schema"."test_identifier"',
),
(
EventTimeFilter(field_name="column", start=datetime(year=2020, month=1, day=1)),
False,
"""(select * from "test_database"."test_schema"."test_identifier" where column >= '2020-01-01 00:00:00')""",
),
(
EventTimeFilter(field_name="column", start=datetime(year=2020, month=1, day=1)),
True,
"""(select * from "test_database"."test_schema"."test_identifier" where column >= '2020-01-01 00:00:00') _dbt_et_filter_subq_test_identifier""",
),
(
EventTimeFilter(field_name="column", end=datetime(year=2020, month=1, day=1)),
False,
"""(select * from "test_database"."test_schema"."test_identifier" where column < '2020-01-01 00:00:00')""",
),
(
EventTimeFilter(
field_name="column",
start=datetime(year=2020, month=1, day=1),
end=datetime(year=2020, month=1, day=2),
),
False,
"""(select * from "test_database"."test_schema"."test_identifier" where column >= '2020-01-01 00:00:00' and column < '2020-01-02 00:00:00')""",
),
],
)
def test_render_event_time_filtered(event_time_filter, require_alias, expected_result):
my_relation = BaseRelation.create(
database="test_database",
schema="test_schema",
identifier="test_identifier",
event_time_filter=event_time_filter,
require_alias=require_alias,
)
actual_result = my_relation.render_event_time_filtered()
assert actual_result == expected_result
assert str(my_relation) == expected_result
def test_render_event_time_filtered_and_limited():
my_relation = BaseRelation.create(
database="test_database",
schema="test_schema",
identifier="test_identifier",
event_time_filter=EventTimeFilter(
field_name="column",
start=datetime(year=2020, month=1, day=1),
end=datetime(year=2020, month=1, day=2),
),
limit=0,
require_alias=False,
)
expected_result = """(select * from (select * from "test_database"."test_schema"."test_identifier" where false limit 0) where column >= '2020-01-01 00:00:00' and column < '2020-01-02 00:00:00')"""
actual_result = my_relation.render_event_time_filtered(my_relation.render_limited())
assert actual_result == expected_result
assert str(my_relation) == expected_result
def test_create_ephemeral_from_uses_identifier():
@dataclass
class Node:
"""Dummy implementation of RelationConfig protocol"""
name: str
identifier: str
node = Node(name="name_should_not_be_used", identifier="test")
ephemeral_relation = BaseRelation.create_ephemeral_from(node)
assert str(ephemeral_relation) == "__dbt__cte__test"