forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_model_config.py
87 lines (67 loc) · 3.69 KB
/
test_model_config.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
from dataclasses import dataclass, field
from dbt.dataclass_schema import dbtClassMixin
from typing import List, Dict
import pytest
from dbt.contracts.graph.model_config import MergeBehavior, ShowBehavior, CompareBehavior
@dataclass
class ThingWithMergeBehavior(dbtClassMixin):
default_behavior: int
appended: List[str] = field(metadata={'merge': MergeBehavior.Append})
updated: Dict[str, int] = field(metadata={'merge': MergeBehavior.Update})
clobbered: str = field(metadata={'merge': MergeBehavior.Clobber})
def test_merge_behavior_meta():
existing = {'foo': 'bar'}
initial_existing = existing.copy()
assert set(MergeBehavior) == {MergeBehavior.Append, MergeBehavior.Update, MergeBehavior.Clobber}
for behavior in MergeBehavior:
assert behavior.meta() == {'merge': behavior}
assert behavior.meta(existing) == {'merge': behavior, 'foo': 'bar'}
assert existing == initial_existing
def test_merge_behavior_from_field():
fields = [f[0] for f in ThingWithMergeBehavior._get_fields()]
fields = {name: f for f, name in ThingWithMergeBehavior._get_fields()}
assert set(fields) == {'default_behavior', 'appended', 'updated', 'clobbered'}
assert MergeBehavior.from_field(fields['default_behavior']) == MergeBehavior.Clobber
assert MergeBehavior.from_field(fields['appended']) == MergeBehavior.Append
assert MergeBehavior.from_field(fields['updated']) == MergeBehavior.Update
assert MergeBehavior.from_field(fields['clobbered']) == MergeBehavior.Clobber
@dataclass
class ThingWithShowBehavior(dbtClassMixin):
default_behavior: int
hidden: str = field(metadata={'show_hide': ShowBehavior.Hide})
shown: float = field(metadata={'show_hide': ShowBehavior.Show})
def test_show_behavior_meta():
existing = {'foo': 'bar'}
initial_existing = existing.copy()
assert set(ShowBehavior) == {ShowBehavior.Hide, ShowBehavior.Show}
for behavior in ShowBehavior:
assert behavior.meta() == {'show_hide': behavior}
assert behavior.meta(existing) == {'show_hide': behavior, 'foo': 'bar'}
assert existing == initial_existing
def test_show_behavior_from_field():
fields = [f[0] for f in ThingWithShowBehavior._get_fields()]
fields = {name: f for f, name in ThingWithShowBehavior._get_fields()}
assert set(fields) == {'default_behavior', 'hidden', 'shown'}
assert ShowBehavior.from_field(fields['default_behavior']) == ShowBehavior.Show
assert ShowBehavior.from_field(fields['hidden']) == ShowBehavior.Hide
assert ShowBehavior.from_field(fields['shown']) == ShowBehavior.Show
@dataclass
class ThingWithCompareBehavior(dbtClassMixin):
default_behavior: int
included: float = field(metadata={'compare': CompareBehavior.Include})
excluded: str = field(metadata={'compare': CompareBehavior.Exclude})
def test_compare_behavior_meta():
existing = {'foo': 'bar'}
initial_existing = existing.copy()
assert set(CompareBehavior) == {CompareBehavior.Include, CompareBehavior.Exclude}
for behavior in CompareBehavior:
assert behavior.meta() == {'compare': behavior}
assert behavior.meta(existing) == {'compare': behavior, 'foo': 'bar'}
assert existing == initial_existing
def test_compare_behavior_from_field():
fields = [f[0] for f in ThingWithCompareBehavior._get_fields()]
fields = {name: f for f, name in ThingWithCompareBehavior._get_fields()}
assert set(fields) == {'default_behavior', 'included', 'excluded'}
assert CompareBehavior.from_field(fields['default_behavior']) == CompareBehavior.Include
assert CompareBehavior.from_field(fields['included']) == CompareBehavior.Include
assert CompareBehavior.from_field(fields['excluded']) == CompareBehavior.Exclude