service_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package service
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "path/filepath"
  7. "testing"
  8. "go-common/app/admin/main/spy/conf"
  9. "go-common/app/admin/main/spy/model"
  10. "go-common/library/ecode"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. var (
  14. s *Service
  15. c = context.TODO()
  16. )
  17. func init() {
  18. var err error
  19. dir, _ := filepath.Abs("../cmd/spy-admin-test.toml")
  20. flag.Set("conf", dir)
  21. if err = conf.Init(); err != nil {
  22. panic(err)
  23. }
  24. s = New(conf.Conf)
  25. }
  26. func TestSetting(t *testing.T) {
  27. Convey("test checkSettingVal", t, func() {
  28. var err = s.checkSettingVal(model.AutoBlock, "abc")
  29. So(err, ShouldEqual, ecode.SpySettingValTypeError)
  30. err = s.checkSettingVal(model.AutoBlock, "3")
  31. So(err, ShouldEqual, ecode.SpySettingValueOutOfRange)
  32. err = s.checkSettingVal(model.AutoBlock, "1")
  33. So(err, ShouldBeNil)
  34. err = s.checkSettingVal(model.LessBlockScore, "abc")
  35. So(err, ShouldEqual, ecode.SpySettingValTypeError)
  36. err = s.checkSettingVal(model.LessBlockScore, "45")
  37. So(err, ShouldEqual, ecode.SpySettingValueOutOfRange)
  38. err = s.checkSettingVal(model.LessBlockScore, "1")
  39. So(err, ShouldBeNil)
  40. err = s.checkSettingVal(model.LimitBlockCount, "abc")
  41. So(err, ShouldEqual, ecode.SpySettingValTypeError)
  42. err = s.checkSettingVal(model.LimitBlockCount, "-2")
  43. So(err, ShouldEqual, ecode.SpySettingValueOutOfRange)
  44. err = s.checkSettingVal(model.LimitBlockCount, "20000")
  45. So(err, ShouldBeNil)
  46. err = s.checkSettingVal("unknown prop", "abc")
  47. So(err, ShouldEqual, ecode.SpySettingUnknown)
  48. })
  49. Convey("test Setting", t, func() {
  50. var (
  51. list []*model.Setting
  52. err error
  53. )
  54. list, err = s.SettingList(c)
  55. So(err, ShouldBeNil)
  56. So(list, ShouldNotBeEmpty)
  57. var (
  58. name = "go test"
  59. prop = list[0].Property
  60. val = list[0].Val
  61. )
  62. err = s.UpdateSetting(c, name, prop, val)
  63. So(err, ShouldBeNil)
  64. err = s.UpdateSetting(c, name, model.LessBlockScore, "100")
  65. So(err, ShouldEqual, ecode.SpySettingValueOutOfRange)
  66. })
  67. }
  68. // go test -test.v -test.run TestStat
  69. func TestStat(t *testing.T) {
  70. var (
  71. state int8 = 1
  72. id int64 = 3
  73. isdel int8 = 1
  74. tid int64 = 1
  75. tmid int64 = 1
  76. ty int8 = 1
  77. count int64 = 10
  78. operater = "admin"
  79. pn = 1
  80. ps = 8
  81. )
  82. Convey("test UpdateState", t, func() {
  83. err := s.UpdateState(c, state, id, operater)
  84. So(err, ShouldBeNil)
  85. })
  86. Convey("test UpdateStatQuantity", t, func() {
  87. err := s.UpdateStatQuantity(c, count, id, operater)
  88. So(err, ShouldBeNil)
  89. })
  90. Convey("test DeleteStat", t, func() {
  91. err := s.DeleteStat(c, isdel, id, operater)
  92. So(err, ShouldBeNil)
  93. })
  94. Convey("test StatPage", t, func() {
  95. page, err := s.StatPage(c, tmid, tid, ty, pn, ps)
  96. So(err, ShouldBeNil)
  97. fmt.Println("page", page)
  98. })
  99. }