forked from rosedblabs/rosedb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_hash_test.go
136 lines (105 loc) · 2.69 KB
/
db_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
package rosedb
import (
"testing"
)
var key = "myhash"
func TestRoseDB_HSet(t *testing.T) {
t.Run("test1", func(t *testing.T) {
db := InitDb()
defer db.Close()
db.HSet(nil, nil, nil)
_, _ = db.HSet([]byte(key), []byte("my_name"), []byte("roseduan"))
})
t.Run("reopen and set", func(t *testing.T) {
db := ReopenDb()
defer db.Close()
_, _ = db.HSet([]byte(key), []byte("my_hobby"), []byte("coding better"))
_, _ = db.HSet([]byte(key), []byte("my_lang"), []byte("Java and Go"))
})
//t.Run("multi data", func(t *testing.T) {
// db := ReopenDb()
// defer db.Close()
//
// rand.Seed(time.Now().Unix())
//
// fieldPrefix := "hash_field_"
// valPrefix := "hash_data_"
//
// var res int
// for i := 0; i < 100000; i++ {
// field := fieldPrefix + strconv.Itoa(rand.Intn(1000000))
// val := valPrefix + strconv.Itoa(rand.Intn(1000000))
//
// res, _ = db.HSet([]byte(key), []byte(field), []byte(val))
// }
// t.Log(res)
//})
}
func TestRoseDB_HSetNx(t *testing.T) {
db := ReopenDb()
defer db.Close()
db.HSetNx(nil, nil, nil)
ok, _ := db.HSetNx([]byte(key), []byte("my_hobby"), []byte("coding better"))
t.Log(ok)
ok, _ = db.HSetNx([]byte(key), []byte("my_new_lang"), []byte("Java Go Python"))
t.Log(ok)
t.Log(db.HLen([]byte(key)))
}
func TestRoseDB_HGet(t *testing.T) {
db := ReopenDb()
defer db.Close()
_ = db.HGet([]byte(key), []byte("my_name"))
_ = db.HGet([]byte(key), []byte("not exist"))
_ = db.HGet([]byte(key), []byte("my_hobby"))
_ = db.HGet([]byte(key), []byte("hash_field_732328"))
_ = db.HGet([]byte(key), []byte("hash_field_112243"))
}
func TestRoseDB_HGetAll(t *testing.T) {
db := ReopenDb()
defer db.Close()
values := db.HGetAll([]byte(key))
for _, v := range values {
t.Log(string(v))
}
}
func TestRoseDB_HDel(t *testing.T) {
db := ReopenDb()
defer db.Close()
db.HDel(nil, nil)
res, _ := db.HDel([]byte(key), []byte("my_name"), []byte("my_name2"), []byte("my_name3"))
t.Log(res)
}
func TestRoseDB_HExists(t *testing.T) {
db := ReopenDb()
defer db.Close()
db.HExists(nil, nil)
ok := db.HExists([]byte(key), []byte("my_name"))
t.Log(ok)
t.Log(db.HExists([]byte(key), []byte("my_hobby")))
t.Log(db.HExists([]byte(key), []byte("my_name1")))
t.Log(db.HExists([]byte(key+"abcd"), []byte("my_hobby")))
}
func TestRoseDB_HKeys(t *testing.T) {
db := ReopenDb()
defer db.Close()
db.HKeys(nil)
keys := db.HKeys([]byte(key))
for _, k := range keys {
t.Log(k)
}
}
func TestRoseDB_HValues(t *testing.T) {
db := ReopenDb()
defer db.Close()
db.HValues(nil)
keys := db.HValues([]byte(key))
for _, k := range keys {
t.Log(string(k))
}
}
func TestRoseDB_HLen(t *testing.T) {
db := ReopenDb()
defer db.Close()
db.HLen(nil)
db.HLen([]byte("11"))
}