dialect_postgres_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package xorm
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/xormplus/core"
  6. )
  7. func TestParsePostgres(t *testing.T) {
  8. tests := []struct {
  9. in string
  10. expected string
  11. valid bool
  12. }{
  13. {"postgres://auser:password@localhost:5432/db?sslmode=disable", "db", true},
  14. {"postgresql://auser:password@localhost:5432/db?sslmode=disable", "db", true},
  15. {"postg://auser:password@localhost:5432/db?sslmode=disable", "db", false},
  16. //{"postgres://auser:pass with space@localhost:5432/db?sslmode=disable", "db", true},
  17. //{"postgres:// auser : password@localhost:5432/db?sslmode=disable", "db", true},
  18. {"postgres://%20auser%20:pass%20with%20space@localhost:5432/db?sslmode=disable", "db", true},
  19. //{"postgres://auser:パスワード@localhost:5432/データベース?sslmode=disable", "データベース", true},
  20. {"dbname=db sslmode=disable", "db", true},
  21. {"user=auser password=password dbname=db sslmode=disable", "db", true},
  22. {"", "db", false},
  23. {"dbname=db =disable", "db", false},
  24. }
  25. driver := core.QueryDriver("postgres")
  26. for _, test := range tests {
  27. uri, err := driver.Parse("postgres", test.in)
  28. if err != nil && test.valid {
  29. t.Errorf("%q got unexpected error: %s", test.in, err)
  30. } else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
  31. t.Errorf("%q got: %#v want: %#v", test.in, uri.DbName, test.expected)
  32. }
  33. }
  34. }