-
Notifications
You must be signed in to change notification settings - Fork 380
Expand file tree
/
Copy pathtest_yaml.py
More file actions
81 lines (64 loc) · 1.92 KB
/
test_yaml.py
File metadata and controls
81 lines (64 loc) · 1.92 KB
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
import os
import pytest
from decimal import Decimal
import sqlmesh.utils.yaml as yaml
from sqlmesh.utils.errors import SQLMeshError
def test_yaml() -> None:
contents = """profile:
target: prod
outputs:
prod:
type: postgres
host: 127.0.0.1
user: "{{ env_var('__SQLMESH_TEST_ENV_USER__') }}"
password: "{{ env_var('__SQLMESH_TEST_ENV_PASSWORD__') }}"
"""
assert contents == yaml.dump(yaml.load(contents, render_jinja=False))
expected_contents = """profile:
target: prod
outputs:
prod:
type: postgres
host: 127.0.0.1
user: user
password: password
"""
os.environ["__SQLMESH_TEST_ENV_USER__"] = "user"
os.environ["__SQLMESH_TEST_ENV_PASSWORD__"] = "password"
assert expected_contents == yaml.dump(yaml.load(contents))
# Return the environment to its previous state
del os.environ["__SQLMESH_TEST_ENV_USER__"]
del os.environ["__SQLMESH_TEST_ENV_PASSWORD__"]
with pytest.raises(SQLMeshError) as ex:
yaml.load("")
assert "YAML source can't be empty." in str(ex.value)
decimal_value = Decimal(123.45)
assert yaml.load(yaml.dump(decimal_value)) == str(decimal_value)
def test_load_keep_last_duplicate_key() -> None:
input_str = """
name: first_name
name: second_name
name: third_name
foo: bar
mapping:
key: first_value
key: second_value
key: third_value
sequence:
- one
- two
"""
# Default behavior of ruamel is to keep the first key encountered
assert yaml.load(input_str, allow_duplicate_keys=True) == {
"name": "first_name",
"foo": "bar",
"mapping": {"key": "first_value"},
"sequence": ["one", "two"],
}
# Test keeping last key
assert yaml.load(input_str, allow_duplicate_keys=True, keep_last_duplicate_key=True) == {
"name": "third_name",
"foo": "bar",
"mapping": {"key": "third_value"},
"sequence": ["one", "two"],
}