gf/util/gconv/gconv_z_unit_maptomap_test.go
黄骞 9f12673631
add ut cases for package gconv (#2272)
* improve gconv.go code coverage

* improve gconv_convert.go code coverage

* improve gconv_float.go code coverage

* improve gconv_map.go code coverage

* improve gconv_maps.go code coverage

* improve gconv_maptomap.go code coverage

* improve gconv_maptomaps.go code coverage
2022-11-07 17:53:51 +08:00

213 lines
4.5 KiB
Go

// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gconv_test
import (
"testing"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/test/gtest"
"github.com/gogf/gf/v2/util/gconv"
)
func Test_MapToMap1(t *testing.T) {
// map[int]int -> map[string]string
// empty original map.
gtest.C(t, func(t *gtest.T) {
m1 := g.MapIntInt{}
m2 := g.MapStrStr{}
t.Assert(gconv.MapToMap(m1, &m2), nil)
t.Assert(len(m1), len(m2))
})
// map[int]int -> map[string]string
gtest.C(t, func(t *gtest.T) {
m1 := g.MapIntInt{
1: 100,
2: 200,
}
m2 := g.MapStrStr{}
t.Assert(gconv.MapToMap(m1, &m2), nil)
t.Assert(m2["1"], m1[1])
t.Assert(m2["2"], m1[2])
})
// map[string]interface{} -> map[string]string
gtest.C(t, func(t *gtest.T) {
m1 := g.Map{
"k1": "v1",
"k2": "v2",
}
m2 := g.MapStrStr{}
t.Assert(gconv.MapToMap(m1, &m2), nil)
t.Assert(m2["k1"], m1["k1"])
t.Assert(m2["k2"], m1["k2"])
})
// map[string]string -> map[string]interface{}
gtest.C(t, func(t *gtest.T) {
m1 := g.MapStrStr{
"k1": "v1",
"k2": "v2",
}
m2 := g.Map{}
t.Assert(gconv.MapToMap(m1, &m2), nil)
t.Assert(m2["k1"], m1["k1"])
t.Assert(m2["k2"], m1["k2"])
})
// map[string]interface{} -> map[interface{}]interface{}
gtest.C(t, func(t *gtest.T) {
m1 := g.MapStrStr{
"k1": "v1",
"k2": "v2",
}
m2 := g.MapAnyAny{}
t.Assert(gconv.MapToMap(m1, &m2), nil)
t.Assert(m2["k1"], m1["k1"])
t.Assert(m2["k2"], m1["k2"])
})
// string -> map[string]interface{}
gtest.C(t, func(t *gtest.T) {
jsonStr := `{"id":100, "name":"john"}`
m1 := g.MapStrAny{}
t.Assert(gconv.MapToMap(jsonStr, &m1), nil)
t.Assert(m1["id"], 100)
m2 := g.MapStrAny{}
t.Assert(gconv.MapToMap([]byte(jsonStr), &m2), nil)
t.Assert(m2["id"], 100)
})
}
func Test_MapToMap2(t *testing.T) {
type User struct {
Id int
Name string
}
params := g.Map{
"key": g.Map{
"id": 1,
"name": "john",
},
}
gtest.C(t, func(t *gtest.T) {
m := make(map[string]User)
err := gconv.MapToMap(params, &m)
t.AssertNil(err)
t.Assert(len(m), 1)
t.Assert(m["key"].Id, 1)
t.Assert(m["key"].Name, "john")
})
gtest.C(t, func(t *gtest.T) {
m := (map[string]User)(nil)
err := gconv.MapToMap(params, &m)
t.AssertNil(err)
t.Assert(len(m), 1)
t.Assert(m["key"].Id, 1)
t.Assert(m["key"].Name, "john")
})
gtest.C(t, func(t *gtest.T) {
m := make(map[string]*User)
err := gconv.MapToMap(params, &m)
t.AssertNil(err)
t.Assert(len(m), 1)
t.Assert(m["key"].Id, 1)
t.Assert(m["key"].Name, "john")
})
gtest.C(t, func(t *gtest.T) {
m := (map[string]*User)(nil)
err := gconv.MapToMap(params, &m)
t.AssertNil(err)
t.Assert(len(m), 1)
t.Assert(m["key"].Id, 1)
t.Assert(m["key"].Name, "john")
})
}
func Test_MapToMapDeep(t *testing.T) {
type Ids struct {
Id int
Uid int
}
type Base struct {
Ids
Time string
}
type User struct {
Base
Name string
}
params := g.Map{
"key": g.Map{
"id": 1,
"name": "john",
},
}
gtest.C(t, func(t *gtest.T) {
m := (map[string]*User)(nil)
err := gconv.MapToMap(params, &m)
t.AssertNil(err)
t.Assert(len(m), 1)
t.Assert(m["key"].Id, 1)
t.Assert(m["key"].Name, "john")
})
}
func Test_MapToMaps(t *testing.T) {
params := g.Slice{
g.Map{"id": 1, "name": "john"},
g.Map{"id": 2, "name": "smith"},
}
gtest.C(t, func(t *gtest.T) {
var s []g.Map
err := gconv.MapToMaps(params, &s)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s, params)
})
gtest.C(t, func(t *gtest.T) {
var s []*g.Map
err := gconv.MapToMaps(params, &s)
t.AssertNil(err)
t.Assert(len(s), 2)
t.Assert(s, params)
})
gtest.C(t, func(t *gtest.T) {
jsonStr := `[{"id":100, "name":"john"},{"id":200, "name":"smith"}]`
var m1 []g.Map
t.Assert(gconv.MapToMaps(jsonStr, &m1), nil)
t.Assert(m1[0]["id"], 100)
t.Assert(m1[1]["id"], 200)
t.Assert(gconv.MapToMaps([]byte(jsonStr), &m1), nil)
t.Assert(m1[0]["id"], 100)
t.Assert(m1[1]["id"], 200)
})
}
func Test_MapToMaps_StructParams(t *testing.T) {
type User struct {
Id int
Name string
}
params := g.Slice{
User{1, "name1"},
User{2, "name2"},
}
gtest.C(t, func(t *gtest.T) {
var s []g.Map
err := gconv.MapToMaps(params, &s)
t.AssertNil(err)
t.Assert(len(s), 2)
})
gtest.C(t, func(t *gtest.T) {
var s []*g.Map
err := gconv.MapToMaps(params, &s)
t.AssertNil(err)
t.Assert(len(s), 2)
})
}