diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-06 16:23:02 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-06 16:23:02 -0500 |
| commit | 263ce3e4b368a50efee1a53af11a76a12af0a119 (patch) | |
| tree | 8917754f3b43830604f6ad4c7115934ed633f1d8 /internal/storage/db_test.go | |
| parent | Fix syntax (diff) | |
| download | cli-263ce3e4b368a50efee1a53af11a76a12af0a119.zip | |
Break up migration repo into fs and db with a service
Diffstat (limited to 'internal/storage/db_test.go')
| -rw-r--r-- | internal/storage/db_test.go | 55 |
1 files changed, 55 insertions, 0 deletions
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) +} |