-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_templating_functions.py
363 lines (290 loc) · 10.2 KB
/
test_templating_functions.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
# Copyright (c) 2024 Snowflake Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from unittest import mock
import pytest
from snowflake.cli.api.exceptions import InvalidTemplate
from snowflake.cli.api.utils.definition_rendering import render_definition_template
from snowflake.cli.api.utils.templating_functions import get_templating_functions
def test_template_unknown_function():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.unknown_func('hello') %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "Could not find template variable fn.unknown_func" in err.value.message
def test_available_templating_functions():
result = get_templating_functions()
assert sorted(result.keys()) == sorted(
[
"id_to_str",
"str_to_id",
"concat_ids",
"get_username",
"sanitize_id",
]
)
@pytest.mark.parametrize(
"input_list, expected_output",
[
# test concatenate a constant with a variable -> quoted
(["'first_'", "ctx.definition_version"], '"first_1.1"'),
# test concatenate valid unquoted values -> non-quoted
(["'first_'", "'second'"], "first_second"),
# test concatenate unquoted ids with unsafe chars -> quoted
(["'first.'", "'second'"], '"first.second"'),
# all safe chars, one with quoted id -> quoted
(["'first_'", "'second_'", "'\"third\"'"], '"first_second_third"'),
# one word, unsafe chars -> quoted
(["'first.'"], '"first."'),
# one word, safe chars -> non-quoted
(["'first'"], "first"),
# blank input -> quoted blank output
(["''", "''"], '""'),
],
)
def test_concat_ids_with_valid_values(input_list, expected_output):
input_list_str = ", ".join(input_list)
definition = {
"definition_version": "1.1",
"env": {
"value": f"<% fn.concat_ids({input_list_str}) %>",
},
}
result = render_definition_template(definition, {}).project_context
env = result.get("ctx", {}).get("env", {})
assert env.get("value") == expected_output
def test_concat_ids_with_no_args():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.concat_ids() %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "concat_ids requires at least 1 argument(s)" in err.value.message
def test_concat_ids_with_non_string_arg():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.concat_ids(123) %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "concat_ids only accepts String values" in err.value.message
@pytest.mark.parametrize(
"input_val, expected_output",
[
# unquoted safe -> unchanged
("first", "first"),
# unquoted unsafe -> unchanged
("first.second", "first.second"),
# looks like quoted but invalid -> unchanged
('"first"second"', '"first"second"'),
# valid quoted -> unquoted
('"first""second"', 'first"second'),
# unquoted blank -> blank
("", ""),
# quoted blank -> blank
('""', ""),
],
)
def test_id_to_str_valid_values(input_val, expected_output):
definition = {
"definition_version": "1.1",
"env": {
"input_value": input_val,
"output_value": "<% fn.id_to_str(ctx.env.input_value) %>",
},
}
result = render_definition_template(definition, {}).project_context
env = result.get("ctx", {}).get("env", {})
assert env.get("output_value") == expected_output
def test_id_to_str_with_no_args():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.id_to_str() %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "id_to_str requires at least 1 argument(s)" in err.value.message
def test_id_to_str_with_two_args():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.id_to_str('a', 'b') %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "id_to_str supports at most 1 argument(s)" in err.value.message
def test_id_to_str_with_non_string_arg():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.id_to_str(123) %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "id_to_str only accepts String values" in err.value.message
@pytest.mark.parametrize(
"input_val, expected_output",
[
# unquoted safe -> unchanged
("first", "first"),
# unquoted unsafe -> quoted
("first.second", '"first.second"'),
# looks like quoted but invalid -> quote it and escape
('"first"second"', '"""first""second"""'),
# valid quoted -> unchanged
('"first""second"', '"first""second"'),
# blank -> quoted blank
("", '""'),
# quoted blank -> unchanged
('""', '""'),
],
)
def test_str_to_id_valid_values(input_val, expected_output):
definition = {
"definition_version": "1.1",
"env": {
"input_value": input_val,
"output_value": "<% fn.str_to_id(ctx.env.input_value) %>",
},
}
result = render_definition_template(definition, {}).project_context
env = result.get("ctx", {}).get("env", {})
assert env.get("output_value") == expected_output
def test_str_to_id_with_no_args():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.str_to_id() %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "str_to_id requires at least 1 argument(s)" in err.value.message
def test_str_to_id_with_two_args():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.str_to_id('a', 'b') %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "str_to_id supports at most 1 argument(s)" in err.value.message
def test_str_to_id_with_non_string_arg():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.str_to_id(123) %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "str_to_id only accepts String values" in err.value.message
@pytest.mark.parametrize(
"os_environ, expected_output",
[
({"USER": "test_user"}, "test_user"),
({"USERNAME": "test_user"}, "test_user"),
({}, ""),
],
)
def test_get_username_valid_values(os_environ, expected_output):
definition = {
"definition_version": "1.1",
"env": {
"output_value": "<% fn.get_username() %>",
},
}
with mock.patch.dict(os.environ, os_environ, clear=True):
result = render_definition_template(definition, {}).project_context
env = result.get("ctx", {}).get("env", {})
assert env.get("output_value") == expected_output
@mock.patch.dict(os.environ, {}, clear=True)
def test_get_username_with_fallback_value():
definition = {
"definition_version": "1.1",
"env": {
"output_value": "<% fn.get_username('fallback_user') %>",
},
}
result = render_definition_template(definition, {}).project_context
env = result.get("ctx", {}).get("env", {})
assert env.get("output_value") == "fallback_user"
def test_get_username_with_two_args_should_fail():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.get_username('a', 'b') %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "get_username supports at most 1 argument(s)" in err.value.message
@pytest.mark.parametrize(
"input_value, expected_output",
[
("test_value", "test_value"),
(" T'EST_Va l.u-e" "", "TEST_Value"),
("", "_"),
('""', "_"),
("_val.ue", "_value"),
("1val.ue", "_1value"),
('"some_id"', "some_id"),
("a." + "b" * 254 + "c", "a" + "b" * 254),
],
)
def test_sanitize_id_valid_values(input_value, expected_output):
definition = {
"definition_version": "1.1",
"env": {
"input_value": input_value,
"output_value": "<% fn.sanitize_id(ctx.env.input_value) %>",
},
}
result = render_definition_template(definition, {}).project_context
env = result.get("ctx", {}).get("env", {})
assert env.get("output_value") == expected_output
def test_sanitize_id_with_no_args():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.sanitize_id() %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "sanitize_id requires at least 1 argument(s)" in err.value.message
def test_sanitize_id_with_two_args():
definition = {
"definition_version": "1.1",
"env": {
"value": "<% fn.sanitize_id('a', 'b') %>",
},
}
with pytest.raises(InvalidTemplate) as err:
render_definition_template(definition, {})
assert "sanitize_id supports at most 1 argument(s)" in err.value.message