From e95bef13315c3f5b4c49fe2f26633c3d66676d35 Mon Sep 17 00:00:00 2001 From: miraclesu Date: Fri, 8 Apr 2016 22:26:28 +0800 Subject: [PATCH] orm: add test case for json & jsonb type support --- orm/models_test.go | 4 ++++ orm/orm_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/orm/models_test.go b/orm/models_test.go index cbc51dcc..d1e3e66e 100644 --- a/orm/models_test.go +++ b/orm/models_test.go @@ -112,6 +112,8 @@ type Data struct { Boolean bool Char string `orm:"size(50)"` Text string `orm:"type(text)"` + Json string `orm:"type(json);default({\"name\":\"json\"})"` + Jsonb string `orm:"type(jsonb)"` Time time.Time `orm:"type(time)"` Date time.Time `orm:"type(date)"` DateTime time.Time `orm:"column(datetime)"` @@ -137,6 +139,8 @@ type DataNull struct { Boolean bool `orm:"null"` Char string `orm:"null;size(50)"` Text string `orm:"null;type(text)"` + Json string `orm:"type(json);null"` + Jsonb string `orm:"type(jsonb);null"` Time time.Time `orm:"null;type(time)"` Date time.Time `orm:"null;type(date)"` DateTime time.Time `orm:"null;column(datetime)"` diff --git a/orm/orm_test.go b/orm/orm_test.go index 0c615328..e8909da8 100644 --- a/orm/orm_test.go +++ b/orm/orm_test.go @@ -241,6 +241,8 @@ var DataValues = map[string]interface{}{ "Boolean": true, "Char": "char", "Text": "text", + "Json": `{"name":"json"}`, + "Jsonb": `{"name": "jsonb"}`, "Time": time.Now(), "Date": time.Now(), "DateTime": time.Now(), @@ -266,6 +268,9 @@ func TestDataTypes(t *testing.T) { ind := reflect.Indirect(reflect.ValueOf(&d)) for name, value := range DataValues { + if name == "Json" { + continue + } e := ind.FieldByName(name) e.Set(reflect.ValueOf(value)) } @@ -310,10 +315,18 @@ func TestNullDataTypes(t *testing.T) { throwFail(t, err) throwFail(t, AssertIs(id, 1)) + data := `{"ok":1,"data":{"arr":[1,2],"msg":"gopher"}}` + d = DataNull{ID: 1, Json: data} + num, err := dORM.Update(&d) + throwFail(t, err) + throwFail(t, AssertIs(num, 1)) + d = DataNull{ID: 1} err = dORM.Read(&d) throwFail(t, err) + throwFail(t, AssertIs(d.Json, data)) + throwFail(t, AssertIs(d.NullBool.Valid, false)) throwFail(t, AssertIs(d.NullString.Valid, false)) throwFail(t, AssertIs(d.NullInt64.Valid, false))