-
Notifications
You must be signed in to change notification settings - Fork 3
/
wgconfig_tests.erl
249 lines (173 loc) · 9.76 KB
/
wgconfig_tests.erl
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
-module(wgconfig_tests).
-include_lib("eunit/include/eunit.hrl").
setup() ->
application:ensure_all_started(wgconfig),
file:set_cwd(code:lib_dir(wgconfig)),
ok.
base_api_test() ->
setup(),
Res = wgconfig:load_configs(["./test/data/my_config.ini", "./test/data/my_config2.ini"]),
?assertEqual([ok, ok], Res),
?assertEqual({ok, <<"test_db">>},
wgconfig:get(<<"database">>, <<"db_name">>)),
?assertEqual({ok, <<"my_user">>},
wgconfig:get(<<"database">>, <<"db_user">>)),
?assertEqual({ok, <<"200">>},
wgconfig:get(<<"workers_pool">>, <<"num_workers">>)),
?assertEqual({ok, <<"300">>},
wgconfig:get(<<"workers_pool">>, <<"max_workers">>)),
?assertEqual({error,not_found},
wgconfig:get(<<"some_section">>, <<"port">>)),
application:stop(wgconfig),
ok.
get_bool_test() ->
setup(),
wgconfig:load_configs(["./test/data/my_config3.ini"]),
?assertEqual(true, wgconfig:get_bool(<<"section 1">>, <<"key_1">>)),
?assertEqual(false, wgconfig:get_bool(<<"section 1">>, <<"key_2">>)),
?assertEqual(true, wgconfig:get_bool(<<"section 2">>, <<"key_1">>)),
?assertEqual(false, wgconfig:get_bool(<<"section 2">>, <<"key_2">>)),
?assertEqual(true, wgconfig:get_bool(<<"section 2">>, <<"key_3">>)),
?assertEqual(false, wgconfig:get_bool(<<"section 2">>, <<"key_4">>)),
?assertEqual(true, wgconfig:get_bool(<<"section 1">>, <<"key_0">>, true)),
?assertEqual(false, wgconfig:get_bool(<<"section 1">>, <<"key_0">>, false)),
?assertThrow({wgconfig_error, <<"invalid bool 'hello'">>, <<"section 1">>, <<"key_3">>},
wgconfig:get_bool(<<"section 1">>, <<"key_3">>)),
?assertThrow({wgconfig_error, value_not_found, <<"section 1">>, <<"key_0">>},
wgconfig:get_bool(<<"section 1">>, <<"key_0">>)),
application:stop(wgconfig),
ok.
get_int_test() ->
setup(),
wgconfig:load_configs(["./test/data/my_config3.ini"]),
?assertEqual(77, wgconfig:get_int(<<"section 1">>, <<"key_4">>)),
?assertEqual(0, wgconfig:get_int(<<"section 3">>, <<"key_5">>)),
?assertEqual(-100, wgconfig:get_int(<<"section 3">>, <<"key_4">>)),
?assertEqual(42, wgconfig:get_int(<<"section 1">>, <<"key_0">>, 42)),
?assertEqual(-999, wgconfig:get_int(<<"section 0">>, <<"key_1">>, -999)),
?assertThrow({wgconfig_error, <<"invalid int 'hello'">>, <<"section 1">>, <<"key_3">>},
wgconfig:get_int(<<"section 1">>, <<"key_3">>)),
?assertThrow({wgconfig_error, value_not_found, <<"section 1">>, <<"key_0">>},
wgconfig:get_int(<<"section 1">>, <<"key_0">>)),
application:stop(wgconfig),
ok.
get_float_test() ->
setup(),
wgconfig:load_configs(["./test/data/my_config3.ini"]),
?assertEqual(77.77, wgconfig:get_float(<<"section 1">>, <<"key_5">>)),
?assertEqual(3.14159265, wgconfig:get_float(<<"section 3">>, <<"key_6">>)),
?assertEqual(-3.14159265, wgconfig:get_float(<<"section 3">>, <<"key_7">>)),
?assertEqual(42.0, wgconfig:get_float(<<"section 1">>, <<"key_0">>, 42.0)),
?assertEqual(0.123, wgconfig:get_float(<<"section 0">>, <<"key_1">>, 0.123)),
?assertThrow({wgconfig_error, <<"invalid float 'a,b,c,d'">>, <<"section 3">>, <<"key_2">>},
wgconfig:get_float(<<"section 3">>, <<"key_2">>)),
?assertThrow({wgconfig_error, value_not_found, <<"section 1">>, <<"key_0">>},
wgconfig:get_float(<<"section 1">>, <<"key_0">>)),
application:stop(wgconfig),
ok.
get_string_test() ->
setup(),
wgconfig:load_configs(["./test/data/my_config3.ini"]),
?assertEqual("true", wgconfig:get_string(<<"section 1">>, <<"key_1">>)),
?assertEqual("false", wgconfig:get_string(<<"section 1">>, <<"key_2">>)),
?assertEqual("hello", wgconfig:get_string(<<"section 1">>, <<"key_3">>)),
?assertEqual("77", wgconfig:get_string(<<"section 1">>, <<"key_4">>)),
?assertEqual("77.77", wgconfig:get_string(<<"section 1">>, <<"key_5">>)),
?assertEqual("my string", wgconfig:get_string(<<"section 1">>, <<"key_0">>, "my string")),
?assertThrow({wgconfig_error, value_not_found, <<"section 1">>, <<"key_0">>},
wgconfig:get_string(<<"section 1">>, <<"key_0">>)),
application:stop(wgconfig),
ok.
get_binary_test() ->
setup(),
wgconfig:load_configs(["./test/data/my_config3.ini"]),
?assertEqual(<<"true">>, wgconfig:get_binary(<<"section 1">>, <<"key_1">>)),
?assertEqual(<<"false">>, wgconfig:get_binary(<<"section 1">>, <<"key_2">>)),
?assertEqual(<<"hello">>, wgconfig:get_binary(<<"section 1">>, <<"key_3">>)),
?assertEqual(<<"77">>, wgconfig:get_binary(<<"section 1">>, <<"key_4">>)),
?assertEqual(<<"77.77">>, wgconfig:get_binary(<<"section 1">>, <<"key_5">>)),
?assertEqual(<<"my string">>, wgconfig:get_binary(<<"section 1">>, <<"key_0">>, <<"my string">>)),
?assertThrow({wgconfig_error, value_not_found, <<"section 1">>, <<"key_0">>},
wgconfig:get_binary(<<"section 1">>, <<"key_0">>)),
application:stop(wgconfig),
ok.
get_string_list_test() ->
setup(),
wgconfig:load_configs(["./test/data/my_config3.ini"]),
?assertEqual(["hello"], wgconfig:get_string_list(<<"section 1">>, <<"key_3">>)),
?assertEqual(["one", "two", "three", "four"], wgconfig:get_string_list(<<"section 3">>, <<"key_1">>)),
?assertEqual(["a", "b", "c", "d"], wgconfig:get_string_list(<<"section 3">>, <<"key_2">>)),
?assertEqual(["a", "b", "c", "d"], wgconfig:get_string_list(<<"section 3">>, <<"key_3">>)),
?assertEqual(["-100"], wgconfig:get_string_list(<<"section 3">>, <<"key_4">>)),
?assertEqual(["bla bla", "bla bla bla", "tata ta ta"], wgconfig:get_string_list(<<"section 3">>, <<"key_8">>)),
?assertEqual(["my", "string"], wgconfig:get_string_list(<<"section 1">>, <<"key_0">>, ["my", "string"])),
?assertThrow({wgconfig_error, value_not_found, <<"section 1">>, <<"key_0">>},
wgconfig:get_string_list(<<"section 1">>, <<"key_0">>)),
application:stop(wgconfig),
ok.
get_binary_list_test() ->
setup(),
wgconfig:load_configs(["./test/data/my_config3.ini"]),
?assertEqual([<<"hello">>], wgconfig:get_binary_list(<<"section 1">>, <<"key_3">>)),
?assertEqual([<<"one">>, <<"two">>, <<"three">>, <<"four">>],
wgconfig:get_binary_list(<<"section 3">>, <<"key_1">>)),
?assertEqual([<<"a">>, <<"b">>, <<"c">>, <<"d">>],
wgconfig:get_binary_list(<<"section 3">>, <<"key_2">>)),
?assertEqual([<<"a">>, <<"b">>, <<"c">>, <<"d">>],
wgconfig:get_binary_list(<<"section 3">>, <<"key_3">>)),
?assertEqual([<<"-100">>], wgconfig:get_binary_list(<<"section 3">>, <<"key_4">>)),
?assertEqual([<<"bla bla">>, <<"bla bla bla">>, <<"tata ta ta">>],
wgconfig:get_binary_list(<<"section 3">>, <<"key_8">>)),
?assertEqual([<<"my binary">>], wgconfig:get_binary_list(<<"section 1">>, <<"key_0">>, [<<"my binary">>])),
?assertThrow({wgconfig_error, value_not_found, <<"section 1">>, <<"key_0">>},
wgconfig:get_binary_list(<<"section 1">>, <<"key_0">>)),
application:stop(wgconfig),
ok.
names_test() ->
setup(),
wgconfig:load_configs(["./test/data/my_config3.ini"]),
?assertEqual({ok, <<"on">>}, wgconfig:get("section 2", key_3)),
?assertEqual(true, wgconfig:get_bool("section 1", <<"key_1">>)),
?assertEqual(false, wgconfig:get_bool(<<"section 1">>, "key_2")),
?assertEqual(77, wgconfig:get_int('section 1', <<"key_4">>)),
?assertEqual(0, wgconfig:get_int(<<"section 3">>, key_5)),
?assertEqual([<<"one">>, <<"two">>, <<"three">>, <<"four">>],
wgconfig:get_binary_list("section 3", key_1)),
application:stop(wgconfig),
ok.
set_test() ->
setup(),
wgconfig:load_configs(["./test/data/my_config3.ini"]),
?assertEqual(false, wgconfig:get_bool(<<"section 1">>, "key_2")),
wgconfig:set(<<"section 1">>, <<"key_2">>, true),
?assertEqual({ok, <<"true">>}, wgconfig:get(<<"section 1">>, <<"key_2">>)),
?assertEqual(true, wgconfig:get_bool(<<"section 1">>, <<"key_2">>)),
wgconfig:set(<<"section 1">>, <<"key_2">>, 100),
?assertEqual({ok, <<"100">>}, wgconfig:get(<<"section 1">>, <<"key_2">>)),
?assertEqual(100, wgconfig:get_int(<<"section 1">>, <<"key_2">>)),
wgconfig:set(<<"section 1">>, <<"key_2">>, 3.14159),
?assertEqual(3.14159, wgconfig:get_float(<<"section 1">>, <<"key_2">>)),
wgconfig:set(<<"section 1">>, <<"key_2">>, "Hello"),
?assertEqual({ok, <<"Hello">>}, wgconfig:get(<<"section 1">>, <<"key_2">>)),
?assertEqual("Hello", wgconfig:get_string(<<"section 1">>, <<"key_2">>)),
wgconfig:set(<<"section 1">>, <<"key_2">>, <<"xxx">>),
?assertEqual({ok, <<"xxx">>}, wgconfig:get(<<"section 1">>, <<"key_2">>)),
?assertEqual(<<"xxx">>, wgconfig:get_binary(<<"section 1">>, <<"key_2">>)),
application:stop(wgconfig),
ok.
errors_test() ->
setup(),
wgconfig:load_config("./test/data/my_config.ini"),
?assertThrow({wgconfig_error, value_not_found, "my_section", "my_key"},
wgconfig:get_string("my_section", "my_key")),
?assertThrow({wgconfig_error, value_not_found, database, replica},
wgconfig:get_binary(database, replica)),
?assertThrow({wgconfig_error, value_not_found, database, replica},
wgconfig:get_binary(database, replica)),
?assertThrow({wgconfig_error, <<"invalid bool 'my_db'">>, database, db_name},
wgconfig:get_bool(database, db_name)),
?assertThrow({wgconfig_error, <<"invalid int 'my_db'">>, database, db_name},
wgconfig:get_int(database, db_name)),
?assertThrow({wgconfig_error, <<"invalid float 'my_db'">>, database, db_name},
wgconfig:get_float(database, db_name)),
ok.