Skip to content

Commit

Permalink
add test cases for internal/sql/connection.go (#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminwilcox committed Sep 18, 2024
1 parent a251276 commit cceaa93
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions internal/sql/e2e_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package sql

import (
"context"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
)

func TestE2E(t *testing.T) {
Expand Down Expand Up @@ -51,3 +57,38 @@ func TestCreateDBConnector(t *testing.T) {
})
}
}

func TestSQLiteConnector_Success(t *testing.T) {
tempFile, err := os.CreateTemp("", "testdb_*.db")
require.NoError(t, err, "Should create a temporary SQLite file successfully")
defer os.Remove(tempFile.Name())

connector := SQLiteConnector{
dbPath: tempFile.Name(),
}

ctx := context.Background()
db, err := connector.Connect(ctx)

require.NoError(t, err, "Should connect to SQLite database without error")
assert.NotNil(t, db, "Database connection should not be nil")
assert.IsType(t, &gorm.DB{}, db, "Should return a Gorm DB instance")

sqlDB, err := db.DB()
require.NoError(t, err, "Should get underlying SQL DB from Gorm instance")
require.NoError(t, sqlDB.Ping(), "SQLite database should be reachable")
}

func TestSQLiteConnector_Failed(t *testing.T) {
invalidDBPath := "/invalid_path/test.db" // Pass an invalid database path to trigger an error
connector := SQLiteConnector{
dbPath: invalidDBPath,
}

ctx := context.Background()
db, err := connector.Connect(ctx)

require.Error(t, err, "Should fail to connect to SQLite database")
assert.Nil(t, db, "Database connection should be nil when there is an error")
assert.Contains(t, err.Error(), "failed to connect to SQLite database")
}

0 comments on commit cceaa93

Please sign in to comment.