parser_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package parser
  2. import (
  3. "sort"
  4. "testing"
  5. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/model"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/util"
  7. "git.i2edu.net/i2/go-zero/tools/goctl/util/stringx"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestParsePlainText(t *testing.T) {
  11. _, err := Parse("plain text")
  12. assert.NotNil(t, err)
  13. }
  14. func TestParseSelect(t *testing.T) {
  15. _, err := Parse("select * from user")
  16. assert.Equal(t, errUnsupportDDL, err)
  17. }
  18. func TestParseCreateTable(t *testing.T) {
  19. table, err := Parse("CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL comment '手\\t机 号',\n `class` bigint NOT NULL comment '班级',\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL comment '姓\n 名',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP comment '创建\\r时间',\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;")
  20. assert.Nil(t, err)
  21. assert.Equal(t, "test_user", table.Name.Source())
  22. assert.Equal(t, "id", table.PrimaryKey.Name.Source())
  23. assert.Equal(t, true, table.ContainsTime())
  24. assert.Equal(t, true, func() bool {
  25. mobileUniqueIndex, ok := table.UniqueIndex["mobile_unique"]
  26. if !ok {
  27. return false
  28. }
  29. classNameUniqueIndex, ok := table.UniqueIndex["class_name_unique"]
  30. if !ok {
  31. return false
  32. }
  33. equal := func(f1, f2 []*Field) bool {
  34. sort.Slice(f1, func(i, j int) bool {
  35. return f1[i].Name.Source() < f1[j].Name.Source()
  36. })
  37. sort.Slice(f2, func(i, j int) bool {
  38. return f2[i].Name.Source() < f2[j].Name.Source()
  39. })
  40. if len(f2) != len(f2) {
  41. return false
  42. }
  43. for index, f := range f1 {
  44. if f1[index].Name.Source() != f.Name.Source() {
  45. return false
  46. }
  47. }
  48. return true
  49. }
  50. if !equal(mobileUniqueIndex, []*Field{
  51. {
  52. Name: stringx.From("mobile"),
  53. DataBaseType: "varchar",
  54. DataType: "string",
  55. SeqInIndex: 1,
  56. },
  57. }) {
  58. return false
  59. }
  60. return equal(classNameUniqueIndex, []*Field{
  61. {
  62. Name: stringx.From("class"),
  63. DataBaseType: "bigint",
  64. DataType: "int64",
  65. SeqInIndex: 1,
  66. },
  67. {
  68. Name: stringx.From("name"),
  69. DataBaseType: "varchar",
  70. DataType: "string",
  71. SeqInIndex: 2,
  72. },
  73. })
  74. }())
  75. assert.True(t, func() bool {
  76. for _, e := range table.Fields {
  77. if e.Comment != util.TrimNewLine(e.Comment) {
  78. return false
  79. }
  80. }
  81. return true
  82. }())
  83. }
  84. func TestConvertColumn(t *testing.T) {
  85. t.Run("missingPrimaryKey", func(t *testing.T) {
  86. columnData := model.ColumnData{
  87. Db: "user",
  88. Table: "user",
  89. Columns: []*model.Column{
  90. {
  91. DbColumn: &model.DbColumn{
  92. Name: "id",
  93. DataType: "bigint",
  94. },
  95. },
  96. },
  97. }
  98. _, err := columnData.Convert()
  99. assert.NotNil(t, err)
  100. assert.Contains(t, err.Error(), "missing primary key")
  101. })
  102. t.Run("jointPrimaryKey", func(t *testing.T) {
  103. columnData := model.ColumnData{
  104. Db: "user",
  105. Table: "user",
  106. Columns: []*model.Column{
  107. {
  108. DbColumn: &model.DbColumn{
  109. Name: "id",
  110. DataType: "bigint",
  111. },
  112. Index: &model.DbIndex{
  113. IndexName: "PRIMARY",
  114. },
  115. },
  116. {
  117. DbColumn: &model.DbColumn{
  118. Name: "mobile",
  119. DataType: "varchar",
  120. Comment: "手机号",
  121. },
  122. Index: &model.DbIndex{
  123. IndexName: "PRIMARY",
  124. },
  125. },
  126. },
  127. }
  128. _, err := columnData.Convert()
  129. assert.NotNil(t, err)
  130. assert.Contains(t, err.Error(), "joint primary key is not supported")
  131. })
  132. t.Run("normal", func(t *testing.T) {
  133. columnData := model.ColumnData{
  134. Db: "user",
  135. Table: "user",
  136. Columns: []*model.Column{
  137. {
  138. DbColumn: &model.DbColumn{
  139. Name: "id",
  140. DataType: "bigint",
  141. Extra: "auto_increment",
  142. },
  143. Index: &model.DbIndex{
  144. IndexName: "PRIMARY",
  145. SeqInIndex: 1,
  146. },
  147. },
  148. {
  149. DbColumn: &model.DbColumn{
  150. Name: "mobile",
  151. DataType: "varchar",
  152. Comment: "手机号",
  153. },
  154. Index: &model.DbIndex{
  155. IndexName: "mobile_unique",
  156. SeqInIndex: 1,
  157. },
  158. },
  159. },
  160. }
  161. table, err := columnData.Convert()
  162. assert.Nil(t, err)
  163. assert.True(t, table.PrimaryKey.Index.IndexName == "PRIMARY" && table.PrimaryKey.Name == "id")
  164. for _, item := range table.Columns {
  165. if item.Name == "mobile" {
  166. assert.True(t, item.Index.NonUnique == 0)
  167. break
  168. }
  169. }
  170. })
  171. }