|
10 | 10 | parameters = {"hello": "world"} |
11 | 11 | kwargs = {"c": "airbyte"} |
12 | 12 |
|
| 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 | + |
13 | 19 |
|
14 | 20 | @pytest.mark.parametrize( |
15 | 21 | "test_name, input_string, expected_value", |
|
24 | 30 | def test_interpolated_string(test_name, input_string, expected_value): |
25 | 31 | s = InterpolatedString.create(input_string, parameters=parameters) |
26 | 32 | 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