db_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package db
  2. import (
  3. "reflect"
  4. "testing"
  5. "github.com/gogf/gf/database/gdb"
  6. )
  7. func NewDB() (gdb.DB, error) {
  8. configNode := gdb.ConfigNode{
  9. Host: "192.168.0.252",
  10. Port: "3306",
  11. User: "root",
  12. Pass: "Dashoo#190801@ali",
  13. Name: "lims_dev",
  14. Type: "mysql",
  15. Role: "master",
  16. Charset: "utf8",
  17. Weight: 1,
  18. MaxIdleConnCount: 10,
  19. MaxOpenConnCount: 10,
  20. MaxConnLifeTime: 600,
  21. }
  22. gdb.AddConfigNode("test", configNode)
  23. return gdb.New()
  24. }
  25. func NewModel(table string) ServiceBase {
  26. db, _ := NewDB()
  27. sv := ServiceBase{
  28. Tenant: "test",
  29. SafeModel: db.Model(table).Safe(),
  30. DB: db,
  31. TableName: table,
  32. }
  33. return sv
  34. }
  35. func TestServiceBase_Exists(t *testing.T) {
  36. type args struct {
  37. tableName string
  38. where []interface{}
  39. }
  40. wheres := make([]interface{}, 0)
  41. wheres = append(wheres, "name = ?", "dongdong")
  42. tests := []struct {
  43. name string
  44. fields ServiceBase
  45. args args
  46. want bool
  47. wantErr bool
  48. }{
  49. {"db exists", NewModel("test"), args{
  50. tableName: "",
  51. where: wheres,
  52. }, false, false},
  53. }
  54. for _, tt := range tests {
  55. t.Run(tt.name, func(t *testing.T) {
  56. s := &ServiceBase{
  57. Tenant: tt.fields.Tenant,
  58. SafeModel: tt.fields.SafeModel,
  59. DB: tt.fields.DB,
  60. TableName: tt.fields.TableName,
  61. }
  62. got, err := s.Exists(tt.args.tableName, tt.args.where...)
  63. if (err != nil) != tt.wantErr {
  64. t.Errorf("Exists() error = %v, wantErr %v", err, tt.wantErr)
  65. return
  66. }
  67. if got != tt.want {
  68. t.Errorf("Exists() got = %v, want %v", got, tt.want)
  69. }
  70. })
  71. }
  72. }
  73. //
  74. func TestServiceBase_GetDictListForWhere(t *testing.T) {
  75. type fields struct {
  76. Tenant string
  77. SafeModel *gdb.Model
  78. DB gdb.DB
  79. TableName string
  80. }
  81. type args struct {
  82. req DictReq
  83. }
  84. tests := []struct {
  85. name string
  86. fields ServiceBase
  87. args args
  88. want []Dict
  89. wantErr bool
  90. }{}
  91. for _, tt := range tests {
  92. t.Run(tt.name, func(t *testing.T) {
  93. s := &ServiceBase{
  94. Tenant: tt.fields.Tenant,
  95. SafeModel: tt.fields.SafeModel,
  96. DB: tt.fields.DB,
  97. TableName: tt.fields.TableName,
  98. }
  99. got, err := s.GetDictListForWhere(tt.args.req)
  100. if (err != nil) != tt.wantErr {
  101. t.Errorf("GetDictListForWhere() error = %v, wantErr %v", err, tt.wantErr)
  102. return
  103. }
  104. if !reflect.DeepEqual(got, tt.want) {
  105. t.Errorf("GetDictListForWhere() got = %v, want %v", got, tt.want)
  106. }
  107. })
  108. }
  109. }