-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let database.Open() use schemeFromURL as well (#271)
* Let database.Open() use schemeFromURL as well Otherwise it will fail on MySQL DSNs. Moved schemeFromURL into the database package. Also removed databaseSchemeFromURL and sourceSchemeFromURL as they were just calling schemeFromURL. Fixes golang-migrate/migrate#265 (comment) * Moved url functions into internal/url Also merged the test cases. * Add some database tests to improve coverage * Fix suggestions
- Loading branch information
FPiety0521
authored and
FPiety0521
committed
Aug 20, 2019
1 parent
c07a48d
commit 69aa53a
Showing
7 changed files
with
191 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,115 @@ | ||
package database | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
) | ||
|
||
func ExampleDriver() { | ||
// see database/stub for an example | ||
|
||
// database/stub/stub.go has the driver implementation | ||
// database/stub/stub_test.go runs database/testing/test.go:Test | ||
} | ||
|
||
// Using database/stub here is not possible as it | ||
// results in an import cycle. | ||
type mockDriver struct { | ||
url string | ||
} | ||
|
||
func (m *mockDriver) Open(url string) (Driver, error) { | ||
return &mockDriver{ | ||
url: url, | ||
}, nil | ||
} | ||
|
||
func (m *mockDriver) Close() error { | ||
return nil | ||
} | ||
|
||
func (m *mockDriver) Lock() error { | ||
return nil | ||
} | ||
|
||
func (m *mockDriver) Unlock() error { | ||
return nil | ||
} | ||
|
||
func (m *mockDriver) Run(migration io.Reader) error { | ||
return nil | ||
} | ||
|
||
func (m *mockDriver) SetVersion(version int, dirty bool) error { | ||
return nil | ||
} | ||
|
||
func (m *mockDriver) Version() (version int, dirty bool, err error) { | ||
return 0, false, nil | ||
} | ||
|
||
func (m *mockDriver) Drop() error { | ||
return nil | ||
} | ||
|
||
func TestRegisterTwice(t *testing.T) { | ||
Register("mock", &mockDriver{}) | ||
|
||
var err interface{} | ||
func() { | ||
defer func() { | ||
err = recover() | ||
}() | ||
Register("mock", &mockDriver{}) | ||
}() | ||
|
||
if err == nil { | ||
t.Fatal("expected a panic when calling Register twice") | ||
} | ||
} | ||
|
||
func TestOpen(t *testing.T) { | ||
// Make sure the driver is registered. | ||
// But if the previous test already registered it just ignore the panic. | ||
// If we don't do this it will be impossible to run this test standalone. | ||
func() { | ||
defer func() { | ||
_ = recover() | ||
}() | ||
Register("mock", &mockDriver{}) | ||
}() | ||
|
||
cases := []struct { | ||
url string | ||
err bool | ||
}{ | ||
{ | ||
"mock://user:pass@tcp(host:1337)/db", | ||
false, | ||
}, | ||
{ | ||
"unknown://bla", | ||
true, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.url, func(t *testing.T) { | ||
d, err := Open(c.url) | ||
|
||
if err == nil { | ||
if c.err { | ||
t.Fatal("expected an error for an unknown driver") | ||
} else { | ||
if md, ok := d.(*mockDriver); !ok { | ||
t.Fatalf("expected *mockDriver got %T", d) | ||
} else if md.url != c.url { | ||
t.Fatalf("expected %q got %q", c.url, md.url) | ||
} | ||
} | ||
} else if !c.err { | ||
t.Fatalf("did not expect %q", err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package url | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
) | ||
|
||
var errNoScheme = errors.New("no scheme") | ||
var errEmptyURL = errors.New("URL cannot be empty") | ||
|
||
// schemeFromURL returns the scheme from a URL string | ||
func SchemeFromURL(url string) (string, error) { | ||
if url == "" { | ||
return "", errEmptyURL | ||
} | ||
|
||
i := strings.Index(url, ":") | ||
|
||
// No : or : is the first character. | ||
if i < 1 { | ||
return "", errNoScheme | ||
} | ||
|
||
return url[0:i], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package url | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestSchemeFromUrl(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
urlStr string | ||
expected string | ||
expectErr error | ||
}{ | ||
{ | ||
name: "Simple", | ||
urlStr: "protocol://path", | ||
expected: "protocol", | ||
}, | ||
{ | ||
// See issue #264 | ||
name: "MySQLWithPort", | ||
urlStr: "mysql://user:pass@tcp(host:1337)/db", | ||
expected: "mysql", | ||
}, | ||
{ | ||
name: "Empty", | ||
urlStr: "", | ||
expectErr: errEmptyURL, | ||
}, | ||
{ | ||
name: "NoScheme", | ||
urlStr: "hello", | ||
expectErr: errNoScheme, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
s, err := SchemeFromURL(tc.urlStr) | ||
if err != tc.expectErr { | ||
t.Fatalf("expected %q, but received %q", tc.expectErr, err) | ||
} | ||
if s != tc.expected { | ||
t.Fatalf("expected %q, but received %q", tc.expected, s) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.