Make default conn string tests runtime dependent

The tests originally only worked on linux. This makes them work on
whatever runtime the tests are running on.
This commit is contained in:
Matthew Wraith 2021-08-05 23:00:59 -07:00
parent 7cc954d8d2
commit 0fd7f79820

View file

@ -4,6 +4,7 @@ import (
"database/sql" "database/sql"
"net/url" "net/url"
"os" "os"
"runtime"
"testing" "testing"
"github.com/amacneil/dbmate/pkg/dbmate" "github.com/amacneil/dbmate/pkg/dbmate"
@ -50,13 +51,24 @@ func TestGetDriver(t *testing.T) {
require.Equal(t, "schema_migrations", drv.migrationsTableName) require.Equal(t, "schema_migrations", drv.migrationsTableName)
} }
func defaultConnString() string {
switch runtime.GOOS {
case "windows":
return "postgres://localhost:5432/foo"
case "linux":
return "postgres://:5432/foo?host=%2Fvar%2Frun%2Fpostgresql"
default:
return "postgres://:5432/foo?host=%2Ftmp"
}
}
func TestConnectionString(t *testing.T) { func TestConnectionString(t *testing.T) {
cases := []struct { cases := []struct {
input string input string
expected string expected string
}{ }{
// defaults // defaults
{"postgres:///foo", "postgres://:5432/foo?host=%2Fvar%2Frun%2Fpostgresql"}, {"postgres:///foo", defaultConnString()},
// support custom url params // support custom url params
{"postgres://bob:secret@myhost:1234/foo?bar=baz", "postgres://bob:secret@myhost:1234/foo?bar=baz"}, {"postgres://bob:secret@myhost:1234/foo?bar=baz", "postgres://bob:secret@myhost:1234/foo?bar=baz"},
// support `host` and `port` via url params // support `host` and `port` via url params
@ -85,11 +97,11 @@ func TestConnectionArgsForDump(t *testing.T) {
expected []string expected []string
}{ }{
// defaults // defaults
{"postgres:///foo", []string{"postgres://:5432/foo?host=%2Fvar%2Frun%2Fpostgresql"}}, {"postgres:///foo", []string{defaultConnString()}},
// support single schema // support single schema
{"postgres:///foo?search_path=foo", []string{"--schema", "foo", "postgres://:5432/foo?host=%2Fvar%2Frun%2Fpostgresql"}}, {"postgres:///foo?search_path=foo", []string{"--schema", "foo", defaultConnString()}},
// support multiple schemas // support multiple schemas
{"postgres:///foo?search_path=foo,public", []string{"--schema", "foo", "--schema", "public", "postgres://:5432/foo?host=%2Fvar%2Frun%2Fpostgresql"}}, {"postgres:///foo?search_path=foo,public", []string{"--schema", "foo", "--schema", "public", defaultConnString()}},
} }
for _, c := range cases { for _, c := range cases {