forked from dbt-labs/dbt-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_jinja.py
793 lines (723 loc) · 27.5 KB
/
test_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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
from contextlib import contextmanager
import pytest
import unittest
import yaml
from dbt.clients.jinja import get_rendered
from dbt.clients.jinja import get_template
from dbt.clients.jinja import extract_toplevel_blocks
from dbt.exceptions import CompilationException, JinjaRenderingException
@contextmanager
def returns(value):
yield value
@contextmanager
def raises(value):
with pytest.raises(value) as exc:
yield exc
def expected_id(arg):
if isinstance(arg, list):
return '_'.join(arg)
jinja_tests = [
# strings
(
'''foo: bar''',
returns('bar'),
returns('bar'),
),
(
'''foo: "bar"''',
returns('bar'),
returns('bar'),
),
(
'''foo: "'bar'"''',
returns("'bar'"),
returns("'bar'"),
),
(
"""foo: '"bar"'""",
returns('"bar"'),
returns('"bar"'),
),
(
'''foo: "{{ 'bar' | as_text }}"''',
returns('bar'),
returns('bar'),
),
(
'''foo: "{{ 'bar' | as_bool }}"''',
returns('bar'),
raises(JinjaRenderingException),
),
(
'''foo: "{{ 'bar' | as_number }}"''',
returns('bar'),
raises(JinjaRenderingException),
),
(
'''foo: "{{ 'bar' | as_native }}"''',
returns('bar'),
returns('bar'),
),
# ints
(
'''foo: 1''',
returns('1'),
returns('1'),
),
(
'''foo: "1"''',
returns('1'),
returns('1'),
),
(
'''foo: "'1'"''',
returns("'1'"),
returns("'1'"),
),
(
"""foo: '"1"'""",
returns('"1"'),
returns('"1"'),
),
(
'''foo: "{{ 1 }}"''',
returns('1'),
returns('1'),
),
(
'''foo: "{{ '1' }}"''',
returns('1'),
returns('1'),
),
(
'''foo: "'{{ 1 }}'"''',
returns("'1'"),
returns("'1'"),
),
(
'''foo: "'{{ '1' }}'"''',
returns("'1'"),
returns("'1'"),
),
(
'''foo: "{{ 1 | as_text }}"''',
returns('1'),
returns('1'),
),
(
'''foo: "{{ 1 | as_bool }}"''',
returns('1'),
raises(JinjaRenderingException),
),
(
'''foo: "{{ 1 | as_number }}"''',
returns('1'),
returns(1),
),
(
'''foo: "{{ 1 | as_native }}"''',
returns('1'),
returns(1),
),
(
'''foo: "{{ '1' | as_text }}"''',
returns('1'),
returns('1'),
),
(
'''foo: "{{ '1' | as_bool }}"''',
returns('1'),
raises(JinjaRenderingException),
),
(
'''foo: "{{ '1' | as_number }}"''',
returns('1'),
returns(1),
),
(
'''foo: "{{ '1' | as_native }}"''',
returns('1'),
returns(1),
),
# booleans.
# Note the discrepancy with true vs True: `true` is recognized by jinja but
# not literal_eval, but `True` is recognized by ast.literal_eval.
# For extra fun, yaml recognizes both.
# unquoted true
(
'''foo: "{{ True }}"''',
returns('True'),
returns('True'),
),
(
'''foo: "{{ True | as_text }}"''',
returns('True'),
returns('True'),
),
(
'''foo: "{{ True | as_bool }}"''',
returns('True'),
returns(True),
),
(
'''foo: "{{ True | as_number }}"''',
returns('True'),
raises(JinjaRenderingException),
),
(
'''foo: "{{ True | as_native }}"''',
returns('True'),
returns(True),
),
# unquoted true
(
'''foo: "{{ true }}"''',
returns("True"),
returns("True"),
),
(
'''foo: "{{ true | as_text }}"''',
returns("True"),
returns("True"),
),
(
'''foo: "{{ true | as_bool }}"''',
returns("True"),
returns(True),
),
(
'''foo: "{{ true | as_number }}"''',
returns("True"),
raises(JinjaRenderingException),
),
(
'''foo: "{{ true | as_native }}"''',
returns("True"),
returns(True),
),
(
'''foo: "{{ 'true' | as_text }}"''',
returns("true"),
returns("true"),
),
# quoted 'true'
(
'''foo: "'{{ true }}'"''',
returns("'True'"),
returns("'True'"),
), # jinja true -> python True -> str(True) -> "True" -> quoted
(
'''foo: "'{{ true | as_text }}'"''',
returns("'True'"),
returns("'True'"),
),
(
'''foo: "'{{ true | as_bool }}'"''',
returns("'True'"),
returns("'True'"),
),
(
'''foo: "'{{ true | as_number }}'"''',
returns("'True'"),
returns("'True'"),
),
(
'''foo: "'{{ true | as_native }}'"''',
returns("'True'"),
returns("'True'"),
),
# unquoted True
(
'''foo: "{{ True }}"''',
returns('True'),
returns('True'),
),
(
'''foo: "{{ True | as_text }}"''',
returns("True"),
returns("True"),
), # True -> string 'True' -> text -> str('True') -> 'True'
(
'''foo: "{{ True | as_bool }}"''',
returns("True"),
returns(True),
),
(
'''foo: "{{ True | as_number }}"''',
returns("True"),
raises(JinjaRenderingException),
),
(
'''foo: "{{ True | as_native }}"''',
returns("True"),
returns(True),
),
# quoted 'True' within rendering
(
'''foo: "{{ 'True' | as_text }}"''',
returns("True"),
returns("True"),
),
# 'True' -> string 'True' -> text -> str('True') -> 'True'
(
'''foo: "{{ 'True' | as_bool }}"''',
returns('True'),
returns(True),
),
# quoted 'True' outside rendering
(
'''foo: "'{{ True }}'"''',
returns("'True'"),
returns("'True'"),
),
(
'''foo: "'{{ True | as_bool }}'"''',
returns("'True'"),
returns("'True'"),
),
# yaml turns 'yes' into a boolean true
(
'''foo: yes''',
returns('True'),
returns('True'),
),
(
'''foo: "yes"''',
returns('yes'),
returns('yes'),
),
# concatenation
(
'''foo: "{{ (a_int + 100) | as_native }}"''',
returns('200'),
returns(200),
),
(
'''foo: "{{ (a_str ~ 100) | as_native }}"''',
returns('100100'),
returns(100100),
),
(
'''foo: "{{( a_int ~ 100) | as_native }}"''',
returns('100100'),
returns(100100),
),
# multiple nodes -> always str
(
'''foo: "{{ a_str | as_native }}{{ a_str | as_native }}"''',
returns('100100'),
returns('100100'),
),
(
'''foo: "{{ a_int | as_native }}{{ a_int | as_native }}"''',
returns('100100'),
returns('100100'),
),
(
'''foo: "'{{ a_int | as_native }}{{ a_int | as_native }}'"''',
returns("'100100'"),
returns("'100100'"),
),
(
'''foo:''',
returns('None'),
returns('None'),
),
(
'''foo: null''',
returns('None'),
returns('None'),
),
(
'''foo: ""''',
returns(''),
returns(''),
),
(
'''foo: "{{ '' | as_native }}"''',
returns(''),
returns(''),
),
# very annoying, but jinja 'none' is yaml 'null'.
(
'''foo: "{{ none | as_native }}"''',
returns('None'),
returns(None),
),
# make sure we don't include comments in the output (see #2707)
(
'''foo: "{# #}hello"''',
returns('hello'),
returns('hello'),
),
(
'''foo: "{% if false %}{% endif %}hello"''',
returns('hello'),
returns('hello'),
),
]
@pytest.mark.parametrize(
'value,text_expectation,native_expectation',
jinja_tests,
ids=expected_id
)
def test_jinja_rendering(value, text_expectation, native_expectation):
foo_value = yaml.safe_load(value)['foo']
ctx = {
'a_str': '100',
'a_int': 100,
'b_str': 'hello'
}
with text_expectation as text_result:
assert text_result == get_rendered(foo_value, ctx, native=False)
with native_expectation as native_result:
assert native_result == get_rendered(foo_value, ctx, native=True)
class TestJinja(unittest.TestCase):
def test_do(self):
s = '{% set my_dict = {} %}\n{% do my_dict.update(a=1) %}'
template = get_template(s, {})
mod = template.make_module()
self.assertEqual(mod.my_dict, {'a': 1})
def test_regular_render(self):
s = '{{ "some_value" | as_native }}'
value = get_rendered(s, {}, native=False)
assert value == 'some_value'
s = '{{ 1991 | as_native }}'
value = get_rendered(s, {}, native=False)
assert value == '1991'
s = '{{ "some_value" | as_text }}'
value = get_rendered(s, {}, native=False)
assert value == 'some_value'
s = '{{ 1991 | as_text }}'
value = get_rendered(s, {}, native=False)
assert value == '1991'
def test_native_render(self):
s = '{{ "some_value" | as_native }}'
value = get_rendered(s, {}, native=True)
assert value == 'some_value'
s = '{{ 1991 | as_native }}'
value = get_rendered(s, {}, native=True)
assert value == 1991
s = '{{ "some_value" | as_text }}'
value = get_rendered(s, {}, native=True)
assert value == 'some_value'
s = '{{ 1991 | as_text }}'
value = get_rendered(s, {}, native=True)
assert value == '1991'
class TestBlockLexer(unittest.TestCase):
def test_basic(self):
body = '{{ config(foo="bar") }}\r\nselect * from this.that\r\n'
block_data = ' \n\r\t{%- mytype foo %}'+body+'{%endmytype -%}'
blocks = extract_toplevel_blocks(block_data, allowed_blocks={'mytype'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].block_type_name, 'mytype')
self.assertEqual(blocks[0].block_name, 'foo')
self.assertEqual(blocks[0].contents, body)
self.assertEqual(blocks[0].full_block, block_data)
def test_multiple(self):
body_one = '{{ config(foo="bar") }}\r\nselect * from this.that\r\n'
body_two = (
'{{ config(bar=1)}}\r\nselect * from {% if foo %} thing '
'{% else %} other_thing {% endif %}'
)
block_data = (
' {% mytype foo %}' + body_one + '{% endmytype %}' +
'\r\n{% othertype bar %}' + body_two + '{% endothertype %}'
)
blocks = extract_toplevel_blocks(block_data, allowed_blocks={'mytype', 'othertype'}, collect_raw_data=False)
self.assertEqual(len(blocks), 2)
def test_comments(self):
body = '{{ config(foo="bar") }}\r\nselect * from this.that\r\n'
comment = '{# my comment #}'
block_data = ' \n\r\t{%- mytype foo %}'+body+'{%endmytype -%}'
blocks = extract_toplevel_blocks(comment+block_data, allowed_blocks={'mytype'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].block_type_name, 'mytype')
self.assertEqual(blocks[0].block_name, 'foo')
self.assertEqual(blocks[0].contents, body)
self.assertEqual(blocks[0].full_block, block_data)
def test_evil_comments(self):
body = '{{ config(foo="bar") }}\r\nselect * from this.that\r\n'
comment = '{# external comment {% othertype bar %} select * from thing.other_thing{% endothertype %} #}'
block_data = ' \n\r\t{%- mytype foo %}'+body+'{%endmytype -%}'
blocks = extract_toplevel_blocks(comment+block_data, allowed_blocks={'mytype'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].block_type_name, 'mytype')
self.assertEqual(blocks[0].block_name, 'foo')
self.assertEqual(blocks[0].contents, body)
self.assertEqual(blocks[0].full_block, block_data)
def test_nested_comments(self):
body = '{# my comment #} {{ config(foo="bar") }}\r\nselect * from {# my other comment embedding {% endmytype %} #} this.that\r\n'
block_data = ' \n\r\t{%- mytype foo %}'+body+'{% endmytype -%}'
comment = '{# external comment {% othertype bar %} select * from thing.other_thing{% endothertype %} #}'
blocks = extract_toplevel_blocks(comment+block_data, allowed_blocks={'mytype'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].block_type_name, 'mytype')
self.assertEqual(blocks[0].block_name, 'foo')
self.assertEqual(blocks[0].contents, body)
self.assertEqual(blocks[0].full_block, block_data)
def test_complex_file(self):
blocks = extract_toplevel_blocks(complex_snapshot_file, allowed_blocks={'mytype', 'myothertype'}, collect_raw_data=False)
self.assertEqual(len(blocks), 3)
self.assertEqual(blocks[0].block_type_name, 'mytype')
self.assertEqual(blocks[0].block_name, 'foo')
self.assertEqual(blocks[0].full_block, '{% mytype foo %} some stuff {% endmytype %}')
self.assertEqual(blocks[0].contents, ' some stuff ')
self.assertEqual(blocks[1].block_type_name, 'mytype')
self.assertEqual(blocks[1].block_name, 'bar')
self.assertEqual(blocks[1].full_block, bar_block)
self.assertEqual(blocks[1].contents, bar_block[16:-15].rstrip())
self.assertEqual(blocks[2].block_type_name, 'myothertype')
self.assertEqual(blocks[2].block_name, 'x')
self.assertEqual(blocks[2].full_block, x_block.strip())
self.assertEqual(blocks[2].contents, x_block[len('\n{% myothertype x %}'):-len('{% endmyothertype %}\n')])
def test_peaceful_macro_coexistence(self):
body = '{# my macro #} {% macro foo(a, b) %} do a thing {%- endmacro %} {# my model #} {% a b %} test {% enda %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'macro', 'a'}, collect_raw_data=True)
self.assertEqual(len(blocks), 4)
self.assertEqual(blocks[0].full_block, '{# my macro #} ')
self.assertEqual(blocks[1].block_type_name, 'macro')
self.assertEqual(blocks[1].block_name, 'foo')
self.assertEqual(blocks[1].contents, ' do a thing')
self.assertEqual(blocks[2].full_block, ' {# my model #} ')
self.assertEqual(blocks[3].block_type_name, 'a')
self.assertEqual(blocks[3].block_name, 'b')
self.assertEqual(blocks[3].contents, ' test ')
def test_macro_with_trailing_data(self):
body = '{# my macro #} {% macro foo(a, b) %} do a thing {%- endmacro %} {# my model #} {% a b %} test {% enda %} raw data so cool'
blocks = extract_toplevel_blocks(body, allowed_blocks={'macro', 'a'}, collect_raw_data=True)
self.assertEqual(len(blocks), 5)
self.assertEqual(blocks[0].full_block, '{# my macro #} ')
self.assertEqual(blocks[1].block_type_name, 'macro')
self.assertEqual(blocks[1].block_name, 'foo')
self.assertEqual(blocks[1].contents, ' do a thing')
self.assertEqual(blocks[2].full_block, ' {# my model #} ')
self.assertEqual(blocks[3].block_type_name, 'a')
self.assertEqual(blocks[3].block_name, 'b')
self.assertEqual(blocks[3].contents, ' test ')
self.assertEqual(blocks[4].full_block, ' raw data so cool')
def test_macro_with_crazy_args(self):
body = '''{% macro foo(a, b=asdf("cool this is 'embedded'" * 3) + external_var, c)%}cool{# block comment with {% endmacro %} in it #} stuff here {% endmacro %}'''
blocks = extract_toplevel_blocks(body, allowed_blocks={'macro'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].block_type_name, 'macro')
self.assertEqual(blocks[0].block_name, 'foo')
self.assertEqual(blocks[0].contents, 'cool{# block comment with {% endmacro %} in it #} stuff here ')
def test_materialization_parse(self):
body = '{% materialization xxx, default %} ... {% endmaterialization %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'materialization'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].block_type_name, 'materialization')
self.assertEqual(blocks[0].block_name, 'xxx')
self.assertEqual(blocks[0].full_block, body)
body = '{% materialization xxx, adapter="other" %} ... {% endmaterialization %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'materialization'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].block_type_name, 'materialization')
self.assertEqual(blocks[0].block_name, 'xxx')
self.assertEqual(blocks[0].full_block, body)
def test_nested_not_ok(self):
# we don't allow nesting same blocks
body = '{% myblock a %} {% myblock b %} {% endmyblock %} {% endmyblock %}'
with self.assertRaises(CompilationException):
extract_toplevel_blocks(body, allowed_blocks={'myblock'})
def test_incomplete_block_failure(self):
fullbody = '{% myblock foo %} {% endmyblock %}'
for length in range(len('{% myblock foo %}'), len(fullbody)-1):
body = fullbody[:length]
with self.assertRaises(CompilationException):
extract_toplevel_blocks(body, allowed_blocks={'myblock'})
def test_wrong_end_failure(self):
body = '{% myblock foo %} {% endotherblock %}'
with self.assertRaises(CompilationException):
extract_toplevel_blocks(body, allowed_blocks={'myblock', 'otherblock'})
def test_comment_no_end_failure(self):
body = '{# '
with self.assertRaises(CompilationException):
extract_toplevel_blocks(body)
def test_comment_only(self):
body = '{# myblock #}'
blocks = extract_toplevel_blocks(body)
self.assertEqual(len(blocks), 1)
blocks = extract_toplevel_blocks(body, collect_raw_data=False)
self.assertEqual(len(blocks), 0)
def test_comment_block_self_closing(self):
# test the case where a comment start looks a lot like it closes itself
# (but it doesn't in jinja!)
body = '{#} {% myblock foo %} {#}'
blocks = extract_toplevel_blocks(body, collect_raw_data=False)
self.assertEqual(len(blocks), 0)
def test_embedded_self_closing_comment_block(self):
body = '{% myblock foo %} {#}{% endmyblock %} {#}{% endmyblock %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'myblock'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].full_block, body)
self.assertEqual(blocks[0].contents, ' {#}{% endmyblock %} {#}')
def test_set_statement(self):
body = '{% set x = 1 %}{% myblock foo %}hi{% endmyblock %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'myblock'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].full_block, '{% myblock foo %}hi{% endmyblock %}')
def test_set_block(self):
body = '{% set x %}1{% endset %}{% myblock foo %}hi{% endmyblock %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'myblock'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].full_block, '{% myblock foo %}hi{% endmyblock %}')
def test_crazy_set_statement(self):
body = '{% set x = (thing("{% myblock foo %}")) %}{% otherblock bar %}x{% endotherblock %}{% set y = otherthing("{% myblock foo %}") %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'otherblock'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].full_block, '{% otherblock bar %}x{% endotherblock %}')
self.assertEqual(blocks[0].block_type_name, 'otherblock')
def test_do_statement(self):
body = '{% do thing.update() %}{% myblock foo %}hi{% endmyblock %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'myblock'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].full_block, '{% myblock foo %}hi{% endmyblock %}')
def test_deceptive_do_statement(self):
body = '{% do thing %}{% myblock foo %}hi{% endmyblock %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'myblock'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].full_block, '{% myblock foo %}hi{% endmyblock %}')
def test_do_block(self):
body = '{% do %}thing.update(){% enddo %}{% myblock foo %}hi{% endmyblock %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'do', 'myblock'}, collect_raw_data=False)
self.assertEqual(len(blocks), 2)
self.assertEqual(blocks[0].contents, 'thing.update()')
self.assertEqual(blocks[0].block_type_name, 'do')
self.assertEqual(blocks[1].full_block, '{% myblock foo %}hi{% endmyblock %}')
def test_crazy_do_statement(self):
body = '{% do (thing("{% myblock foo %}")) %}{% otherblock bar %}x{% endotherblock %}{% do otherthing("{% myblock foo %}") %}{% myblock x %}hi{% endmyblock %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'myblock', 'otherblock'}, collect_raw_data=False)
self.assertEqual(len(blocks), 2)
self.assertEqual(blocks[0].full_block, '{% otherblock bar %}x{% endotherblock %}')
self.assertEqual(blocks[0].block_type_name, 'otherblock')
self.assertEqual(blocks[1].full_block, '{% myblock x %}hi{% endmyblock %}')
self.assertEqual(blocks[1].block_type_name, 'myblock')
def test_awful_jinja(self):
blocks = extract_toplevel_blocks(
if_you_do_this_you_are_awful,
allowed_blocks={'snapshot', 'materialization'},
collect_raw_data=False
)
self.assertEqual(len(blocks), 2)
self.assertEqual(len([b for b in blocks if b.block_type_name == '__dbt__data']), 0)
self.assertEqual(blocks[0].block_type_name, 'snapshot')
self.assertEqual(blocks[0].contents, '\n '.join([
'''{% set x = ("{% endsnapshot %}" + (40 * '%})')) %}''',
'{# {% endsnapshot %} #}',
'{% embedded %}',
' some block data right here',
'{% endembedded %}'
]))
self.assertEqual(blocks[1].block_type_name, 'materialization')
self.assertEqual(blocks[1].contents, '\nhi\n')
def test_quoted_endblock_within_block(self):
body = '{% myblock something -%} {% set x = ("{% endmyblock %}") %} {% endmyblock %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'myblock'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].block_type_name, 'myblock')
self.assertEqual(blocks[0].contents, '{% set x = ("{% endmyblock %}") %} ')
def test_docs_block(self):
body = '{% docs __my_doc__ %} asdf {# nope {% enddocs %}} #} {% enddocs %} {% docs __my_other_doc__ %} asdf "{% enddocs %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'docs'}, collect_raw_data=False)
self.assertEqual(len(blocks), 2)
self.assertEqual(blocks[0].block_type_name, 'docs')
self.assertEqual(blocks[0].contents, ' asdf {# nope {% enddocs %}} #} ')
self.assertEqual(blocks[0].block_name, '__my_doc__')
self.assertEqual(blocks[1].block_type_name, 'docs')
self.assertEqual(blocks[1].contents, ' asdf "')
self.assertEqual(blocks[1].block_name, '__my_other_doc__')
def test_docs_block_expr(self):
body = '{% docs more_doc %} asdf {{ "{% enddocs %}" ~ "}}" }}{% enddocs %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'docs'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].block_type_name, 'docs')
self.assertEqual(blocks[0].contents, ' asdf {{ "{% enddocs %}" ~ "}}" }}')
self.assertEqual(blocks[0].block_name, 'more_doc')
def test_unclosed_model_quotes(self):
# test case for https://github.com/fishtown-analytics/dbt/issues/1533
body = '{% model my_model -%} select * from "something"."something_else{% endmodel %}'
blocks = extract_toplevel_blocks(body, allowed_blocks={'model'}, collect_raw_data=False)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].block_type_name, 'model')
self.assertEqual(blocks[0].contents, 'select * from "something"."something_else')
self.assertEqual(blocks[0].block_name, 'my_model')
def test_if(self):
# if you conditionally define your macros/models, don't
body = '{% if true %}{% macro my_macro() %} adsf {% endmacro %}{% endif %}'
with self.assertRaises(CompilationException):
extract_toplevel_blocks(body)
def test_if_innocuous(self):
body = '{% if true %}{% something %}asdfasd{% endsomething %}{% endif %}'
blocks = extract_toplevel_blocks(body)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].full_block, body)
def test_for(self):
# no for-loops over macros.
body = '{% for x in range(10) %}{% macro my_macro() %} adsf {% endmacro %}{% endfor %}'
with self.assertRaises(CompilationException):
extract_toplevel_blocks(body)
def test_for_innocuous(self):
# no for-loops over macros.
body = '{% for x in range(10) %}{% something my_something %} adsf {% endsomething %}{% endfor %}'
blocks = extract_toplevel_blocks(body)
self.assertEqual(len(blocks), 1)
self.assertEqual(blocks[0].full_block, body)
def test_endif(self):
body = '{% snapshot foo %}select * from thing{% endsnapshot%}{% endif %}'
with self.assertRaises(CompilationException) as err:
extract_toplevel_blocks(body)
self.assertIn('Got an unexpected control flow end tag, got endif but never saw a preceeding if (@ 1:53)', str(err.exception))
def test_if_endfor(self):
body = '{% if x %}...{% endfor %}{% endif %}'
with self.assertRaises(CompilationException) as err:
extract_toplevel_blocks(body)
self.assertIn('Got an unexpected control flow end tag, got endfor but expected endif next (@ 1:13)', str(err.exception))
def test_if_endfor_newlines(self):
body = '{% if x %}\n ...\n {% endfor %}\n{% endif %}'
with self.assertRaises(CompilationException) as err:
extract_toplevel_blocks(body)
self.assertIn('Got an unexpected control flow end tag, got endfor but expected endif next (@ 3:4)', str(err.exception))
bar_block = '''{% mytype bar %}
{# a comment
that inside it has
{% mytype baz %}
{% endmyothertype %}
{% endmytype %}
{% endmytype %}
{#
{% endmytype %}#}
some other stuff
{%- endmytype%}'''
x_block = '''
{% myothertype x %}
before
{##}
and after
{% endmyothertype %}
'''
complex_snapshot_file = '''
{#some stuff {% mytype foo %} #}
{% mytype foo %} some stuff {% endmytype %}
'''+bar_block+x_block
if_you_do_this_you_are_awful = '''
{#} here is a comment with a block inside {% block x %} asdf {% endblock %} {#}
{% do
set('foo="bar"')
%}
{% set x = ("100" + "hello'" + '%}') %}
{% snapshot something -%}
{% set x = ("{% endsnapshot %}" + (40 * '%})')) %}
{# {% endsnapshot %} #}
{% embedded %}
some block data right here
{% endembedded %}
{%- endsnapshot %}
{% raw %}
{% set x = SYNTAX ERROR}
{% endraw %}
{% materialization whatever, adapter='thing' %}
hi
{% endmaterialization %}
'''