config_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package middleware
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. "github.com/smartystreets/goconvey/convey"
  7. "go-common/app/admin/main/aegis/model"
  8. "go-common/app/admin/main/aegis/model/resource"
  9. )
  10. var (
  11. d = &Aggregate{
  12. Hitn: "extra1",
  13. Hitv: "2,3,4,5,6,7",
  14. Mapn: "extra1",
  15. Mapv: "2",
  16. Order: 1,
  17. }
  18. ds = MiddleAggregate{
  19. Cfg: []*Aggregate{d},
  20. Encode: true,
  21. }
  22. )
  23. func TestMiddleware_AggregateProcess(t *testing.T) {
  24. var (
  25. data = &model.AuditInfo{
  26. Resource: &resource.Res{Extra1: 6},
  27. }
  28. encode bool = true
  29. data2 = &model.SearchParams{
  30. Extra1: "2",
  31. }
  32. )
  33. convey.Convey("AggregateProcess", t, func(ctx convey.C) {
  34. d.Process(data, encode) //将extra1=6替换成extra1=2
  35. d.Process(data2, !encode) //将extra1=2替换成extra1=2,3,4,5,6,7
  36. ctx.Convey("extra1 equal", func(ctx convey.C) {
  37. ctx.So(fmt.Sprintf("%d", data.Resource.Extra1), convey.ShouldEqual, d.Mapv)
  38. ctx.So(data2.Extra1, convey.ShouldEqual, d.Hitv)
  39. })
  40. })
  41. }
  42. func TestMiddlewaregetFieldByName(t *testing.T) {
  43. var (
  44. v = reflect.ValueOf(&model.AuditInfo{
  45. Resource: &resource.Res{Extra1: 6},
  46. })
  47. name = "extra1"
  48. )
  49. convey.Convey("getFieldByName", t, func(ctx convey.C) {
  50. res, ok := getFieldByName(v, name)
  51. ctx.Convey("Then res,ok should not be nil.", func(ctx convey.C) {
  52. ctx.So(ok, convey.ShouldEqual, true)
  53. ctx.So(res, convey.ShouldNotBeNil)
  54. })
  55. })
  56. }
  57. func TestMiddlewareProcess(t *testing.T) {
  58. var (
  59. data = &model.AuditInfo{
  60. Resource: &resource.Res{Extra1: 6},
  61. }
  62. )
  63. convey.Convey("Process", t, func(ctx convey.C) {
  64. ds.Encode = true
  65. ds.Process(data)
  66. ctx.Convey("No return values", func(ctx convey.C) {
  67. //将extra1=6替换成extra1=2
  68. ctx.So(fmt.Sprintf("%d", data.Resource.Extra1), convey.ShouldEqual, d.Mapv)
  69. })
  70. })
  71. }
  72. func TestMiddlewareLen(t *testing.T) {
  73. convey.Convey("Len", t, func(ctx convey.C) {
  74. p1 := AggregateArr(ds.Cfg).Len()
  75. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  76. ctx.So(p1, convey.ShouldEqual, 1)
  77. })
  78. })
  79. }
  80. func TestMiddlewareLess(t *testing.T) {
  81. var (
  82. i = int(0)
  83. j = int(0)
  84. )
  85. convey.Convey("Less", t, func(ctx convey.C) {
  86. p1 := AggregateArr(ds.Cfg).Less(i, j)
  87. ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
  88. ctx.So(p1, convey.ShouldEqual, false)
  89. })
  90. })
  91. }
  92. func TestMiddlewareSwap(t *testing.T) {
  93. var (
  94. i = int(0)
  95. j = int(0)
  96. )
  97. convey.Convey("Swap", t, func(ctx convey.C) {
  98. AggregateArr(ds.Cfg).Swap(i, j)
  99. ctx.Convey("No return values", func(ctx convey.C) {
  100. })
  101. })
  102. }