|
| 1 | +package postgresql |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "sort" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/blang/semver" |
| 10 | +) |
| 11 | + |
| 12 | +func TestConfigConnParams(t *testing.T) { |
| 13 | + var tests = []struct { |
| 14 | + input *Config |
| 15 | + want []string |
| 16 | + }{ |
| 17 | + {&Config{ConnectTimeoutSec: 10}, []string{"connect_timeout=10"}}, |
| 18 | + {&Config{Scheme: "postgres", SSLMode: "disable"}, []string{"connect_timeout=0", "sslmode=disable"}}, |
| 19 | + {&Config{Scheme: "awspostgres", SSLMode: "disable"}, []string{"connect_timeout=0"}}, |
| 20 | + {&Config{ExpectedVersion: semver.MustParse("9.0.0"), ApplicationName: "Terraform provider"}, []string{"connect_timeout=0", "fallback_application_name=Terraform+provider"}}, |
| 21 | + {&Config{ExpectedVersion: semver.MustParse("8.0.0"), ApplicationName: "Terraform provider"}, []string{"connect_timeout=0"}}, |
| 22 | + {&Config{SSLClientCert: &ClientCertificateConfig{CertificatePath: "/path/to/public-certificate.pem", KeyPath: "/path/to/private-key.pem"}}, []string{"connect_timeout=0", "sslcert=%2Fpath%2Fto%2Fpublic-certificate.pem", "sslkey=%2Fpath%2Fto%2Fprivate-key.pem"}}, |
| 23 | + {&Config{SSLRootCertPath: "/path/to/root.pem"}, []string{"connect_timeout=0", "sslrootcert=%2Fpath%2Fto%2Froot.pem"}}, |
| 24 | + } |
| 25 | + |
| 26 | + for _, test := range tests { |
| 27 | + |
| 28 | + connParams := test.input.connParams() |
| 29 | + |
| 30 | + sort.Strings(connParams) |
| 31 | + sort.Strings(test.want) |
| 32 | + |
| 33 | + if !reflect.DeepEqual(connParams, test.want) { |
| 34 | + t.Errorf("Config.connParams(%+v) returned %#v, want %#v", test.input, connParams, test.want) |
| 35 | + } |
| 36 | + |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestConfigConnStr(t *testing.T) { |
| 41 | + var tests = []struct { |
| 42 | + input *Config |
| 43 | + wantDbURL string |
| 44 | + wantDbParams []string |
| 45 | + }{ |
| 46 | + {&Config{Scheme: "postgres", Host: "localhost", Port: 5432, Username: "postgres_user", Password: "postgres_password", SSLMode: "disable"}, "postgres://postgres_user:postgres_password@localhost:5432/postgres", []string{"connect_timeout=0", "sslmode=disable"}}, |
| 47 | + } |
| 48 | + |
| 49 | + for _, test := range tests { |
| 50 | + |
| 51 | + connStr := test.input.connStr("postgres") |
| 52 | + |
| 53 | + splitConnStr := strings.Split(connStr, "?") |
| 54 | + |
| 55 | + if splitConnStr[0] != test.wantDbURL { |
| 56 | + t.Errorf("Config.connStr(%+v) returned %#v, want %#v", test.input, splitConnStr[0], test.wantDbURL) |
| 57 | + } |
| 58 | + |
| 59 | + connParams := strings.Split(splitConnStr[1], "&") |
| 60 | + |
| 61 | + sort.Strings(connParams) |
| 62 | + sort.Strings(test.wantDbParams) |
| 63 | + |
| 64 | + if !reflect.DeepEqual(connParams, test.wantDbParams) { |
| 65 | + t.Errorf("Config.connStr(%+v) returned %#v, want %#v", test.input, connParams, test.wantDbParams) |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | +} |
0 commit comments