forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tree_sitter_jinja.py
354 lines (314 loc) · 9.59 KB
/
test_tree_sitter_jinja.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
from functools import reduce
from pprint import pprint
import dbt.tree_sitter_jinja.extractor as extractor
# tree-sitter parser
parser = extractor.parser
#----- helper functions -----#
def extraction(input, expected):
got = extractor.extract_from_source(input)
passed = expected == got
if not passed:
source_bytes = bytes(input, "utf8")
tree = parser.parse(source_bytes)
count = extractor.error_count(tree.root_node)
print(f"parser error count: {count}")
print("TYPE CHECKER OUTPUT")
pprint(extractor.type_check(source_bytes, tree.root_node))
print(":: EXPECTED ::")
pprint(expected)
print(":: GOT ::")
pprint(got)
return passed
def exctracted(refs=[], sources=[], configs=[], python_jinja=False):
return {
'refs': refs,
'sources': set(sources),
'configs': configs,
'python_jinja': python_jinja
}
# runs the parser and type checker and prints debug messaging if it fails
def type_checks(source_text):
source_bytes = bytes(source_text, "utf8")
tree = parser.parse(source_bytes)
# If we couldn't parse the source we can't typecheck it.
if extractor.error_count(tree.root_node) > 0:
print("parser failed")
return False
res = extractor.type_check(source_bytes, tree.root_node)
# if it returned a list of errors, it didn't typecheck
if isinstance(res, extractor.TypeCheckFailure):
print(res)
return False
else:
return True
def type_check_fails(source_text):
return not type_checks(source_text)
# same as `type_checks` but operates on a list of source strings
def all_type_check(l):
return reduce(lambda x, y: x and y, map(type_checks, l))
# same as `type_checks_all` but returns true iff none of the strings typecheck
def none_type_check(l):
return reduce(lambda x, y: x and y, map(type_check_fails, l))
def produces_tree(source_text, ast):
source_bytes = bytes(source_text, "utf8")
tree = parser.parse(source_bytes)
# If we couldn't parse the source we can't typecheck it.
if extractor.error_count(tree.root_node) > 0:
print("parser failed")
return False
res = extractor.type_check(source_bytes, tree.root_node)
# if it returned a list of errors, it didn't typecheck
if isinstance(res, extractor.TypeCheckFailure):
print(res)
return False
elif res != ast:
print(":: EXPECTED ::")
print(ast)
print(":: GOT ::")
print(res)
return False
else:
return True
def fails_with(source_text, msg):
source_bytes = bytes(source_text, "utf8")
tree = parser.parse(source_bytes)
# If we couldn't parse the source we can't typecheck it.
if extractor.error_count(tree.root_node) > 0:
print("parser failed")
return False
res = extractor.type_check(source_bytes, tree.root_node)
# if it returned a list of errors, it didn't typecheck
if isinstance(res, extractor.TypeCheckFailure):
if msg == res.msg:
return True
print(":: EXPECTED ::")
print(extractor.TypeCheckFailure(msg))
print(":: GOT ::")
print(res)
return False
#---------- Type Checker Tests ----------#
def test_recognizes_ref_source_config():
assert all_type_check([
"select * from {{ ref('my_table') }}",
"{{ config(key='value') }}",
"{{ source('a', 'b') }}"
])
def test_recognizes_multiple_jinja_calls():
assert all_type_check([
"{{ ref('x') }} {{ ref('y') }}",
"{{ config(key='value') }} {{ config(k='v') }}",
"{{ source('a', 'b') }} {{ source('c', 'd') }}"
])
def test_fails_on_other_fn_names():
assert none_type_check([
"select * from {{ reff('my_table') }}",
"{{ fn(key='value') }}",
"{{ REF('a', 'b') }}"
])
def test_config_all_inputs():
assert all_type_check([
"{{ config(key='value') }}",
"{{ config(key=True) }}",
"{{ config(key=False) }}",
"{{ config(key=['v1,','v2']) }}",
"{{ config(key={'k': 'v'}) }}",
"{{ config(key=[{'k':['v', {'x': 'y'}]}, ['a', 'b', 'c']]) }}"
])
def test_config_fails_non_kwarg_inputs():
assert none_type_check([
"{{ config('value') }}",
"{{ config(True) }}",
"{{ config(['v1,','v2']) }}",
"{{ config({'k': 'v'}) }}"
])
def test_source_keyword_args():
assert all_type_check([
"{{ source(source_name='src', table_name='table') }}",
"{{ source('src', table_name='table') }}",
"{{ source(source_name='src', 'table') }}",
"{{ source('src', 'table') }}"
])
def test_source_keyword_args():
assert none_type_check([
"{{ source(source_name='src', BAD_NAME='table') }}",
"{{ source(BAD_NAME='src', table_name='table') }}",
"{{ source(BAD_NAME='src', BAD_NAME='table') }}"
])
def test_source_must_have_2_args():
assert none_type_check([
"{{ source('one isnt enough') }}",
"{{ source('three', 'is', 'too many') }}",
"{{ source('one', 'two', 'three', 'four') }}",
"{{ source(source_name='src', table_name='table', 'extra') }}",
])
def test_source_args_must_be_strings():
assert none_type_check([
"{{ source(True, False) }}",
"{{ source(key='str', key2='str2') }}",
"{{ source([], []) }}",
"{{ source({}, {}) }}",
])
def test_ref_accepts_one_and_two_strings():
assert all_type_check([
"{{ ref('two', 'args') }}",
"{{ ref('one arg') }}"
])
def test_ref_bad_inputs_fail():
assert none_type_check([
"{{ ref('too', 'many', 'strings') }}",
"{{ ref() }}",
"{{ ref(kwarg='is_wrong') }}",
"{{ ref(['list is wrong']) }}"
])
def test_nested_fn_calls_fail():
assert none_type_check([
"{{ [ref('my_table')] }}",
"{{ [config(x='y')] }}",
"{{ config(x=ref('my_table')) }}",
"{{ source(ref('my_table')) }}"
])
def test_config_excluded_kwargs():
assert none_type_check([
"{{ config(pre_hook='x') }}",
"{{ config(pre-hook='x') }}",
"{{ config(post_hook='x') }}",
"{{ config(post-hook='x') }}"
])
def test_jinja_expressions_fail_everywhere():
assert none_type_check([
"{% config(x='y') %}",
"{% if(whatever) do_something() %}",
"doing stuff {{ ref('str') }} stuff {% expression %}",
"{{ {% psych! nested expression %} }}"
])
def test_top_level_kwargs_are_rejected():
assert none_type_check([
"{{ kwarg='value' }}"
])
# this triggers "missing" not "error" nodes from tree-sitter
def test_fails_on_open_jinja_brackets():
assert none_type_check([
"{{ ref()",
"{{ True",
"{{",
"{{ 'str' "
])
def test_ref_ast():
assert produces_tree(
"{{ ref('my_table') }}"
,
('root', ('ref', 'my_table'))
)
def test_buried_refs_ast():
assert produces_tree(
"""
select
field1,
field2,
field3
from {{ ref('x') }}
join {{ ref('y') }}
"""
,
('root',
('ref', 'x'),
('ref', 'y')
)
)
def test_config_ast():
assert produces_tree(
"{{ config(k1={'dict': ['value']}, k2='str') }}"
,
('root',
('config',
('kwarg',
'k1',
('dict',
('dict',
('list',
'value'
)
)
)
),
('kwarg',
'k2',
'str'
)
)
)
)
def test_source_ast():
assert produces_tree(
"{{ source('x', table_name='y') }}"
,
('root',
('source',
'x',
'y'
)
)
)
def test_jinja_expression_ast():
assert fails_with(
"{% expression %}"
,
"jinja expressions are unsupported: {% syntax like this %}"
)
def test_kwarg_order():
assert fails_with(
"{{ source(source_name='kwarg', 'positional') }}"
,
"keyword arguments must all be at the end"
)
#---------- Extractor Tests ----------#
def test_ref():
assert extraction(
"{{ ref('my_table') }} {{ ref('other_table')}}"
,
exctracted(
refs=[['my_table'], ['other_table']]
)
)
def test_config():
assert extraction(
"{{ config(key='value') }}"
,
exctracted(
configs=[('key', 'value')]
)
)
def test_source():
assert extraction(
"{{ source('package', 'table') }} {{ source('x', 'y') }}"
,
exctracted(
sources=[('package', 'table'), ('x', 'y')]
)
)
def test_all():
assert extraction(
"{{ source('package', 'table') }} {{ ref('x') }} {{ config(k='v', x=True) }}"
,
exctracted(
sources=[('package', 'table')],
refs=[['x']],
configs=[('k', 'v'), ('x', True)]
)
)
def test_deeply_nested_config():
assert extraction(
"{{ config(key=[{'k':['v', {'x': 'y'}]}, ['a', 'b', 'c']]) }}"
,
exctracted(
configs=[('key', [{'k':['v', {'x': 'y'}]}, ['a', 'b', 'c']])]
)
)
def test_extracts_dict_with_multiple_keys():
assert extraction(
"{{ config(dict={'a':'x', 'b': 'y', 'c':'z'}) }}"
,
exctracted(
configs=[('dict', {'a': 'x', 'b': 'y', 'c':'z'})]
)
)