dialect_postgres_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package xorm
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/jackc/pgx/stdlib"
  6. "github.com/xormplus/core"
  7. )
  8. func TestParsePostgres(t *testing.T) {
  9. tests := []struct {
  10. in string
  11. expected string
  12. valid bool
  13. }{
  14. {"postgres://auser:password@localhost:5432/db?sslmode=disable", "db", true},
  15. {"postgresql://auser:password@localhost:5432/db?sslmode=disable", "db", true},
  16. {"postg://auser:password@localhost:5432/db?sslmode=disable", "db", false},
  17. //{"postgres://auser:pass with space@localhost:5432/db?sslmode=disable", "db", true},
  18. //{"postgres:// auser : password@localhost:5432/db?sslmode=disable", "db", true},
  19. {"postgres://%20auser%20:pass%20with%20space@localhost:5432/db?sslmode=disable", "db", true},
  20. //{"postgres://auser:パスワード@localhost:5432/データベース?sslmode=disable", "データベース", true},
  21. {"dbname=db sslmode=disable", "db", true},
  22. {"user=auser password=password dbname=db sslmode=disable", "db", true},
  23. {"", "db", false},
  24. {"dbname=db =disable", "db", false},
  25. }
  26. driver := core.QueryDriver("postgres")
  27. for _, test := range tests {
  28. uri, err := driver.Parse("postgres", test.in)
  29. if err != nil && test.valid {
  30. t.Errorf("%q got unexpected error: %s", test.in, err)
  31. } else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
  32. t.Errorf("%q got: %#v want: %#v", test.in, uri.DbName, test.expected)
  33. }
  34. }
  35. }
  36. func TestParsePgx(t *testing.T) {
  37. tests := []struct {
  38. in string
  39. expected string
  40. valid bool
  41. }{
  42. {"postgres://auser:password@localhost:5432/db?sslmode=disable", "db", true},
  43. {"postgresql://auser:password@localhost:5432/db?sslmode=disable", "db", true},
  44. {"postg://auser:password@localhost:5432/db?sslmode=disable", "db", false},
  45. //{"postgres://auser:pass with space@localhost:5432/db?sslmode=disable", "db", true},
  46. //{"postgres:// auser : password@localhost:5432/db?sslmode=disable", "db", true},
  47. {"postgres://%20auser%20:pass%20with%20space@localhost:5432/db?sslmode=disable", "db", true},
  48. //{"postgres://auser:パスワード@localhost:5432/データベース?sslmode=disable", "データベース", true},
  49. {"dbname=db sslmode=disable", "db", true},
  50. {"user=auser password=password dbname=db sslmode=disable", "db", true},
  51. {"", "db", false},
  52. {"dbname=db =disable", "db", false},
  53. }
  54. driver := core.QueryDriver("pgx")
  55. for _, test := range tests {
  56. uri, err := driver.Parse("pgx", test.in)
  57. if err != nil && test.valid {
  58. t.Errorf("%q got unexpected error: %s", test.in, err)
  59. } else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
  60. t.Errorf("%q got: %#v want: %#v", test.in, uri.DbName, test.expected)
  61. }
  62. // Register DriverConfig
  63. drvierConfig := stdlib.DriverConfig{}
  64. stdlib.RegisterDriverConfig(&drvierConfig)
  65. uri, err = driver.Parse("pgx",
  66. drvierConfig.ConnectionString(test.in))
  67. if err != nil && test.valid {
  68. t.Errorf("%q got unexpected error: %s", test.in, err)
  69. } else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
  70. t.Errorf("%q got: %#v want: %#v", test.in, uri.DbName, test.expected)
  71. }
  72. }
  73. }