-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminder_test.go
166 lines (150 loc) · 4.87 KB
/
reminder_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
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
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/gin-gonic/gin"
)
func TestReminderMatchesDayAndTime(t *testing.T) {
now := time.Now()
year := now.Year()
month := now.Month()
location := now.Location()
zone := location.String()
tests := []struct {
description string
expected bool
reminder Reminder
time time.Time
}{
{
description: "Reminder is Daily and time matches",
expected: true,
reminder: Reminder{Frequency: Daily, Hour: 1, Minute: 1, Second: 1, Zone: zone},
time: time.Date(year, month, now.Day(), 1, 1, 1, 0, location),
},
{
description: "Reminder is Daily and time does not match",
expected: false,
reminder: Reminder{Frequency: Daily, Hour: 1, Minute: 1, Second: 1, Zone: zone},
time: time.Date(year, month, now.Day(), 1, 2, 1, 0, location),
},
{
description: "Reminder is Weekly and time and day of week both match",
expected: true,
reminder: Reminder{Frequency: Weekly, DayOfWeek: now.Weekday().String(), Hour: 1, Minute: 1, Second: 1, Zone: zone},
time: time.Date(year, month, now.Day(), 1, 1, 1, 0, location),
},
{
description: "Reminder is Weekly and time matches but day of week does not",
expected: false,
reminder: Reminder{Frequency: Weekly, DayOfWeek: now.Weekday().String(), Hour: 1, Minute: 1, Second: 1, Zone: zone},
time: time.Date(year, month, now.Day()+1, 1, 1, 1, 0, location),
},
{
description: "Reminder is Monthly and time and day of month both match",
expected: true,
reminder: Reminder{Frequency: Monthly, Day: now.Day(), Hour: 1, Minute: 1, Second: 1, Zone: zone},
time: time.Date(year, month, now.Day(), 1, 1, 1, 0, location),
},
{
description: "Reminder is Weekly and time matches but day of month does not",
expected: false,
reminder: Reminder{Frequency: Monthly, Day: now.Day(), Hour: 1, Minute: 1, Second: 1, Zone: zone},
time: time.Date(year, month, now.Day()+1, 1, 1, 1, 0, location),
},
{
description: "Reminder is Once and time, day, month, and year all match",
expected: true,
reminder: Reminder{Frequency: Once, Day: now.Day(), Month: month.String(), Year: year, Hour: 1, Minute: 1, Second: 1, Zone: zone},
time: time.Date(year, month, now.Day(), 1, 1, 1, 0, location),
},
{
description: "Reminder is Once and time, day, and month matches but year does not",
expected: false,
reminder: Reminder{Frequency: Once, Day: now.Day(), Month: month.String(), Year: year, Hour: 1, Minute: 1, Second: 1, Zone: zone},
time: time.Date(year+1, month, now.Day(), 1, 1, 1, 0, location),
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
actual := test.reminder.MatchesDayAndTime(test.time)
if actual != test.expected {
t.Errorf(
"Reminder.MatchesDayAndTime(%v) returned %v, expected %v",
test.time,
actual,
test.expected,
)
}
})
}
}
func TestPostReminders(t *testing.T) {
tests := []struct {
description string
params map[string]interface{}
statusCode int
remindersCount int
}{
{
description: "Valid parameters are passed",
params: map[string]interface{}{
"message": "test message",
"frequency": "Daily",
"hour": 1,
"minute": 2,
"zone": "America/Los_Angeles",
},
statusCode: 201,
remindersCount: 1,
},
{
description: "Invalid frequnecy is passed",
params: map[string]interface{}{
"message": "test message",
"frequency": "Dailyyyyyy",
"hour": 1,
"minute": 2,
"zone": "America/Los_Angeles",
},
statusCode: 400,
remindersCount: 0,
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
preReminders := []Reminder{}
db.Find(&preReminders)
payload, err := json.Marshal(test.params)
if err != nil {
t.Fatalf("Failed to build payload for params: %v", test.params)
}
req, err := http.NewRequest("POST", "localhost:8080/reminders", bytes.NewBuffer(payload))
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
rec := httptest.NewRecorder()
c, _ := gin.CreateTestContext(rec)
c.Request = req
postReminders(c)
res := rec.Result()
defer res.Body.Close()
if res.StatusCode != test.statusCode {
t.Errorf("postReminders expected status code %v, got %v", test.statusCode, res.StatusCode)
}
reminders := []Reminder{}
db.Find(&reminders)
remindersAdded := len(reminders) - len(preReminders)
if remindersAdded != test.remindersCount {
t.Errorf("postReminders expected %v reminders, got %v", test.remindersCount, len(reminders))
}
if len(watcher.reminders) != len(reminders) {
t.Errorf("Watcher reminders length expected to be %v, got %v", len(reminders), len(watcher.reminders))
}
})
}
}