-
Notifications
You must be signed in to change notification settings - Fork 139
/
node_test.go
135 lines (110 loc) · 3.28 KB
/
node_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
package storm
import (
"testing"
"github.com/asdine/storm/v3/codec/gob"
"github.com/asdine/storm/v3/codec/json"
bolt "go.etcd.io/bbolt"
"github.com/stretchr/testify/require"
)
func TestNode(t *testing.T) {
db, cleanup := createDB(t, Root("a"))
defer cleanup()
n1 := db.From("b", "c")
node1, ok := n1.(*node)
require.True(t, ok)
require.Equal(t, db, node1.s)
require.NotEqual(t, db.Node, n1)
require.Equal(t, []string{"a"}, db.Node.(*node).rootBucket)
require.Equal(t, []string{"a", "b", "c"}, node1.rootBucket)
n2 := n1.From("d", "e")
node2, ok := n2.(*node)
require.True(t, ok)
require.Equal(t, []string{"a", "b", "c", "d", "e"}, node2.rootBucket)
}
func TestNodeWithTransaction(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
var user User
db.Bolt.Update(func(tx *bolt.Tx) error {
dbx := db.WithTransaction(tx)
err := dbx.Save(&User{ID: 10, Name: "John"})
require.NoError(t, err)
err = dbx.One("ID", 10, &user)
require.NoError(t, err)
require.Equal(t, "John", user.Name)
return nil
})
err := db.One("ID", 10, &user)
require.NoError(t, err)
}
func TestNodeWithCodec(t *testing.T) {
t.Run("Inheritance", func(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
n := db.From("a").(*node)
require.Equal(t, json.Codec, n.codec)
n = n.From("b", "c", "d").(*node)
require.Equal(t, json.Codec, n.codec)
n = db.WithCodec(gob.Codec).(*node)
n = n.From("e").(*node)
require.Equal(t, gob.Codec, n.codec)
o := n.From("f").WithCodec(json.Codec).(*node)
require.Equal(t, gob.Codec, n.codec)
require.Equal(t, json.Codec, o.codec)
})
t.Run("CodecCall", func(t *testing.T) {
db, cleanup := createDB(t)
defer cleanup()
type User struct {
ID int
Name string `storm:"index"`
}
requireBytesEqual := func(raw []byte, expected interface{}) {
var u User
err := gob.Codec.Unmarshal(raw, &u)
require.NoError(t, err)
require.Equal(t, expected, u)
}
n := db.From("a").WithCodec(gob.Codec)
err := n.Set("gobBucket", "key", &User{ID: 10, Name: "John"})
require.NoError(t, err)
b, err := n.GetBytes("gobBucket", "key")
require.NoError(t, err)
requireBytesEqual(b, User{ID: 10, Name: "John"})
id, err := toBytes(10, n.(*node).codec)
require.NoError(t, err)
err = n.Save(&User{ID: 10, Name: "John"})
require.NoError(t, err)
b, err = n.GetBytes("User", id)
require.NoError(t, err)
requireBytesEqual(b, User{ID: 10, Name: "John"})
err = n.Update(&User{ID: 10, Name: "Jack"})
require.NoError(t, err)
b, err = n.GetBytes("User", id)
require.NoError(t, err)
requireBytesEqual(b, User{ID: 10, Name: "Jack"})
err = n.UpdateField(&User{ID: 10}, "Name", "John")
require.NoError(t, err)
b, err = n.GetBytes("User", id)
require.NoError(t, err)
requireBytesEqual(b, User{ID: 10, Name: "John"})
var users []User
err = n.Find("Name", "John", &users)
require.NoError(t, err)
var user User
err = n.One("Name", "John", &user)
require.NoError(t, err)
err = n.AllByIndex("Name", &users)
require.NoError(t, err)
err = n.All(&users)
require.NoError(t, err)
err = n.Range("Name", "J", "K", &users)
require.NoError(t, err)
err = n.Prefix("Name", "J", &users)
require.NoError(t, err)
_, err = n.Count(new(User))
require.NoError(t, err)
err = n.Select().Find(&users)
require.NoError(t, err)
})
}