forked from openwallet-foundation/acapy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_argparse.py
290 lines (235 loc) · 8.88 KB
/
test_argparse.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from configargparse import ArgumentTypeError
from asynctest import TestCase as AsyncTestCase, mock as async_mock
from .. import argparse
from ..error import ArgsParseError
from ..util import BoundedInt, ByteSize
class TestArgParse(AsyncTestCase):
async def test_groups(self):
"""Test optional argument parsing."""
parser = argparse.create_argument_parser()
groups = (
g
for g in argparse.group.get_registered()
if g is not argparse.TransportGroup
)
argparse.load_argument_groups(parser, *groups)
parser.parse_args([])
async def test_transport_settings(self):
"""Test required argument parsing."""
parser = argparse.create_argument_parser()
group = argparse.TransportGroup()
group.add_arguments(parser)
with async_mock.patch.object(parser, "exit") as exit_parser:
parser.parse_args(["-h"])
exit_parser.assert_called_once()
result = parser.parse_args(
[
"--inbound-transport",
"http",
"0.0.0.0",
"80",
"--outbound-transport",
"http",
"--max-outbound-retry",
"5",
]
)
assert result.inbound_transports == [["http", "0.0.0.0", "80"]]
assert result.outbound_transports == ["http"]
settings = group.get_settings(result)
assert settings.get("transport.inbound_configs") == [["http", "0.0.0.0", "80"]]
assert settings.get("transport.outbound_configs") == ["http"]
assert result.max_outbound_retry == 5
async def test_outbound_is_required(self):
"""Test that either -ot or -oq are required"""
parser = argparse.create_argument_parser()
group = argparse.TransportGroup()
group.add_arguments(parser)
result = parser.parse_args(
[
"--inbound-transport",
"http",
"0.0.0.0",
"80",
]
)
with self.assertRaises(argparse.ArgsParseError):
settings = group.get_settings(result)
async def test_redis_outbound_queue(self):
"""Test Redis outbound queue connection string."""
parser = argparse.create_argument_parser()
group = argparse.TransportGroup()
group.add_arguments(parser)
result = parser.parse_args(
[
"--inbound-transport",
"http",
"0.0.0.0",
"80",
"--outbound-queue",
"redis://test:1234",
]
)
settings = group.get_settings(result)
self.assertEqual(settings.get("transport.outbound_queue"), "redis://test:1234")
self.assertEqual(settings.get("transport.outbound_queue_prefix"), "acapy")
self.assertEqual(
settings.get("transport.outbound_queue_class"),
"aries_cloudagent.transport.outbound.queue.redis:RedisOutboundQueue",
)
async def test_redis_outbound_queue_prefix(self):
"""Test Redis outbound queue prefix."""
parser = argparse.create_argument_parser()
group = argparse.TransportGroup()
group.add_arguments(parser)
result = parser.parse_args(
[
"--inbound-transport",
"http",
"0.0.0.0",
"80",
"--outbound-queue",
"redis://test:1234",
"--outbound-queue-prefix",
"foo",
]
)
settings = group.get_settings(result)
self.assertEqual(settings.get("transport.outbound_queue"), "redis://test:1234")
self.assertEqual(settings.get("transport.outbound_queue_prefix"), "foo")
async def test_redis_outbound_queue_class(self):
"""Test Redis outbound queue custom class."""
parser = argparse.create_argument_parser()
group = argparse.TransportGroup()
group.add_arguments(parser)
result = parser.parse_args(
[
"--inbound-transport",
"http",
"0.0.0.0",
"80",
"--outbound-queue",
"redis://test:1234",
"--outbound-queue-class",
"mymodule:MyClass",
]
)
settings = group.get_settings(result)
self.assertEqual(settings.get("transport.outbound_queue"), "redis://test:1234")
self.assertEqual(
settings.get("transport.outbound_queue_class"), "mymodule:MyClass"
)
async def test_general_settings_file(self):
"""Test file argument parsing."""
parser = argparse.create_argument_parser()
group = argparse.GeneralGroup()
group.add_arguments(parser)
with async_mock.patch.object(parser, "exit") as exit_parser:
parser.parse_args(["-h"])
exit_parser.assert_called_once()
result = parser.parse_args(
[
"--arg-file",
"./aries_cloudagent/config/tests/test-general-args.yaml",
]
)
assert result.external_plugins == ["foo"]
assert result.storage_type == "bar"
settings = group.get_settings(result)
assert settings.get("external_plugins") == ["foo"]
assert settings.get("storage_type") == "bar"
async def test_plugin_config_file(self):
"""Test file argument parsing."""
parser = argparse.create_argument_parser()
group = argparse.GeneralGroup()
group.add_arguments(parser)
result = parser.parse_args(
[
"--endpoint",
"localhost",
"--plugin-config",
"./aries_cloudagent/config/tests/test_plugins_config.yaml",
]
)
assert (
result.plugin_config
== "./aries_cloudagent/config/tests/test_plugins_config.yaml"
)
settings = group.get_settings(result)
assert settings.get("plugin_config").get("mock_resolver") == {
"methods": ["sov", "btcr"]
}
async def test_transport_settings_file(self):
"""Test file argument parsing."""
parser = argparse.create_argument_parser()
group = argparse.GeneralGroup()
group.add_arguments(parser)
group = argparse.TransportGroup()
group.add_arguments(parser)
with async_mock.patch.object(parser, "exit") as exit_parser:
parser.parse_args(["-h"])
exit_parser.assert_called_once()
result = parser.parse_args(
[
"--arg-file",
"./aries_cloudagent/config/tests/test-transport-args.yaml",
]
)
# no asserts, just testing that the parser doesn't fail
def test_bytesize(self):
bs = ByteSize()
with self.assertRaises(ArgumentTypeError):
bs(None)
with self.assertRaises(ArgumentTypeError):
bs("")
with self.assertRaises(ArgumentTypeError):
bs("a")
with self.assertRaises(ArgumentTypeError):
bs("1.5")
with self.assertRaises(ArgumentTypeError):
bs("-1")
assert bs("101") == 101
assert bs("101b") == 101
assert bs("101KB") == 103424
assert bs("2M") == 2097152
assert bs("1G") == 1073741824
assert bs("1t") == 1099511627776
bs = ByteSize(min=10)
with self.assertRaises(ArgumentTypeError):
bs("5")
assert bs("12") == 12
bs = ByteSize(max=10)
with self.assertRaises(ArgumentTypeError):
bs("15")
assert bs("10") == 10
assert repr(bs) == "bytes"
def test_bounded_int(self):
bounded = BoundedInt()
with self.assertRaises(ArgumentTypeError):
bounded(None)
with self.assertRaises(ArgumentTypeError):
bounded("")
with self.assertRaises(ArgumentTypeError):
bounded("a")
with self.assertRaises(ArgumentTypeError):
bounded("1.5")
assert bounded("101") == 101
assert bounded("-99") == -99
bounded = BoundedInt(min=10)
with self.assertRaises(ArgumentTypeError):
bounded("5")
assert bounded("12") == 12
bounded = BoundedInt(max=10)
with self.assertRaises(ArgumentTypeError):
bounded("15")
assert bounded("10") == 10
assert repr(bounded) == "integer"
async def test_mediation_x_clear_and_default(self):
parser = argparse.create_argument_parser()
group = argparse.MediationGroup()
group.add_arguments(parser)
with self.assertRaises(argparse.ArgsParseError):
args = parser.parse_args(
["--clear-default-mediator", "--default-mediator-id", "asdf"]
)
group.get_settings(args)