互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. package utils
  2. import (
  3. "fmt"
  4. "github.com/kirinlabs/HttpRequest"
  5. "github.com/shopspring/decimal"
  6. . "github.com/smartystreets/goconvey/convey"
  7. "go.uber.org/atomic"
  8. "testing"
  9. "time"
  10. )
  11. func TestQrcode2Base64(t *testing.T) {
  12. Convey("测试生成二维码并把二维码变成base64编码", t, func() {
  13. urlPath := "https://www.baidu.com"
  14. code, err := Qrcode2Base64(urlPath)
  15. So(err, ShouldBeNil)
  16. So(code, ShouldStartWith, "data:image/png;base64,")
  17. })
  18. }
  19. func TestFloat64CusDecimal(t *testing.T) {
  20. Convey("测试把float64精度截断", t, func() {
  21. f1 := 2000.22202
  22. f := Float64CusDecimal(f1, 2)
  23. So(f, ShouldEqual, 2000.22)
  24. f2 := 5000.4
  25. f = Float64CusDecimal(f2, 1)
  26. So(f, ShouldEqual, f2)
  27. f3 := 5000.509
  28. f = Float64CusDecimal(f3, 2)
  29. So(f, ShouldNotEqual, 5000.50)
  30. })
  31. }
  32. func TestGif2Base64(t *testing.T) {
  33. Convey("测试把gif编码为base64", t, func() {
  34. urlPath := "http://photocdn.sohu.com/20150721/mp23627612_1437451852870_2.gif"
  35. gif, err := Gif2Base64(urlPath)
  36. So(err, ShouldEqual, nil)
  37. So(gif, ShouldStartWith, "data:image/gif;base64")
  38. urlPath = "https://www.baidu.com"
  39. gif, err = Gif2Base64(urlPath)
  40. So(err, ShouldBeNil)
  41. So(urlPath, ShouldEqual, urlPath)
  42. urlPath = "xxxx.gif"
  43. gif, err = Gif2Base64(urlPath)
  44. So(err, ShouldNotEqual, nil)
  45. })
  46. }
  47. func TestPathExists(t *testing.T) {
  48. Convey("测试文件是否存在", t, func() {
  49. // 存在状态
  50. path := "utils.go"
  51. exist, err := PathExists(path)
  52. So(err, ShouldEqual, nil)
  53. So(exist, ShouldBeTrue)
  54. // 不存在状态
  55. path = "utils.+"
  56. exist, err = PathExists(path)
  57. So(err, ShouldBeNil)
  58. So(exist, ShouldBeFalse)
  59. })
  60. }
  61. func TestCountDownFormat(t *testing.T) {
  62. //Convey("测试倒计时格式", t, func() {
  63. // //num := 12000
  64. // //str := CountDownFormat(num)
  65. // //So(str, ShouldEqual, "3:20:0")
  66. //})
  67. d, _ := decimal.NewFromString("-123.45")
  68. d2, _ := decimal.NewFromString(".0001")
  69. a := d.Add(d2)
  70. fmt.Println(a.Float64())
  71. }
  72. func login() string {
  73. loginUrl := "https://api.ouxuanhudong.com/PcClient/Client/UserCtl/entryLogin?activity_id=410&account=18926327519&password=123456"
  74. resp, _ := HttpRequest.NewRequest().Get(loginUrl)
  75. var body = make(map[string]interface{}, 0)
  76. resp.Json(&body)
  77. token := ""
  78. if data, ok := body["data"].(map[string]interface{}); ok {
  79. if people, ok := data["people"].(map[string]interface{}); ok {
  80. token = people["token"].(string)
  81. }
  82. }
  83. //fmt.Println(token)
  84. return token
  85. }
  86. var incr = new(atomic.Int32)
  87. func order(token string) {
  88. orderUrl := "https://api.ouxuanhudong.com/PcClient/Client/OrderEntryCtl/manualOrder"
  89. resp, _ := HttpRequest.NewRequest().Post(orderUrl, map[string]interface{}{
  90. "name": "1222",
  91. "phone": "22222222",
  92. "goods": `[{"good_id":92,"good_num":1}]`,
  93. "token": token,
  94. })
  95. var data = make(map[string]interface{})
  96. resp.Json(&data)
  97. //fmt.Println(data)
  98. incr.Add(1)
  99. }
  100. func TestRequest(t *testing.T) {
  101. var now = time.Now()
  102. tr := time.NewTicker(1 * time.Second)
  103. for {
  104. select {
  105. case <-tr.C:
  106. fmt.Println(incr)
  107. fmt.Println(time.Now().Sub(now).Seconds())
  108. default:
  109. go order(login())
  110. }
  111. }
  112. }