-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyvalue_test.go
164 lines (144 loc) · 3.97 KB
/
keyvalue_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
152
153
154
155
156
157
158
159
160
161
162
163
164
package simplehstore
import (
"testing"
"github.com/xyproto/pinterface"
)
const keyvaluename = "testkeyvalue"
func TestKeyValue(t *testing.T) {
Verbose = true
//host := New() // locally
host := NewHost(defaultConnectionString)
defer host.Close()
keyvalue, err := NewKeyValue(host, keyvaluename)
if err != nil {
t.Error(err)
}
keyvalue.Clear()
key := "password"
value := "hunter1"
if err := keyvalue.Set(key, value); err != nil {
t.Errorf("Error, could not set value in keyvalue! %s", err.Error())
}
// Twice
if err := keyvalue.Set(key, value); err != nil {
t.Errorf("Error, could not set value in keyvalue! %s", err.Error())
}
item, err := keyvalue.Get(key)
if err != nil {
t.Errorf("Error, could not fetch value from keyvalue! %s", err.Error())
}
if item != value {
t.Errorf("Error, expected %s, got %s!", value, item)
}
err = keyvalue.Remove()
if err != nil {
t.Errorf("Error, could not remove keyvalue! %s", err.Error())
}
// Check that keyvalue qualifies for the IKeyValue interface
var _ pinterface.IKeyValue = keyvalue
}
func TestInc(t *testing.T) {
Verbose = true
const (
kvname = "kv_234_test_test_test"
testkey = "key_234_test_test_test"
testvalue0 = "9"
testvalue1 = "10"
testvalue2 = "1"
)
host := NewHost(defaultConnectionString)
defer host.Close()
kv, err := NewKeyValue(host, kvname)
if err != nil {
t.Error(err)
}
if err := kv.Set(testkey, testvalue0); err != nil {
t.Errorf("Error, could not set key and value! %s", err.Error())
}
if err := kv.Set(testkey+"2", testvalue0); err != nil {
t.Errorf("Error, could not set key and value! %s", err.Error())
}
if err := kv.Set(testkey+"3", testvalue0); err != nil {
t.Errorf("Error, could not set key and value! %s", err.Error())
}
allValues, err := kv.All()
if err != nil {
t.Error(err)
}
if len(allValues) != 3 {
t.Error("should be 3 keys")
}
if val, err := kv.Get(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != testvalue0 {
t.Errorf("Error, wrong value! %s != %s", val, testvalue0)
}
incval, err := kv.Inc(testkey)
if err != nil {
t.Errorf("Error, could not INCR key! %s", err.Error())
}
if val, err := kv.Get(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != testvalue1 {
t.Errorf("Error, wrong value! %s != %s", val, testvalue1)
} else if incval != testvalue1 {
t.Errorf("Error, wrong inc value! %s != %s", incval, testvalue1)
}
kv.Remove()
if _, err := kv.Get(testkey); err == nil {
t.Errorf("Error, could get key! %s", err.Error())
}
// Creates "0" and increases the value with 1
kv.Inc(testkey)
if val, err := kv.Get(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != testvalue2 {
t.Errorf("Error, wrong value! %s != %s", val, testvalue2)
}
kv.Remove()
if _, err := kv.Get(testkey); err == nil {
t.Errorf("Error, could get key! %s", err.Error())
}
}
func TestInc2(t *testing.T) {
Verbose = true
const (
kvname = "kv_237_test_test_test_inc"
testkey = "key_237_test_test_test_inc"
emptyvalue = "1"
)
host := NewHost(defaultConnectionString)
defer host.Close()
kv, err := NewKeyValue(host, kvname)
if err != nil {
t.Error(err)
}
kv.Del(testkey)
if val, err := kv.Inc(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != emptyvalue {
t.Errorf("Error, wrong value! %s != %s", val, emptyvalue)
}
kv.Remove()
}
func TestInc3(t *testing.T) {
Verbose = true
const (
kvname = "kv_237_test_test_test_inc's-x"
testkey = "key_237_test_test_test_inc's-x"
emptyvalue = "1"
)
host := NewHost(defaultConnectionString)
defer host.Close()
kv, err := NewKeyValue(host, kvname)
if err != nil {
t.Error(err)
}
kv.Del(testkey)
if val, err := kv.Inc(testkey); err != nil {
t.Errorf("Error, could not get key! %s", err.Error())
} else if val != emptyvalue {
t.Errorf("Error, wrong value! %s != %s", val, emptyvalue)
}
kv.Remove()
}