-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleveldb_test.go
77 lines (67 loc) · 1.84 KB
/
leveldb_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
package ezdb
import (
"fmt"
"testing"
)
func TestLevelDB(t *testing.T) {
type Student struct {
Name string `json:"name"`
Age int `json:"age"`
}
path := ".leveldb"
marshaler := JSON(func() *Student {
return &Student{}
})
collection := LevelDB(path, marshaler, nil)
cmp := func(expected, actual *Student) error {
if expected == nil && actual != nil {
return fmt.Errorf("expected no student (got %v)", actual)
} else if expected != nil && actual == nil {
return fmt.Errorf("expected student %v (got nil)", expected)
}
if actual.Name != expected.Name {
return fmt.Errorf("student has wrong name (expected '%s', got '%s')", expected.Name, actual.Name)
} else if actual.Age != expected.Age {
return fmt.Errorf("student has wrong age (expected '%d', got '%d')", expected.Age, actual.Age)
}
return nil
}
tester := &CollectionTester[*Student]{
C: collection,
Cmp: cmp,
Data: map[string]*Student{
"annie": {Name: "Annie", Age: 32},
"ben": {Name: "Ben", Age: 50},
"clive": {Name: "Clive", Age: 21},
},
}
named := func(name string, do func() error) func() {
return func() {
if err := do(); err != nil {
t.Errorf("failed test %q: %v", name, err)
} else {
t.Logf("passed test %q", name)
}
}
}
sequence := []func(){
named("init 1", tester.Init),
named("has", tester.Has),
named("get", tester.Get),
named("getAll 1", tester.GetAll),
named("deleteOne 1", tester.DeleteOne),
named("deleteOne 2", tester.DeleteOne),
named("init 2", tester.Init),
named("close 1", tester.Close),
named("open", tester.Open),
named("getAll 2", tester.GetAll),
named("iterCount", tester.IterCount),
named("iterSortKeys", tester.IterSortKeys),
named("deleteAll", tester.DeleteAll),
named("close", tester.Close),
named("destroy", collection.Destroy),
}
for _, do := range sequence {
do()
}
}