-
Notifications
You must be signed in to change notification settings - Fork 2
/
server_test.go
135 lines (127 loc) · 3.56 KB
/
server_test.go
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
package main
import (
"testing"
"time"
)
type UserPreferencesScoreTest struct {
Pref interval
Slot interval
Score float64
}
func mustTimeParse(format string, t string) time.Time {
tm, err := time.Parse(format, t)
if err != nil {
panic(err)
}
return tm
}
func TestUserPreferencesScore(t *testing.T) {
tfmt := "2006-01-02T15:04:05Z"
tests := []UserPreferencesScoreTest{
// user preferences don't span a UTC day
// slot doesn't span
UserPreferencesScoreTest{
Pref: interval{
Start: mustTimeParse(tfmt, "0000-01-01T09:00:00Z"),
End: mustTimeParse(tfmt, "0000-01-01T17:00:00Z"),
},
Slot: interval{
Start: mustTimeParse(tfmt, "2015-04-08T16:30:00Z"),
End: mustTimeParse(tfmt, "2015-04-08T17:00:00Z"),
},
Score: 0.0,
},
UserPreferencesScoreTest{
Pref: interval{
Start: mustTimeParse(tfmt, "0000-01-01T09:00:00Z"),
End: mustTimeParse(tfmt, "0000-01-01T17:00:00Z"),
},
Slot: interval{
Start: mustTimeParse(tfmt, "2015-04-08T17:00:00Z"),
End: mustTimeParse(tfmt, "2015-04-08T17:30:00Z"),
},
Score: -1000.0,
},
// user preferences don't span a UTC day
// slot does span
UserPreferencesScoreTest{
Pref: interval{
Start: mustTimeParse(tfmt, "0000-01-01T09:00:00Z"),
End: mustTimeParse(tfmt, "0000-01-01T17:00:00Z"),
},
Slot: interval{
Start: mustTimeParse(tfmt, "2015-04-08T23:30:00Z"),
End: mustTimeParse(tfmt, "2015-04-09T00:00:00Z"),
},
Score: -1000.0,
},
// user preferences span a UTC day
// slot does not span
UserPreferencesScoreTest{
Pref: interval{
Start: mustTimeParse(tfmt, "0000-01-01T20:00:00Z"),
End: mustTimeParse(tfmt, "0000-01-02T04:00:00Z"),
},
Slot: interval{
Start: mustTimeParse(tfmt, "2015-04-08T03:30:00Z"),
End: mustTimeParse(tfmt, "2015-04-08T04:00:00Z"),
},
Score: 0.0,
},
UserPreferencesScoreTest{
Pref: interval{
Start: mustTimeParse(tfmt, "0000-01-01T20:00:00Z"),
End: mustTimeParse(tfmt, "0000-01-02T04:00:00Z"),
},
Slot: interval{
Start: mustTimeParse(tfmt, "2015-04-08T04:00:00Z"),
End: mustTimeParse(tfmt, "2015-04-08T04:30:00Z"),
},
Score: -1000.0,
},
// user preferences span a UTC day
// slot does span
UserPreferencesScoreTest{
Pref: interval{
Start: mustTimeParse(tfmt, "0000-01-01T20:00:00Z"),
End: mustTimeParse(tfmt, "0000-01-02T04:00:00Z"),
},
Slot: interval{
Start: mustTimeParse(tfmt, "2015-04-08T23:30:00Z"),
End: mustTimeParse(tfmt, "2015-04-09T00:00:00Z"),
},
Score: 0.0,
},
UserPreferencesScoreTest{
Pref: interval{
Start: mustTimeParse(tfmt, "0000-01-01T20:00:00Z"),
End: mustTimeParse(tfmt, "0000-01-02T00:15:00Z"),
},
Slot: interval{
Start: mustTimeParse(tfmt, "2015-04-08T23:45:00Z"),
End: mustTimeParse(tfmt, "2015-04-08T00:30:00Z"),
},
Score: -1000.0,
},
// times sent to scorer do not have a uniform time zone
UserPreferencesScoreTest{
Pref: interval{
Start: mustTimeParse("2006-01-02T15:04:05 -0700", "0000-01-01T09:00:00 -0700"),
End: mustTimeParse("2006-01-02T15:04:05 -0700", "0000-01-01T17:00:00 -0700"),
},
Slot: interval{
Start: mustTimeParse(tfmt, "2015-04-15T15:30:00Z"),
End: mustTimeParse(tfmt, "2015-04-15T16:00:00Z"),
},
Score: -1000.0,
},
}
for _, test := range tests {
user := User{}
user.Prefs.StartTime = test.Pref.Start
user.Prefs.EndTime = test.Pref.End
if got := (UserPreferencesScorer{User: user}).Score(test.Slot); got != test.Score {
t.Errorf("got %f, expected %f. test case: %v", got, test.Score, test)
}
}
}