-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
test_parser.py
206 lines (186 loc) · 8.1 KB
/
test_parser.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
import pytest
from types import SimpleNamespace
from collections import deque
from yamlpath.parser import Parser
from yamlpath.exceptions import YAMLPathException
from yamlpath.wrappers import ConsolePrinter
from yamlpath.enums import PathSeperators, PathSegmentTypes
@pytest.fixture
def parser():
"""Returns a Parser with a quiet logger."""
args = SimpleNamespace(verbose=False, quiet=True, debug=False)
logger = ConsolePrinter(args)
return Parser(logger)
def test_empty_str_path(parser):
assert parser.str_path("") == ""
@pytest.mark.parametrize("yaml_path,stringified", [
("&topArrayAnchor[0]", "&topArrayAnchor[0]"),
("aliases[&anchor]", "aliases[&anchor]"),
("a l i a s e s [ & a n c h o r ]", "aliases[&anchor]"),
("aliases[2]", "aliases[2]"),
("namespaced::hash.sub", "namespaced::hash.sub"),
("db_creds[2].pass.encrypted", "db_creds[2].pass.encrypted"),
("lookup::credentials.backend.database.password.hash", "lookup::credentials.backend.database.password.hash"),
("does::not[7].exist[4]", "does::not[7].exist[4]"),
("messy.messy.'dotted.sub.key'.child", r"messy.messy.dotted\.sub\.key.child"),
])
def test_happy_str_path_translations_simple(parser, yaml_path, stringified):
assert parser.str_path(yaml_path) == stringified
@pytest.mark.parametrize("yaml_path,stringified", [
('some[search="Name Here"]', r"some[search=Name\ Here]"),
('some[search=="Name Here"]', r"some[search=Name\ Here]"),
('some[search^"Name "]', r"some[search^Name\ ]"),
('some[search$" Here"]', r"some[search$\ Here]"),
('some[search%"e H"]', r"some[search%e\ H]"),
('some[search>50]', "some[search>50]"),
('some[search<42]', "some[search<42]"),
('some[search>=5280]', "some[search>=5280]"),
('some[search<=14000]', "some[search<=14000]"),
('some[search = "Name Here"]', r"some[search=Name\ Here]"),
('some[search == "Name Here"]', r"some[search=Name\ Here]"),
('some[search ^ "Name "]', r"some[search^Name\ ]"),
('some[search $ " Here"]', r"some[search$\ Here]"),
('some[search % "e H"]', r"some[search%e\ H]"),
('some[search > 50]', "some[search>50]"),
('some[search < 42]', "some[search<42]"),
('some[search >= 5280]', "some[search>=5280]"),
('some[search <= 14000]', "some[search<=14000]"),
('some[search != "Name Here"]', r"some[search!=Name\ Here]"),
('some[search !== "Name Here"]', r"some[search!=Name\ Here]"),
('some[search !^ "Name "]', r"some[search!^Name\ ]"),
('some[search !$ " Here"]', r"some[search!$\ Here]"),
('some[search !% "e H"]', r"some[search!%e\ H]"),
('some[search !> 50]', "some[search!>50]"),
('some[search !< 42]', "some[search!<42]"),
('some[search !>= 5280]', "some[search!>=5280]"),
('some[search !<= 14000]', "some[search!<=14000]"),
('some[!search == "Name Here"]', r"some[search!=Name\ Here]"),
('some[!search ^ "Name "]', r"some[search!^Name\ ]"),
('some[!search $ " Here"]', r"some[search!$\ Here]"),
('some[!search % "e H"]', r"some[search!%e\ H]"),
('some[!search > 50]', "some[search!>50]"),
('some[!search < 42]', "some[search!<42]"),
('some[!search >= 5280]', "some[search!>=5280]"),
('some[!search <= 14000]', "some[search!<=14000]"),
])
def test_happy_str_path_translations_simple_searches(parser, yaml_path, stringified):
assert parser.str_path(yaml_path) == stringified
@pytest.mark.parametrize("yaml_path,stringified", [
(r'some[search =~ /^\d{5}$/]', r'some[search=~/^\d{5}$/]'),
])
def test_happy_str_path_translations_regex_searches(parser, yaml_path, stringified):
assert parser.str_path(yaml_path) == stringified
@pytest.mark.parametrize("yaml_path,stringified", [
('"aliases[&some_name]"', r'aliases\[\&some_name\]'),
('"&topArrayAnchor[0]"', r'\&topArrayAnchor\[0\]'),
('"&subHashAnchor.child1.attr_tst"', r'\&subHashAnchor\.child1\.attr_tst'),
("'&topArrayAnchor[!.=~/[Oo]riginal/]'", r"\&topArrayAnchor\[!\.=~/\[Oo\]riginal/\]"),
])
def test_happy_str_path_translations_bad_quotes(parser, yaml_path, stringified):
assert parser.str_path(yaml_path) == stringified
@pytest.mark.parametrize("yaml_path,stringified", [
("aliases[.%' ']", r"aliases[.%\ ]"),
(r"aliases[.%\ ]", r"aliases[.%\ ]"),
])
def test_happy_str_path_translations_weird_escapes(parser, yaml_path, stringified):
assert parser.str_path(yaml_path) == stringified
@pytest.mark.parametrize("yaml_path,stringified", [
('key\\with\\slashes', 'key\\with\\slashes'),
])
def test_escaped_translations(parser, yaml_path, stringified):
assert parser.str_path(yaml_path) == stringified
def test_happy_parse_path_list_to_deque(parser):
assert isinstance(parser.parse_path(["item1", "item2"]), deque)
@pytest.mark.parametrize("yaml_path", [
('some[search ^^ "Name "]'),
('some[search $$ " Here"]'),
(r'some[search %% "e H"]'),
('some[search >> 50]'),
('some[search << 42]'),
('some[search >>== 5280]'),
('some[search <<== 14000]'),
('some[search !!= "Name Here"]'),
('some[search !!== "Name Here"]'),
('some[search !!^ "Name "]'),
('some[search !!$ " Here"]'),
('some[search !!% "e H"]'),
('some[search !!> 50]'),
('some[search !!< 42]'),
('some[search !!>= 5280]'),
('some[search !!<= 14000]'),
('some[!search != "Name Here"]'),
('some[!search !== "Name Here"]'),
('some[!search !^ "Name "]'),
('some[!search !$ " Here"]'),
('some[!search !% "e H"]'),
('some[!search !> 50]'),
('some[!search !< 42]'),
('some[!search !>= 5280]'),
('some[!search !<= 14000]'),
('some[= "missing LHS operand"]'),
('some[search ~ "meaningless tilde"]'),
('some[search = "unterminated demarcation]'),
('some[search =~ /unterminated RegEx]'),
('some[search ^= "meaningless operator"]'),
('array[4F]'),
({}),
])
def test_uphappy_str_path_translations(parser, yaml_path):
with pytest.raises(YAMLPathException):
parser.str_path(yaml_path)
@pytest.mark.parametrize("pathsep,yaml_path,stringified", [
('.', "some.hash.key", "some.hash.key"),
('/', "/some/hash/key", "/some/hash/key"),
('.', "/some/hash/key", "some.hash.key"),
('/', "some.hash.key", "/some/hash/key"),
('/', "/&someAnchoredArray[0]", "/&someAnchoredArray[0]"),
('.', "&someAnchoredArray[0]", "&someAnchoredArray[0]"),
('.', "/&someAnchoredArray[0]", "&someAnchoredArray[0]"),
('/', "&someAnchoredArray[0]", "/&someAnchoredArray[0]"),
])
def test_pathsep(parser, pathsep, yaml_path, stringified):
assert parser.str_path(yaml_path, pathsep=pathsep) == stringified
@pytest.mark.parametrize("pathsep,compare", [
(PathSeperators.DOT, PathSeperators.DOT),
(PathSeperators.FSLASH, PathSeperators.FSLASH),
('.', PathSeperators.DOT),
('/', PathSeperators.FSLASH),
])
def test_pretyped_pathsep(pathsep, compare):
parser = Parser(None, pathsep=pathsep)
assert compare == parser.pathsep
def test_bad_pathsep():
with pytest.raises(YAMLPathException):
_ = Parser(None, pathsep="no such seperator!")
@pytest.mark.parametrize("yaml_path,strip_escapes", [
("some.hash.key", True),
("some.hash.key", False),
("/some/hash/key", True),
("/some/hash/key", False),
])
def test_repeat_parsings(parser, yaml_path, strip_escapes):
orig = parser._parse_path(yaml_path, strip_escapes)
comp = parser._parse_path(yaml_path, strip_escapes)
assert orig == comp
def test_no_dict_parsings(parser):
with pytest.raises(YAMLPathException):
_ = parser._parse_path({})
def test_list_to_deque_parsing(parser):
parsed_list = [
(PathSegmentTypes.KEY, 'aliases'),
(PathSegmentTypes.ANCHOR, 'secretIdentity')
]
verify_queue = deque([
(PathSegmentTypes.KEY, 'aliases'),
(PathSegmentTypes.ANCHOR, 'secretIdentity')
])
assert verify_queue == parser._parse_path(parsed_list)
def test_deque_to_deque_parsings(parser):
verify_queue = deque([
(PathSegmentTypes.KEY, 'aliases'),
(PathSegmentTypes.ANCHOR, 'secretIdentity')
])
assert verify_queue == parser._parse_path(verify_queue)
def test_none_to_empty_deque_parsings(parser):
verify_queue = deque()
assert verify_queue == parser._parse_path(None)