Support different value for valuer and setter value
@@ -15,12 +15,17 @@ func format(value interface{}) string {
return fmt.Sprint(utils.Indirect(reflect.ValueOf(value)).Interface())
}
-func checkMeta(record interface{}, meta *resource.Meta, value interface{}, t *testing.T) {
+func checkMeta(record interface{}, meta *resource.Meta, value interface{}, t *testing.T, expectedValues ...string) {
var (
context = &qor.Context{DB: testutils.TestDB()}
metaValue = &resource.MetaValue{Name: meta.Name, Value: value}
+ expectedValue = fmt.Sprint(value)
)
+ for _, v := range expectedValues {
+ expectedValue = v
+ }
+
meta.PreInitialize()
meta.Initialize()
@@ -29,8 +34,8 @@ func checkMeta(record interface{}, meta *resource.Meta, value interface{}, t *te
t.Errorf("No error should happen, but got %v", context.Errors)
}
- if result := meta.Valuer(record, context); format(result) != fmt.Sprint(value) {
- t.Errorf("Wrong value, should be %v, but got %v", format(value), result)
+ if result := meta.Valuer(record, context); format(result) != expectedValue {
+ t.Errorf("Wrong value, should be %v, but got %v", expectedValue, format(result))
}
}
@@ -95,3 +100,63 @@ func TestIntMetaValuerAndSetter(t *testing.T) {
checkMeta(&user, meta4, "48", t)
}
+
+func TestFloatMetaValuerAndSetter(t *testing.T) {
+ user := &struct {
+ Age float64
+ Age2 *float64
+ }{}
+
+ res := resource.New(&user)
+
+ meta := &resource.Meta{
+ Name: "Age",
+ BaseResource: res,
+ }
+
+ checkMeta(&user, meta, 18.5, t)
+
+ meta2 := &resource.Meta{
+ Name: "Age2",
+ BaseResource: res,
+ }
+
+ checkMeta(&user, meta2, "28.5", t)
+}
+
+func TestBoolMetaValuerAndSetter(t *testing.T) {
+ user := &struct {
+ Actived bool
+ Actived2 *bool
+ }{}
+
+ res := resource.New(&user)
+
+ meta := &resource.Meta{
+ Name: "Actived",
+ BaseResource: res,
+ }
+
+ checkMeta(&user, meta, "true", t)
+
+ meta2 := &resource.Meta{
+ Name: "Actived2",
+ BaseResource: res,
+ }
+
+ checkMeta(&user, meta2, "true", t)
+
+ meta3 := &resource.Meta{
+ Name: "Actived",
+ BaseResource: res,
+ }
+
+ checkMeta(&user, meta3, "", t, "false")
+
+ meta4 := &resource.Meta{
+ Name: "Actived2",
+ BaseResource: res,
+ }
+
+ checkMeta(&user, meta4, "f", t, "false")
+}
