forked from databricks/databricks-sql-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statement_test.go
166 lines (153 loc) · 4.95 KB
/
statement_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
package dbsql
import (
"context"
"database/sql/driver"
"github.com/apache/thrift/lib/go/thrift"
"github.com/databricks/databricks-sql-go/internal/cli_service"
"github.com/databricks/databricks-sql-go/internal/client"
"github.com/databricks/databricks-sql-go/internal/config"
"github.com/stretchr/testify/assert"
"testing"
)
func TestStmt_Close(t *testing.T) {
t.Run("Close is not applicable", func(t *testing.T) {
testStmt := stmt{
conn: &conn{},
query: "query string",
}
err := testStmt.Close()
assert.Nil(t, err)
})
}
func TestStmt_NumInput(t *testing.T) {
t.Run("NumInput is not applicable", func(t *testing.T) {
testStmt := stmt{
conn: &conn{},
query: "query string",
}
numInput := testStmt.NumInput()
assert.Equal(t, -1, numInput)
})
}
func TestStmt_Exec(t *testing.T) {
t.Run("Exec is not implemented", func(t *testing.T) {
testStmt := stmt{
conn: &conn{},
query: "query string",
}
res, err := testStmt.Exec([]driver.Value{})
assert.Nil(t, res)
assert.Error(t, err)
})
}
func TestStmt_Query(t *testing.T) {
t.Run("Query is not implemented", func(t *testing.T) {
testStmt := stmt{
conn: &conn{},
query: "query string",
}
res, err := testStmt.Query([]driver.Value{})
assert.Nil(t, res)
assert.Error(t, err)
})
}
func TestStmt_ExecContext(t *testing.T) {
t.Run("ExecContext returns number of rows modified when execution is successful", func(t *testing.T) {
var executeStatementCount, getOperationStatusCount int
var savedQueryString string
executeStatement := func(ctx context.Context, req *cli_service.TExecuteStatementReq) (r *cli_service.TExecuteStatementResp, err error) {
executeStatementCount++
savedQueryString = req.Statement
executeStatementResp := &cli_service.TExecuteStatementResp{
Status: &cli_service.TStatus{
StatusCode: cli_service.TStatusCode_SUCCESS_STATUS,
},
OperationHandle: &cli_service.TOperationHandle{
OperationId: &cli_service.THandleIdentifier{
GUID: []byte{1, 2, 3, 4, 2, 23, 4, 2, 3, 2, 3, 4, 4, 223, 34, 54},
Secret: []byte("b"),
},
},
}
return executeStatementResp, nil
}
getOperationStatus := func(ctx context.Context, req *cli_service.TGetOperationStatusReq) (r *cli_service.TGetOperationStatusResp, err error) {
getOperationStatusCount++
getOperationStatusResp := &cli_service.TGetOperationStatusResp{
OperationState: cli_service.TOperationStatePtr(cli_service.TOperationState_FINISHED_STATE),
NumModifiedRows: thrift.Int64Ptr(10),
}
return getOperationStatusResp, nil
}
testClient := &client.TestClient{
FnExecuteStatement: executeStatement,
FnGetOperationStatus: getOperationStatus,
}
testConn := &conn{
session: getTestSession(),
client: testClient,
cfg: config.WithDefaults(),
}
testQuery := "insert 10"
testStmt := &stmt{
conn: testConn,
query: testQuery,
}
res, err := testStmt.ExecContext(context.Background(), []driver.NamedValue{})
assert.NoError(t, err)
assert.NotNil(t, res)
rowsAffected, _ := res.RowsAffected()
assert.Equal(t, int64(10), rowsAffected)
assert.Equal(t, 1, executeStatementCount)
assert.Equal(t, testQuery, savedQueryString)
})
}
func TestStmt_QueryContext(t *testing.T) {
t.Run("QueryContext returns rows object upon successful query", func(t *testing.T) {
var executeStatementCount, getOperationStatusCount int
var savedQueryString string
executeStatement := func(ctx context.Context, req *cli_service.TExecuteStatementReq) (r *cli_service.TExecuteStatementResp, err error) {
executeStatementCount++
savedQueryString = req.Statement
executeStatementResp := &cli_service.TExecuteStatementResp{
Status: &cli_service.TStatus{
StatusCode: cli_service.TStatusCode_SUCCESS_STATUS,
},
OperationHandle: &cli_service.TOperationHandle{
OperationId: &cli_service.THandleIdentifier{
GUID: []byte{1, 2, 3, 4, 2, 23, 4, 2, 3, 2, 3, 4, 4, 223, 34, 54},
Secret: []byte("b"),
},
},
}
return executeStatementResp, nil
}
getOperationStatus := func(ctx context.Context, req *cli_service.TGetOperationStatusReq) (r *cli_service.TGetOperationStatusResp, err error) {
getOperationStatusCount++
getOperationStatusResp := &cli_service.TGetOperationStatusResp{
OperationState: cli_service.TOperationStatePtr(cli_service.TOperationState_FINISHED_STATE),
NumModifiedRows: thrift.Int64Ptr(10),
}
return getOperationStatusResp, nil
}
testClient := &client.TestClient{
FnExecuteStatement: executeStatement,
FnGetOperationStatus: getOperationStatus,
}
testConn := &conn{
session: getTestSession(),
client: testClient,
cfg: config.WithDefaults(),
}
testQuery := "select 1"
testStmt := &stmt{
conn: testConn,
query: testQuery,
}
rows, err := testStmt.QueryContext(context.Background(), []driver.NamedValue{})
assert.NoError(t, err)
assert.NotNil(t, rows)
assert.Equal(t, 1, executeStatementCount)
assert.Equal(t, testQuery, savedQueryString)
})
}