-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvsync_test.go
206 lines (171 loc) · 4.14 KB
/
kvsync_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
package kvsync_test
import (
"context"
"fmt"
"github.com/ndthuan/kvsync"
"github.com/stretchr/testify/assert"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"testing"
)
type SyncedUser struct {
gorm.Model
UUID string
Username string
}
func (u SyncedUser) SyncKeys() map[string]string {
return map[string]string{
"id": fmt.Sprintf("user:id:%d", u.ID),
"uuid": fmt.Sprintf("user:uuid:%s", u.UUID),
"composite": fmt.Sprintf("user:composite:%d_%s", u.ID, u.UUID),
}
}
type UnsyncedUser struct {
gorm.Model
UUID string
Username string
}
func TestAutomatedSync(t *testing.T) {
var expectedDoneCount = 9 // 3 keys per SyncedUser
var actualDoneCount int
store := &kvsync.InMemoryStore{
Store: make(map[string]any),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
kvSync := kvsync.NewKVSync(ctx, kvsync.Options{
Store: store,
Workers: 4,
ReportCallback: func(r kvsync.Report) {
if r.Err == nil {
actualDoneCount++
}
},
})
db := setUpDB()
defer tearDownDB(db)
if err := db.Callback().Create().After("gorm:create").Register("kvsync:create", kvSync.GormCallback()); err != nil {
t.Fatal("failed to register gorm:create callback", err)
}
if err := db.Callback().Update().After("gorm:update").Register("kvsync:update", kvSync.GormCallback()); err != nil {
t.Fatal("failed to register gorm:update callback", err)
}
// single model
db.Create(&SyncedUser{
UUID: "test-uuid",
Username: "test-username",
})
// slice of models
db.Create(&[]SyncedUser{
{
UUID: "test-uuid-2",
Username: "test-username-2",
},
{
UUID: "test-uuid-3",
Username: "test-username-3",
},
})
db.Create(&UnsyncedUser{
UUID: "test-uuid-4",
})
for {
if actualDoneCount >= expectedDoneCount {
break
}
}
fetchedUser := SyncedUser{
UUID: "test-uuid",
}
err := kvSync.Fetch(&fetchedUser, "uuid")
assert.NoError(t, err)
assert.Equal(t, "test-username", fetchedUser.Username)
assert.Equal(t, expectedDoneCount, len(store.Store))
//assert.Equal(t, expectedErrorCount, actualErrorCount)
}
func TestManualSync(t *testing.T) {
store := &kvsync.InMemoryStore{
Store: make(map[string]any),
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
kvSync := kvsync.NewKVSync(ctx, kvsync.Options{
Store: store,
Workers: 2,
})
db := setUpDB()
defer tearDownDB(db)
// single model
kvSync.Sync(&SyncedUser{
UUID: "test-uuid-manual",
Username: "test-username-manual",
})
kvSync.Sync(&UnsyncedUser{
UUID: "test-uuid-manual-2",
Username: "test-username-manual-2",
})
assert.Equal(t, 3, len(store.Store))
fetchUser := SyncedUser{
UUID: "test-uuid-manual",
}
err := kvSync.Fetch(&fetchUser, "uuid")
assert.NoError(t, err)
assert.Equal(t, "test-username-manual", fetchUser.Username)
}
func TestFetch_Errors(t *testing.T) {
store := &kvsync.InMemoryStore{
Store: make(map[string]any),
}
kvSync := kvsync.NewKVSync(context.Background(), kvsync.Options{
Store: store,
})
testCases := []struct {
name string
dest kvsync.Syncable
keyName string
}{
{
name: "invalid dest (non-pointer)",
dest: SyncedUser{},
keyName: "uuid",
},
{
name: "key not found",
dest: &SyncedUser{},
keyName: "uuid",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := kvSync.Fetch(tc.dest, tc.keyName)
assert.Error(t, err)
})
}
}
func TestFetch_KeyNotFound(t *testing.T) {
store := &kvsync.InMemoryStore{
Store: make(map[string]any),
}
kvSync := kvsync.NewKVSync(context.Background(), kvsync.Options{
Store: store,
})
err := kvSync.Fetch(&SyncedUser{}, "uuid")
assert.Error(t, err)
}
func setUpDB() *gorm.DB {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
if err != nil {
panic(fmt.Sprintf("Failed to connect to database: %v", err))
}
if err = db.AutoMigrate(&SyncedUser{}, &UnsyncedUser{}); err != nil {
panic(fmt.Sprintf("Failed to auto migrate: %v", err))
}
return db
}
func tearDownDB(db *gorm.DB) {
_ = db.Migrator().DropTable(&SyncedUser{}, &UnsyncedUser{})
conn, err := db.DB()
if err == nil {
_ = conn.Close()
}
}