package db import ( "strings" "testing" ) func TestDBName(t *testing.T) { for sub, want := range map[string]string{"alice": "blog_alice", "my-blog": "blog_my_blog", "a-b-c": "blog_a_b_c", "www": "blog_www"} { if got := DBName(sub); got != want || !dbNameRe.MatchString(got) { t.Errorf("DBName(%q) = %q, want %q", sub, got, want) } } // the longest subdomain the admin form accepts still fits a Postgres identifier if n := len(DBName(strings.Repeat("a-", 29))); n > 63 { // 58 chars, the admin form's limit t.Errorf("database name too long: %d", n) } } func TestWithDatabase(t *testing.T) { got := withDatabase("postgres://u:p@db:5432/blogspace?sslmode=disable", "blog_alice") if want := "postgres://u:p@db:5432/blog_alice?sslmode=disable"; got != want { t.Errorf("got %q, want %q", got, want) } }