Beautify error handling in tests
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/gruntwork-io/terratest/modules/terraform"
|
"github.com/gruntwork-io/terratest/modules/terraform"
|
||||||
"github.com/gruntwork-io/terratest/modules/test-structure"
|
"github.com/gruntwork-io/terratest/modules/test-structure"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -92,10 +93,7 @@ func TestCloudSQLMySql(t *testing.T) {
|
|||||||
logger.Logf(t, "Connecting to: %s", publicIp)
|
logger.Logf(t, "Connecting to: %s", publicIp)
|
||||||
db, err := sql.Open("mysql",
|
db, err := sql.Open("mysql",
|
||||||
connectionString)
|
connectionString)
|
||||||
|
require.NoError(t, err, "Failed to open DB connection")
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Failed to open DB connection: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure we clean up properly
|
// Make sure we clean up properly
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
@@ -121,21 +119,16 @@ func TestCloudSQLMySql(t *testing.T) {
|
|||||||
// Insert data to check that our auto-increment flags worked
|
// Insert data to check that our auto-increment flags worked
|
||||||
logger.Logf(t, "Insert data: %s", MYSQL_INSERT_TEST_ROW)
|
logger.Logf(t, "Insert data: %s", MYSQL_INSERT_TEST_ROW)
|
||||||
stmt, err := db.Prepare(MYSQL_INSERT_TEST_ROW)
|
stmt, err := db.Prepare(MYSQL_INSERT_TEST_ROW)
|
||||||
if err != nil {
|
require.NoError(t, err, "Failed to prepare statement")
|
||||||
t.Fatalf("Failed to prepare statement: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute the statement
|
// Execute the statement
|
||||||
res, err := stmt.Exec("Grunt")
|
res, err := stmt.Exec("Grunt")
|
||||||
if err != nil {
|
require.NoError(t, err, "Failed to execute statement")
|
||||||
t.Fatalf("Failed to execute statement: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the last insert id
|
// Get the last insert id
|
||||||
lastId, err := res.LastInsertId()
|
lastId, err := res.LastInsertId()
|
||||||
if err != nil {
|
require.NoError(t, err, "Failed to get last insert id")
|
||||||
t.Fatalf("Failed to get last insert id: %v", err)
|
|
||||||
}
|
|
||||||
// Since we set the auto increment to 5, modulus should always be 0
|
// Since we set the auto increment to 5, modulus should always be 0
|
||||||
assert.Equal(t, int64(0), int64(lastId%5))
|
assert.Equal(t, int64(0), int64(lastId%5))
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user