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 | |
| parent | Fix syntax (diff) | |
| download | cli-263ce3e4b368a50efee1a53af11a76a12af0a119.zip | |
Break up migration repo into fs and db with a service
Diffstat (limited to 'internal/storage')
| -rw-r--r-- | internal/storage/db.go (renamed from internal/storage/storage.go) | 0 | ||||
| -rw-r--r-- | internal/storage/db_test.go (renamed from internal/storage/storage_test.go) | 41 | ||||
| -rw-r--r-- | internal/storage/dbtest/dbtest.go | 24 |
3 files changed, 36 insertions, 29 deletions
diff --git a/internal/storage/storage.go b/internal/storage/db.go index 25db283..25db283 100644 --- a/internal/storage/storage.go +++ b/internal/storage/db.go diff --git a/internal/storage/storage_test.go b/internal/storage/db_test.go index 3b36a34..2583cfd 100644 --- a/internal/storage/storage_test.go +++ b/internal/storage/db_test.go @@ -2,52 +2,37 @@ package storage_test import ( "fmt" - "strings" "testing" "github.com/eugenetriguba/bolt/internal/configloader" - "github.com/eugenetriguba/bolt/internal/dbtest" "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") - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) conn, err := storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - if err != nil { - t.Fatalf("could not connect to postgres db using %s: %s", - storage.DBConnectionString(cfg), - err, - ) - } + assert.NilError(t, err) defer conn.Close() _, err = conn.Exec("SELECT 1;") - if err != nil { - t.Fatalf("could not execute query against postgres db: %s", err) - } + assert.NilError(t, err) } func TestDBConnectMalformedConnectionString(t *testing.T) { _, err := storage.DBConnect("postgres", "pizza=123") - if err == nil || (err != nil && !strings.Contains(err.Error(), "connection refused")) { - t.Fatalf("expected connection error, got %s", err) - } + assert.ErrorContains(t, err, "connection refused") } func TestDBConnectUnsupportedDriver(t *testing.T) { cfg, err := dbtest.NewTestConnectionConfig("redis") - if err != nil { - t.Fatal(err) - } + assert.NilError(t, err) _, err = storage.DBConnect(cfg.Driver, storage.DBConnectionString(cfg)) - if err == nil || (err != nil && !strings.Contains(err.Error(), `unknown driver "redis"`)) { - t.Fatalf("expected bad driver, got %s", err) - } + assert.ErrorContains(t, err, `unknown driver "redis"`) } func TestDBConnectionString(t *testing.T) { @@ -59,14 +44,12 @@ func TestDBConnectionString(t *testing.T) { 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, ) - - cs := storage.DBConnectionString(&cfg) - - if cs != expectedConnectionString { - t.Fatalf("Expected %s, got %s", expectedConnectionString, cs) - } + assert.Equal(t, cs, expectedConnectionString) } diff --git a/internal/storage/dbtest/dbtest.go b/internal/storage/dbtest/dbtest.go new file mode 100644 index 0000000..c53c04a --- /dev/null +++ b/internal/storage/dbtest/dbtest.go @@ -0,0 +1,24 @@ +package dbtest + +import ( + "os" + "strconv" + + "github.com/eugenetriguba/bolt/internal/configloader" +) + +func NewTestConnectionConfig(driver string) (*configloader.ConnectionConfig, error) { + port, err := strconv.Atoi(os.Getenv("DB_PORT")) + if err != nil { + return nil, err + } + + return &configloader.ConnectionConfig{ + Driver: driver, + DBName: os.Getenv("DB_NAME"), + Host: os.Getenv("DB_HOST"), + Port: port, + User: os.Getenv("DB_USERNAME"), + Password: os.Getenv("DB_PASSWORD"), + }, nil +} |