-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_test.go
More file actions
229 lines (195 loc) · 6.33 KB
/
server_test.go
File metadata and controls
229 lines (195 loc) · 6.33 KB
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
229
//
// //github.com// SQLite Cloud
// //github.com///github.com///github.com///github.com/ //github.com/
// //github.com/ //github.com/ //github.com/ Product : SQLite Cloud GO SDK
// //github.com/ //github.com/ //github.com/ Version : 1.1.0
// // //github.com/ //github.com/ //github.com/ Date : 2021/10/08
// //github.com/ //github.com/ //github.com/ //github.com/ Author : Andreas Pfeil
// //github.com/ //github.com/ //github.com/ //github.com/
// //github.com/ //github.com///github.com///github.com// //github.com/ //github.com/ Description : Test program for the
// //github.com// //github.com/ //github.com/ SQLite Cloud internal
// //github.com// //github.com///github.com///github.com// //github.com/ server commands.
// //github.com// //github.com//
// //github.com// //github.com///
// //github.com/ Copyright : 2021 by SQLite Cloud Inc.
//
// -----------------------------------------------------------------------TAB=2
package test
import (
"fmt"
"os"
"strings"
"testing"
sqlitecloud "github.com/sqlitecloud/sqlitecloud-go"
"github.com/stretchr/testify/assert"
)
const testDbnameServer = "test-gosdk-server-db.sqlite"
func TestServer(t *testing.T) {
connectionString, _ := os.LookupEnv("SQLITE_CONNECTION_STRING")
apikey, _ := os.LookupEnv("SQLITE_API_KEY")
db, err := sqlitecloud.Connect(connectionString + "?apikey=" + apikey)
if err != nil {
t.Fatal("Connect: ", err.Error())
}
// Checking wrong AUTH
username, _ := os.LookupEnv("SQLITE_USER")
password, _ := os.LookupEnv("SQLITE_PASSWORD")
if err := db.Auth(username, "wrong password"); err == nil {
t.Fatal("AUTH: Expected authorization failed, got authorized")
}
db.Close()
// reopen the connection (it was closed because of the auth command with wrong credentials)
db, err = sqlitecloud.Connect(connectionString + "?apikey=" + apikey)
if err != nil {
t.Fatal(err.Error())
}
defer db.Close()
// Checking PING
if db.Ping() != nil {
t.Fatal(err.Error())
}
// Checking AUTH
if err := db.Auth(username, password); err != nil {
t.Fatal("Checking AUTH: ", err.Error())
}
if err := db.RemoveDatabase(testDbnameServer, true); err != nil { // Database, NoError
t.Fatal("REMOVE DATABASE: ", err.Error())
}
// Checking CREATE DATABASE
if err := db.CreateDatabase(testDbnameServer, "", "", false); err != nil { // Database, Key, Encoding, NoError
t.Fatal("CREATE DATABASE: ", err.Error())
}
// Checking LIST DATABASES
if databases, err := db.ListDatabases(); err != nil {
t.Fatal("LIST DATABASES: ", err.Error())
} else {
if len(databases) == 0 || !contains(databases, testDbnameServer) {
t.Fatal("LIST DATABASES: ", fmt.Sprintf("Database %s not found in LIST DATABASES", testDbnameServer))
}
}
// USE DATABASE
if err := db.UseDatabase(testDbnameServer); err != nil { // Database
t.Fatal("USE DATABASES: ", err.Error())
}
// Checking SET KEY
testKey := "TestKey"
testKeyValue := "1405"
if err := db.SetKey(testKey, testKeyValue); err != nil { // Key, Value
t.Fatal("SET KEY: ", err.Error())
}
testKey = strings.ToLower(testKey)
// LIST KEYS
if keys, err := db.ListKeys(); err != nil {
t.Fatal("LIST KEYS: ", err.Error())
} else {
if _, found := keys[testKey]; !found {
t.Fatal("LIST KEY: ", fmt.Sprintf("Key %s not found in LIST KEYS", testKey))
}
}
// GET KEY
if val, err := db.GetKey(testKey); err != nil {
t.Fatal("GET KEY: ", err.Error())
} else {
if val != testKeyValue {
t.Fatal("GET KEY: ", fmt.Sprintf("Expected value %s, Got %s.", testKeyValue, val))
}
}
// REMOVE KEY
if err := db.RemoveKey(testKey); err != nil {
t.Fatal("REMOVE KEY: ", err.Error())
} else {
if keys, err := db.ListKeys(); err != nil {
t.Fatal("REMOVE KEY, LIST KEY: ", err.Error())
} else {
if _, found := keys[testKey]; found {
t.Fatal("REMOVE KEY, LIST KEY: ", fmt.Sprintf("Key %s still found in LIST KEYS", testKey))
}
}
}
// LIST COMMANDS
if commands, err := db.ListCommands(); err != nil {
t.Fatal("LIST COMMANDS: ", err.Error())
} else {
if len(commands) == 0 {
t.Fatal("LIST COMMANDS: Invalid result (0 rows).")
}
}
// LIST CONNECTIONS
if connections, err := db.ListConnections(); err != nil {
t.Fatal("LIST CONNECTIONS: ", err.Error())
} else {
if len(connections) == 0 {
t.Fatal("LIST CONNECTIONS: Invalid result (no connections).")
}
}
// LIST DATABASE CONNECTIONS ()...")
if connections, err := db.ListDatabaseConnections(testDbnameServer); err != nil {
t.Fatal("LIST DATABASE CONNECTIONS: ", err.Error())
} else {
if len(connections) == 0 {
t.Fatal("LIST DATABASE CONNECTIONS: Invalid result (0 connections).")
}
}
// LIST INFO
if _, err := db.GetInfo(); err != nil {
t.Fatal("LIST INFO: ", err.Error())
}
// CREATE TABLE
testTableServer := "TestTable"
if err := db.Execute(fmt.Sprintf("CREATE TABLE IF NOT EXISTS '%s' (a INTEGER PRIMARY KEY, b)", testTableServer)); err != nil {
t.Fatal("CREATE TABLE: ", err.Error())
}
// LIST TABLES
if tables, err := db.ListTables(); err != nil {
t.Fatal("LIST TABLES: ", err.Error())
} else {
if len(tables) < 1 || !contains(tables, testTableServer) {
t.Fatal("LIST TABLES: ", fmt.Sprintf("Table %s not found in LIST TABLES", testTableServer))
}
}
// LIST PLUGINS
if _, err := db.ListPlugins(); err != nil {
t.Fatal("LIST PLUGINS: ", err.Error())
}
// LIST CLIENT KEYS
if ckeys, err := db.ListClientKeys(); err != nil {
t.Fatal("LIST CLIENT KEYS: ", err.Error())
} else if len(ckeys) == 0 {
t.Fatal("LIST CLIENT KEYS: Invalid result")
}
// LIST NODES
if nodes, err := db.ListNodes(); err != nil {
t.Fatal("LIST NODES: ", err.Error())
} else {
if len(nodes) == 0 {
t.Fatal("LIST NODES: Ivalid result.")
}
}
// LIST DATABASE KEYS
if _, err := db.ListDatabaseKeys(testDbnameServer); err != nil {
t.Fatal("LIST DATABASE KEYS:", err.Error())
}
// UNUSE DATABASE
if err := db.UnuseDatabase(); err != nil {
t.Fatal("UNUSE DATABASE:", err.Error())
}
// REMOVE DATABASE
if err := db.RemoveDatabase(testDbnameServer, false); err != nil { // Database, NoError
t.Fatal("REMOVE DATABASES: ", err.Error())
}
// fmt.Printf( "Checking CLOSE CONNECTION..." )
// if err := db.CloseConnection( "14" ); err != nil { // ConnectionID
// fail( err.Error() )
// }
// fmt.Printf( "ok.\r\n" )
}
func TestCompressionEnabledByDefault(t *testing.T) {
db, cleaup := setupDatabase(t)
defer cleaup()
result, _ := db.Select("GET CLIENT KEY COMPRESSION")
value, err := result.GetString()
if err != nil {
t.Fatal(err.Error())
}
assert.Equal(t, "1", value)
}