예제 #1
0
func TestTransformEmptyArray(t *testing.T) {
	ser := New().UseSnakeCase().Pick("ID", "FirstName")
	users := []User{}
	result := ser.MustTransformArray(users)
	assert.NotNil(t, result)
	assert.IsType(t, []map[string]interface{}{}, result)
	assert.Len(t, result, 0)
}
예제 #2
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)
}
예제 #3
0
func TestMustTransformArray(t *testing.T) {
	ser := New().UseSnakeCase().Pick("ID", "FirstName")
	users := []User{user, User{ID: 8, FirstName: "Me"}}
	assert.Len(t, ser.MustTransformArray(users), 2)
	assert.Panics(t, func() { ser.MustTransformArray(1) })
}