-
Notifications
You must be signed in to change notification settings - Fork 0
/
srs_test.go
249 lines (194 loc) · 5.41 KB
/
srs_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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package srs_test
import (
ulidPkg "github.com/oklog/ulid/v2"
badger "github.com/outcaste-io/badger/v3"
"math/rand"
"os"
"testing"
"time"
"github.com/revelaction/go-srs"
"github.com/revelaction/go-srs/algo/sm2"
bdg "github.com/revelaction/go-srs/db/badger"
"github.com/revelaction/go-srs/review"
"github.com/revelaction/go-srs/uid/ulid"
)
func TestInsertNewDeckIdAndDue(t *testing.T) {
// Algo
now := time.Date(2020, time.November, 1, 0, 0, 0, 0, time.UTC)
sm2 := sm2.New(now)
// Badger dir random
dir, err := os.MkdirTemp(".", "badger")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
opts := badger.DefaultOptions(dir)
opts.Logger = nil
bad, _ := badger.Open(opts)
defer bad.Close()
db := bdg.New(bad, sm2)
// fixed entropy value
ti := time.Unix(1000000, 0)
entropy := ulidPkg.Monotonic(rand.New(rand.NewSource(ti.UnixNano())), 0)
uid := ulid.New(entropy)
hdl := srs.New(db, uid)
// Empty review without UID
r := review.Review{}
r.Items = []review.ReviewItem{
{Quality: 4},
{Quality: 3},
}
res, err := hdl.Update(r)
if err != nil {
t.Fatal(err)
}
t.Logf("☑ Created DeckId is : %s", res.DeckId)
// the new cards are made for tomorrow, check for two days
tdue := time.Now().UTC().AddDate(0, 0, 2)
due, err := hdl.Due(res.DeckId, tdue)
if err != nil {
t.Fatal(err)
}
t.Logf("☑ Response is : %#v", due)
// 2 new cards, 2 inserted, 2 returned for next day
wantLen := 2
if len(due.Items) != wantLen {
t.Errorf("\nCheking len:\ngot %d\nwant %d", len(due.Items), wantLen)
}
// Check cardId start with 1
for idx, item := range due.Items {
wantId := idx + 1
if item.CardId != wantId {
t.Errorf("\ngot cardId %d\nwant cardId %d", item.CardId, wantId)
}
}
}
func TestInsertWithDeckIdAndDue(t *testing.T) {
// Algo
now := time.Date(2020, time.November, 1, 0, 0, 0, 0, time.UTC)
sm2 := sm2.New(now)
// Badger dir random
dir, err := os.MkdirTemp(".", "badger")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
opts := badger.DefaultOptions(dir)
opts.Logger = nil
bad, _ := badger.Open(opts)
defer bad.Close()
db := bdg.New(bad, sm2)
// fixed entropy value
ti := time.Unix(1000000, 0)
entropy := ulidPkg.Monotonic(rand.New(rand.NewSource(ti.UnixNano())), 0)
uid := ulid.New(entropy)
//t.Logf("☑ uid is: %#v", uid.Create())
hdl := srs.New(db, uid)
// Empty review without UID
r1 := review.Review{}
r1.Items = []review.ReviewItem{
{Quality: review.NoReview},
{Quality: review.NoReview},
}
res, err := hdl.Update(r1)
if err != nil {
t.Fatal(err)
}
t.Logf("☑ review 1: Created DeckId is : %s", res.DeckId)
// Insert 3 additional cards in the created DeckId
// Empty review without UID
r2 := review.Review{}
r2.DeckId = res.DeckId
r2.Items = []review.ReviewItem{
{Quality: review.NoReview},
{Quality: review.NoReview},
{Quality: review.NoReview},
}
res, err = hdl.Update(r2)
if err != nil {
t.Fatal(err)
}
t.Logf("☑ review 2: Created DeckId is : %s", res.DeckId)
// the new cards are made for tomorrow, check for 1 day 1 second, when it
// becomes overdue
tdue := time.Now().UTC().AddDate(0, 0, 1).Add(time.Second * 1)
dueAfterReview2, err := hdl.Due(res.DeckId, tdue)
if err != nil {
t.Fatal(err)
}
wantLen := 5
if len(dueAfterReview2.Items) != wantLen {
t.Errorf("\nCheking len:\ngot %d\nwant %d", len(dueAfterReview2.Items), wantLen)
}
// Check cardId start with 1 -> 5
for idx, item := range dueAfterReview2.Items {
wantId := idx + 1
if item.CardId != wantId {
t.Errorf("\ngot cardId %d\nwant cardId %d", item.CardId, wantId)
}
}
}
func TestInsertUpdateDue(t *testing.T) {
// Algo
now := time.Date(2020, time.November, 1, 0, 0, 0, 0, time.UTC)
sm2 := sm2.New(now)
dir, err := os.MkdirTemp(".", "badger")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir) // clean up
opts := badger.DefaultOptions(dir)
opts.Logger = nil
bad, _ := badger.Open(opts)
defer bad.Close()
db := bdg.New(bad, sm2)
// fixed entropy value
ti := time.Unix(1000000, 0)
entropy := ulidPkg.Monotonic(rand.New(rand.NewSource(ti.UnixNano())), 0)
uid := ulid.New(entropy)
hdl := srs.New(db, uid)
// Empty review without UID
r := review.Review{}
r.Items = []review.ReviewItem{
{Quality: 3},
{Quality: 4},
}
res, err := hdl.Update(r)
if err != nil {
t.Fatal(err)
}
t.Logf("☑ Created new DeckId is : %s", res.DeckId)
t.Logf("☑ First Saved in db with Response is : %#v", res)
// New review with existing DeckId
r = review.Review{}
r.DeckId = res.DeckId
// Fill new review with retrieved
for _, item := range res.Items {
r.Items = append(r.Items, review.ReviewItem{CardId: item.CardId, Quality: 3}) // minimal
}
t.Logf("☑ created next review for same DeckId %s: %#v", r.DeckId, r)
res, err = hdl.Update(r)
if err != nil {
t.Fatal(err)
}
t.Logf("☑ Second Saved in db with response : %#v", res)
// the new cards are made for tomorrow, check for 30 days
tdue := time.Now().UTC().AddDate(0, 0, 30)
due, err := hdl.Due(res.DeckId, tdue)
if err != nil {
t.Fatal(err)
}
t.Logf("☑ Response is : %#v", due)
// 2 new cards, 2 inserted, 2 returned for next day
wantLen := 2
if len(due.Items) != wantLen {
t.Errorf("\nCheking len:\ngot %d\nwant %d", len(due.Items), wantLen)
}
// Check cardId start with 1
for idx, item := range due.Items {
wantId := idx + 1
if item.CardId != wantId {
t.Errorf("\ngot cardId %d\nwant cardId %d", item.CardId, wantId)
}
}
}