keys_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package gen
  2. import (
  3. "sort"
  4. "testing"
  5. "git.i2edu.net/i2/go-zero/tools/goctl/model/sql/parser"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/util/stringx"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestGenCacheKeys(t *testing.T) {
  10. primaryField := &parser.Field{
  11. Name: stringx.From("id"),
  12. DataBaseType: "bigint",
  13. DataType: "int64",
  14. Comment: "自增id",
  15. SeqInIndex: 1,
  16. }
  17. mobileField := &parser.Field{
  18. Name: stringx.From("mobile"),
  19. DataBaseType: "varchar",
  20. DataType: "string",
  21. Comment: "手机号",
  22. SeqInIndex: 1,
  23. }
  24. classField := &parser.Field{
  25. Name: stringx.From("class"),
  26. DataBaseType: "varchar",
  27. DataType: "string",
  28. Comment: "班级",
  29. SeqInIndex: 1,
  30. }
  31. nameField := &parser.Field{
  32. Name: stringx.From("name"),
  33. DataBaseType: "varchar",
  34. DataType: "string",
  35. Comment: "姓名",
  36. SeqInIndex: 2,
  37. }
  38. primariCacheKey, uniqueCacheKey := genCacheKeys(parser.Table{
  39. Name: stringx.From("user"),
  40. PrimaryKey: parser.Primary{
  41. Field: *primaryField,
  42. AutoIncrement: true,
  43. },
  44. UniqueIndex: map[string][]*parser.Field{
  45. "mobile_unique": {
  46. mobileField,
  47. },
  48. "class_name_unique": {
  49. classField,
  50. nameField,
  51. },
  52. },
  53. NormalIndex: nil,
  54. Fields: []*parser.Field{
  55. primaryField,
  56. mobileField,
  57. classField,
  58. nameField,
  59. {
  60. Name: stringx.From("createTime"),
  61. DataBaseType: "timestamp",
  62. DataType: "time.Time",
  63. Comment: "创建时间",
  64. },
  65. {
  66. Name: stringx.From("updateTime"),
  67. DataBaseType: "timestamp",
  68. DataType: "time.Time",
  69. Comment: "更新时间",
  70. },
  71. },
  72. })
  73. t.Run("primaryCacheKey", func(t *testing.T) {
  74. assert.Equal(t, true, func() bool {
  75. return cacheKeyEqual(primariCacheKey, Key{
  76. VarLeft: "cacheUserIdPrefix",
  77. VarRight: `"cache:user:id:"`,
  78. VarExpression: `cacheUserIdPrefix = "cache:user:id:"`,
  79. KeyLeft: "userIdKey",
  80. KeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
  81. DataKeyRight: `fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
  82. KeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, id)`,
  83. DataKeyExpression: `userIdKey := fmt.Sprintf("%s%v", cacheUserIdPrefix, data.Id)`,
  84. FieldNameJoin: []string{"id"},
  85. })
  86. }())
  87. })
  88. t.Run("uniqueCacheKey", func(t *testing.T) {
  89. assert.Equal(t, true, func() bool {
  90. expected := []Key{
  91. {
  92. VarLeft: "cacheUserClassNamePrefix",
  93. VarRight: `"cache:user:class:name:"`,
  94. VarExpression: `cacheUserClassNamePrefix = "cache:user:class:name:"`,
  95. KeyLeft: "userClassNameKey",
  96. KeyRight: `fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, class, name)`,
  97. DataKeyRight: `fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, data.Class, data.Name)`,
  98. KeyExpression: `userClassNameKey := fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, class, name)`,
  99. DataKeyExpression: `userClassNameKey := fmt.Sprintf("%s%v:%v", cacheUserClassNamePrefix, data.Class, data.Name)`,
  100. FieldNameJoin: []string{"class", "name"},
  101. },
  102. {
  103. VarLeft: "cacheUserMobilePrefix",
  104. VarRight: `"cache:user:mobile:"`,
  105. VarExpression: `cacheUserMobilePrefix = "cache:user:mobile:"`,
  106. KeyLeft: "userMobileKey",
  107. KeyRight: `fmt.Sprintf("%s%v", cacheUserMobilePrefix, mobile)`,
  108. DataKeyRight: `fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)`,
  109. KeyExpression: `userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, mobile)`,
  110. DataKeyExpression: `userMobileKey := fmt.Sprintf("%s%v", cacheUserMobilePrefix, data.Mobile)`,
  111. FieldNameJoin: []string{"mobile"},
  112. },
  113. }
  114. sort.Slice(uniqueCacheKey, func(i, j int) bool {
  115. return uniqueCacheKey[i].VarLeft < uniqueCacheKey[j].VarLeft
  116. })
  117. if len(expected) != len(uniqueCacheKey) {
  118. return false
  119. }
  120. for index, each := range uniqueCacheKey {
  121. expecting := expected[index]
  122. if !cacheKeyEqual(expecting, each) {
  123. return false
  124. }
  125. }
  126. return true
  127. }())
  128. })
  129. }
  130. func cacheKeyEqual(k1, k2 Key) bool {
  131. k1Join := k1.FieldNameJoin
  132. k2Join := k2.FieldNameJoin
  133. sort.Strings(k1Join)
  134. sort.Strings(k2Join)
  135. if len(k1Join) != len(k2Join) {
  136. return false
  137. }
  138. for index, each := range k1Join {
  139. k2Item := k2Join[index]
  140. if each != k2Item {
  141. return false
  142. }
  143. }
  144. return k1.VarLeft == k2.VarLeft &&
  145. k1.VarRight == k2.VarRight &&
  146. k1.VarExpression == k2.VarExpression &&
  147. k1.KeyLeft == k2.KeyLeft &&
  148. k1.KeyRight == k2.KeyRight &&
  149. k1.DataKeyRight == k2.DataKeyRight &&
  150. k1.DataKeyExpression == k2.DataKeyExpression &&
  151. k1.KeyExpression == k2.KeyExpression
  152. }