-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_LogStats.py
130 lines (104 loc) · 4.24 KB
/
test_LogStats.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
import unittest
import random
import os
import time
import datetime
from logStats import LogStats
from config import log_folder
class TestLogStats(unittest.TestCase):
def setUp(self):
"""
Selects a random log file for each test.
"""
log_files = os.listdir(log_folder)
random_log_name = log_files[random.randrange(len(log_files))]
print random_log_name
self.logStats = LogStats(random_log_name)
self.parser = self.logStats.parser
log_file = self.logStats.get_log_file()
self.num_lines = len(log_file.readlines())
log_file.seek(0, 0)
self.entries = self.logStats.get_entries_day()
log_file.seek(0, 0)
def tearDown(self):
self.logStats.get_log_file().close()
def test_read_lines(self):
"""
Helped me find why a different test didn't pass, best to be kept.
"""
for line_num in range(random.randrange(self.num_lines)):
line = self.logStats.get_entry()
if not line:
continue
self.assertIsNotNone(line)
def validate_date(self, date):
result = None
try:
result = datetime.datetime.strptime(date, '%d/%b/%Y')
except ValueError:
print('Invalid date!')
return result
def test_parse_date(self):
for line_number in range(random.randrange(self.num_lines)):
current_line = self.logStats.get_entry()
if not current_line:
continue
date = self.parser.parse_date(current_line)
self.assertIsNotNone(self.validate_date(date))
def validate_interval(self, interval):
since = interval[0]
until = interval[1]
result = None
try:
since_date = datetime.datetime.fromtimestamp(float(since)).strftime('%Y-%m-%d %H:%M:%S')
until_date = datetime.datetime.fromtimestamp(float(until)).strftime('%Y-%m-%d %H:%M:%S')
result = (since_date, until_date)
except ValueError:
print('Invalid since/until date!')
return result
def test_parse_interval_limits(self):
'''
Tests parser on returning 'since' and 'until' values.
'''
for line_number in range(random.randrange(self.num_lines)):
current_line = self.logStats.get_entry()
if not current_line:
continue
interval = self.parser.parse_interval(current_line)
if not self.parser.is_interval_valid(interval):
continue
self.assertIsNotNone(self.validate_interval(interval))
def test_organize_by_day(self):
for line_number in range(random.randrange(self.num_lines)):
current_line = self.logStats.get_entry()
if not current_line:
continue
date = self.parser.parse_date(current_line)
interval = self.parser.parse_interval(current_line)
if not self.parser.is_interval_valid(interval):
continue
self.assertTrue(date in self.entries)
self.assertTrue(interval in self.entries[date])
def test_previous_intervals_dates(self):
for date in self.entries:
prev_intervals = self.logStats.get_previous_intervals(date)
self.assertEqual(len(prev_intervals), 90 / 5)
for prev_date in prev_intervals:
self.assertTrue(type(prev_date) is datetime.datetime)
def test_convert_dates_timestamp(self):
for date in self.entries:
prev_intervals = self.logStats.get_previous_intervals(date)
conv_intervals = self.logStats.convert_timestamp(prev_intervals)
self.assertEqual(len(conv_intervals), 90 / 5)
for interval in conv_intervals:
self.assertTrue(type(interval) is float)
def test_get_stats(self):
for date in self.entries:
prev_intervals = self.logStats.get_previous_intervals(date)
conv_intervals = self.logStats.convert_timestamp(prev_intervals)
day_stats = self.logStats.get_stats(conv_intervals, self.entries[date])
self.assertIsNotNone(day_stats)
def main():
unittest.main()
if __name__ == '__main__':
main()