/
SetIndicatorstype_test.py
138 lines (113 loc) · 4.88 KB
/
SetIndicatorstype_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
from SetIndicatorstype import parse_data, main
from unittest.mock import patch
import demistomock as demisto
import random
# Tests that the function returns the expected dictionary when given valid data.
def test_parse_data_happy_path():
# Arrange
list_content = ['apple', 'banana', 'orange', 'apple', 'banana', 'grape', 'kiwi', 'pear', 'peach', 'watermelon']
# Act
result = parse_data(list_content)
# Assert
assert result['Type'] == 17
assert result['ContentsFormat'] == 'pie'
assert len(result['Contents']['stats']) == 8
assert result['Contents']['params']['layout'] == 'horizontal'
# Tests that the function returns an empty dictionary when given an empty list.
def test_parse_data_empty_list():
# Arrange
list_content: list = []
# Act
result = parse_data(list_content)
# Assert
assert result['Type'] == 17
assert result['ContentsFormat'] == 'pie'
assert len(result['Contents']['stats']) == 0
assert result['Contents']['params']['layout'] == 'horizontal'
# Tests that the function correctly handles duplicate values in the list_content parameter.
def test_parse_data_duplicate_values():
# Arrange
list_content = ['apple', 'banana', 'orange', 'apple', 'banana', 'grape', 'kiwi', 'pear', 'peach', 'watermelon', 'watermelon']
# Act
result = parse_data(list_content)
# Assert
assert result['Type'] == 17
assert result['ContentsFormat'] == 'pie'
assert len(result['Contents']['stats']) == 8
assert result['Contents']['params']['layout'] == 'horizontal'
# Tests that the function returns an empty dictionary when the top_lists parameter is empty.
def test_parse_data_empty_top_lists():
# Arrange
list_content = ['apple', 'banana', 'orange']
# Act
result = parse_data(list_content)
# Assert
assert result['Type'] == 17
assert result['ContentsFormat'] == 'pie'
assert len(result['Contents']['stats']) == 3
assert result['Contents']['params']['layout'] == 'horizontal'
# Tests that the function correctly handles cases where there are less than 10 unique values in list_content.
def test_parse_data_less_than_10_unique_values():
# Arrange
list_content = ['apple', 'banana', 'orange', 'grape', 'kiwi', 'pear', 'peach']
# Act
result = parse_data(list_content)
# Assert
assert result['Type'] == 17
assert result['ContentsFormat'] == 'pie'
assert len(result['Contents']['stats']) == 7
assert result['Contents']['params']['layout'] == 'horizontal'
# Helper function to generate random data for testing
def generate_test_data():
data = []
for i in range(5):
data.append({"Indicator" + str(i): ["value"] * random.randint(1, 10)})
return data
def test_parse_data():
test_list_content = ["List1", "List1", "List2", "List1", "List2", "List3"]
# Patch random.randint to return a fixed value for consistent testing
with patch.object(random, 'randint', return_value=123456) as mocked_randint:
actual_results = parse_data(test_list_content)
mocked_randint.assert_called_with(0, 16777215) # Check if randint was called with the correct parameters
assert actual_results['Type'] == 17
assert actual_results['ContentsFormat'] == 'pie'
assert len(actual_results['Contents']['stats']) == 3
assert actual_results['Contents']['params']['layout'] == 'horizontal'
def test_main():
test_data = [
{"Indicator1": ["value1", "value2"]},
{"Indicator2": ["value1"]},
{"Indicator3": ["value1", "value2", "value3"]},
]
# Patch demisto.context and argToList functions
with patch.object(demisto, 'context', return_value={'ExtractedIndicators': test_data}), patch.object(demisto, 'results'), \
patch.object(random, 'randint', return_value=123456):
main()
result = demisto.results.call_args[0][0]
assert result.get('Type') == 17
assert result.get('ContentsFormat') == 'pie'
assert result.get('Contents', {}).get('params', {}).get('layout') == 'horizontal'
def test_main_without_data():
# Mock the demisto context and argToList functions
with patch.object(demisto, 'context', return_value={'ExtractedIndicators': None}), \
patch.object(demisto, 'results') as mocked_results:
main()
# Check if the results function was called with the correct parameters for the empty data case
mocked_results.assert_called_with({
"Type": 17,
"ContentsFormat": "bar",
"Contents": {
"stats": [
{
"data": [0],
"groups": None,
"name": "N/A",
"label": "N/A",
"color": "rgb(255, 23, 68)"
},
],
"params": {
"layout": "horizontal"
}
}
})