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
217
218
219
220
221
222
223
224
225
226
227
228
229
|
package chore
import (
"encoding/json"
"testing"
"time"
chModel "donetick.com/core/internal/chore/model"
)
func TestScheduleNextDueDateBasic(t *testing.T) {
choreTime := time.Now()
freqencyMetadataBytes := `{"time":"2024-07-07T14:30:00-04:00"}`
testTable := []struct {
chore *chModel.Chore
expected time.Time
}{
{
chore: &chModel.Chore{
FrequencyType: chModel.FrequancyTypeDaily,
NextDueDate: &choreTime,
FrequencyMetadata: &freqencyMetadataBytes,
},
expected: choreTime.AddDate(0, 0, 1),
},
{
chore: &chModel.Chore{
FrequencyType: chModel.FrequancyTypeWeekly,
NextDueDate: &choreTime,
FrequencyMetadata: &freqencyMetadataBytes,
},
expected: choreTime.AddDate(0, 0, 7),
},
{
chore: &chModel.Chore{
FrequencyType: chModel.FrequancyTypeMonthly,
NextDueDate: &choreTime,
FrequencyMetadata: &freqencyMetadataBytes,
},
expected: choreTime.AddDate(0, 1, 0),
},
{
chore: &chModel.Chore{
FrequencyType: chModel.FrequancyTypeYearly,
NextDueDate: &choreTime,
FrequencyMetadata: &freqencyMetadataBytes,
},
expected: choreTime.AddDate(1, 0, 0),
},
//
}
for _, tt := range testTable {
t.Run(string(tt.chore.FrequencyType), func(t *testing.T) {
actual, err := scheduleNextDueDate(tt.chore, choreTime)
if err != nil {
t.Errorf("Error: %v", err)
}
if actual != nil && actual.UTC().Format(time.RFC3339) != tt.expected.UTC().Format(time.RFC3339) {
t.Errorf("Expected: %v, Actual: %v", tt.expected, actual)
}
})
}
}
func TestScheduleNextDueDateDayOfTheWeek(t *testing.T) {
choreTime := time.Now()
Monday := "monday"
Wednesday := "wednesday"
timeOfChore := "2024-07-07T16:30:00-04:00"
getExpectedTime := func(choreTime time.Time, timeOfChore string) time.Time {
t, err := time.Parse(time.RFC3339, timeOfChore)
if err != nil {
return time.Time{}
}
return time.Date(choreTime.Year(), choreTime.Month(), choreTime.Day(), t.Hour(), t.Minute(), 0, 0, t.Location())
}
nextSaturday := choreTime.AddDate(0, 0, 1)
for nextSaturday.Weekday() != time.Saturday {
nextSaturday = nextSaturday.AddDate(0, 0, 1)
}
nextMonday := choreTime.AddDate(0, 0, 1)
for nextMonday.Weekday() != time.Monday {
nextMonday = nextMonday.AddDate(0, 0, 1)
}
nextTuesday := choreTime.AddDate(0, 0, 1)
for nextTuesday.Weekday() != time.Tuesday {
nextTuesday = nextTuesday.AddDate(0, 0, 1)
}
nextWednesday := choreTime.AddDate(0, 0, 1)
for nextWednesday.Weekday() != time.Wednesday {
nextWednesday = nextWednesday.AddDate(0, 0, 1)
}
nextThursday := choreTime.AddDate(0, 0, 1)
for nextThursday.Weekday() != time.Thursday {
nextThursday = nextThursday.AddDate(0, 0, 1)
}
testTable := []struct {
chore *chModel.Chore
frequencyMetadata *chModel.FrequencyMetadata
expected time.Time
}{
{
chore: &chModel.Chore{
FrequencyType: chModel.FrequancyTypeDayOfTheWeek,
NextDueDate: &nextSaturday,
},
frequencyMetadata: &chModel.FrequencyMetadata{
Time: timeOfChore,
Days: []*string{&Monday, &Wednesday},
},
expected: getExpectedTime(nextMonday, timeOfChore),
},
{
chore: &chModel.Chore{
FrequencyType: chModel.FrequancyTypeDayOfTheWeek,
NextDueDate: &nextMonday,
},
frequencyMetadata: &chModel.FrequencyMetadata{
Time: timeOfChore,
Days: []*string{&Monday, &Wednesday},
},
expected: getExpectedTime(nextWednesday, timeOfChore),
},
}
for _, tt := range testTable {
t.Run(string(tt.chore.FrequencyType), func(t *testing.T) {
bytesFrequencyMetadata, err := json.Marshal(tt.frequencyMetadata)
stringFrequencyMetadata := string(bytesFrequencyMetadata)
if err != nil {
t.Errorf("Error: %v", err)
}
tt.chore.FrequencyMetadata = &stringFrequencyMetadata
actual, err := scheduleNextDueDate(tt.chore, choreTime)
if err != nil {
t.Errorf("Error: %v", err)
}
if actual != nil && actual.UTC().Format(time.RFC3339) != tt.expected.UTC().Format(time.RFC3339) {
t.Errorf("Expected: %v, Actual: %v", tt.expected, actual)
}
})
}
}
func TestScheduleAdaptiveNextDueDate(t *testing.T) {
getTimeFromDate := func(timeOfChore string) *time.Time {
t, err := time.Parse(time.RFC3339, timeOfChore)
if err != nil {
return nil
}
return &t
}
testTable := []struct {
description string
history []*chModel.ChoreHistory
chore *chModel.Chore
expected *time.Time
completeDate *time.Time
}{
{
description: "Every Two days",
chore: &chModel.Chore{
NextDueDate: getTimeFromDate("2024-07-13T01:30:00-00:00"),
},
history: []*chModel.ChoreHistory{
{
CompletedAt: getTimeFromDate("2024-07-11T01:30:00-00:00"),
},
// {
// CompletedAt: getTimeFromDate("2024-07-09T01:30:00-00:00"),
// },
// {
// CompletedAt: getTimeFromDate("2024-07-07T01:30:00-00:00"),
// },
},
expected: getTimeFromDate("2024-07-15T01:30:00-00:00"),
},
{
description: "Every 8 days",
chore: &chModel.Chore{
NextDueDate: getTimeFromDate("2024-07-13T01:30:00-00:00"),
},
history: []*chModel.ChoreHistory{
{
CompletedAt: getTimeFromDate("2024-07-05T01:30:00-00:00"),
},
{
CompletedAt: getTimeFromDate("2024-06-27T01:30:00-00:00"),
},
},
expected: getTimeFromDate("2024-07-21T01:30:00-00:00"),
},
{
description: "40 days with limit Data",
chore: &chModel.Chore{
NextDueDate: getTimeFromDate("2024-07-13T01:30:00-00:00"),
},
history: []*chModel.ChoreHistory{
{CompletedAt: getTimeFromDate("2024-06-03T01:30:00-00:00")},
},
expected: getTimeFromDate("2024-08-22T01:30:00-00:00"),
},
}
for _, tt := range testTable {
t.Run(tt.description, func(t *testing.T) {
expectedNextDueDate := tt.expected
actualNextDueDate, err := scheduleAdaptiveNextDueDate(tt.chore, *tt.chore.NextDueDate, tt.history)
if err != nil {
t.Errorf("Error: %v", err)
}
if actualNextDueDate == nil || !actualNextDueDate.Equal(*expectedNextDueDate) {
t.Errorf("Expected: %v, Actual: %v", expectedNextDueDate, actualNextDueDate)
}
})
}
}
|