dialect_postgres_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package xorm
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/jackc/pgx/stdlib"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/xormplus/core"
  8. )
  9. func TestParsePostgres(t *testing.T) {
  10. tests := []struct {
  11. in string
  12. expected string
  13. valid bool
  14. }{
  15. {"postgres://auser:password@localhost:5432/db?sslmode=disable", "db", true},
  16. {"postgresql://auser:password@localhost:5432/db?sslmode=disable", "db", true},
  17. {"postg://auser:password@localhost:5432/db?sslmode=disable", "db", false},
  18. //{"postgres://auser:pass with space@localhost:5432/db?sslmode=disable", "db", true},
  19. //{"postgres:// auser : password@localhost:5432/db?sslmode=disable", "db", true},
  20. {"postgres://%20auser%20:pass%20with%20space@localhost:5432/db?sslmode=disable", "db", true},
  21. //{"postgres://auser:パスワード@localhost:5432/データベース?sslmode=disable", "データベース", true},
  22. {"dbname=db sslmode=disable", "db", true},
  23. {"user=auser password=password dbname=db sslmode=disable", "db", true},
  24. {"", "db", false},
  25. {"dbname=db =disable", "db", false},
  26. }
  27. driver := core.QueryDriver("postgres")
  28. for _, test := range tests {
  29. uri, err := driver.Parse("postgres", test.in)
  30. if err != nil && test.valid {
  31. t.Errorf("%q got unexpected error: %s", test.in, err)
  32. } else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
  33. t.Errorf("%q got: %#v want: %#v", test.in, uri.DbName, test.expected)
  34. }
  35. }
  36. }
  37. func TestParsePgx(t *testing.T) {
  38. tests := []struct {
  39. in string
  40. expected string
  41. valid bool
  42. }{
  43. {"postgres://auser:password@localhost:5432/db?sslmode=disable", "db", true},
  44. {"postgresql://auser:password@localhost:5432/db?sslmode=disable", "db", true},
  45. {"postg://auser:password@localhost:5432/db?sslmode=disable", "db", false},
  46. //{"postgres://auser:pass with space@localhost:5432/db?sslmode=disable", "db", true},
  47. //{"postgres:// auser : password@localhost:5432/db?sslmode=disable", "db", true},
  48. {"postgres://%20auser%20:pass%20with%20space@localhost:5432/db?sslmode=disable", "db", true},
  49. //{"postgres://auser:パスワード@localhost:5432/データベース?sslmode=disable", "データベース", true},
  50. {"dbname=db sslmode=disable", "db", true},
  51. {"user=auser password=password dbname=db sslmode=disable", "db", true},
  52. {"", "db", false},
  53. {"dbname=db =disable", "db", false},
  54. }
  55. driver := core.QueryDriver("pgx")
  56. for _, test := range tests {
  57. uri, err := driver.Parse("pgx", test.in)
  58. if err != nil && test.valid {
  59. t.Errorf("%q got unexpected error: %s", test.in, err)
  60. } else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
  61. t.Errorf("%q got: %#v want: %#v", test.in, uri.DbName, test.expected)
  62. }
  63. // Register DriverConfig
  64. drvierConfig := stdlib.DriverConfig{}
  65. stdlib.RegisterDriverConfig(&drvierConfig)
  66. uri, err = driver.Parse("pgx",
  67. drvierConfig.ConnectionString(test.in))
  68. if err != nil && test.valid {
  69. t.Errorf("%q got unexpected error: %s", test.in, err)
  70. } else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
  71. t.Errorf("%q got: %#v want: %#v", test.in, uri.DbName, test.expected)
  72. }
  73. }
  74. }
  75. func TestGetIndexColName(t *testing.T) {
  76. t.Run("Index", func(t *testing.T) {
  77. s := "CREATE INDEX test2_mm_idx ON test2 (major);"
  78. colNames := getIndexColName(s)
  79. assert.Equal(t, []string{"major"}, colNames)
  80. })
  81. t.Run("Multicolumn indexes", func(t *testing.T) {
  82. s := "CREATE INDEX test2_mm_idx ON test2 (major, minor);"
  83. colNames := getIndexColName(s)
  84. assert.Equal(t, []string{"major", "minor"}, colNames)
  85. })
  86. t.Run("Indexes and ORDER BY", func(t *testing.T) {
  87. s := "CREATE INDEX test2_mm_idx ON test2 (major NULLS FIRST, minor DESC NULLS LAST);"
  88. colNames := getIndexColName(s)
  89. assert.Equal(t, []string{"major", "minor"}, colNames)
  90. })
  91. t.Run("Combining Multiple Indexes", func(t *testing.T) {
  92. s := "CREATE INDEX test2_mm_cm_idx ON public.test2 USING btree (major, minor) WHERE ((major <> 5) AND (minor <> 6))"
  93. colNames := getIndexColName(s)
  94. assert.Equal(t, []string{"major", "minor"}, colNames)
  95. })
  96. t.Run("unique", func(t *testing.T) {
  97. s := "CREATE UNIQUE INDEX test2_mm_uidx ON test2 (major);"
  98. colNames := getIndexColName(s)
  99. assert.Equal(t, []string{"major"}, colNames)
  100. })
  101. t.Run("Indexes on Expressions", func(t *testing.T) {})
  102. }