Exemplo n.º 1
0
func TestTransformArray(t *testing.T) {
	otherUser := User{ID: 8, FirstName: "Me"}
	ser := New().UseSnakeCase().Pick("ID", "FirstName")
	cases := []interface{}{[]User{user, otherUser}, [2]User{user, otherUser}}
	for _, c := range cases {
		arr, err := ser.TransformArray(c)
		assert.Nil(t, err)
		assert.Len(t, arr, 2)
		assert.Equal(t, arr[0]["id"], user.ID)
		assert.Equal(t, arr[1]["id"], otherUser.ID)
		assert.Equal(t, arr[0]["first_name"], user.FirstName)
	}
	_, err := ser.TransformArray(1)
	assert.NotNil(t, err)
}
Exemplo n.º 2
0
func TestAddFunc(t *testing.T) {
	m := New().AddFunc("FullName", func(u interface{}) interface{} {
		return u.(User).FirstName + " " + u.(User).LastName
	}).Transform(user)
	assert.Contains(t, m, "FullName")
	assert.Equal(t, user.FirstName+" "+user.LastName, m["FullName"])
}
Exemplo n.º 3
0
func TestAddIf(t *testing.T) {
	m := New().AddIf(alwaysFalse, "Foo", "Bar").Transform(user)
	assert.NotContains(t, m, "Foo")
	m = New().AddIf(alwaysTrue, "Foo", "Bar").Transform(user)
	assert.Contains(t, m, "Foo")
	assert.Equal(t, "Bar", m["Foo"])
}
Exemplo n.º 4
0
func TestPickFunc(t *testing.T) {
	m := New().PickFunc(func(t interface{}) interface{} {
		return t.(time.Time).Format(time.RFC3339)
	}, "CreatedAt", "UpdatedAt").Transform(user)
	assert.Contains(t, m, "CreatedAt")
	assert.Contains(t, m, "UpdatedAt")
	assert.Equal(t, m["CreatedAt"], user.CreatedAt.Format(time.RFC3339))
}
Exemplo n.º 5
0
func TestAddFuncIf(t *testing.T) {
	m := New().AddFuncIf(alwaysTrue, "Foo", func(u interface{}) interface{} {
		return "Bar"
	}).Transform(user)
	assert.Contains(t, m, "Foo")
	assert.Equal(t, m["Foo"], "Bar")
	m = New().AddFuncIf(alwaysFalse, "Foo", identity).Transform(user)
	assert.NotContains(t, m, "Foo")
}
Exemplo n.º 6
0
func TestPickFuncIf(t *testing.T) {
	m := New().PickFuncIf(alwaysTrue, func(t interface{}) interface{} {
		return t.(time.Time).Format(time.RFC3339)
	}, "CreatedAt", "UpdatedAt").PickFuncIf(alwaysFalse, identity, "Email").Transform(user)
	assert.Contains(t, m, "CreatedAt")
	assert.Contains(t, m, "UpdatedAt")
	assert.NotContains(t, m, "Email")
	assert.Equal(t, m["CreatedAt"], user.CreatedAt.Format(time.RFC3339))
}
Exemplo n.º 7
0
func TestPick(t *testing.T) {
	m := New().Pick("ID", "Age").Transform(user)
	assert.Contains(t, m, "ID")
	assert.Contains(t, m, "Age")
	assert.NotContains(t, m, "FirstName")
	m = New().Pick("ID").Pick("Age").Transform(user)
	assert.Contains(t, m, "ID")
	assert.Contains(t, m, "Age")
	assert.Equal(t, m["ID"], user.ID)
}
Exemplo n.º 8
0
func TestAdd(t *testing.T) {
	m := New().Add("Foo", "Bar").Transform(user)
	assert.Contains(t, m, "Foo")
	assert.Equal(t, "Bar", m["Foo"])
}