From 4034aaf4b14ccec4662bbbc66e76c8cde8f2ae60 Mon Sep 17 00:00:00 2001 From: Leonidas Vrachnis Date: Thu, 26 Sep 2024 20:20:54 +0200 Subject: [PATCH] use go-sqlmock instead of sqlite --- stats/collectors/sqldb_test.go | 10 ++++++---- stats/otel_collector_test.go | 9 ++++++--- stats/statsd_test.go | 10 ++++++---- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/stats/collectors/sqldb_test.go b/stats/collectors/sqldb_test.go index 56409523..f0cd3a2d 100644 --- a/stats/collectors/sqldb_test.go +++ b/stats/collectors/sqldb_test.go @@ -1,10 +1,9 @@ package collectors_test import ( - "database/sql" "testing" - _ "github.com/mattn/go-sqlite3" // Import the SQLite driver + "github.com/DATA-DOG/go-sqlmock" "github.com/stretchr/testify/require" @@ -14,8 +13,11 @@ import ( ) func TestSQLDatabase(t *testing.T) { - db, err := sql.Open("sqlite3", ":memory:") - require.NoError(t, err) + db, _, err := sqlmock.New() + if err != nil { + t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) + } + defer db.Close() m, err := memstats.New() require.NoError(t, err) diff --git a/stats/otel_collector_test.go b/stats/otel_collector_test.go index c915993c..d0f8ca7c 100644 --- a/stats/otel_collector_test.go +++ b/stats/otel_collector_test.go @@ -2,7 +2,6 @@ package stats_test import ( "context" - "database/sql" "fmt" "net/http" "os" @@ -11,6 +10,7 @@ import ( "testing" "time" + "github.com/DATA-DOG/go-sqlmock" promClient "github.com/prometheus/client_model/go" "github.com/samber/lo" "github.com/stretchr/testify/require" @@ -154,8 +154,11 @@ func TestOTelPeriodicStats(t *testing.T) { }) t.Run("sql collector", func(t *testing.T) { - db, err := sql.Open("sqlite3", ":memory:") - require.NoError(t, err) + db, _, err := sqlmock.New() + if err != nil { + t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) + } + defer db.Close() runTest(t, []expectation{ diff --git a/stats/statsd_test.go b/stats/statsd_test.go index 2036e94a..19a9e3f9 100644 --- a/stats/statsd_test.go +++ b/stats/statsd_test.go @@ -2,7 +2,6 @@ package stats_test import ( "context" - "database/sql" "fmt" "io" "net" @@ -14,7 +13,7 @@ import ( "testing" "time" - _ "github.com/mattn/go-sqlite3" + "github.com/DATA-DOG/go-sqlmock" "github.com/stretchr/testify/require" "github.com/rudderlabs/rudder-go-kit/config" @@ -452,8 +451,11 @@ func TestStatsdRegisterCollector(t *testing.T) { }) t.Run("sql collector", func(t *testing.T) { - db, err := sql.Open("sqlite3", ":memory:") - require.NoError(t, err) + db, _, err := sqlmock.New() + if err != nil { + t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) + } + defer db.Close() runTest(t, []string{