model_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package model
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "time"
  10. "github.com/smartystreets/goconvey/convey"
  11. "go-common/app/admin/main/aegis/model/common"
  12. "go-common/app/admin/main/aegis/model/resource"
  13. xtime "go-common/library/time"
  14. )
  15. func Test_SubmitOption(t *testing.T) {
  16. sopt := &SubmitOptions{
  17. EngineOption: EngineOption{
  18. BaseOptions: common.BaseOptions{
  19. BusinessID: 1,
  20. FlowID: 1,
  21. OID: "oid-123456",
  22. RID: 1,
  23. Uname: "cxf",
  24. },
  25. Forbid: &Forbid{
  26. Comment: "dqqq",
  27. Reason: "我的",
  28. },
  29. TaskID: 1,
  30. Result: &resource.Result{
  31. Attribute: 3,
  32. Note: "哎",
  33. RejectReason: "",
  34. ReasonID: 3,
  35. AttributeList: map[string]int8{
  36. "no_comment": 1,
  37. "no_forward": 0,
  38. },
  39. },
  40. ExtraData: map[string]interface{}{
  41. "mailto": "chenxuefeng",
  42. "mail": 1,
  43. "mid": 10086,
  44. "notify": 1,
  45. },
  46. },
  47. Binds: []int64{2, 3},
  48. }
  49. mapformat := map[string]*ActionParam{
  50. "auditor": {
  51. Value: "Uname",
  52. },
  53. "oid": {
  54. Value: "OID",
  55. },
  56. "uid": {
  57. Value: "ExtraData.mid",
  58. },
  59. "forbid_params": {
  60. Value: "Forbid",
  61. },
  62. "no_comment": {
  63. Value: "Result.AttributeList.no_comment",
  64. },
  65. "no_forward": {
  66. Value: "Result.AttributeList.no_forward",
  67. },
  68. "notify": {
  69. Value: "ExtraData.notify",
  70. },
  71. "reason": {
  72. Value: "Result.RejectReason",
  73. Default: " ",
  74. },
  75. }
  76. ot := reflect.TypeOf(*sopt)
  77. ov := reflect.ValueOf(*sopt)
  78. params := make(map[string]interface{})
  79. for k, v := range mapformat {
  80. SubReflect(ot, ov, k, strings.Split(v.Value, "."), v.Default, params)
  81. }
  82. fmt.Println("params:", params)
  83. values := url.Values{}
  84. for k, v := range params {
  85. values.Set(k, fmt.Sprint(v))
  86. }
  87. fmt.Println("values:", values.Encode())
  88. t.Fail()
  89. }
  90. func Test_reflectMap(t *testing.T) {
  91. mv := map[string]int8{"1": 1, "2": 2}
  92. v := reflect.ValueOf(mv)
  93. fmt.Println("a:", v.MapIndex(reflect.ValueOf("1")))
  94. fmt.Println("b:", v.MapIndex(reflect.ValueOf("b")))
  95. type A struct {
  96. Name string
  97. Age int
  98. }
  99. type B struct {
  100. Info *A
  101. Extra string
  102. }
  103. b := &B{
  104. Info: &A{
  105. Name: "name",
  106. Age: 1,
  107. },
  108. Extra: "extra",
  109. }
  110. vbp := reflect.ValueOf(b)
  111. vb := vbp.Elem()
  112. fmt.Println("nb:", vb.FieldByName("Info").Elem().FieldByName("Name"))
  113. bs, err := json.Marshal(vbp)
  114. fmt.Println("bs1:", string(bs))
  115. fmt.Println("err1:", err)
  116. bs, err = json.Marshal(vbp.Interface())
  117. fmt.Println("bs2:", string(bs))
  118. fmt.Println("er2:", err)
  119. t.Fail()
  120. }
  121. func Test_Report(t *testing.T) {
  122. time1, _ := time.ParseInLocation("2006-01-02 15:04:05", "2018-11-29 11:11:28", time.Local)
  123. time2 := time1.Add(+10 * time.Minute)
  124. time3 := time1.Add(+2 * time.Hour)
  125. time4 := time1.Add(+4 * time.Hour)
  126. btime, _ := time.ParseInLocation("2006-01-02 15:04:05", "2018-11-29 11:00:00", time.Local)
  127. etime, _ := time.ParseInLocation("2006-01-02 15:04:05", "2018-11-29 20:00:00", time.Local)
  128. /*
  129. 1.同一小时内同用户
  130. 2.同一小时内不同用户
  131. */
  132. metas := []*ReportMeta{
  133. {Mtime: xtime.Time(time1.Unix()),
  134. Content: `{"ds":4,"dy":1,"rl":2}`,
  135. UID: 1,
  136. Uname: "1",
  137. },
  138. {Mtime: xtime.Time(time2.Unix()),
  139. Content: `{"ds":4,"rl":3}`,
  140. UID: 1,
  141. Uname: "1",
  142. },
  143. {Mtime: xtime.Time(time1.Unix()),
  144. Content: `{"ds":4}`,
  145. UID: 2,
  146. Uname: "2",
  147. },
  148. {Mtime: xtime.Time(time2.Unix()),
  149. Content: `{"ds":0,"st_3_225":1,"ut":0}`,
  150. UID: 2,
  151. Uname: "2",
  152. },
  153. {Mtime: xtime.Time(time3.Unix()),
  154. Content: `{"ds":13,"dy":5,"rl":6,"rs_-1":1,"st_3_736":1,"st_4_0":2,"st_4_736":1,"ut":0}`,
  155. UID: 1,
  156. Uname: "1",
  157. },
  158. {Mtime: xtime.Time(time4.Unix()),
  159. Content: `{"ds":2,"rs_1":1,"st_3_2":1,"ut":9}`,
  160. UID: 2,
  161. Uname: "2",
  162. },
  163. }
  164. opt := &OptReport{
  165. Btime: btime,
  166. Etime: etime,
  167. }
  168. mnames := map[int64]string{
  169. 1: "1",
  170. 2: "2",
  171. }
  172. tempres := Gentempres(opt, mnames, metas)
  173. bs1, _ := json.Marshal(tempres)
  174. fmt.Printf("1: %s\n", string(bs1))
  175. res := Genres(opt, tempres, mnames)
  176. bs2, _ := json.Marshal(res)
  177. fmt.Printf("2: %s\n", string(bs2))
  178. form := Genform(res)
  179. bs3, _ := json.Marshal(form)
  180. fmt.Printf("3: %s\n", string(bs3))
  181. t.Fail()
  182. }
  183. func Test_MemberStat(t *testing.T) {
  184. time1, _ := time.ParseInLocation("2006-01-02 15:04:05", "2018-11-30 11:11:28", time.Local)
  185. /*
  186. 1.处理量
  187. 2.处理率
  188. 3.通过率
  189. 4.平均耗时
  190. */
  191. metas := []*ReportMeta{
  192. {Mtime: xtime.Time(time1.Unix()),
  193. Content: `{"ds":10,"dy":5,"rl":5,"rs_-1":1,"st_3_1":2,"st_4_0":2,"st_4_736":1,"ut":10}`,
  194. UID: 1,
  195. Uname: "1",
  196. },
  197. {Mtime: xtime.Time(time1.Unix()),
  198. Content: `{"ds":5,"dy":5,"rl":5,"rs_-1":1,"rs_0":3,"st_3_1":2,"st_4_0":2,"st_4_736":1,"ut":10}`,
  199. UID: 1,
  200. Uname: "1",
  201. },
  202. {Mtime: xtime.Time(time1.Unix()),
  203. Content: `{"ds":5,"dy":0,"rl":0,"rs_-1":1,"rs_0":3,"st_3_1":1,"st_4_0":2,"st_4_736":1,"ut":10}`,
  204. UID: 1,
  205. Uname: "1",
  206. },
  207. {Mtime: xtime.Time(time1.Unix()),
  208. Content: `{"ds":0,"st_3_0":1,"ut":0} `,
  209. UID: 2,
  210. Uname: "2",
  211. },
  212. {Mtime: xtime.Time(time1.Unix()),
  213. Content: `{"ds":0,"st_3_0":1,"ut":0}`,
  214. UID: 2,
  215. Uname: "2",
  216. },
  217. {Mtime: xtime.Time(time1.Unix()),
  218. Content: `{"ds":5,"rs_0":5,"st_3_2":5,"ut":21}`,
  219. UID: 2,
  220. Uname: "2",
  221. },
  222. {Mtime: xtime.Time(time1.Unix()),
  223. Content: `{"ds":2}`,
  224. UID: 3,
  225. Uname: "3",
  226. },
  227. {Mtime: xtime.Time(time1.Unix()),
  228. Content: `{"ds":0,"rl":2}`,
  229. UID: 3,
  230. Uname: "3",
  231. },
  232. {Mtime: xtime.Time(time1.Unix()),
  233. Content: `{"ds":0,"st_4_4":4,"ut":0} `,
  234. UID: 4,
  235. Uname: "4",
  236. },
  237. }
  238. res, _ := GenMemberStat(metas, 0)
  239. bs, _ := json.Marshal(res)
  240. fmt.Printf("%s\n", string(bs))
  241. t.Fail()
  242. }
  243. func Test_Column(t *testing.T) {
  244. column := `[
  245. {
  246. "name":"id",
  247. "chname":"呵呵"
  248. },{
  249. "name":"id2",
  250. "chname":"呵呵",
  251. "enum":{
  252. "1":"上",
  253. "2":"下"
  254. }
  255. }
  256. ]`
  257. cs := []*Column{}
  258. e := json.Unmarshal([]byte(column), &cs)
  259. fmt.Println(e)
  260. fmt.Printf("cs:%+v\n", cs)
  261. t.Fail()
  262. }
  263. func TestLogFieldTemp(t *testing.T) {
  264. s := LogFieldTemp(LogFieldPID, 1, 0, true)
  265. convey.Convey("LogFieldTemp", t, func() {
  266. convey.So(s, convey.ShouldNotEqual, "")
  267. })
  268. }
  269. func TestGetEmptyInfo(t *testing.T) {
  270. s := GetEmptyInfo()
  271. convey.Convey("GetEmptyInfo", t, func() {
  272. convey.So(s, convey.ShouldNotBeNil)
  273. })
  274. }
  275. func TestEmptyListItem(t *testing.T) {
  276. s := EmptyListItem()
  277. convey.Convey("EmptyListItem", t, func() {
  278. convey.So(s, convey.ShouldNotBeNil)
  279. })
  280. }