Skip to content

Commit 9a73079

Browse files
feat(cdk): more unit test for jinja interpolation (#572)
1 parent 5f98bd2 commit 9a73079

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

unit_tests/sources/declarative/interpolation/test_interpolated_string.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
parameters = {"hello": "world"}
1111
kwargs = {"c": "airbyte"}
1212

13+
JINJA_FLOAT_PARSING = "{{ record['some_calculation'] | float if record.get('some_calculation') is not none and record.get('some_calculation') != '' else none }}"
14+
JINJA_FLOAT_PARSING_MULTILINE = (
15+
"{% set v = record.get('some_calculation') %}\n"
16+
"{{ v | replace('%', '') | float if v is not none and v != '' else None }}"
17+
)
18+
1319

1420
@pytest.mark.parametrize(
1521
"test_name, input_string, expected_value",
@@ -24,3 +30,38 @@
2430
def test_interpolated_string(test_name, input_string, expected_value):
2531
s = InterpolatedString.create(input_string, parameters=parameters)
2632
assert s.eval(config, **{"kwargs": kwargs}) == expected_value
33+
34+
35+
@pytest.mark.parametrize(
36+
"test_name, input_string, record_value, expected_value",
37+
[
38+
("test_non_zero_string_value", JINJA_FLOAT_PARSING, "4.4", 4.4),
39+
("test_non_zero_float_value", JINJA_FLOAT_PARSING, 4.4, 4.4),
40+
("test_empty_string", JINJA_FLOAT_PARSING, "", None),
41+
("test_none_value", JINJA_FLOAT_PARSING, None, None),
42+
("test_zero_string_value", JINJA_FLOAT_PARSING, "0.0", 0.0),
43+
("test_zero_2f_string_value", JINJA_FLOAT_PARSING, "0.00", 0.0),
44+
("test_float_value", JINJA_FLOAT_PARSING, 0.0, 0.0),
45+
("test_multiline_non_zero_string_value", JINJA_FLOAT_PARSING_MULTILINE, "4.4", 4.4),
46+
("test_multiline_non_zero_float_value", JINJA_FLOAT_PARSING_MULTILINE, 4.4, 4.4),
47+
("test_multiline_empty_string", JINJA_FLOAT_PARSING_MULTILINE, "", None),
48+
("test_multiline_none_value", JINJA_FLOAT_PARSING_MULTILINE, None, None),
49+
("test_multiline_zero_string_value", JINJA_FLOAT_PARSING_MULTILINE, "0.0", 0.0),
50+
("test_multiline_zero_2f_string_value", JINJA_FLOAT_PARSING_MULTILINE, "0.00", 0.0),
51+
("test_multiline_float_value", JINJA_FLOAT_PARSING_MULTILINE, 0.0, 0.0),
52+
],
53+
)
54+
def test_parsing_record_data(test_name, input_string, record_value, expected_value):
55+
"""
56+
This is very similar to what happens in stream transformations.
57+
"""
58+
s = InterpolatedString.create(input_string, parameters=parameters)
59+
record = {"some_calculation": record_value}
60+
val = s.eval(config, **{"record": record})
61+
assert val == expected_value
62+
if expected_value is None:
63+
assert val is None, f"Expected None for value {record_value} in test {test_name}"
64+
else:
65+
assert float == type(val), (
66+
f"Expected float, got {type(val)} for value {val} in test {test_name}"
67+
)

0 commit comments

Comments
 (0)