Files
baseapi/utils/utils_test.go

279 lines
13 KiB
Go
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package utils
import (
"fmt"
"testing"
"git.rosy.net.cn/baseapi"
"go.uber.org/zap"
)
var (
sugarLogger *zap.SugaredLogger
)
func init() {
logger, _ := zap.NewDevelopment()
sugarLogger = logger.Sugar()
baseapi.Init(sugarLogger)
}
func TestDictKeysMan(t *testing.T) {
testData := map[string]interface{}{"k1": "v1", "k2": "v2"}
result1 := DictKeysMan(testData, []string{"k1"}, nil).(map[string]interface{})
if _, ok := result1["k1"]; ok {
t.Error("Params keysToRemove can not remove key!")
}
if k2Value, ok := result1["k2"]; !ok {
t.Error("Params keysToRemove removed wrong key!")
} else {
k2ValueStr, _ := k2Value.(string)
if k2ValueStr != "v2" {
t.Logf("k2Value:%s", k2ValueStr)
t.Error("wrong data when removeKeys")
}
}
result2 := DictKeysMan([]interface{}{testData, map[string]string{"k1": "v11", "k2": "v22"}}, nil, []string{"k1"}).([]interface{})
result20 := result2[0].(map[string]interface{})
if _, ok := result20["k1"]; !ok {
t.Error("Params keysToKeep can not keep key!")
}
if _, ok := result20["k2"]; ok {
t.Error("Params keysToKeep keep wrong key!")
}
}
type TestModel struct {
IntData int
Int64Data int64
FloatData float64
StrData string
}
type TestModel2 struct {
IntData int
Int64Data int64
FloatData float64
AddData string
}
func TestUnmarshal2Map(t *testing.T) {
jsonStr := `
{
"IntData" : 1,
"Int64Data" : 2,
"NotField" : "hello",
"StrData" : "world"
}
`
var kk TestModel
result, err := Unmarshal2Map([]byte(jsonStr), &kk)
if err != nil {
t.Fatal("Unmarshal2Map error")
}
// t.Log(result)
if _, ok := result["NotField"]; ok {
t.Fatal("NotField should not exist")
}
if result["IntData"] != 1 {
t.Fatal("IntData should equal to 1")
}
if result["Int64Data"] != int64(2) {
t.Fatal("Int64Data should equal to int64 2")
}
if result["StrData"] != "world" {
t.Fatal("StrData should equal to world")
}
jsonStr = ""
result, err = Unmarshal2Map([]byte(jsonStr), &kk)
if err == nil {
t.Fatal("Unmarshal2Map should error")
}
jsonStr = "{}"
result, err = Unmarshal2Map([]byte(jsonStr), &kk)
if err != nil {
t.Fatal("Unmarshal2Map error")
}
}
func unmarshStr(t *testing.T, jsonStr string, obj interface{}) {
err := UnmarshalUseNumber([]byte(jsonStr), obj)
if err != nil {
t.Fatal(err)
} else {
fmt.Println(obj)
}
}
func TestJson(t *testing.T) {
jsonStr := `
{
"IntData": 1,
"Int64Data": 1234567890123456,
"FloatData": 1234.5678,
"StrData": "str"
}
`
obj1 := make(map[string]interface{})
var obj2 interface{}
var obj3 TestModel
var obj4 TestModel2
unmarshStr(t, jsonStr, &obj1)
unmarshStr(t, jsonStr, &obj2)
unmarshStr(t, jsonStr, &obj3)
unmarshStr(t, jsonStr, &obj4)
t.Skip("skip the flollowing")
t.Fatal(1)
t.Fail()
}
func BenchmarkFilterMb4(b *testing.B) {
b.ResetTimer()
str := `
{"code":"0","detail":null,"msg":"成功","result":{"maxPageSize":100,"page":1,"pageNo":1,"pageSize":20,"resultList":[{"acceptDeadline":"2018-07-18 09:00:00","adjustCount":0,"adjustId":0,"adjustIsExists":false,"appVersion":"H5","businessTag":"dj_new_cashier;dj_aging_nextday;dj_sx;","businessType":1,"buyerCity":"1315","buyerCityName":"厦门市","buyerCoordType":2,"buyerCountry":"3486","buyerCountryName":"湖里区","buyerFullAddress":"厦门市湖里区厦门湖里万达广场SOHO公寓D1栋308室","buyerFullName":"女神","buyerIp":1879427610,"buyerLat":24.50275,"buyerLng":118.17988,"buyerMobile":"13285650460","buyerPin":"JD_21e20cdd6f92b6d","buyerPinType":0,"buyerPoi":"厦门湖里万达广场SOHO公寓","buyerProvince":"0","buyerTelephone":"13285650460","cancelOrderIsReadable":true,"clientOrderPreStartDeliveryTime":"2018-07-18 09:30:00","deliveryBillNo":"817096693000041","deliveryCarrierName":"达达专送","deliveryCarrierNo":"9966","deliveryDelayAgreed":0,"deliveryPackageVolume":0.0,"deliveryPackageWeight":4.45,"deliveryStationName":"京西菜市-钟宅店","deliveryStationNo":"11736984","deliveryStationNoIsv":"100163","deliveryType":1,"discount":[{"discountCode":"201807171-101224909#317801315","discountDetailType":1,"discountName":"钟宅39减10","discountPrice":1000,"discountType":3,"orderShareRatioData":"promotionId=101224909\u0026venderShareProportion=100\u0026platShareProportion=0\u0026platPayMoney=0\u0026venderPayMoney=1000\u0026createPin=jialuluxm","skuIds":"2013749586,2013496693,2013267451,2013587798"}],"equipmentId":"864372039949845-6091f3baaa99","industryTag":3,"isDeleted":false,"isGroupon":false,"isJDGetCash":true,"lastFourDigitsOfBuyerMobile":"1007","localDeliveryMoney":0,"merchantPaymentDistanceFreightMoney":200,"middleNumBindingId":"12180717210601378227528","middleNumBindingTime":"2018-07-19 10:00:00","orderAdditionFreightMoney":0,"orderAgingType":9,"orderBalanceUsed":0,"orderBaseFreightMoney":400,"orderBaseReceivableFreight":0,"orderBuyerPayableMoney":4831,"orderBuyerRemark":"\n如遇缺货其他商品继续配送缺货商品退款","orderDiscountMoney":1000,"orderDistanceStepFreight":0,"orderFinanceOrgCode":706,"orderFreightMoney":400,"orderGoodsMoney":5381,"orderId":817096693000041,"orderInvoiceOpenMark":2,"orderIsClosed":false,"orderJingdouMoney":0,"orderLadderFreightMoney":0,"orderNum":3,"orderPayType":4,"orderPreDeliveryTime":"2018-07-18 10:00:00","orderPreEndDeliveryTime":"2018-07-18 09:30:00","orderPreStartDeliveryTime":"2018-07-18 09:30:00","orderPurchaseTime":"2018-07-17 21:06:01","orderReceivableFreight":400,"orderSkuType":1,"orderStartTime":"2018-07-17 21:04:52","orderStatus":41000,"orderStatusTime":"2018-07-17 21:06:01","orderStockOwner":3,"orderTotalMoney":5381,"orderType":10000,"orderVenderChargeMoney":0,"orgCode":"320406","packagingMoney":50,"payChannel":9002,"pickDeadline":"2018-07-18 09:00:00","pickMark":1,"platformPointsDeductionMoney":0,"printMark":1,"produceStationName":"京西菜市-钟宅店","produceStationNo":"11736984","produceStationNoIsv":"100163","product":[{"adjustMode":0,"canteenMoney":0,"categoryId":"20307,20353,20362","firstCategoryName":"水果/蔬菜","isGift":false,"promotionType":1,"skuCount":1,"skuId":2013749586,"skuIdIsv":"4375","skuJdPrice":3800,"skuName":"土猪猪蹄约800g/份","skuSpuId":0,"skuStockOwner":0,"skuStorePrice":3800,"skuWeight":0.8},{"adjustMode":0,"canteenMoney":0,"categoryId":"20307,20353,20361","firstCategoryName":"水果/蔬菜","isGift":false,"promotionType":1,"skuCount":1,"skuId":2013496693,"skuIdIsv":"3569","skuJdPrice":85,"skuName":"小香葱约50g/份","skuSpuId":0,"skuStockOwner":0,"skuStorePrice":85,"skuWeight":0.05},{"adjustMode":0,"canteenMoney":0,"categoryId":"20307,20353,20362","firstCategoryName":"水果/蔬菜","isGift":false,"promotionType":1,"skuCount":1,"skuId":2013267451,"skuIdIsv":"3340","skuJdPrice":998,"skuName":"高山娃娃菜750g/包","skuSpuId":0,"skuStockOwner":0,"skuStorePrice":998,"skuWeight":3.0},{"adjustMode":0,"canteenMoney":0,"categoryId":"20307,20353,20362","firstCategoryName":"水果/蔬菜","isGift":false,"promotionType":1,"skuCount":1,"skuId":2013587798,"skuIdIsv":"4114","skuJdPrice":498,"skuName":"[鲜]甜白菜约600g/份","skuSpuId":0,"skuStockOwner":0,"skuStorePrice":498,"skuWeight":0.6}],"srcInnerOrderId":0,"srcInnerType":0,"srcOrderId":"817096693000041","srcOrderType":2,"srcPlatId":4,"storeLat":24.52385,"storeLng":118.16938,"tips":0,"ts":"2018-07-17 21:06:01","updatePin":"ocs-buffer","userTip":0,"venderBalanceDeductMoney":0,"venderIntegralDeductMoney":0,"yn":false}],"totalCount":1,"totalPage":1},"success":true}
`
for i := 0; i < b.N; i++ {
_ = FilterMb4(str)
}
}
func TestFilterEmoji(t *testing.T) {
for _, v := range [][]string{
[]string{
`
{"realip":"120.52.149.107","@timestamp":"2018-07-17T21:30:19+08:00","host":"callback.jingxicaishi.com","request":"POST /djsw/pushDeliveryStatus HTTP/1.1","req_body":"timestamp=2018-07-17%2B21%253A30%253A19&sign=0027B8694AF7FF818A7E4FCFF13A8A5C&v=1.0&jd_param_json=%257B%2522createPin%2522%253A%2522JD_21028ea53427da6%2522%252C%2522deliveryCarrierName%2522%253A%2522%25E8%25BE%25BE%25E8%25BE%25BE%25E5%25B9%25B3%25E5%258F%25B0%2522%252C%2522deliveryCarrierNo%2522%253A%25229966%2522%252C%2522deliveryManName%2522%253A%2522%25E8%25AE%25B8%25E8%25BF%259E%25E8%25BE%2589%2522%252C%2522deliveryManNo%2522%253A%252213731713474%2522%252C%2522deliveryManPhone%2522%253A%252213731713474%2522%252C%2522deliveryStatus%2522%253A%252240%2522%252C%2522deliveryStatusTime%2522%253A%25222018-07-17%2B21%253A30%253A19%2522%252C%2522failType%2522%253A%2522%2522%252C%2522opTime%2522%253A%25221531834219636%2522%252C%2522orderId%2522%253A%2522817091517000222%2522%252C%2522remark%2522%253A%2522%25E5%25B7%25B2%25E5%25AE%258C%25E6%2588%2590%2522%257D&token=91633f2a-c5f5-4982-a925-a220d19095c3&app_key=1dba76d40cac446ca500c0391a0b6c9d&format=json","status":"200","size":38,"ua":"Jakarta Commons-HttpClient/3.1","cookie":"","req_time":"0.063","uri":"/djsw/pushDeliveryStatus","referer":"","xff":"","ups_status":"200","ups_addr":"172.27.0.10:8080","ups_time":"0.008"}
`,
`
{"realip":"120.52.149.107","@timestamp":"2018-07-17T21:30:19+08:00","host":"callback.jingxicaishi.com","request":"POST /djsw/pushDeliveryStatus HTTP/1.1","req_body":"timestamp=2018-07-17%2B21%253A30%253A19&sign=0027B8694AF7FF818A7E4FCFF13A8A5C&v=1.0&jd_param_json=%257B%2522createPin%2522%253A%2522JD_21028ea53427da6%2522%252C%2522deliveryCarrierName%2522%253A%2522%25E8%25BE%25BE%25E8%25BE%25BE%25E5%25B9%25B3%25E5%258F%25B0%2522%252C%2522deliveryCarrierNo%2522%253A%25229966%2522%252C%2522deliveryManName%2522%253A%2522%25E8%25AE%25B8%25E8%25BF%259E%25E8%25BE%2589%2522%252C%2522deliveryManNo%2522%253A%252213731713474%2522%252C%2522deliveryManPhone%2522%253A%252213731713474%2522%252C%2522deliveryStatus%2522%253A%252240%2522%252C%2522deliveryStatusTime%2522%253A%25222018-07-17%2B21%253A30%253A19%2522%252C%2522failType%2522%253A%2522%2522%252C%2522opTime%2522%253A%25221531834219636%2522%252C%2522orderId%2522%253A%2522817091517000222%2522%252C%2522remark%2522%253A%2522%25E5%25B7%25B2%25E5%25AE%258C%25E6%2588%2590%2522%257D&token=91633f2a-c5f5-4982-a925-a220d19095c3&app_key=1dba76d40cac446ca500c0391a0b6c9d&format=json","status":"200","size":38,"ua":"Jakarta Commons-HttpClient/3.1","cookie":"","req_time":"0.063","uri":"/djsw/pushDeliveryStatus","referer":"","xff":"","ups_status":"200","ups_addr":"172.27.0.10:8080","ups_time":"0.008"}
`,
},
[]string{
"😋abcd中国人😋",
"abcd中国人",
},
[]string{
"a❄日配冷藏❄❄🀄A❄",
"a日配冷藏A",
},
} {
str := FilterEmoji(v[0])
if str != v[1] {
t.Fatalf("%s failed\nshould be:%s\nbut it's:%s", v[0], v[1], str)
}
}
}
func TestRemoveGeneralMapKeys(t *testing.T) {
obj1 := map[string]interface{}{
"key1": 1,
"key2": "2",
"key3": 3,
"key4": nil,
}
obj11 := RemoveGeneralMapKeys(obj1, "key2", "key3", "key4")
if obj11["key1"] != 1 {
t.Fatal("RemoveGeneralMapKeys doesn't keep key correctly")
}
if _, ok := obj11["key2"]; ok {
t.Fatal("RemoveGeneralMapKeys doesn't remove key correctly")
}
if _, ok := obj11["key4"]; ok {
t.Fatal("RemoveGeneralMapKeys doesn't remove key correctly")
}
t.Log(obj11)
if RemoveGeneralMapKeys(nil, "key1") != nil {
t.Fatal("RemoveGeneralMapKeys handle nil wrong")
}
}
func TestTrimBlanChar(t *testing.T) {
for _, strList := range [][]string{
[]string{
"大小21",
`
大小21
`,
},
[]string{
"13518183468",
"13518183468", // 末尾有一个隐藏字符
},
} {
t.Logf("%d,%d", len(strList[0]), len(strList[1]))
if TrimBlankChar(strList[1]) != strList[0] {
t.Fatal("TrimBlanChar doesn't work")
}
}
}
func TestLimitUTF8StringLen(t *testing.T) {
for _, v := range [][]interface{}{
[]interface{}{
"123456789",
6,
"123456",
},
[]interface{}{
"123456789",
0,
"123456789",
},
[]interface{}{
"一二345六789",
6,
"一二345六",
},
[]interface{}{
"1二345六789",
0,
"1二345六789",
},
} {
str := LimitUTF8StringLen(v[0].(string), v[1].(int))
if str != v[2] {
t.Fatalf("%v处理错误预期:%v实际:%s", v[0], v[2], str)
}
}
for _, v := range [][]interface{}{
[]interface{}{
"大家好abc",
10,
"大家好a",
},
[]interface{}{
"a大家好123",
8,
"a大家",
},
} {
str := LimitMixedStringLen(v[0].(string), v[1].(int))
if str != v[2] {
t.Fatalf("%v处理错误预期:%v实际:%s", v[0], v[2], str)
}
}
t.Log(LimitStringLen("a大家好1234567", 8))
t.Log(LimitUTF8StringLen("a大家好1234567", 8))
t.Log(LimitMixedStringLen("a大家好1234567", 8))
}
func TestGenerateGetURL(t *testing.T) {
fullURL := GenerateGetURL("http://www.163.com", "api", map[string]interface{}{
"k1": "v1",
"k2": 2,
})
t.Log(fullURL)
if fullURL != "http://www.163.com/api?k1=v1&k2=2" {
t.Fatal("GenerateGetURL错误")
}
fullURL = GenerateGetURL("http://www.163.com", "api", nil)
t.Log(fullURL)
if fullURL != "http://www.163.com/api" {
t.Fatal("GenerateGetURL错误")
}
}