-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhandlersocket_test.go
222 lines (165 loc) · 4.81 KB
/
handlersocket_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
Copyright 2011 Brian Ketelsen
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
CREATE TABLE `gotest`.`kvs` (
`id` VARCHAR(255) NOT NULL ,
`content` VARCHAR(255) NULL ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB DEFAULT CHARSET=utf8;
*/
package handlersocket
import (
"testing"
"strconv"
)
func TestOpenIndex(t *testing.T) {
// Create new instance
hs := New()
// Enable logging - which doesn't do much yet
hs.Logging = true
// Connect to database
hs.Connect("127.0.0.1", 9998, 9999)
defer hs.Close()
hs.OpenIndex(1, "gotest", "kvs", "PRIMARY", "id", "content")
}
func TestDelete(t *testing.T) {
hs := New()
// Enable logging
hs.Logging = true
// Connect to database
hs.Connect("127.0.0.1", 9998, 9999)
defer hs.Close()
// id is varchar(255), content is text
hs.OpenIndex(3, "gotest", "kvs", "PRIMARY", "id", "content")
var keys, newvals []string
keys = make([]string,1)
newvals = make([]string,0)
for n:=1; n<10; n++ {
keys[0] = "blue" + strconv.Itoa(n)
_, err := hs.Modify(3, "=", 10, 0, "D", keys, newvals)
if err != nil {
t.Error(err)
}
}
}
func TestWrite(t *testing.T) {
hs := New()
// Enable logging
hs.Logging = true
// Connect to database
hs.Connect("127.0.0.1", 9998, 9999)
defer hs.Close()
// id is varchar(255), content is text
hs.OpenIndex(3, "gotest", "kvs", "PRIMARY", "id", "content")
err := hs.Insert(3, "blue1", "a quick brown fox jumped over a lazy dog")
if err != nil {
// We receive an error if the PK already exists. This might not be a real "fail".
// To test for sure, change the PK above before testing.
//TODO: make a new PK each time.
t.Error(err)
}
err = hs.Insert(3, "blue2", "a quick brown fox jumped over a lazy dog")
if err != nil {
// We receive an error if the PK already exists. This might not be a real "fail".
// To test for sure, change the PK above before testing.
//TODO: make a new PK each time.
t.Error(err)
}
// Chinese Data
err = hs.Insert(3, "chinese", "中文測試資料")
if err != nil {
t.Error(err)
}
}
func TestModify(t *testing.T) {
hs := New()
// Enable logging
hs.Logging = true
// Connect to database
hs.Connect("127.0.0.1", 9998, 9999)
defer hs.Close()
// id is varchar(255), content is text
hs.OpenIndex(3, "gotest", "kvs", "PRIMARY", "id", "content")
err := hs.Insert(3, "blue3", "a quick brown fox jumped over a lazy dog")
if err != nil {
// We receive an error if the PK already exists. This might not be a real "fail".
// To test for sure, change the PK above before testing.
//TODO: make a new PK each time.
t.Error(err)
}
err = hs.Insert(3, "blue4", "a quick brown fox jumped over a lazy dog")
if err != nil {
// We receive an error if the PK already exists. This might not be a real "fail".
// To test for sure, change the PK above before testing.
//TODO: make a new PK each time.
t.Error(err)
}
var keys, newvals []string
keys = make([]string,1)
newvals = make([]string,2)
keys[0] = "blue3"
newvals[0] = "blue7"
newvals[1] = "some new thing"
_, err = hs.Modify(3, "=", 1, 0, "U", keys, newvals)
if err != nil {
t.Error(err)
}
keys[0] = "blue4"
newvals[0] = "blue5"
newvals[1] = "My new value!"
_, err = hs.Modify(3, "=", 1, 0, "U", keys, newvals)
if err != nil {
t.Error(err)
}
}
func TestRead(t *testing.T) {
hs := New()
// Enable logging
hs.Logging = true
// Connect to database
hs.Connect("127.0.0.1", 9998, 9999)
defer hs.Close()
hs.OpenIndex(1, "gotest", "kvs", "PRIMARY", "id", "content")
found, _ := hs.Find(1, "=", 1, 0, "blue7")
if len(found) < 1 {
t.Error("Expected one record for blue7")
}
found, _ = hs.Find(1, "=", 1, 0, "chinese")
if len(found) < 1 {
t.Error("Expected one record for chinese")
}
if found[0].Data["content"] != "中文測試資料" {
t.Error("Expected one record content is 中文測試資料 but get", found[0].Data["content"])
}
}
func BenchmarkOpenIndex(b *testing.B) {
b.StopTimer()
hs := New()
defer hs.Close()
hs.Connect("127.0.0.1", 9998, 9999)
b.StartTimer()
for i := 0; i < b.N; i++ {
hs.OpenIndex(1, "gotest", "kvs", "PRIMARY", "id", "content")
}
}
func BenchmarkFind(b *testing.B) {
b.StopTimer()
hs := New()
defer hs.Close()
hs.Connect("127.0.0.1", 9998, 9999)
hs.OpenIndex(1, "gotest", "kvs", "PRIMARY", "id", "content")
b.StartTimer()
for i := 0; i < b.N; i++ {
hs.Find(1, "=", 1, 0, "brian")
}
}