diff options
| author | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-06 11:15:53 -0500 |
|---|---|---|
| committer | Eugene Triguba <eugenetriguba@gmail.com> | 2024-01-06 11:15:53 -0500 |
| commit | 3d66d23b7f1f8d496711f01a164ac1d7717527ca (patch) | |
| tree | b4ce8a4deda41c57b1ab13ac4e89bfb9e6978cf0 /internal/storage/storage_test.go | |
| parent | Add to changelog (diff) | |
| download | cli-3d66d23b7f1f8d496711f01a164ac1d7717527ca.zip | |
Add database integration tests
Diffstat (limited to 'internal/storage/storage_test.go')
| -rw-r--r-- | internal/storage/storage_test.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/internal/storage/storage_test.go b/internal/storage/storage_test.go new file mode 100644 index 0000000..53f7b49 --- /dev/null +++ b/internal/storage/storage_test.go @@ -0,0 +1,76 @@ +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" +) + +func TestDBConnectWithPostgres(t *testing.T) { + cfg, err := dbtest.NewTestConnectionConfig("postgres") + if err != nil { + t.Fatal(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, + ) + } + defer conn.Close() + + _, err = conn.Exec("SELECT 1;") + if err != nil { + t.Fatalf("could not execute query against postgres db: %s", 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) + } +} + +func TestDBConnectUnsupportedDriver(t *testing.T) { + cfg, err := dbtest.NewTestConnectionConfig("redis") + if err != nil { + t.Fatal(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) + } +} + +func TestDBConnectionString(t *testing.T) { + cfg := configloader.Config{ + MigrationsDir: "", + Connection: configloader.ConnectionConfig{ + Driver: "postgres", + Host: "db1", + Port: 5432, + User: "testuser", + Password: "supersecretpassword", + DBName: "testdb", + }, + } + expectedConnectionString := fmt.Sprintf( + "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", + cfg.Connection.Host, cfg.Connection.Port, cfg.Connection.User, + cfg.Connection.Password, cfg.Connection.DBName, + ) + + cs := storage.DBConnectionString(&cfg.Connection) + + if cs != expectedConnectionString { + t.Fatalf("Expected %s, got %s", expectedConnectionString, cs) + } +} |