-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_test.go
90 lines (70 loc) · 2.4 KB
/
model_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
package boltx_test
import (
"testing"
"github.com/boltdb/bolt"
"github.com/simia-tech/boltx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPutModelAndGetModel(t *testing.T) {
db, tearDown := setUpTestDB(t)
defer tearDown()
inTestBucket(t, db, func(bucket *bolt.Bucket) {
key, value := []byte("test"), &model{field: "test"}
require.NoError(t, boltx.PutModel(bucket, key, value))
found, err := boltx.GetModel(bucket, key, value)
require.NoError(t, err)
assert.True(t, found)
assert.Equal(t, "test", value.field)
})
}
func TestPutModelAndGetModelOfInvalidModel(t *testing.T) {
db, tearDown := setUpTestDB(t)
defer tearDown()
inTestBucket(t, db, func(bucket *bolt.Bucket) {
key, value := []byte("test"), &model{field: "invalid"}
assert.Error(t, boltx.PutModel(bucket, key, value))
require.NoError(t, bucket.Put(key, []byte("invalid")))
_, err := boltx.GetModel(bucket, key, value)
assert.Error(t, err)
})
}
func TestPutModelInReadOnlyBucket(t *testing.T) {
db, tearDown := setUpTestDB(t)
defer tearDown()
inReadOnlyTestBucket(t, db, func(bucket *bolt.Bucket) {
key, value := []byte("test"), &model{field: "test"}
assert.Error(t, boltx.PutModel(bucket, key, value))
})
}
func TestGetModelOfMissingModel(t *testing.T) {
db, tearDown := setUpTestDB(t)
defer tearDown()
inTestBucket(t, db, func(bucket *bolt.Bucket) {
value := &model{}
found, err := boltx.GetModel(bucket, []byte("missing"), value)
require.NoError(t, err)
assert.False(t, found)
assert.Equal(t, "", value.field)
})
}
func TestPutModelInBucketAndGetModelFromBucket(t *testing.T) {
db, tearDown := setUpTestDB(t)
defer tearDown()
name, key := []byte("test"), []byte("test")
value := &model{field: "test"}
assert.Error(t, boltx.PutModelInBucket(db, []byte(""), key, value))
assert.Error(t, boltx.PutModelInBucket(db, name, key, &model{field: "invalid"}))
require.NoError(t, boltx.PutModelInBucket(db, name, key, value))
found, err := boltx.GetModelFromBucket(db, []byte("missing"), key, value)
require.NoError(t, err)
assert.False(t, found)
found, err = boltx.GetModelFromBucket(db, name, key, value)
require.NoError(t, err)
assert.True(t, found)
assert.Equal(t, "test", value.field)
require.NoError(t, boltx.PutInBucket(db, name, key, []byte("invalid")))
found, err = boltx.GetModelFromBucket(db, name, key, value)
assert.False(t, found)
assert.Error(t, err)
}