Skip to content

Commit

Permalink
test(connection): add test cases to test connection strings validation
Browse files Browse the repository at this point in the history
  • Loading branch information
danvergara committed Apr 22, 2021
1 parent c69bcf3 commit 45636c7
Showing 1 changed file with 193 additions and 2 deletions.
195 changes: 193 additions & 2 deletions pkg/connection/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func TestBuildConnectionFromOpts(t *testing.T) {
given given
want want
}{
// postgres
{
name: "valid postgres localhost",
given: given{
Expand Down Expand Up @@ -84,6 +85,69 @@ func TestBuildConnectionFromOpts(t *testing.T) {
err: ErrInvalidUPostgresRLFormat,
},
},
// mysql
{
name: "valid mysql localhost",
given: given{
opts: command.Options{
Driver: "mysql",
URL: "mysql://user:password@tcp(localhost:3306)/db",
},
},
want: want{
uri: "mysql://user:password@tcp(localhost:3306)/db",
},
},
{
name: "valid mysql localhost with params",
given: given{
opts: command.Options{
Driver: "mysql",
URL: "mysql://user:password@tcp(localhost:3306)/db?charset=utf8",
},
},
want: want{
uri: "mysql://user:password@tcp(localhost:3306)/db?charset=utf8",
},
},
{
name: "valid mysql remote url",
given: given{
opts: command.Options{
Driver: "mysql",
URL: "mysql://user:password@tcp(your-amazonaws-uri.com:3306)/dbname",
},
},
want: want{
uri: "mysql://user:password@tcp(your-amazonaws-uri.com:3306)/dbname",
},
},
{
name: "error misspelled mysql",
given: given{
opts: command.Options{
Driver: "mysql",
URL: "mysq://user:password@tcp(localhost:3306)/db?charset=utf8",
},
},
want: want{
hasError: true,
err: ErrInvalidUMySQLRLFormat,
},
},
{
name: "error invalid url",
given: given{
opts: command.Options{
Driver: "mysql",
URL: "not-a-url",
},
},
want: want{
hasError: true,
err: ErrInvalidUMySQLRLFormat,
},
},
}

for _, tc := range cases {
Expand Down Expand Up @@ -122,7 +186,70 @@ func TestFormatPostgresURL(t *testing.T) {
name string
given given
want want
}{}
}{
{
name: "valid postgres localhost",
given: given{
opts: command.Options{
Driver: "postgres",
URL: "postgres://user:password@localhost:5432/db?sslmode=disable",
},
},
want: want{
uri: "postgres://user:password@localhost:5432/db?sslmode=disable",
},
},
{
name: "valid postgres localhost but add sslmode",
given: given{
opts: command.Options{
Driver: "postgres",
URL: "postgres://user:password@localhost:5432/db",
},
},
want: want{
uri: "postgres://user:password@localhost:5432/db?sslmode=disable",
},
},
{
name: "valid postgres localhost postgresql as protocol",
given: given{
opts: command.Options{
Driver: "postgres",
URL: "postgresql://user:password@localhost:5432/db",
},
},
want: want{
uri: "postgresql://user:password@localhost:5432/db?sslmode=disable",
},
},
{
name: "error misspelled postgres",
given: given{
opts: command.Options{
Driver: "postgres",
URL: "potgre://user:password@localhost:5432/db",
},
},
want: want{
hasError: true,
err: ErrInvalidUPostgresRLFormat,
},
},
{
name: "error invalid url",
given: given{
opts: command.Options{
Driver: "postgres",
URL: "not-a-url",
},
},
want: want{
hasError: true,
err: ErrInvalidUPostgresRLFormat,
},
},
}

for _, tc := range cases {
tc := tc
Expand Down Expand Up @@ -159,12 +286,76 @@ func TestFormatMySQLURL(t *testing.T) {
name string
given given
want want
}{}
}{
{
name: "valid mysql localhost",
given: given{
opts: command.Options{
Driver: "mysql",
URL: "mysql://user:password@tcp(localhost:3306)/db",
},
},
want: want{
uri: "mysql://user:password@tcp(localhost:3306)/db",
},
},
{
name: "valid mysql localhost with params",
given: given{
opts: command.Options{
Driver: "mysql",
URL: "mysql://user:password@tcp(localhost:3306)/db?charset=utf8",
},
},
want: want{
uri: "mysql://user:password@tcp(localhost:3306)/db?charset=utf8",
},
},
{
name: "valid mysql remote url",
given: given{
opts: command.Options{
Driver: "mysql",
URL: "mysql://user:password@tcp(your-amazonaws-uri.com:3306)/dbname",
},
},
want: want{
uri: "mysql://user:password@tcp(your-amazonaws-uri.com:3306)/dbname",
},
},
{
name: "error misspelled mysql",
given: given{
opts: command.Options{
Driver: "mysql",
URL: "mysq://user:password@tcp(localhost:3306)/db?charset=utf8",
},
},
want: want{
hasError: true,
err: ErrInvalidUMySQLRLFormat,
},
},
{
name: "error invalid url",
given: given{
opts: command.Options{
Driver: "mysql",
URL: "not-a-url",
},
},
want: want{
hasError: true,
err: ErrInvalidUMySQLRLFormat,
},
},
}

for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

uri, err := formatMySQLURL(tc.given.opts)

if tc.want.hasError {
Expand Down

0 comments on commit 45636c7

Please sign in to comment.