From 263ce3e4b368a50efee1a53af11a76a12af0a119 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 6 Jan 2024 16:23:02 -0500 Subject: Break up migration repo into fs and db with a service --- internal/storage/db_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 internal/storage/db_test.go (limited to 'internal/storage/db_test.go') diff --git a/internal/storage/db_test.go b/internal/storage/db_test.go new file mode 100644 index 0000000..2583cfd --- /dev/null +++ b/internal/storage/db_test.go @@ -0,0 +1,55 @@ +package storage_test + +import ( + "fmt" + "testing" + + "github.com/eugenetriguba/bolt/internal/configloader" + "github.com/eugenetriguba/bolt/internal/storage" + "github.com/eugenetriguba/bolt/internal/storage/dbtest" + "gotest.tools/v3/assert" +) + +func TestDBConnectWithPostgres(t *testing.T) { + cfg, err := dbtest.NewTestConnectionConfig("postgres") + assert.NilError(t, err) + + conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) + assert.NilError(t, err) + defer conn.Close() + + _, err = conn.Exec("SELECT 1;") + assert.NilError(t, err) +} + +func TestDBConnectMalformedConnectionString(t *testing.T) { + _, err := storage.DBConnect("postgres", "pizza=123") + assert.ErrorContains(t, err, "connection refused") +} + +func TestDBConnectUnsupportedDriver(t *testing.T) { + cfg, err := dbtest.NewTestConnectionConfig("redis") + assert.NilError(t, err) + + _, err = storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) + assert.ErrorContains(t, err, `unknown driver "redis"`) +} + +func TestDBConnectionString(t *testing.T) { + cfg := configloader.ConnectionConfig{ + Driver: "postgres", + Host: "db1", + Port: 5432, + User: "testuser", + Password: "supersecretpassword", + DBName: "testdb", + } + + cs := storage.DBConnectionString(&cfg) + + expectedConnectionString := fmt.Sprintf( + "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", + cfg.Host, cfg.Port, cfg.User, cfg.Password, cfg.DBName, + ) + assert.Equal(t, cs, expectedConnectionString) +} -- cgit v1.2.3