dao_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package dao
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "testing"
  8. "time"
  9. "go-common/app/admin/main/spy/conf"
  10. "go-common/app/admin/main/spy/model"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. var (
  14. dataMID = int64(15555180)
  15. noDataMID = int64(1)
  16. d *Dao
  17. )
  18. const (
  19. _cleanFactorSQL = "delete from spy_factor where nick_name = ? AND service_id = ? AND event_id = ? AND risk_level = ?"
  20. _cleanEventSQL = "delete from spy_event where name = ? AND service_id = ? AND status = ?"
  21. _cleanServiceSQL = "delete from spy_service where name = ? AND status = ?"
  22. _cleanGroupSQL = "delete from spy_factor_group where name = ?"
  23. )
  24. func CleanMysql() {
  25. ctx := context.Background()
  26. d.db.Exec(ctx, _cleanFactorSQL, "test", 1, 1, 2)
  27. d.db.Exec(ctx, _cleanEventSQL, "test", 1, 1)
  28. d.db.Exec(ctx, _cleanServiceSQL, "test", 1)
  29. d.db.Exec(ctx, _cleanGroupSQL, "test")
  30. }
  31. func TestMain(m *testing.M) {
  32. if os.Getenv("DEPLOY_ENV") != "" {
  33. flag.Set("app_id", "main.account-law.spy-admin")
  34. flag.Set("conf_token", "bc3d60c2bb2b08a1b690b004a1953d3c")
  35. flag.Set("tree_id", "2857")
  36. flag.Set("conf_version", "docker-1")
  37. flag.Set("deploy_env", "uat")
  38. flag.Set("conf_host", "config.bilibili.co")
  39. flag.Set("conf_path", "/tmp")
  40. flag.Set("region", "sh")
  41. flag.Set("zone", "sh001")
  42. } else {
  43. flag.Set("conf", "../cmd/spy-admin-test.toml")
  44. }
  45. flag.Parse()
  46. if err := conf.Init(); err != nil {
  47. panic(err)
  48. }
  49. d = New(conf.Conf)
  50. CleanMysql()
  51. m.Run()
  52. os.Exit(0)
  53. }
  54. func WithMysql(f func(d *Dao)) func() {
  55. return func() {
  56. f(d)
  57. }
  58. }
  59. func Test_Mysql(t *testing.T) {
  60. Convey("get user info", t, WithMysql(func(d *Dao) {
  61. res, err := d.Info(context.TODO(), dataMID)
  62. So(err, ShouldBeNil)
  63. So(res, ShouldNotBeEmpty)
  64. }))
  65. Convey("get user info no data ", t, WithMysql(func(d *Dao) {
  66. res, err := d.Info(context.TODO(), noDataMID)
  67. So(err, ShouldBeNil)
  68. So(res, ShouldNotBeEmpty)
  69. }))
  70. Convey("get event history", t, WithMysql(func(d *Dao) {
  71. hpConf := &model.HisParamReq{
  72. Mid: 46333,
  73. Ps: 10,
  74. Pn: 1,
  75. }
  76. res, err := d.HistoryPage(context.TODO(), hpConf)
  77. So(err, ShouldBeNil)
  78. So(res, ShouldNotBeEmpty)
  79. }))
  80. Convey("get event history count", t, WithMysql(func(d *Dao) {
  81. hpConf := &model.HisParamReq{
  82. Mid: 46333,
  83. Ps: 10,
  84. Pn: 1,
  85. }
  86. res, err := d.HistoryPageTotalC(context.TODO(), hpConf)
  87. So(err, ShouldBeNil)
  88. fmt.Printf("history count : %d\n", res)
  89. So(res, ShouldNotBeEmpty)
  90. }))
  91. Convey("get setting list", t, WithMysql(func(d *Dao) {
  92. res, err := d.SettingList(context.TODO())
  93. So(err, ShouldBeNil)
  94. So(res, ShouldNotBeEmpty)
  95. }))
  96. Convey("update setting", t, WithMysql(func(d *Dao) {
  97. list, err := d.SettingList(context.TODO())
  98. So(err, ShouldBeNil)
  99. setting := list[0]
  100. res, err := d.UpdateSetting(context.TODO(), setting.Val, setting.Property)
  101. So(err, ShouldBeNil)
  102. fmt.Println(res)
  103. }))
  104. Convey(" add factor ", t, WithMysql(func(d *Dao) {
  105. res, err := d.AddFactor(context.TODO(), &model.Factor{
  106. NickName: "test",
  107. ServiceID: int64(1),
  108. EventID: int64(1),
  109. GroupID: int64(1),
  110. RiskLevel: int8(2),
  111. FactorVal: float32(1),
  112. CategoryID: int8(1),
  113. CTime: time.Now(),
  114. MTime: time.Now(),
  115. })
  116. So(err, ShouldBeNil)
  117. So(res == 1, ShouldBeTrue)
  118. }))
  119. Convey(" add event ", t, WithMysql(func(d *Dao) {
  120. res, err := d.AddEvent(context.TODO(), &model.Event{
  121. Name: "test",
  122. NickName: "nickname",
  123. ServiceID: 1,
  124. Status: 1,
  125. CTime: time.Now(),
  126. MTime: time.Now(),
  127. })
  128. So(err, ShouldBeNil)
  129. So(res == 1, ShouldBeTrue)
  130. }))
  131. Convey(" add service ", t, WithMysql(func(d *Dao) {
  132. res, err := d.AddService(context.TODO(), &model.Service{
  133. Name: "test",
  134. NickName: "nickname",
  135. Status: 1,
  136. CTime: time.Now(),
  137. MTime: time.Now(),
  138. })
  139. So(err, ShouldBeNil)
  140. So(res == 1, ShouldBeTrue)
  141. }))
  142. Convey(" add group ", t, WithMysql(func(d *Dao) {
  143. res, err := d.AddGroup(context.TODO(), &model.FactorGroup{
  144. Name: "test",
  145. CTime: time.Now(),
  146. })
  147. So(err, ShouldBeNil)
  148. So(res == 1, ShouldBeTrue)
  149. }))
  150. }