mydb/adapter/mongo/connection_test.go

115 lines
2.3 KiB
Go
Raw Normal View History

2023-09-18 15:15:42 +08:00
package mongo
import (
"testing"
)
func TestConnectionURL(t *testing.T) {
c := ConnectionURL{}
// Default connection string is only the protocol.
if c.String() != "" {
t.Fatal(`Expecting default connectiong string to be empty, got:`, c.String())
}
// Adding a database name.
c.Database = "myfilename"
if c.String() != "mongodb://myfilename" {
t.Fatal(`Test failed, got:`, c.String())
}
// Adding an option.
c.Options = map[string]string{
"cache": "foobar",
"mode": "ro",
}
// Adding username and password
c.User = "user"
c.Password = "pass"
// Setting host.
c.Host = "localhost"
if c.String() != "mongodb://user:pass@localhost/myfilename?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}
// Setting host and port.
c.Host = "localhost:27017"
if c.String() != "mongodb://user:pass@localhost:27017/myfilename?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}
// Setting cluster.
c.Host = "localhost,1.2.3.4,example.org:1234"
if c.String() != "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/myfilename?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}
// Setting another database.
c.Database = "another_database"
if c.String() != "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/another_database?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
}
}
func TestParseConnectionURL(t *testing.T) {
var u ConnectionURL
var s string
var err error
s = "mongodb:///mydatabase"
if u, err = ParseURL(s); err != nil {
t.Fatal(err)
}
if u.Database != "mydatabase" {
t.Fatal("Failed to parse database.")
}
s = "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/another_database?cache=foobar&mode=ro"
if u, err = ParseURL(s); err != nil {
t.Fatal(err)
}
if u.Database != "another_database" {
t.Fatal("Failed to get database.")
}
if u.Options["cache"] != "foobar" {
t.Fatal("Expecting option.")
}
if u.Options["mode"] != "ro" {
t.Fatal("Expecting option.")
}
if u.User != "user" {
t.Fatal("Expecting user.")
}
if u.Password != "pass" {
t.Fatal("Expecting password.")
}
if u.Host != "localhost,1.2.3.4,example.org:1234" {
t.Fatal("Expecting host.")
}
s = "http://example.org"
if _, err = ParseURL(s); err == nil {
t.Fatal("Expecting error.")
}
}