forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_parse_manifest.py
145 lines (114 loc) · 5.29 KB
/
test_parse_manifest.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
import unittest
from unittest import mock
from .utils import config_from_parts_or_dicts, normalize
from dbt.contracts.files import SourceFile, FileHash, FilePath
from dbt.parser import ParseResult
from dbt.parser.search import FileBlock
from dbt.parser import manifest
class MatchingHash(FileHash):
def __init__(self):
return super().__init__('', '')
def __eq__(self, other):
return True
class MismatchedHash(FileHash):
def __init__(self):
return super().__init__('', '')
def __eq__(self, other):
return False
class TestLoader(unittest.TestCase):
def setUp(self):
profile_data = {
'target': 'test',
'quoting': {},
'outputs': {
'test': {
'type': 'redshift',
'host': 'localhost',
'schema': 'analytics',
'user': 'test',
'pass': 'test',
'dbname': 'test',
'port': 1,
}
}
}
root_project = {
'name': 'root',
'version': '0.1',
'profile': 'test',
'project-root': normalize('/usr/src/app'),
'config-version': 2,
}
self.root_project_config = config_from_parts_or_dicts(
project=root_project,
profile=profile_data,
cli_vars='{"test_schema_name": "foo"}'
)
self.parser = mock.MagicMock()
self.patched_result_builder = mock.patch('dbt.parser.manifest.make_parse_result')
self.mock_result_builder = self.patched_result_builder.start()
self.patched_result_builder.return_value = self._new_results()
self.loader = manifest.ManifestLoader(
self.root_project_config,
{'root': self.root_project_config}
)
def _new_results(self):
return ParseResult(MatchingHash(), MatchingHash(), {})
def _mismatched_file(self, searched, name):
return self._new_file(searched, name, False)
def _matching_file(self, searched, name):
return self._new_file(searched, name, True)
def _new_file(self, searched, name, match):
if match:
checksum = MatchingHash()
else:
checksum = MismatchedHash()
path = FilePath(
searched_path=normalize(searched),
relative_path=normalize(name),
project_root=normalize(self.root_project_config.project_root),
)
return SourceFile(path=path, checksum=checksum)
def test_model_no_cache(self):
source_file = self._matching_file('models', 'model_1.sql')
self.parser.load_file.return_value = source_file
old_results = None
self.loader.parse_with_cache(source_file.path, self.parser, old_results)
# there was nothing in the cache, so parse_file should get called
# with a FileBlock that has the given source file in it
self.parser.parse_file.assert_called_once_with(FileBlock(file=source_file))
def test_model_cache_hit(self):
source_file = self._matching_file('models', 'model_1.sql')
self.parser.load_file.return_value = source_file
source_file_dupe = self._matching_file('models', 'model_1.sql')
source_file_dupe.nodes.append('model.root.model_1')
old_results = self._new_results()
old_results.files[source_file_dupe.path.search_key] = source_file_dupe
old_results.nodes = {'model.root.model_1': mock.MagicMock()}
self.loader.parse_with_cache(source_file.path, self.parser, old_results)
# there was a cache hit, so parse_file should never have been called
self.parser.parse_file.assert_not_called()
def test_model_cache_mismatch_checksum(self):
source_file = self._mismatched_file('models', 'model_1.sql')
self.parser.load_file.return_value = source_file
source_file_dupe = self._mismatched_file('models', 'model_1.sql')
source_file_dupe.nodes.append('model.root.model_1')
old_results = self._new_results()
old_results.files[source_file_dupe.path.search_key] = source_file_dupe
old_results.nodes = {'model.root.model_1': mock.MagicMock()}
self.loader.parse_with_cache(source_file.path, self.parser, old_results)
# there was a cache checksum mismatch, so parse_file should get called
# with a FileBlock that has the given source file in it
self.parser.parse_file.assert_called_once_with(FileBlock(file=source_file))
def test_model_cache_missing_file(self):
source_file = self._matching_file('models', 'model_1.sql')
self.parser.load_file.return_value = source_file
source_file_different = self._matching_file('models', 'model_2.sql')
source_file_different.nodes.append('model.root.model_2')
old_results = self._new_results()
old_results.files[source_file_different.path.search_key] = source_file_different
old_results.nodes = {'model.root.model_2': mock.MagicMock()}
self.loader.parse_with_cache(source_file.path, self.parser, old_results)
# the filename wasn't in the cache, so parse_file should get called
# with a FileBlock that has the given source file in it.
self.parser.parse_file.assert_called_once_with(FileBlock(file=source_file))