package utils import ( "fmt" "github.com/kirinlabs/HttpRequest" "github.com/shopspring/decimal" . "github.com/smartystreets/goconvey/convey" "go.uber.org/atomic" "testing" "time" ) func TestQrcode2Base64(t *testing.T) { Convey("测试生成二维码并把二维码变成base64编码", t, func() { urlPath := "https://www.baidu.com" code, err := Qrcode2Base64(urlPath) So(err, ShouldBeNil) So(code, ShouldStartWith, "data:image/png;base64,") }) } func TestFloat64CusDecimal(t *testing.T) { Convey("测试把float64精度截断", t, func() { f1 := 2000.22202 f := Float64CusDecimal(f1, 2) So(f, ShouldEqual, 2000.22) f2 := 5000.4 f = Float64CusDecimal(f2, 1) So(f, ShouldEqual, f2) f3 := 5000.509 f = Float64CusDecimal(f3, 2) So(f, ShouldNotEqual, 5000.50) }) } func TestGif2Base64(t *testing.T) { Convey("测试把gif编码为base64", t, func() { urlPath := "http://photocdn.sohu.com/20150721/mp23627612_1437451852870_2.gif" gif, err := Gif2Base64(urlPath) So(err, ShouldEqual, nil) So(gif, ShouldStartWith, "data:image/gif;base64") urlPath = "https://www.baidu.com" gif, err = Gif2Base64(urlPath) So(err, ShouldBeNil) So(urlPath, ShouldEqual, urlPath) urlPath = "xxxx.gif" gif, err = Gif2Base64(urlPath) So(err, ShouldNotEqual, nil) }) } func TestPathExists(t *testing.T) { Convey("测试文件是否存在", t, func() { // 存在状态 path := "utils.go" exist, err := PathExists(path) So(err, ShouldEqual, nil) So(exist, ShouldBeTrue) // 不存在状态 path = "utils.+" exist, err = PathExists(path) So(err, ShouldBeNil) So(exist, ShouldBeFalse) }) } func TestCountDownFormat(t *testing.T) { //Convey("测试倒计时格式", t, func() { // //num := 12000 // //str := CountDownFormat(num) // //So(str, ShouldEqual, "3:20:0") //}) d, _ := decimal.NewFromString("-123.45") d2, _ := decimal.NewFromString(".0001") a := d.Add(d2) fmt.Println(a.Float64()) } func login() string { loginUrl := "https://api.ouxuanhudong.com/PcClient/Client/UserCtl/entryLogin?activity_id=410&account=18926327519&password=123456" resp, _ := HttpRequest.NewRequest().Get(loginUrl) var body = make(map[string]interface{}, 0) resp.Json(&body) token := "" if data, ok := body["data"].(map[string]interface{}); ok { if people, ok := data["people"].(map[string]interface{}); ok { token = people["token"].(string) } } //fmt.Println(token) return token } var incr = new(atomic.Int32) func order(token string) { orderUrl := "https://api.ouxuanhudong.com/PcClient/Client/OrderEntryCtl/manualOrder" resp, _ := HttpRequest.NewRequest().Post(orderUrl, map[string]interface{}{ "name": "1222", "phone": "22222222", "goods": `[{"good_id":92,"good_num":1}]`, "token": token, }) var data = make(map[string]interface{}) resp.Json(&data) //fmt.Println(data) incr.Add(1) } func TestRequest(t *testing.T) { var now = time.Now() tr := time.NewTicker(1 * time.Second) for { select { case <-tr.C: fmt.Println(incr) fmt.Println(time.Now().Sub(now).Seconds()) default: go order(login()) } } }