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) }
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) }
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) }) }