-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_cli.py
178 lines (157 loc) · 5.55 KB
/
test_cli.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
from __future__ import absolute_import, print_function
from bragly.cli import parse_args
import arrow
# Test argument generation
def check_args(args, result_keys):
args = args.split()
parser, args = parse_args(args)
assert (set(result_keys) == set(args.keys()))
def test_args():
test_cases = [
{
'args': 'w test message',
'result_keys': ['message', 'func', 'timestamp', 'tags']
},
{
'args': 's --start=2015-01-01 --period=month --tags tag1 tag2 --text text1 text2',
'result_keys': ['period', 'tags', 'text', 'start', 'end', 'func', 'form', 'all_args']
},
{
'args': 'r --start=2015-01-01 --period=month',
'result_keys': ['period', 'start', 'end', 'func', 'form']
},
]
for test_case in test_cases:
yield check_args, test_case['args'], test_case['result_keys']
#
# Check actuall parsing of arguments (and associated values)
#
# Check parsing of write related commands
def check_write_parsing(args, result):
args = args.split()
parser, args = parse_args(args)
comp_keys = ['message', 'timestamp', 'tags']
for key in comp_keys:
if key in result:
assert (args[key] == result[key])
else:
assert (args[key] is None)
assert (args['func'].__name__ == result['func'])
def test_write_parsing():
test_cases = [
{
'args': 'w test message',
'result': {'message': ['test', 'message'], 'func': 'write'},
},
{
'args': 'w test message --tags tag1',
'result': {'message': ['test', 'message'], 'func': 'write', 'tags': ['tag1']},
},
{
'args': 'w test message --tags tag1 tag2',
'result': {'message': ['test', 'message'], 'func': 'write', 'tags': ['tag1', 'tag2']},
},
]
for test_case in test_cases:
yield check_write_parsing, test_case['args'], test_case['result']
# Check parsing of read related commands
def check_read_parsing(args, result):
args = args.split()
parser, args = parse_args(args)
comp_keys = ['start', 'end', 'period', 'form']
for key in comp_keys:
if key in result:
assert (args[key] == result[key])
else:
assert (args[key] is None)
assert (args['func'].__name__ == result['func'])
def test_read_parsing():
test_cases = [
{
'args': 'r --start=2015-01-01 --end=2016-01-01',
'result': {'func': 'read', 'form': 'json', 'start': arrow.get('2015-01-01'), 'end': arrow.get('2016-01-01')}
},
{
'args': 'r --start=2015-01-01 --period=month',
'result': {'func': 'read', 'form': 'json', 'start': arrow.get('2015-01-01'), 'period': 'month'}
},
]
for test_case in test_cases:
yield check_read_parsing, test_case['args'], test_case['result']
# Check parsing of search command
def check_search_parsing(args, result):
args = args.split()
parser, args = parse_args(args)
comp_keys = ['start', 'end', 'period', 'form']
for key in comp_keys:
if key in result:
assert (args[key] == result[key])
else:
assert (args[key] is None)
assert (args['func'].__name__ == result['func'])
def test_serach_parsing():
test_cases = [
{
'args': 's --start=2015-01-01 --end=2016-01-01',
'result': {'func': 'search', 'form': 'json', 'start': arrow.get('2015-01-01'),
'end': arrow.get('2016-01-01')}
},
{
'args': 's --start=2015-01-01 --period=month',
'result': {'func': 'search', 'form': 'json', 'start': arrow.get('2015-01-01'), 'period': 'month'}
},
{
'args': 's --start=2015-01-01 --end=2016-01-01',
'result': {
'func': 'search',
'form': 'json',
'start': arrow.get('2015-01-01'),
'end': arrow.get('2016-01-01'),
},
},
{
'args': 's --start=2015-01-01 --period=day --tags tag1 tag2 --text text1 text2',
'result': {
'func': 'search',
'form': 'json',
'start': arrow.get('2015-01-01'),
'period': 'day',
'tags': ['tag1', 'tag2'],
'text': ['text1', 'text2']}
},
{
'args': 's --start=2015-01-01 --period year --tags tag1 tag2',
'result': {
'func': 'search',
'form': 'json',
'start': arrow.get('2015-01-01'),
'period': 'year',
'tags': ['tag1', 'tag2'],
'all_args': False,
},
},
{
'args': 's --start=2015-01-01 --period year --tags tag1 tag2 --any',
'result': {
'func': 'search',
'form': 'json',
'start': arrow.get('2015-01-01'),
'period': 'year',
'tags': ['tag1', 'tag2'],
'all_args': False,
},
},
{
'args': 's --start=2015-01-01 --period year --tags tag1 tag2 --all',
'result': {
'func': 'search',
'form': 'json',
'start': arrow.get('2015-01-01'),
'period': 'year',
'tags': ['tag1', 'tag2'],
'all_args': True,
},
}
]
for test_case in test_cases:
yield check_search_parsing, test_case['args'], test_case['result']