forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_docs_blocks.py
210 lines (176 loc) · 7.08 KB
/
test_docs_blocks.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import os
import unittest
from dbt.contracts.files import SourceFile, FileHash, FilePath
from dbt.contracts.graph.manifest import Manifest
from dbt.contracts.graph.parsed import ParsedDocumentation
from dbt.node_types import NodeType
from dbt.parser import docs
from dbt.parser.search import FileBlock
from .utils import config_from_parts_or_dicts
SNOWPLOW_SESSIONS_DOCS = r'''
This table contains one record for every session recorded by Snowplow.
A session is itself comprised of pageviews that all occur within 30 minutes
of each other. If more than 30 minutes elapse between pageviews, then a
new session is created. Given the following pageviews:
| session_id | page_view_id | page_title |
| ---------- | ------------ | ---------- |
| abc | 123 | Home |
| abc | 456 | About |
| abc | 789 | Home |
The following sessions will be created:
| session_id | first_page_title | count_pageviews |
| ---------- | ---------------- | --------------- |
| abc | 123 | 2 |
| abc | 789 | 1 |
'''
SNOWPLOW_SESSIONS_SESSION_ID_DOCS = r'''
This column is the unique identifier for a Snowplow session. It is generated by
a cookie then expires after 30 minutes of inactivity.
'''
SNOWPLOW_SESSIONS_BLOCK = r'''
{{% docs snowplow_sessions %}}
{snowplow_sessions_docs}
{{% enddocs %}}
'''.format(
snowplow_sessions_docs=SNOWPLOW_SESSIONS_DOCS
).strip()
SNOWPLOW_SESSIONS_SESSION_ID_BLOCK = r'''
{{% docs snowplow_sessions__session_id %}}
{snowplow_sessions_session_id_docs}
{{% enddocs %}}
'''.format(
snowplow_sessions_session_id_docs=SNOWPLOW_SESSIONS_SESSION_ID_DOCS
).strip()
TEST_DOCUMENTATION_FILE = r'''
{sessions_block}
{session_id_block}
'''.format(
sessions_block=SNOWPLOW_SESSIONS_BLOCK,
session_id_block=SNOWPLOW_SESSIONS_SESSION_ID_BLOCK,
)
MULTIPLE_RAW_BLOCKS = r'''
{% docs some_doc %}
{% raw %}
```
{% docs %}some doc{% enddocs %}
```
{% endraw %}
{% enddocs %}
{% docs other_doc %}
{% raw %}
```
{% docs %}other doc{% enddocs %}
```
{% endraw %}
{% enddocs %}
'''
class DocumentationParserTest(unittest.TestCase):
def setUp(self):
if os.name == 'nt':
self.root_path = 'C:\\test_root'
self.subdir_path = 'C:\\test_root\\test_subdir'
self.testfile_path = 'C:\\test_root\\test_subdir\\test_file.md'
else:
self.root_path = '/test_root'
self.subdir_path = '/test_root/test_subdir'
self.testfile_path = '/test_root/test_subdir/test_file.md'
profile_data = {
'outputs': {
'test': {
'type': 'postgres',
'host': 'localhost',
'schema': 'analytics',
'user': 'test',
'pass': 'test',
'dbname': 'test',
'port': 1,
}
},
'target': 'test',
}
root_project = {
'name': 'root',
'version': '0.1',
'profile': 'test',
'project-root': self.root_path,
'config-version': 2,
}
subdir_project = {
'name': 'some_package',
'version': '0.1',
'profile': 'test',
'project-root': self.subdir_path,
'quoting': {},
'config-version': 2,
}
self.root_project_config = config_from_parts_or_dicts(
project=root_project, profile=profile_data
)
self.subdir_project_config = config_from_parts_or_dicts(
project=subdir_project, profile=profile_data
)
def _build_file(self, contents, relative_path) -> FileBlock:
match = FilePath(
relative_path=relative_path,
project_root=self.root_path,
searched_path=self.subdir_path,
modification_time=0.0,
)
source_file = SourceFile(path=match, checksum=FileHash.empty())
source_file.contents = contents
return FileBlock(file=source_file)
def test_load_file(self):
parser = docs.DocumentationParser(
root_project=self.root_project_config,
manifest=Manifest(),
project=self.subdir_project_config,
)
file_block = self._build_file(TEST_DOCUMENTATION_FILE, 'test_file.md')
parser.parse_file(file_block)
docs_values = sorted(parser.manifest.docs.values(), key=lambda n: n.name)
self.assertEqual(len(docs_values), 2)
for result in docs_values:
self.assertIsInstance(result, ParsedDocumentation)
self.assertEqual(result.package_name, 'some_package')
self.assertEqual(result.original_file_path, self.testfile_path)
self.assertEqual(result.root_path, self.subdir_path)
self.assertEqual(result.resource_type, NodeType.Documentation)
self.assertEqual(result.path, 'test_file.md')
self.assertEqual(docs_values[0].name, 'snowplow_sessions')
self.assertEqual(docs_values[1].name, 'snowplow_sessions__session_id')
def test_load_file_extras(self):
TEST_DOCUMENTATION_FILE + '{% model foo %}select 1 as id{% endmodel %}'
parser = docs.DocumentationParser(
root_project=self.root_project_config,
manifest=Manifest(),
project=self.subdir_project_config,
)
file_block = self._build_file(TEST_DOCUMENTATION_FILE, 'test_file.md')
parser.parse_file(file_block)
docs_values = sorted(parser.manifest.docs.values(), key=lambda n: n.name)
self.assertEqual(len(docs_values), 2)
for result in docs_values:
self.assertIsInstance(result, ParsedDocumentation)
self.assertEqual(docs_values[0].name, 'snowplow_sessions')
self.assertEqual(docs_values[1].name, 'snowplow_sessions__session_id')
def test_multiple_raw_blocks(self):
parser = docs.DocumentationParser(
root_project=self.root_project_config,
manifest=Manifest(),
project=self.subdir_project_config,
)
file_block = self._build_file(MULTIPLE_RAW_BLOCKS, 'test_file.md')
parser.parse_file(file_block)
docs_values = sorted(parser.manifest.docs.values(), key=lambda n: n.name)
self.assertEqual(len(docs_values), 2)
for result in docs_values:
self.assertIsInstance(result, ParsedDocumentation)
self.assertEqual(result.package_name, 'some_package')
self.assertEqual(result.original_file_path, self.testfile_path)
self.assertEqual(result.root_path, self.subdir_path)
self.assertEqual(result.resource_type, NodeType.Documentation)
self.assertEqual(result.path, 'test_file.md')
self.assertEqual(docs_values[0].name, 'other_doc')
self.assertEqual(docs_values[0].block_contents, '```\n {% docs %}other doc{% enddocs %}\n ```')
self.assertEqual(docs_values[1].name, 'some_doc')
self.assertEqual(docs_values[1].block_contents, '```\n {% docs %}some doc{% enddocs %}\n ```', )