/
GetListRow_test.py
210 lines (188 loc) · 8.28 KB
/
GetListRow_test.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
import pytest
from CommonServerPython import *
RETURN_ERROR_TARGET = 'GetListRow.return_error'
DATA_WITH_CUSTOM_SEP = [{"Contents": "name;id\nname1;id1\nname2;id2"}]
DATA_WITH_NEW_LINE_SEP = [{"Contents": "name\nid\nname1\nid1\nname2\nid2"}]
DATA_WITH_TAB_SEP = [{"Contents": "name id\nname1 id1\nname2 id2"}]
DATA_WITH_TAB_SEP_2 = [{"Contents": "name\tid\nname1\tid1\nname2\tid2"}]
@pytest.mark.parametrize(
"parse_all, header, value, expected",
[
("True", "", "", 0),
("true", "", "test", 0),
("False", "", "test", 1),
("False", "", "", 1),
("False", "test", "test", 0)
]
)
def test_validate_args(mocker, parse_all, header, value, expected):
from GetListRow import validate_args
return_error_mock = mocker.patch(RETURN_ERROR_TARGET)
validate_args(parse_all, header, value)
assert return_error_mock.call_count == expected
@pytest.mark.parametrize(
"list_result, expected",
[
("False", 0),
("id, name, status, title", 0),
("Item not found", 1),
("", 1)
]
)
def test_does_list_exist(mocker, list_result, expected):
from GetListRow import validate_list_exists
return_error_mock = mocker.patch(RETURN_ERROR_TARGET)
validate_list_exists(list_result)
assert return_error_mock.call_count == expected
@pytest.mark.parametrize(
"headers, header, expected",
[
(['id', 'name', 'title', 'status'], 'id', 0),
(['id', 'name', 'title', 'status'], 'status', 0),
(['id', 'name', 'title', 'status'], "statu", 1),
(['id', 'name'], "title", 1)
]
)
def test_does_header_exist(mocker, headers, header, expected):
from GetListRow import validate_header_exists
return_error_mock = mocker.patch(RETURN_ERROR_TARGET)
validate_header_exists(headers, header)
assert return_error_mock.call_count == expected
@pytest.mark.parametrize(
"parse_all, header, value, list_name, list_separator, expected",
[
("True", "", "on", "getListRow", ',', "List Result"),
("False", "status", "on", "getListRow", ',', "List Result"),
("False", "status", "n", "getListRow", ',', "No results found")
]
)
def test_parse_list(mocker, parse_all, header, value, list_name, list_separator, expected):
from GetListRow import parse_list
mocker.patch.object(demisto, "executeCommand", return_value=[{"Contents": '''id,name,title,status
1,Chanoch,junior,on
2,Or,manager,off
3,Chen,developer,on'''}])
res = parse_list(parse_all, header, value, list_name, list_separator)
assert expected in res.readable_output
@pytest.mark.parametrize(
"data, parse_all, header, value, list_name, list_separator, expected",
[
(DATA_WITH_CUSTOM_SEP, "True", "name", "id", "getListRow", ';',
[{'name': 'name1', 'id': 'id1'}, {'name': 'name2', 'id': 'id2'}]),
(DATA_WITH_CUSTOM_SEP, "False", "name", "name2", "getListRow", ';', [{'name': 'name2', 'id': 'id2'}])
]
)
def test_custom_list_separator(mocker, data, parse_all, header, value, list_name, list_separator, expected):
"""
Given:
- Data that should be split by the custom separator ';'.
When:
- Running the script with a custom separator arg
Then:
- Ensure the parsed sata was split correctly
"""
from GetListRow import parse_list
mocker.patch.object(demisto, "executeCommand", return_value=data)
res = parse_list(parse_all, header, value, list_name, list_separator)
assert res.outputs.get('Results') == expected
@pytest.mark.parametrize(
"data, parse_all, header, value, list_name, list_separator, expected",
[
(DATA_WITH_NEW_LINE_SEP, "True", "name", "id", "getListRow", '\n',
[{'name': 'id'}, {'name': 'name1'}, {'name': 'id1'}, {'name': 'name2'}, {'name': 'id2'}]),
(DATA_WITH_NEW_LINE_SEP, "False", "name", "name2", "getListRow", '\n', [{'name': 'name2'}])
]
)
def test_custom_list_new_line_sep(mocker, data, parse_all, header, value, list_name, list_separator, expected):
"""
Given:
- Data that should be split by the custom separator '\n'.
When:
- Running the script with a custom separator arg
Then:
- Ensure the parsed sata was split correctly
"""
from GetListRow import parse_list
mocker.patch.object(demisto, "executeCommand", return_value=data)
res = parse_list(parse_all, header, value, list_name, list_separator)
assert res.outputs.get('Results') == expected
@pytest.mark.parametrize(
"data, parse_all, header, value, list_name, list_separator, expected",
[
(DATA_WITH_TAB_SEP, "True", "name", "id", "getListRow", '\t',
[{'name': 'name1', 'id': 'id1'}, {'name': 'name2', 'id': 'id2'}]),
(DATA_WITH_TAB_SEP, "False", "name", "name2", "getListRow", ' ', [{'name': 'name2', 'id': 'id2'}]),
(DATA_WITH_TAB_SEP_2, "True", "name", "id", "getListRow", '\\t',
[{'name': 'name1', 'id': 'id1'}, {'name': 'name2', 'id': 'id2'}]),
(DATA_WITH_TAB_SEP_2, "True", "name", "id", "getListRow", '\t',
[{'name': 'name1', 'id': 'id1'}, {'name': 'name2', 'id': 'id2'}]),
]
)
def test_custom_list_tab_sep(mocker, data, parse_all, header, value, list_name, list_separator, expected):
"""
Given:
- Data that should be split by the tab separator (' ').
When:
- Running the script with a custom separator arg
Then:
- Ensure the parsed sata was split correctly
"""
import GetListRow
mock_args = {
'list_name': list_name,
'parse_all': parse_all,
'header': header,
'value': value,
'list_separator': list_separator
}
mocker.patch.object(demisto, "args", return_value=mock_args)
mocker.patch.object(demisto, "executeCommand", return_value=data)
mocker.patch.object(GetListRow, "return_results", return_value=data)
GetListRow.main()
assert GetListRow.return_results.call_args[0][0].outputs.get('Results') == expected
@pytest.mark.parametrize(
"data, parse_all, header, value, expected_context_key",
[
(DATA_WITH_NEW_LINE_SEP, "True", "name", "id", ['list_name', 'parse_all']),
(DATA_WITH_NEW_LINE_SEP, "False", "name", "name2", ['list_name', 'parse_all', 'header', 'value'])
]
)
def test_context_path_are_correct(mocker, data, parse_all, header, value, expected_context_key):
from GetListRow import parse_list
mocker.patch.object(demisto, "executeCommand", return_value=data)
res = parse_list(parse_all, header, value, list_name="getListRow", list_separator='\n')
assert res.outputs_prefix == 'GetListRow'
assert res.outputs_key_field == expected_context_key
@pytest.mark.parametrize(
"list_data, expected_headers, expected_lines",
[
("header_a ,header_b\nline_1_item_a,line_1_item_b\nline_2_item_a,line_2_item_b",
['header_a ', 'header_b'],
[['line_1_item_a', 'line_1_item_b'], ['line_2_item_a', 'line_2_item_b']]),
("header_a ,header_b\nline\r_1_item_a,line_1_item_b\nline\r_2_item_a,line_2_item_b",
['header_a ', 'header_b'],
[['line\r_1_item_a', 'line_1_item_b'], ['line\r_2_item_a', 'line_2_item_b']]),
("header_a ,header_b\r\nline_1_item_a,line_1_item_b\r\nline_2_item_a,line_2_item_b",
['header_a ', 'header_b'],
[['line_1_item_a', 'line_1_item_b'], ['line_2_item_a', 'line_2_item_b']])
]
)
def test_list_to_headers_and_lines(list_data, expected_headers, expected_lines):
"""
Given:
- list_data.
- Case 1: list data without any \r.
- Case 2: list data with \r in the middle of a line.
- Case 3: list data with \r followed by \n at the end of each line (except for last).
When:
- Running list_to_headers_and_lines.
Then:
- Ensure that the right parts of line was parsed into headers & lines.
- Case 1: Should split the lines by \n.
- Case 2: Should split the lines by \n and not do anything about the \r.
- Case 3: Should split the lines by \r\n.
"""
from GetListRow import list_to_headers_and_lines
headers, lines = list_to_headers_and_lines(list_data, ",")
assert expected_headers == headers
assert expected_lines == lines