keys_test.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package gen
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
  7. "github.com/tal-tech/go-zero/tools/goctl/util/stringx"
  8. )
  9. func TestGenCacheKeys(t *testing.T) {
  10. m, err := genCacheKeys(parser.Table{
  11. Name: stringx.From("user"),
  12. PrimaryKey: parser.Primary{
  13. Field: parser.Field{
  14. Name: stringx.From("id"),
  15. DataBaseType: "bigint",
  16. DataType: "int64",
  17. IsPrimaryKey: true,
  18. IsUniqueKey: false,
  19. Comment: "自增id",
  20. },
  21. AutoIncrement: true,
  22. },
  23. Fields: []parser.Field{
  24. {
  25. Name: stringx.From("mobile"),
  26. DataBaseType: "varchar",
  27. DataType: "string",
  28. IsPrimaryKey: false,
  29. IsUniqueKey: true,
  30. Comment: "手机号",
  31. },
  32. {
  33. Name: stringx.From("name"),
  34. DataBaseType: "varchar",
  35. DataType: "string",
  36. IsPrimaryKey: false,
  37. IsUniqueKey: true,
  38. Comment: "姓名",
  39. },
  40. {
  41. Name: stringx.From("createTime"),
  42. DataBaseType: "timestamp",
  43. DataType: "time.Time",
  44. IsPrimaryKey: false,
  45. IsUniqueKey: false,
  46. Comment: "创建时间",
  47. },
  48. {
  49. Name: stringx.From("updateTime"),
  50. DataBaseType: "timestamp",
  51. DataType: "time.Time",
  52. IsPrimaryKey: false,
  53. IsUniqueKey: false,
  54. Comment: "更新时间",
  55. },
  56. },
  57. })
  58. assert.Nil(t, err)
  59. for fieldName, key := range m {
  60. name := stringx.From(fieldName)
  61. assert.Equal(t, fmt.Sprintf(`cacheUser%sPrefix = "cache#User#%s#"`, name.ToCamel(), name.Untitle()), key.VarExpression)
  62. assert.Equal(t, fmt.Sprintf(`cacheUser%sPrefix`, name.ToCamel()), key.Left)
  63. assert.Equal(t, fmt.Sprintf(`cache#User#%s#`, name.Untitle()), key.Right)
  64. assert.Equal(t, fmt.Sprintf(`user%sKey`, name.ToCamel()), key.Variable)
  65. assert.Equal(t, `user`+name.ToCamel()+`Key := fmt.Sprintf("%s%v", cacheUser`+name.ToCamel()+`Prefix,`+name.Untitle()+`)`, key.KeyExpression)
  66. }
  67. }