-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkaffeefee_test.go
More file actions
217 lines (180 loc) · 4.97 KB
/
kaffeefee_test.go
File metadata and controls
217 lines (180 loc) · 4.97 KB
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
211
212
213
214
215
216
package main
import (
"fmt"
"math"
"strings"
"testing"
"time"
)
const (
user1="user 1"
)
func TestGetPeriod(t *testing.T) {
now := time.Now()
from, to := getPeriod(now)
if from.Weekday() != time.Monday {
t.Error("expected from: Monday, got ", from.Weekday())
}
if from.Hour() != 0 || from.Minute() != 0 || from.Second() != 0 || from.Nanosecond() != 0 {
t.Error("expected from: 00:00:00.0, got ",
fmt.Sprintf("%d:%d:%d.%d", from.Hour(), from.Minute(), from.Second(), from.Nanosecond()))
}
if to.Weekday() != time.Sunday {
t.Error("expected from: Sunday, got ", from.Weekday())
}
if to.Hour() != 23 || to.Minute() != 59 || to.Second() != 59 || to.Nanosecond() != 0 {
t.Error("expected from: 00:00:00.0, got ",
fmt.Sprintf("%d:%d:%d.%d", to.Hour(), to.Minute(), to.Second(), to.Nanosecond()))
}
}
func TestMapping(t *testing.T) {
input := map[time.Time]int64{}
layout := "2006-01-02 15:04:05"
inputDate, err := time.Parse(layout, "2016-11-12 15:00:00")
if err != nil {
t.Error(err)
}
input[inputDate] = 10
inputDate, err = time.Parse(layout, "2016-11-10 12:00:00")
if err != nil {
t.Error(err)
}
input[inputDate] = 11
inputDate, err = time.Parse(layout, "2016-11-11 15:00:00")
if err != nil {
t.Error(err)
}
input[inputDate] = 12
output := mapping(input)
if len(output) != 3 {
t.Error("expected length 3, was ", len(output))
}
}
func TestCalculateSumForUser(t *testing.T) {
logs := userlogs{
log{ID: 1, UserID: 1, Number: 1, Timestamp: 10},
log{ID: 2, UserID: 1, Number: 1, Timestamp: 12},
log{ID: 3, UserID: 1, Number: 1, Timestamp: 14},
log{ID: 4, UserID: 1, Number: 1, Timestamp: 16},
log{ID: 5, UserID: 1, Number: 1, Timestamp: 18},
log{ID: 1, UserID: 1, Number: 1, Timestamp: 20},
log{ID: 2, UserID: 1, Number: 1, Timestamp: 22},
log{ID: 3, UserID: 1, Number: 1, Timestamp: 24},
log{ID: 4, UserID: 1, Number: 1, Timestamp: 26},
log{ID: 5, UserID: 1, Number: 1, Timestamp: 28},
}
sum := logs.calculateSumForUser(10, 28)
if sum != 10 {
t.Error("expected 10, got ", sum)
}
sum = logs.calculateSumForUser(20, 28)
if sum != 5 {
t.Error("expected 5, got ", sum)
}
}
func TestEmptyUser(t *testing.T) {
users := userList{
user{UserID: 1, Name: user1, Today: 10},
user{UserID: 2, Name: "user 2", Today: 10},
user{UserID: 3, Name: "user 3", Today: 10},
}
userMap := users.emptyMap()
if len(userMap) != 3 {
t.Error("expected 3 elements, got ", len(userMap))
}
if cnt, ok := userMap[user1]; !ok || cnt != 0.0 {
t.Error("error accessing userMap")
}
}
func TestGetDailyCount(t *testing.T) {
logs := userlogs{
log{ID: 1, UserID: 1, Number: 1, Timestamp: 10},
log{ID: 2, UserID: 1, Number: 1, Timestamp: 20},
log{ID: 3, UserID: 1, Number: 1, Timestamp: 30},
log{ID: 4, UserID: 1, Number: 1, Timestamp: 1000000},
}
start := time.Unix(50, 0)
data := logs.getDailyCount(start, 0)
if len(data) != 1 {
t.Error("Expected 1 element, got ", len(data))
}
for _, d := range data {
if d != 3 {
t.Error("expected count of 3, got ", d)
}
}
}
func TestGetWeeklyCount(t *testing.T) {
logs := userlogs{
log{ID: 1, UserID: 1, Number: 1, Timestamp: 10},
log{ID: 2, UserID: 1, Number: 1, Timestamp: 20},
log{ID: 3, UserID: 1, Number: 1, Timestamp: 30},
log{ID: 4, UserID: 1, Number: 1, Timestamp: 10000000},
}
start := time.Unix(604800, 0)
data := logs.getWeeklyCount(start, 0)
if len(data) != 2 {
t.Error("Expected 2 element, got ", len(data))
}
}
func TestGetMonthlyCount(t *testing.T) {
logs := userlogs{
log{ID: 1, UserID: 1, Number: 1, Timestamp: 10},
log{ID: 2, UserID: 1, Number: 1, Timestamp: 20},
log{ID: 3, UserID: 1, Number: 1, Timestamp: 30},
log{ID: 4, UserID: 1, Number: 1, Timestamp: 100000000},
}
start := time.Unix(0, 0)
data := logs.getMonthlyCount(start, 0)
if len(data) != 1 {
t.Error("Expected 1 element, got ", len(data))
}
for _, d := range data {
if d != 3 {
t.Error("expected count of 3, got ", d)
}
}
}
func TestRenderPage(t *testing.T) {
users := []user{user{UserID: 1, Name: user1, Today: 0}}
output := renderPage(true, users)
u, ok := output["Users"]
if !ok {
t.Error("expected one user but got none")
}
testUser := u.([]user)
if len(testUser) != 1 {
t.Error("expected one element, got ", len(testUser))
}
}
func TestGetUserIDFromName(t *testing.T) {
users := userList{user{UserID: 1, Name: user1, Today: 0}}
id := users.getUserIDFromName("user 1")
if id != 1 {
t.Error("expected user id 1, got ", id)
}
id = users.getUserIDFromName("user2")
if id != -1 {
t.Error("expected user id -1, got ", id)
}
}
func TestHasLocalImage(t *testing.T) {
ret, path := hasLocalImage("Default")
fmt.Println(path)
if !ret {
t.Error("expected placeholder not found")
}
if strings.HasPrefix(path, "Default.png") {
t.Error("encountered unexpected image path")
}
}
func TestCheckIsNaN(t *testing.T) {
val := checkNaN(math.NaN())
if val != 0.0 {
t.Errorf("expected 0.0 but got %f", val)
}
val = checkNaN(1.0)
if val != 1.0 {
t.Errorf("expected 1.0 but got %f", val)
}
}