2023-11-01 23:40:39 +03:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2023-11-02 01:31:26 +03:00
|
|
|
"net/url"
|
2023-11-01 23:40:39 +03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParseDSN(t *testing.T) {
|
|
|
|
cfg, err := ParseDSN("postgres://username:p@ssword#@host:12345/dbname?key1=val2&key2=val2")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if cfg.Password != "p@ssword#" {
|
|
|
|
t.Fatalf("parsing error")
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 01:31:26 +03:00
|
|
|
|
|
|
|
func TestFormatDSN(t *testing.T) {
|
|
|
|
src := "postgres://username:p@ssword#@host:12345/dbname?key1=val2&key2=val2"
|
|
|
|
cfg, err := ParseDSN(src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
dst, err := url.PathUnescape(cfg.FormatDSN())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if src != dst {
|
|
|
|
t.Fatalf("\n%s\n%s", src, dst)
|
|
|
|
}
|
|
|
|
}
|