forked from gitter-badger/python-consistent-toml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_contoml.py
268 lines (198 loc) · 5.66 KB
/
test_contoml.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
# -*- coding: utf-8 -*-
from collections import OrderedDict
import json
import contoml
from prettytoml.errors import TOMLError, DuplicateKeysError, DuplicateTablesError, InvalidTOMLFileError
def test_loading_toml_without_trailing_newline():
toml_text = '[main]\nname = "azmy"'
toml = contoml.loads(toml_text)
assert toml['main']['name'] == 'azmy'
def test_array_edge_cases():
# Parsing an empty array value
toml_text = """[section]
key = []"""
toml = contoml.loads(toml_text)
assert 'section' in toml
assert len(toml['section']['key']) == 0
def test_loading_an_empty_toml_source():
toml_text = ''
contoml.loads(toml_text)
# Should not fail
def test_parsing_section_with_indentation_and_comment_lines():
toml = """[main]
listen = ":8966"
redis_host = "localhost:6379"
redis_password = ""
[influxdb]
host = "localhost:8086"
db = "agentcontroller"
user = "ac"
password = "acctrl"
[handlers]
binary = "python2.7"
cwd = "./handlers"
[handlers.env]
PYTHONPATH = "/opt/jumpscale7/lib:../client"
SYNCTHING_URL = "http://localhost:8384/"
SYNCTHING_SHARED_FOLDER_ID = "jumpscripts"
#SYNCTHING_API_KEY = ""
REDIS_ADDRESS = "localhost"
REDIS_PORT = "6379"
#REDIS_PASSWORD = ""
"""
f = contoml.loads(toml)
assert f['handlers']['env']['REDIS_ADDRESS'] == 'localhost'
assert 'REDIS_PASSWORD' not in f['handlers']['env']
f['handlers']['env']['REDIS_PASSWORD'] = 'MYPASSWORD'
expected = """[main]
listen = ":8966"
redis_host = "localhost:6379"
redis_password = ""
[influxdb]
host = "localhost:8086"
db = "agentcontroller"
user = "ac"
password = "acctrl"
[handlers]
binary = "python2.7"
cwd = "./handlers"
[handlers.env]
PYTHONPATH = "/opt/jumpscale7/lib:../client"
SYNCTHING_URL = "http://localhost:8384/"
SYNCTHING_SHARED_FOLDER_ID = "jumpscripts"
#SYNCTHING_API_KEY = ""
REDIS_ADDRESS = "localhost"
REDIS_PORT = "6379"
REDIS_PASSWORD = "MYPASSWORD"
#REDIS_PASSWORD = ""
"""
assert expected == f.dumps()
def test_loading_complex_file_1():
toml = """
[main]
gid = 1
nid = 10
max_jobs = 100
message_id_file = "./.mid"
history_file = "./.history"
agent_controllers = ["http://localhost:8966/"]
[cmds]
[cmds.execute_js_py]
binary = "python2.7"
cwd = "./jumpscripts"
script = "{domain}/{name}.py"
[cmds.sync]
#syncthing extension
binary = "python2.7"
cwd = "./extensions/sync"
script = "{name}.py"
[cmds.sync.env]
PYTHONPATH = "../"
JUMPSCRIPTS_HOME = "../../jumpscripts"
SYNCTHING_URL = "http://localhost:8384"
[channel]
cmds = [0] # long polling from agent 0
[logging]
[logging.db]
type = "DB"
log_dir = "./logs"
levels = [2, 4, 7, 8, 9] # (all error messages) empty for all
[logging.ac]
type = "AC"
flush_int = 300 # seconds (5min)
batch_size = 1000 # max batch size, force flush if reached this count.
agent_controllers = [] # to all agents
levels = [2, 4, 7, 8, 9] # (all error messages) empty for all
[logging.console]
type = "console"
levels = [2, 4, 7, 8, 9]
[stats]
interval = 60 # seconds
agent_controllers = []
"""
contoml.loads(toml)
def test_weird_edge_case_1():
toml_text = """l = "t"
creativity = "on vacation"
"""
f = contoml.loads(toml_text)
assert f['']['l'] == 't'
def test_accessing_deeply_nested_dicts():
t = """[cmds]
[cmds.sync]
#syncthing extension
binary = "python2.7"
cwd = "./extensions/sync"
script = "{name}.py"
[cmds.sync.env]
PYTHONPATH = "../"
JUMPSCRIPTS_HOME = "../../jumpscripts"
SYNCTHING_URL = "http://localhost:8384"
"""
f = contoml.loads(t)
assert f['cmds']['sync']['env']['SYNCTHING_URL'] == 'http://localhost:8384'
f['cmds']['sync']['env']['SYNCTHING_URL'] = 'Nowhere'
expected_toml = """[cmds]
[cmds.sync]
#syncthing extension
binary = "python2.7"
cwd = "./extensions/sync"
script = "{name}.py"
[cmds.sync.env]
PYTHONPATH = "../"
JUMPSCRIPTS_HOME = "../../jumpscripts"
SYNCTHING_URL = "Nowhere"
"""
assert expected_toml == f.dumps()
def test_table_with_pound_in_title():
toml = """["key#group"]
answer = 42"""
parsed = contoml.loads(toml)
assert parsed.primitive['key#group']['answer'] == 42
def test_fails_to_parse_bad_escape_characters():
toml = r"""
invalid-escape = r"This string has a bad \a escape character."
"""
try:
contoml.loads(toml)
assert False, "Should raise an exception before getting here"
except TOMLError:
pass
def test_parsing_multiline_strings_correctly():
toml = r'''multiline_empty_one = """"""
multiline_empty_two = """
"""
multiline_empty_three = """\
"""
multiline_empty_four = """\
\
\
"""
equivalent_one = "The quick brown fox jumps over the lazy dog."
equivalent_two = """
The quick brown \
fox jumps over \
the lazy dog."""
equivalent_three = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""
'''
parsed = contoml.loads(toml)
assert parsed['']['multiline_empty_one'] == parsed['']['multiline_empty_two'] == \
parsed['']['multiline_empty_three'] == parsed['']['multiline_empty_four']
def test_unicode_string_literals():
toml = u'answer = "δ"\n'
parsed = contoml.loads(toml)
assert parsed['']['answer'] == u"δ"
def test_one_entry_array_of_tables():
t = '''[[people]]
first_name = "Bruce"
last_name = "Springsteen"
'''
parsed = contoml.loads(t)
assert parsed['people'][0]['first_name'] == 'Bruce'
assert parsed['people'][0]['last_name'] == 'Springsteen'
def non_empty(iterable):
return tuple(filter(bool, iterable))