-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
228 lines (179 loc) · 4.71 KB
/
client_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
223
224
225
226
227
228
/* Copyright (C) 2021 Pankaj Kargirwar <[email protected]>
This file is part of prosql-agent
prosql-agent is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
prosql-agent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with prosql-agent. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"net/url"
"strconv"
"testing"
"time"
"github.com/dchest/uniuri"
)
const BASE_URL = "http://localhost:23890"
var client = &http.Client{Timeout: 10 * time.Second}
var creds = map[string]string{
"user": "server",
"pass": "dev-server",
"host": "127.0.0.1",
"port": "3306",
"db": "test-generico",
}
var ctx = context.Background()
const N = 1000
func TestSid(t *testing.T) {
sid := "VPtBxUGvRy9Eg3M3"
setdb_test(t, sid, "test-generico")
cid := execute_test(t, sid, "select * from `authorized-devices`")
fetch_test(t, sid, cid)
}
func TestCorrupted(t *testing.T) {
//login and get session id
sid := login_test(t)
//get db list
cid := execute_test(t, sid, "select description from `vouchers` where id = 606")
rows := fetch_test(t, sid, cid)
log.Println(rows)
}
func TestClientExecute(t *testing.T) {
//login and get session id
sid := login_test(t)
//get db list
cid := execute_test(t, sid, "select * from users limit 1000")
fetch_test(t, sid, cid)
}
func TestClientLogin(t *testing.T) {
//login and get session id
sid := login_test(t)
//get db list
cid := execute_test(t, sid, "show databases")
fetch_test(t, sid, cid)
//get table list
cid = execute_test(t, sid, "show tables from `test-generico`")
fetch_test(t, sid, cid)
//select database
//setdb_test(t, sid, "test-generico")
//get data
cid = execute_test(t, sid, "select * from inventory limit 1000")
fetch_test(t, sid, cid)
//t.Log(rows)
//change database
//setdb_test(t, sid, "dev3-generico")
//get data
cid = execute_test(t, sid, "select * from users limit 1")
fetch_test(t, sid, cid)
//t.Log(rows)
//change database
//setdb_test(t, sid, "test-generico")
//get data
cid = execute_test(t, sid, "select * from `invoice-items-1` limit 1000")
fetch_test(t, sid, cid)
//t.Log(rows)
}
func login_test(t *testing.T) string {
defer TimeTrack(ctx, time.Now())
r := &Response{}
err := getJson(getQuery(t, "login", creds), r)
if err != nil {
t.Fatalf(err.Error())
}
sid := getValue(t, r, "session-id")
return sid
}
func setdb_test(t *testing.T, sid, db string) {
defer TimeTrack(ctx, time.Now())
r := &Response{}
params := map[string]string{
"session-id": sid,
"db": db,
}
err := getJson(getQuery(t, "set-db", params), r)
if err != nil {
t.Fatalf(err.Error())
}
if r.Status != "ok" {
t.Fatalf(r.Msg)
}
}
func execute_test(t *testing.T, sid, query string) string {
defer TimeTrack(ctx, time.Now())
r := &Response{}
params := map[string]string{
"session-id": sid,
"query": query,
}
err := getJson(getQuery(t, "execute", params), r)
if err != nil {
t.Fatalf(err.Error())
}
if r.Status != "ok" {
t.Fatalf(r.Msg)
}
return getValue(t, r, "cursor-id")
}
func fetch_test(t *testing.T, sid, cid string) interface{} {
defer TimeTrack(ctx, time.Now())
r := &Response{}
params := map[string]string{
"session-id": sid,
"cursor-id": cid,
"num-of-rows": strconv.Itoa(N),
}
err := getJson(getQuery(t, "fetch", params), r)
if err != nil {
t.Fatalf(err.Error())
}
if r.Status != "ok" {
t.Errorf(r.Msg)
return r.Data
}
return r.Data
}
func getValue(t *testing.T, r *Response, k string) string {
m, ok := r.Data.(map[string]interface{})
if !ok {
t.Fatalf("Unable to parse JSON")
}
return m[k].(string)
}
func getQuery(t *testing.T, path string, m map[string]string) string {
base, err := url.Parse(BASE_URL)
if err != nil {
t.Fatalf(err.Error())
}
// Path params
base.Path += path
// Query params
params := url.Values{}
for k, v := range m {
params.Add(k, v)
base.RawQuery = params.Encode()
}
return base.String()
}
func getJson(url string, target interface{}) error {
r, err := http.NewRequest("GET", url, nil)
if err != nil {
return err
}
r.Header.Add("X-Request-Id", uniuri.New())
res, err := client.Do(r)
defer res.Body.Close()
//body, err := ioutil.ReadAll(res.Body)
//bodyString := string(body)
//fmt.Println(bodyString)
return json.NewDecoder(res.Body).Decode(target)
}