-
Notifications
You must be signed in to change notification settings - Fork 6
/
test_unit.py
153 lines (124 loc) · 4.35 KB
/
test_unit.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
from datetime import datetime
import numpy
import pymongo
import pytest
from modules.background_task import generate_time_intervals, get_data_in_interval
from modules.background_task import get_result_table
from modules.background_task.sliding_window_method import average
from modules.background_task.sliding_window_method import cal_entropy_per_window
from modules.background_task.sliding_window_method import cal_std_deviation
from modules.background_task.variance_method import cal_average_entropy_for_each_conn
from modules.background_task.variance_method import cal_entropy
from modules.background_task.variance_method import cal_variance
from modules.background_task.variance_method import find_possibles
from modules.background_task.variance_method import key_in_dict
from modules.config import Config
from modules.JSONEncoder import JSONEncoder
from test_data import data_list
def test_config():
assert Config
assert Config.MONGO_URI
assert int(Config.PORT)
assert Config.DEBUG
assert Config.NODE_NAME
assert Config.MONGO_DATABASE
assert int(Config.BETA)
assert not int(Config.OMEGA)
assert int(Config.SLIDING_WINDOW)
assert int(Config.SLIDING_WINDOW_PIECE)
@pytest.fixture(scope="module")
def test_get_data_in_interval():
my_client = pymongo.MongoClient(Config.MONGO_URI)
my_db = my_client[Config.MONGO_DATABASE]
mongo_res = my_db[Config.MONGO_LOG_COLLECTION]
start = int(datetime.now().timestamp() * 10000 - float(Config.SLIDING_WINDOW) * 1000)
end = int(datetime.now().timestamp() * 10000)
cursor = get_data_in_interval(mongo_res, start, end)
print(type(cursor))
assert type(cursor) == pymongo.cursor.Cursor
return cursor
def test_generate_time_intervals():
start = 500
end = 5400
win = 5
expected = 980
lst = generate_time_intervals(start, win, end)
assert type(lst) == list
assert len(lst) == expected
assert type(lst[0]) == list
def test_get_result_table(test_get_data_in_interval):
res = get_result_table(test_get_data_in_interval)
assert type(res) == dict
assert res.keys() is not None
with pytest.raises(AssertionError):
assert type(res) == list
@pytest.fixture(scope="module")
def test_cal_entropy_per_window():
out = cal_entropy_per_window(data_list)
assert type(out) == list
assert len(out) != 0
for i in range(len(out)):
assert type(out[i]) == numpy.float64
return out
@pytest.fixture(scope="module")
def test_average(test_cal_entropy_per_window):
a = average([0, 0, 0, 0, 0])
b = average(test_cal_entropy_per_window)
c = average([])
assert a == 0
assert c == 0
assert b == 3.777233940320172
with pytest.raises(TypeError):
average(45)
with pytest.raises(TypeError):
average("string")
return b
def test_cal_std_deviation(test_cal_entropy_per_window, test_average):
a = cal_std_deviation(test_cal_entropy_per_window, test_average)
b = cal_std_deviation([0, 0, 0, 0], 0)
c = cal_std_deviation([0, 0, 0, 0], 45)
assert a == 0.6291135436856912
assert b == 0
assert c == 45
with pytest.raises(TypeError):
cal_std_deviation(343, 432)
return a
def test_key_in_dict():
a = key_in_dict(3, {"4": 4, "3": 3})
b = key_in_dict(23, {23: 32})
assert not a
assert b
assert type(b) == int
@pytest.fixture(scope="module")
def test_cal_entropy():
a = cal_entropy(data_list)
b = cal_entropy([{}, {}, {}])
c = cal_entropy([])
assert type(a) == dict
assert b == {}
assert c == {}
return a
@pytest.fixture(scope="module")
def test_cal_average_entropy_for_each_conn(test_cal_entropy):
a = cal_average_entropy_for_each_conn(test_cal_entropy)
assert type(a) == dict
for i in a.keys():
assert type(i) == str
assert type(a[i]) == numpy.float64
return a
@pytest.fixture(scope="module")
def test_cal_variance(test_cal_entropy, test_cal_average_entropy_for_each_conn):
a = cal_variance(test_cal_entropy, test_cal_average_entropy_for_each_conn)
b = cal_variance({}, {})
assert not b
assert type(a) == dict
return a
def test_find_possibles(test_cal_variance):
a = find_possibles(test_cal_variance, data_list)
for i in a.keys():
assert type(i) == str
assert type(a[i]) == dict
assert a
return a
if __name__ == "__main__":
test_get_data_in_interval()