Skip to content

Commit 0944563

Browse files
authored
Increasing code coverage in connection.go (#51)
Increasing code coverage in connection.go to account for not supported methods. Will add code coverage for Close() in a separate PR. Signed-off-by: Matthew Kim <[email protected]>
2 parents a457dc9 + 4b54d26 commit 0944563

File tree

3 files changed

+123
-8
lines changed

3 files changed

+123
-8
lines changed

connection.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,12 @@ type conn struct {
1818
session *cli_service.TOpenSessionResp
1919
}
2020

21-
// The driver does not really implements prepared statements.
22-
// Statement ExecContext is the same as connection ExecContext
23-
// Statement QueryContext is the same as connection QueryContext
21+
// The driver does not really implement prepared statements.
2422
func (c *conn) Prepare(query string) (driver.Stmt, error) {
2523
return &stmt{conn: c, query: query}, nil
2624
}
2725

28-
// The driver does not really implements prepared statements.
29-
// Statement ExecContext is the same as connection ExecContext
30-
// Statement QueryContext is the same as connection QueryContext
26+
// The driver does not really implement prepared statements.
3127
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
3228
return &stmt{conn: c, query: query}, nil
3329
}
@@ -78,6 +74,7 @@ func (c *conn) IsValid() bool {
7874
// as an INSERT or UPDATE.
7975
//
8076
// ExecContext honors the context timeout and return when it is canceled.
77+
// Statement ExecContext is the same as connection ExecContext
8178
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
8279
if len(args) > 0 {
8380
return nil, fmt.Errorf("databricks: query parameters are not supported")
@@ -96,6 +93,7 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
9693
// SELECT.
9794
//
9895
// QueryContext honors the context timeout and return when it is canceled.
96+
// Statement QueryContext is the same as connection QueryContext
9997
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
10098
if len(args) > 0 {
10199
return nil, fmt.Errorf("databricks: query parameters are not supported")

connection_test.go

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ func TestConn_QueryContext(t *testing.T) {
874874
client: testClient,
875875
cfg: config.WithDefaults(),
876876
}
877-
res, err := testConn.ExecContext(context.Background(), "select 1", []driver.NamedValue{
877+
res, err := testConn.QueryContext(context.Background(), "select 1", []driver.NamedValue{
878878
{Value: 1, Name: "name"},
879879
})
880880

@@ -909,7 +909,7 @@ func TestConn_QueryContext(t *testing.T) {
909909
client: testClient,
910910
cfg: config.WithDefaults(),
911911
}
912-
res, err := testConn.ExecContext(context.Background(), "select 1", []driver.NamedValue{})
912+
res, err := testConn.QueryContext(context.Background(), "select 1", []driver.NamedValue{})
913913

914914
assert.Error(t, err)
915915
assert.Nil(t, res)
@@ -960,6 +960,121 @@ func TestConn_QueryContext(t *testing.T) {
960960
})
961961
}
962962

963+
func TestConn_Ping(t *testing.T) {
964+
t.Run("ping returns ErrBadConn when executeStatement fails", func(t *testing.T) {
965+
var executeStatementCount int
966+
executeStatement := func(ctx context.Context, req *cli_service.TExecuteStatementReq) (r *cli_service.TExecuteStatementResp, err error) {
967+
executeStatementCount++
968+
executeStatementResp := &cli_service.TExecuteStatementResp{
969+
Status: &cli_service.TStatus{
970+
StatusCode: cli_service.TStatusCode_ERROR_STATUS,
971+
},
972+
OperationHandle: &cli_service.TOperationHandle{
973+
OperationId: &cli_service.THandleIdentifier{
974+
GUID: []byte("2"),
975+
Secret: []byte("b"),
976+
},
977+
},
978+
}
979+
return executeStatementResp, nil
980+
}
981+
982+
testClient := &client.TestClient{
983+
FnExecuteStatement: executeStatement,
984+
}
985+
testConn := &conn{
986+
session: getTestSession(),
987+
client: testClient,
988+
cfg: config.WithDefaults(),
989+
}
990+
err := testConn.Ping(context.Background())
991+
992+
assert.Error(t, err)
993+
assert.Equal(t, driver.ErrBadConn, err)
994+
assert.Equal(t, 1, executeStatementCount)
995+
})
996+
997+
t.Run("ping returns nil error when driver can establish connection", func(t *testing.T) {
998+
var executeStatementCount int
999+
executeStatement := func(ctx context.Context, req *cli_service.TExecuteStatementReq) (r *cli_service.TExecuteStatementResp, err error) {
1000+
executeStatementCount++
1001+
executeStatementResp := &cli_service.TExecuteStatementResp{
1002+
Status: &cli_service.TStatus{
1003+
StatusCode: cli_service.TStatusCode_SUCCESS_STATUS,
1004+
},
1005+
OperationHandle: &cli_service.TOperationHandle{
1006+
OperationId: &cli_service.THandleIdentifier{
1007+
GUID: []byte("2"),
1008+
Secret: []byte("b"),
1009+
},
1010+
},
1011+
}
1012+
return executeStatementResp, nil
1013+
}
1014+
1015+
getOperationStatus := func(ctx context.Context, req *cli_service.TGetOperationStatusReq) (r *cli_service.TGetOperationStatusResp, err error) {
1016+
getOperationStatusResp := &cli_service.TGetOperationStatusResp{
1017+
OperationState: cli_service.TOperationStatePtr(cli_service.TOperationState_FINISHED_STATE),
1018+
NumModifiedRows: thrift.Int64Ptr(10),
1019+
}
1020+
return getOperationStatusResp, nil
1021+
}
1022+
1023+
testClient := &client.TestClient{
1024+
FnExecuteStatement: executeStatement,
1025+
FnGetOperationStatus: getOperationStatus,
1026+
}
1027+
1028+
testConn := &conn{
1029+
session: getTestSession(),
1030+
client: testClient,
1031+
cfg: config.WithDefaults(),
1032+
}
1033+
err := testConn.Ping(context.Background())
1034+
1035+
assert.Nil(t, err)
1036+
assert.Equal(t, 1, executeStatementCount)
1037+
})
1038+
}
1039+
1040+
func TestConn_Begin(t *testing.T) {
1041+
t.Run("Begin not supported", func(t *testing.T) {
1042+
testConn := &conn{
1043+
session: getTestSession(),
1044+
client: &client.TestClient{},
1045+
cfg: config.WithDefaults(),
1046+
}
1047+
res, err := testConn.Begin()
1048+
assert.Nil(t, res)
1049+
assert.Error(t, err)
1050+
})
1051+
}
1052+
1053+
func TestConn_BeginTx(t *testing.T) {
1054+
t.Run("BeginTx not supported", func(t *testing.T) {
1055+
testConn := &conn{
1056+
session: getTestSession(),
1057+
client: &client.TestClient{},
1058+
cfg: config.WithDefaults(),
1059+
}
1060+
res, err := testConn.BeginTx(context.Background(), driver.TxOptions{})
1061+
assert.Nil(t, res)
1062+
assert.Error(t, err)
1063+
})
1064+
}
1065+
1066+
func TestConn_ResetSession(t *testing.T) {
1067+
t.Run("ResetSession not currently supported", func(t *testing.T) {
1068+
testConn := &conn{
1069+
session: getTestSession(),
1070+
client: &client.TestClient{},
1071+
cfg: config.WithDefaults(),
1072+
}
1073+
res := testConn.ResetSession(context.Background())
1074+
assert.Nil(t, res)
1075+
})
1076+
}
1077+
9631078
func getTestSession() *cli_service.TOpenSessionResp {
9641079
return &cli_service.TOpenSessionResp{SessionHandle: &cli_service.TSessionHandle{
9651080
SessionId: &cli_service.THandleIdentifier{

statement.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func (s *stmt) Query(args []driver.Value) (driver.Rows, error) {
3434
// as an INSERT or UPDATE.
3535
//
3636
// ExecContext honors the context timeout and return when it is canceled.
37+
// Statement ExecContext is the same as connection ExecContext
3738
func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error) {
3839
return s.conn.ExecContext(ctx, s.query, args)
3940
}
@@ -42,6 +43,7 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive
4243
// SELECT.
4344
//
4445
// QueryContext honors the context timeout and return when it is canceled.
46+
// Statement QueryContext is the same as connection QueryContext
4547
func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error) {
4648
return s.conn.QueryContext(ctx, s.query, args)
4749
}

0 commit comments

Comments
 (0)