THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.1KB

  1. package xorm
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "xorm.io/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. uri, err = driver.Parse("pgx", test.in)
  64. if err != nil && test.valid {
  65. t.Errorf("%q got unexpected error: %s", test.in, err)
  66. } else if err == nil && !reflect.DeepEqual(test.expected, uri.DbName) {
  67. t.Errorf("%q got: %#v want: %#v", test.in, uri.DbName, test.expected)
  68. }
  69. }
  70. }
  71. func TestGetIndexColName(t *testing.T) {
  72. t.Run("Index", func(t *testing.T) {
  73. s := "CREATE INDEX test2_mm_idx ON test2 (major);"
  74. colNames := getIndexColName(s)
  75. assert.Equal(t, []string{"major"}, colNames)
  76. })
  77. t.Run("Multicolumn indexes", func(t *testing.T) {
  78. s := "CREATE INDEX test2_mm_idx ON test2 (major, minor);"
  79. colNames := getIndexColName(s)
  80. assert.Equal(t, []string{"major", "minor"}, colNames)
  81. })
  82. t.Run("Indexes and ORDER BY", func(t *testing.T) {
  83. s := "CREATE INDEX test2_mm_idx ON test2 (major NULLS FIRST, minor DESC NULLS LAST);"
  84. colNames := getIndexColName(s)
  85. assert.Equal(t, []string{"major", "minor"}, colNames)
  86. })
  87. t.Run("Combining Multiple Indexes", func(t *testing.T) {
  88. s := "CREATE INDEX test2_mm_cm_idx ON public.test2 USING btree (major, minor) WHERE ((major <> 5) AND (minor <> 6))"
  89. colNames := getIndexColName(s)
  90. assert.Equal(t, []string{"major", "minor"}, colNames)
  91. })
  92. t.Run("unique", func(t *testing.T) {
  93. s := "CREATE UNIQUE INDEX test2_mm_uidx ON test2 (major);"
  94. colNames := getIndexColName(s)
  95. assert.Equal(t, []string{"major"}, colNames)
  96. })
  97. t.Run("Indexes on Expressions", func(t *testing.T) {})
  98. }