-
Notifications
You must be signed in to change notification settings - Fork 29
/
tx_str_test.go
100 lines (81 loc) · 1.7 KB
/
tx_str_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
package flashdb
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFlashDB_GetSet(t *testing.T) {
db := getTestDB()
defer db.Close()
defer os.RemoveAll(tmpDir)
if err := db.Update(func(tx *Tx) error {
err := tx.Set("foo", "bar")
assert.Equal(t, nil, err)
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.View(func(tx *Tx) error {
val, err := tx.Get("foo")
assert.Equal(t, nil, err)
assert.Equal(t, "bar", val)
return nil
}); err != nil {
t.Fatal(err)
}
}
func TestFlashDB_SetEx(t *testing.T) {
db := getTestDB()
defer db.Close()
defer os.RemoveAll(tmpDir)
if err := db.Update(func(tx *Tx) error {
err := tx.SetEx("foo", "1", -4)
assert.NotEmpty(t, err)
err = tx.SetEx("foo", "1", 993)
assert.Empty(t, err)
return nil
}); err != nil {
t.Fatal(err)
}
}
func TestFlashDB_Delete(t *testing.T) {
db := getTestDB()
defer db.Close()
defer os.RemoveAll(tmpDir)
if err := db.Update(func(tx *Tx) error {
err := tx.Set("foo", "bar")
assert.Equal(t, err, nil)
err = tx.Delete("foo")
assert.Equal(t, err, nil)
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.View(func(tx *Tx) error {
val, err := tx.Get("foo")
assert.Empty(t, val)
assert.Equal(t, ErrInvalidKey, err)
return nil
}); err != nil {
t.Fatal(err)
}
}
func TestFlashDB_TTL(t *testing.T) {
db := getTestDB()
defer db.Close()
defer os.RemoveAll(tmpDir)
if err := db.Update(func(tx *Tx) error {
err := tx.SetEx("foo", "bar", 20)
assert.Equal(t, err, nil)
return nil
}); err != nil {
t.Fatal(err)
}
if err := db.View(func(tx *Tx) error {
ttl := tx.TTL("foo")
assert.Equal(t, int(ttl), 20)
return nil
}); err != nil {
t.Fatal(err)
}
}