Skip to content

Commit

Permalink
use assertions in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jl committed Oct 27, 2023
1 parent 9e3556b commit c157c4f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 60 deletions.
4 changes: 4 additions & 0 deletions assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"testing"
)

func assertNilE(t *testing.T, actual any, descriptions ...string) {
errorOnNonEmpty(t, validateNil(actual, descriptions...))
}

func assertNilF(t *testing.T, actual any, descriptions ...string) {
fatalOnNonEmpty(t, validateNil(actual, descriptions...))
}
Expand Down
2 changes: 1 addition & 1 deletion auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ func TestUnitAuthenticateWithConfigOkta(t *testing.T) {
sc.ctx = context.Background()

err = authenticateWithConfig(sc)
assertNilF(t, err, "expected to have no error.")
assertNilE(t, err, "expected to have no error.")

sr.FuncPostAuthSAML = postAuthSAMLError
err = authenticateWithConfig(sc)
Expand Down
26 changes: 8 additions & 18 deletions authokta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,22 +246,16 @@ func TestUnitAuthenticateBySAML(t *testing.T) {
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
assertNotNilF(t, err, "should have failed at FuncPostAuthSAML.")
driverErr, ok := err.(*SnowflakeError)
if !ok {
t.Fatalf("should be snowflake error. err: %v", err)
}
if driverErr.Number != ErrCodeIdpConnectionError {
t.Fatalf("unexpected error code. expected: %v, got: %v", ErrCodeIdpConnectionError, driverErr.Number)
}
assertTrueF(t, ok, "should be a SnowflakeError")
assertEqualE(t, driverErr.Number, ErrCodeIdpConnectionError)

sr.FuncPostAuthSAML = postAuthSAMLAuthSuccessButInvalidURL
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
assertNotNilF(t, err, "should have failed at FuncPostAuthSAML.")
driverErr, ok = err.(*SnowflakeError)
if !ok {
t.Fatalf("should be snowflake error. err: %v", err)
}
if driverErr.Number != ErrCodeIdpConnectionError {
t.Fatalf("unexpected error code. expected: %v, got: %v", ErrCodeIdpConnectionError, driverErr.Number)
}
assertTrueF(t, ok, "should be a SnowflakeError")
assertEqualE(t, driverErr.Number, ErrCodeIdpConnectionError)

sr.FuncPostAuthSAML = postAuthSAMLAuthSuccessButInvalidTokenURL
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
assertNotNilF(t, err, "should have failed at FuncPostAuthSAML.")
Expand Down Expand Up @@ -297,10 +291,6 @@ func TestUnitAuthenticateBySAML(t *testing.T) {
_, err = authenticateBySAML(context.Background(), sr, authenticator, application, account, user, password)
assertNotNilF(t, err, "should have failed at FuncGetSSO.")
driverErr, ok = err.(*SnowflakeError)
if !ok {
t.Fatalf("should be snowflake error. err: %v", err)
}
if driverErr.Number != ErrCodeSSOURLNotMatch {
t.Fatalf("unexpected error code. expected: %v, got: %v", ErrCodeSSOURLNotMatch, driverErr.Number)
}
assertTrueF(t, ok, "should be a SnowflakeError")
assertEqualE(t, driverErr.Number, ErrCodeSSOURLNotMatch)
}
4 changes: 0 additions & 4 deletions connection_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ func (sc *snowflakeConn) isClientSessionKeepAliveEnabled() bool {
return strings.Compare(*v, "true") == 0
}

func (sc *snowflakeConn) isHeartbeatNil() bool {
return sc.rest == nil || sc.rest.HeartBeat == nil
}

func (sc *snowflakeConn) startHeartBeat() {
if sc.cfg != nil && !sc.isClientSessionKeepAliveEnabled() {
return
Expand Down
10 changes: 4 additions & 6 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,9 @@ func TestConnectorWithMissingConfig(t *testing.T) {
connector := NewConnector(&mock, config)
_, err := connector.Connect(context.Background())
assertNotNilF(t, err, "the connection should have failed due to empty account.")

driverErr, ok := err.(*SnowflakeError)
if !ok {
t.Fatalf("Snowflake error is expected. err: %v", err.Error())
}
if driverErr.Number != expectedErr.Number || driverErr.Message != expectedErr.Message {
t.Fatalf("Snowflake error did not match. expected: %v, got: %v", expectedErr, driverErr)
}
assertTrueF(t, ok, "should be a SnowflakeError")
assertEqualE(t, driverErr.Number, expectedErr.Number)
assertEqualE(t, driverErr.Message, expectedErr.Message)
}
8 changes: 4 additions & 4 deletions dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -706,10 +706,10 @@ func TestParseDSN(t *testing.T) {
ocspMode: ocspModeFailOpen,
err: nil,
},
{
dsn: "u:[email protected]:443?authenticator=http%3A%2F%2Fsc.okta.com&ocspFailOpen=true&validateDefaultParameters=true",
err: errFailedToParseAuthenticator(),
},
{
dsn: "u:[email protected]:443?authenticator=http%3A%2F%2Fsc.okta.com&ocspFailOpen=true&validateDefaultParameters=true",
err: errFailedToParseAuthenticator(),
},
}

for _, at := range []AuthType{AuthTypeExternalBrowser, AuthTypeOAuth} {
Expand Down
40 changes: 13 additions & 27 deletions heartbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,24 @@ func TestUnitPostHeartbeat(t *testing.T) {
restful: sr,
}
err := heartbeat.heartbeatMain()
if err != nil {
t.Fatalf("failed to heartbeat and renew session. err: %v", err)
}
assertNilF(t, err, "failed to heartbeat and renew session")

heartbeat.restful.FuncPost = postTestError
err = heartbeat.heartbeatMain()
if err == nil {
t.Fatal("should have failed")
}
assertNotNilF(t, err, "should have failed to start heartbeat")
assertEqualE(t, err.Error(), "failed to run post method")

heartbeat.restful.FuncPost = postTestSuccessButInvalidJSON
err = heartbeat.heartbeatMain()
if err == nil {
t.Fatal("should have failed")
}
assertNotNilF(t, err, "should have failed to start heartbeat")
assertHasPrefixE(t, err.Error(), "invalid character")

heartbeat.restful.FuncPost = postTestAppForbiddenError
err = heartbeat.heartbeatMain()
if err == nil {
t.Fatal("should have failed")
}
assertNotNilF(t, err, "should have failed to start heartbeat")
driverErr, ok := err.(*SnowflakeError)
if !ok {
t.Fatalf("should be snowflake error. err: %v", err)
}
if driverErr.Number != ErrFailedToHeartbeat {
t.Fatalf("unexpected error code. expected: %v, got: %v", ErrFailedToHeartbeat, driverErr.Number)
}
assertTrueF(t, ok, "connection should be snowflakeConn")
assertEqualE(t, driverErr.Number, ErrFailedToHeartbeat)
})
}

Expand All @@ -64,15 +54,11 @@ func TestHeartbeatStartAndStop(t *testing.T) {
}

conn, ok := db.(*snowflakeConn)
if ok && conn.isHeartbeatNil() {
t.Fatalf("heartbeat should not be nil")
}
assertTrueF(t, ok, "connection should be snowflakeConn")
assertNotNilF(t, conn.rest, "heartbeat should not be nil")
assertNotNilF(t, conn.rest.HeartBeat, "heartbeat should not be nil")

err = db.Close()
if err != nil {
t.Fatalf("should not cause error in Close. err: %v", err)
}
if ok && !conn.isHeartbeatNil() {
t.Fatalf("heartbeat should be nil")
}
assertNilF(t, err, "should not cause error in Close")
assertNilF(t, conn.rest, "heartbeat should be nil")
}

0 comments on commit c157c4f

Please sign in to comment.