dialect_postgres_test.go 1.4 KB

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