parser_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package parser
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/tal-tech/go-zero/tools/goctl/model/sql/model"
  6. )
  7. func TestParsePlainText(t *testing.T) {
  8. _, err := Parse("plain text")
  9. assert.NotNil(t, err)
  10. }
  11. func TestParseSelect(t *testing.T) {
  12. _, err := Parse("select * from user")
  13. assert.Equal(t, errUnsupportDDL, err)
  14. }
  15. func TestParseCreateTable(t *testing.T) {
  16. table, err := Parse("CREATE TABLE `user_snake` (\n `id` bigint(10) NOT NULL AUTO_INCREMENT,\n `name` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户名称',\n `password` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '用户密码',\n `mobile` varchar(255) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '' COMMENT '手机号',\n `gender` char(5) COLLATE utf8mb4_general_ci NOT NULL COMMENT '男|女|未公开',\n `nickname` varchar(255) COLLATE utf8mb4_general_ci DEFAULT '' COMMENT '用户昵称',\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `name_index` (`name`),\n KEY `mobile_index` (`mobile`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;")
  17. assert.Nil(t, err)
  18. assert.Equal(t, "user_snake", table.Name.Source())
  19. assert.Equal(t, "id", table.PrimaryKey.Name.Source())
  20. assert.Equal(t, true, table.ContainsTime())
  21. }
  22. func TestConvertColumn(t *testing.T) {
  23. _, err := ConvertColumn("user", "user", []*model.Column{
  24. {
  25. Name: "id",
  26. DataType: "bigint",
  27. Key: "",
  28. Extra: "",
  29. Comment: "",
  30. },
  31. })
  32. assert.NotNil(t, err)
  33. assert.Contains(t, err.Error(), "missing primary key")
  34. _, err = ConvertColumn("user", "user", []*model.Column{
  35. {
  36. Name: "id",
  37. DataType: "bigint",
  38. Key: "PRI",
  39. Extra: "",
  40. Comment: "",
  41. },
  42. {
  43. Name: "mobile",
  44. DataType: "varchar",
  45. Key: "PRI",
  46. Extra: "",
  47. Comment: "手机号",
  48. },
  49. })
  50. assert.NotNil(t, err)
  51. assert.Contains(t, err.Error(), "only one primary key expected")
  52. table, err := ConvertColumn("user", "user", []*model.Column{
  53. {
  54. Name: "id",
  55. DataType: "bigint",
  56. Key: "PRI",
  57. Extra: "auto_increment",
  58. Comment: "",
  59. },
  60. {
  61. Name: "mobile",
  62. DataType: "varchar",
  63. Key: "UNI",
  64. Extra: "",
  65. Comment: "手机号",
  66. },
  67. })
  68. assert.Nil(t, err)
  69. assert.True(t, table.PrimaryKey.AutoIncrement && table.PrimaryKey.IsPrimaryKey)
  70. assert.Equal(t, "id", table.PrimaryKey.Name.Source())
  71. for _, item := range table.Fields {
  72. if item.Name.Source() == "mobile" {
  73. assert.True(t, item.IsUniqueKey)
  74. break
  75. }
  76. }
  77. }