Пример #1
0
func TestPickIf(t *testing.T) {
	m := New().
		PickIf(alwaysTrue, "ID", "FirstName").
		PickIf(alwaysFalse, "Email").Transform(user)
	assert.Contains(t, m, "ID")
	assert.Contains(t, m, "FirstName")
	assert.NotContains(t, m, "Email")
	assert.NotContains(t, m, "Age")
}
Пример #2
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"])
}
Пример #3
0
func TestConvertKeys(t *testing.T) {
	m := New().PickAll().ConvertKeys(func(s string) string {
		return "dummy_" + s
	}).Transform(user)
	assert.Contains(t, m, "dummy_ID")
	assert.Contains(t, m, "dummy_FirstName")
	assert.NotContains(t, m, "ID")
}
Пример #4
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")
}
Пример #5
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))
}
Пример #6
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)
}
Пример #7
0
func TestOmitIf(t *testing.T) {
	m := New().PickAll().OmitIf(func(u interface{}) bool {
		return u.(User).HideName
	}, "FirstName", "LastName").Transform(user)
	assert.NotContains(t, m, "FirstName", "LastName")

	m = New().PickAll().OmitIf(func(u interface{}) bool {
		return u.(User).Age < 18
	}, "Birthday", "Age").Transform(user)
	assert.Contains(t, m, "Birthday")
	assert.Contains(t, m, "Age")
}
Пример #8
0
func TestUseCamelCase(t *testing.T) {
	m := New().UseCamelCase().PickAll().Transform(user)
	assert.Contains(t, m, "id")
	assert.Contains(t, m, "firstName")
	assert.NotContains(t, m, "FirstName")
}
Пример #9
0
func TestOmit(t *testing.T) {
	m := New().PickAll().Omit("Birthday", "FirstName").Transform(user)
	assert.NotContains(t, m, "Birthday")
	assert.NotContains(t, m, "FirstName")
	assert.Contains(t, m, "ID")
}