-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathall_test.go
302 lines (245 loc) · 6.65 KB
/
all_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package gitdb_test
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
"testing"
"github.com/bouggo/log"
"github.com/gogitdb/gitdb/v2"
)
var testDb gitdb.GitDb
var messageId int
const testData = "/tmp/gitdb-test"
const dbPath = testData + "/data"
const fakeRemote = testData + "/online"
//Test flags for more interactivity
var flagLogLevel int
var flagFakeRemote bool
func TestMain(m *testing.M) {
flag.IntVar(&flagLogLevel, "loglevel", int(log.LevelTest), "control verbosity of test logs")
flag.BoolVar(&flagFakeRemote, "fakerepo", true, "create fake remote repo for tests")
flag.Parse()
//fail test if git is not installed
if _, err := exec.LookPath("git"); err != nil {
log.Test("git is required to run tests")
return
}
gitdb.SetLogLevel(gitdb.LogLevel(flagLogLevel))
m.Run()
}
func setup(t testing.TB, cfg *gitdb.Config) func(t testing.TB) {
if cfg == nil {
cfg = getConfig()
}
if flagFakeRemote && !strings.HasPrefix(cfg.OnlineRemote, "git@") {
fakeOnlineRepo(t)
}
//if DbPath is pointing to existing test data, create a fake .git folder
//so that it passes the git repo check on db.boot
if strings.HasPrefix(cfg.DBPath, "./testdata") {
if err := os.MkdirAll(filepath.Join(cfg.DBPath, "data", ".git"), 0755); err != nil {
t.Errorf("fake .git failed: %s", err.Error())
}
}
testDb = getDbConn(t, cfg)
messageId = 0
testDb.RegisterModel("Message", &Message{})
testDb.RegisterModel("MessageV2", &MessageV2{})
return teardown
}
func teardown(t testing.TB) {
testDb.Close()
log.Test("truncating test data")
if err := os.RemoveAll(testData); err != nil {
t.Errorf("cleanup failed - %s", err.Error())
}
}
func fakeOnlineRepo(t testing.TB) {
log.Test("creating fake online repo")
if err := os.MkdirAll(fakeRemote, 0755); err != nil {
t.Errorf("fake repo failed: %s", err.Error())
return
}
cmd := exec.Command("git", "-C", fakeRemote, "init", "--bare")
if _, err := cmd.CombinedOutput(); err != nil {
t.Errorf("fake repo failed: %s", err.Error())
return
}
}
func getDbConn(t testing.TB, cfg *gitdb.Config) gitdb.GitDb {
conn, err := gitdb.Open(cfg)
if err != nil {
t.Errorf("getDbConn failed: %s", err)
return nil
}
log.Test("test db connection opened")
conn.SetUser(gitdb.NewUser("Tester", "tester@io"))
return conn
}
func getConfig() *gitdb.Config {
config := gitdb.NewConfig(dbPath)
config.EncryptionKey = "b61ba8270ccc3c1d42b4417e7bd60b71"
if flagFakeRemote {
config.OnlineRemote = fakeRemote
}
return config
}
func getMockConfig() *gitdb.Config {
config := gitdb.NewConfig("/mock")
config.Mock = true
return config
}
func getReadTestConfig(version string) *gitdb.Config {
cfg := gitdb.NewConfig("./testdata/" + version + "/data")
if version == "v1" {
//add a factory method
cfg.Factory = func(dataset string) gitdb.Model {
switch dataset {
case "Message":
return &Message{}
case "MessageV2":
return &MessageV2{}
}
return &Message{}
}
}
return cfg
}
func getTestMessage() *Message {
m := getTestMessageWithId(messageId)
messageId++
return m
}
func getTestMessageWithId(messageId int) *Message {
m := &Message{
MessageId: messageId,
From: "[email protected]",
To: "[email protected]",
Body: "Hello",
}
return m
}
type Message struct {
gitdb.TimeStampedModel
MessageId int
From string
To string
Body string
}
func (m *Message) GetSchema() *gitdb.Schema {
name := "Message"
block := "b0"
// block := gitdb.AutoBlock(dbPath, m, gitdb.BlockByCount, 2)
record := fmt.Sprintf("%d", m.MessageId)
//Indexes speed up searching
indexes := make(map[string]interface{})
indexes["From"] = m.From
return gitdb.NewSchema(name, block, record, indexes)
}
func (m *Message) Validate() error { return nil }
func (m *Message) IsLockable() bool { return true }
func (m *Message) ShouldEncrypt() bool { return true }
func (m *Message) GetLockFileNames() []string {
return []string{
fmt.Sprintf("%d-%s", m.MessageId, m.From),
}
}
// func (m *Message) BeforeInsert() error { return nil }
type MessageV2 struct {
gitdb.TimeStampedModel
MessageId int
From string
To string
Body string
}
func (m *MessageV2) GetSchema() *gitdb.Schema {
name := "MessageV2"
block := m.CreatedAt.Format("200601")
record := fmt.Sprintf("%d", m.MessageId)
//Indexes speed up searching
indexes := make(map[string]interface{})
indexes["From"] = m.From
return gitdb.NewSchema(name, block, record, indexes)
}
func (m *MessageV2) Validate() error { return nil }
func (m *MessageV2) IsLockable() bool { return false }
func (m *MessageV2) ShouldEncrypt() bool { return false }
func (m *MessageV2) GetLockFileNames() []string { return []string{} }
// func (m *MessageV2) BeforeInsert() error { return nil }
//count the number of records in fetched block
func countRecords(dataset string) int {
datasetPath := getConfig().DBPath + "/data/" + dataset + "/"
cmd := exec.Command("/bin/bash", "-c", "grep "+dataset+" "+datasetPath+"*.json | wc -l | awk '{print $1}'")
b, err := cmd.CombinedOutput()
if err != nil {
println(err.Error())
}
v := strings.TrimSpace(string(b))
want, err := strconv.Atoi(v)
if err != nil {
println(v)
println(err.Error())
want = 0
}
return want
}
func generateInserts(t testing.TB, count int) {
log.Test(fmt.Sprintf("inserting %d records\n", count))
for i := 0; i < count; i++ {
if err := testDb.Insert(getTestMessage()); err != nil {
t.Errorf("generateInserts failed: %s", err)
}
}
log.Test("done inserting")
}
func insert(m gitdb.Model, benchmark bool) error {
if err := testDb.Insert(m); err != nil {
return err
}
if !benchmark && !m.ShouldEncrypt() {
//check that block file exist
recordID := gitdb.ID(m)
dataset, block, _, err := gitdb.ParseID(recordID)
if err != nil {
return err
}
cfg := getConfig()
blockFile := filepath.Join(cfg.DBPath, "data", dataset, block+".json")
if _, err := os.Stat(blockFile); err != nil {
//return err
return errors.New("insert test stat failed - " + blockFile)
} else {
b, err := ioutil.ReadFile(blockFile)
if err != nil {
return err
}
rep := strings.NewReplacer("\n", "", "\\", "", "\t", "", "\"{", "{", "}\"", "}", " ", "")
got := rep.Replace(string(b))
w := map[string]interface{}{
recordID: struct {
Version string
Indexes map[string]interface{}
Data gitdb.Model
}{
gitdb.RecVersion,
gitdb.Indexes(m),
m,
},
}
x, _ := json.Marshal(w)
want := string(x)
want = want[1 : len(want)-1]
if !strings.Contains(got, want) {
return fmt.Errorf("Want: %s, Got: %s", want, got)
}
}
}
return nil
}