-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconnection_test.go
185 lines (159 loc) · 6.66 KB
/
connection_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
package fireboltgosdk
import (
"context"
"errors"
"strings"
"testing"
)
// TestConnectionPrepareStatement, tests that prepare statement doesn't result into an error
func TestConnectionPrepareStatement(t *testing.T) {
emptyClient := ClientImplV0{}
fireboltConnection := fireboltConnection{&emptyClient, "engine_url", map[string]string{}, nil}
queryMock := "SELECT 1"
_, err := fireboltConnection.Prepare(queryMock)
if err != nil {
t.Errorf("Prepare failed, but it shouldn't: %v", err)
}
}
// TestConnectionClose, tests that connection close doesn't result an error
// and prepare statement on closed connection is not possible
func TestConnectionClose(t *testing.T) {
emptyClient := ClientImplV0{}
fireboltConnection := fireboltConnection{&emptyClient, "engine_url", map[string]string{}, nil}
if err := fireboltConnection.Close(); err != nil {
t.Errorf("Close failed with an err: %v", err)
}
_, err := fireboltConnection.Prepare("SELECT 1")
if err == nil {
t.Errorf("Prepare on closed connection didn't fail, but it should")
}
}
func runProcessSetStatementFail(t *testing.T, value string) {
emptyClient := ClientImpl{} // Client version is irrelevant for this test
fireboltConnection := fireboltConnection{&emptyClient, "engine_url", map[string]string{}, nil}
expectedError := "could not set parameter"
_, err := processSetStatement(context.TODO(), &fireboltConnection, value)
if err == nil {
t.Errorf("processSetStatement didn't fail, but it should")
} else if !strings.Contains(err.Error(), expectedError) {
t.Errorf("processSetStatement failed with unexpected error, expected error to contain: %v got: %v", expectedError, err)
}
}
func TestProcessSetStatement(t *testing.T) {
runProcessSetStatementFail(t, "SET database=my_db")
runProcessSetStatementFail(t, "SET engine=my_engine")
runProcessSetStatementFail(t, "SET output_format='json'")
}
func TestSetParameter(t *testing.T) {
connector := FireboltConnector{}
emptyClient := ClientImpl{} // Client version is irrelevant for this test
fireboltConnection := fireboltConnection{&emptyClient, "engine_url", map[string]string{}, &connector}
fireboltConnection.setParameter("key", "value")
if fireboltConnection.parameters["key"] != "value" {
t.Errorf("setParameter didn't set parameter correctly")
}
if connector.cachedParameters["key"] != "value" {
t.Errorf("setParameter didn't set parameter in connector correctly")
}
}
// MockClient rudimentary mocks Client and tracks the parameters passed to Query
type MockClient struct {
ParametersCalled []map[string]string
}
func (m *MockClient) Query(ctx context.Context, engineUrl, query string, parameters map[string]string, control connectionControl) (*QueryResponse, error) {
m.ParametersCalled = append(m.ParametersCalled, parameters)
return nil, nil
}
func (m *MockClient) GetConnectionParameters(ctx context.Context, engineName string, databaseName string) (string, map[string]string, error) {
// Implement to satisfy Client interface
return "", nil, nil
}
func TestMultipleSetParameters(t *testing.T) {
connector := FireboltConnector{}
emptyClient := MockClient{}
fireboltConnection := fireboltConnection{&emptyClient, "engine_url", map[string]string{}, &connector}
var err error
_, err = processSetStatement(context.TODO(), &fireboltConnection, "SET key1=value1")
raiseIfError(t, err)
_, err = processSetStatement(context.TODO(), &fireboltConnection, "SET key2=value")
raiseIfError(t, err)
// Check if parameters were set correctly
if len(emptyClient.ParametersCalled) != 2 {
t.Errorf("processSetStatement didn't set parameters correctly")
}
if _, ok := emptyClient.ParametersCalled[0]["key1"]; !ok {
t.Errorf("processSetStatement didn't set parameter correctly")
}
if _, ok := emptyClient.ParametersCalled[1]["key2"]; !ok {
t.Errorf("processSetStatement didn't set parameter correctly")
}
if _, ok := emptyClient.ParametersCalled[1]["key1"]; !ok {
t.Errorf("processSetStatement didn't use previous parameters correctly")
}
}
// MockClient rudimentary mocks Client and tracks the parameters passed to Query
type MockClientFailingQuery struct {
ParametersCalled []map[string]string
}
func (m *MockClientFailingQuery) Query(ctx context.Context, engineUrl, query string, parameters map[string]string, control connectionControl) (*QueryResponse, error) {
m.ParametersCalled = append(m.ParametersCalled, parameters)
return nil, errors.New("dummy error")
}
func (m *MockClientFailingQuery) GetConnectionParameters(ctx context.Context, engineName string, databaseName string) (string, map[string]string, error) {
// Implement to satisfy Client interface
return "", nil, nil
}
func TestFailingQueryDoesntSetParameter(t *testing.T) {
connector := FireboltConnector{}
emptyClient := MockClientFailingQuery{}
fireboltConnection := fireboltConnection{&emptyClient, "engine_url", map[string]string{}, &connector}
var err error
_, err = processSetStatement(context.TODO(), &fireboltConnection, "SET key1=value1")
if err == nil {
t.Errorf("processSetStatement didn't fail, but it should")
}
_, err = processSetStatement(context.TODO(), &fireboltConnection, "SET key2=value")
if err == nil {
t.Errorf("processSetStatement didn't fail, but it should")
}
// Check if parameters were set correctly
if len(emptyClient.ParametersCalled) != 2 {
t.Errorf("processSetStatement didn't set parameters correctly")
}
if _, ok := emptyClient.ParametersCalled[0]["key1"]; !ok {
t.Errorf("processSetStatement didn't set parameter correctly")
}
if _, ok := emptyClient.ParametersCalled[1]["key2"]; !ok {
t.Errorf("processSetStatement didn't set parameter correctly")
}
if _, ok := emptyClient.ParametersCalled[1]["key1"]; ok {
t.Errorf("processSetStatement used previous parameter even though query failed")
}
if len(fireboltConnection.parameters) != 0 {
t.Errorf("processSetStatement set parameters even though query failed")
}
}
func TestResetParameters(t *testing.T) {
connector := FireboltConnector{}
connector.cachedParameters = map[string]string{
"database": "db",
"engine": "engine",
"output_format": "output_format",
"key": "value",
}
emptyClient := ClientImpl{} // Client version is irrelevant for this test
fireboltConnection := fireboltConnection{&emptyClient, "engine_url", map[string]string{}, &connector}
fireboltConnection.parameters = map[string]string{
"database": "db",
"engine": "engine",
"output_format": "output_format",
"key": "value",
}
fireboltConnection.resetParameters()
if len(fireboltConnection.parameters) != 3 {
t.Errorf("resetParameters didn't remove parameters correctly")
}
if len(connector.cachedParameters) != 3 {
t.Errorf("resetParameters didn't remove parameters in connector correctly")
}
}