-
Notifications
You must be signed in to change notification settings - Fork 29
/
tx_hash_test.go
151 lines (128 loc) · 2.73 KB
/
tx_hash_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
package flashdb
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
var (
testKey = "dummy"
tmpDir = "tmp"
)
func testConfig() *Config {
return &Config{
Addr: DefaultAddr,
Path: tmpDir,
NoSync: true,
}
}
func getTestDB() *FlashDB {
db, _ := New(testConfig())
return db
}
func TestFlashDB_HGetSet(t *testing.T) {
db := getTestDB()
defer db.Close()
defer os.RemoveAll(tmpDir)
if err := db.Update(func(tx *Tx) error {
_, err := tx.HSet(testKey, "bar", "1")
assert.NoError(t, err)
_, err = tx.HSet(testKey, "baz", "2")
assert.NoError(t, err)
return nil
}); err != nil {
t.Fatal(err)
}
db.View(func(tx *Tx) error {
val := tx.HGet(testKey, "bar")
assert.Equal(t, "1", val)
val = tx.HGet(testKey, "baz")
assert.Equal(t, "2", val)
return nil
})
}
func TestFlashDB_HGetAll(t *testing.T) {
db := getTestDB()
defer db.Close()
defer os.RemoveAll(tmpDir)
if err := db.Update(func(tx *Tx) error {
tx.HSet(testKey, "bar", "1")
tx.HSet(testKey, "baz", "2")
return nil
}); err != nil {
t.Fatal(err)
}
db.View(func(tx *Tx) error {
values := tx.HGetAll(testKey)
assert.Equal(t, 4, len(values))
return nil
})
}
func TestFlashDB_HDel(t *testing.T) {
db := getTestDB()
defer db.Close()
defer os.RemoveAll(tmpDir)
if err := db.Update(func(tx *Tx) error {
tx.HSet(testKey, "bar", "1")
tx.HSet(testKey, "baz", "2")
res, err := tx.HDel(testKey, "bar", "baz")
assert.Nil(t, err)
assert.Equal(t, 2, res)
return nil
}); err != nil {
t.Fatal(err)
}
db.View(func(tx *Tx) error {
assert.Empty(t, tx.HGet(testKey, "bar"))
assert.Empty(t, tx.HGet(testKey, "baz"))
return nil
})
}
func TestFlashDB_HExists(t *testing.T) {
db := getTestDB()
defer db.Close()
defer os.RemoveAll(tmpDir)
if err := db.Update(func(tx *Tx) error {
tx.HSet(testKey, "bar", "1")
tx.HSet(testKey, "baz", "2")
return nil
}); err != nil {
t.Fatal(err)
}
db.View(func(tx *Tx) error {
assert.True(t, tx.HExists(testKey, "bar"))
assert.True(t, tx.HExists(testKey, "baz"))
assert.False(t, tx.HExists(testKey, "ben"))
return nil
})
}
func TestFlashDB_HKeyExists(t *testing.T) {
db := getTestDB()
defer db.Close()
defer os.RemoveAll(tmpDir)
if err := db.Update(func(tx *Tx) error {
tx.HSet(testKey, "bar", "1")
return nil
}); err != nil {
t.Fatal(err)
}
db.View(func(tx *Tx) error {
assert.True(t, tx.HKeyExists(testKey))
assert.False(t, tx.HKeyExists("yolo"))
return nil
})
}
func TestFlashDB_HLen(t *testing.T) {
db := getTestDB()
defer db.Close()
defer os.RemoveAll(tmpDir)
if err := db.Update(func(tx *Tx) error {
tx.HSet(testKey, "bar", "1")
return nil
}); err != nil {
t.Fatal(err)
}
db.View(func(tx *Tx) error {
assert.Equal(t, tx.HLen(testKey), 1)
return nil
})
}