bcc8f606 by slene

orm add test about CustomField

1 parent 6f93b2bc
...@@ -3,6 +3,7 @@ package orm ...@@ -3,6 +3,7 @@ package orm
3 import ( 3 import (
4 "fmt" 4 "fmt"
5 "os" 5 "os"
6 "strings"
6 "time" 7 "time"
7 8
8 _ "github.com/go-sql-driver/mysql" 9 _ "github.com/go-sql-driver/mysql"
...@@ -10,6 +11,56 @@ import ( ...@@ -10,6 +11,56 @@ import (
10 _ "github.com/mattn/go-sqlite3" 11 _ "github.com/mattn/go-sqlite3"
11 ) 12 )
12 13
14 // A true/false field.
15 type SliceStringField []string
16
17 func (e SliceStringField) Value() []string {
18 return []string(e)
19 }
20
21 func (e *SliceStringField) Set(d []string) {
22 *e = SliceStringField(d)
23 }
24
25 func (e *SliceStringField) Add(v string) {
26 *e = append(*e, v)
27 }
28
29 func (e *SliceStringField) String() string {
30 return strings.Join(e.Value(), ",")
31 }
32
33 func (e *SliceStringField) FieldType() int {
34 return TypeCharField
35 }
36
37 func (e *SliceStringField) SetRaw(value interface{}) error {
38 switch d := value.(type) {
39 case []string:
40 e.Set(d)
41 case string:
42 if len(d) > 0 {
43 parts := strings.Split(d, ",")
44 v := make([]string, 0, len(parts))
45 for _, p := range parts {
46 v = append(v, strings.TrimSpace(p))
47 }
48 e.Set(v)
49 }
50 default:
51 return fmt.Errorf("<SliceStringField.SetRaw> unknown value `%v`", value)
52 }
53 return nil
54 }
55
56 func (e *SliceStringField) RawValue() interface{} {
57 return e.String()
58 }
59
60 func (e *SliceStringField) Clean() error {
61 return nil
62 }
63
13 type Data struct { 64 type Data struct {
14 Id int 65 Id int
15 Boolean bool 66 Boolean bool
...@@ -78,6 +129,7 @@ type User struct { ...@@ -78,6 +129,7 @@ type User struct {
78 Posts []*Post `orm:"reverse(many)" json:"-"` 129 Posts []*Post `orm:"reverse(many)" json:"-"`
79 ShouldSkip string `orm:"-"` 130 ShouldSkip string `orm:"-"`
80 Nums int 131 Nums int
132 Langs SliceStringField `orm:"size(100)"`
81 } 133 }
82 134
83 func (u *User) TableIndex() [][]string { 135 func (u *User) TableIndex() [][]string {
......
...@@ -472,6 +472,23 @@ The program—and web server—godoc processes Go source files to extract docume ...@@ -472,6 +472,23 @@ The program—and web server—godoc processes Go source files to extract docume
472 } 472 }
473 } 473 }
474 474
475 func TestCustomField(t *testing.T) {
476 user := User{Id: 2}
477 err := dORM.Read(&user)
478 throwFailNow(t, err)
479
480 user.Langs = append(user.Langs, "zh-CN", "en-US")
481 _, err = dORM.Update(&user, "Langs")
482 throwFailNow(t, err)
483
484 user = User{Id: 2}
485 err = dORM.Read(&user)
486 throwFailNow(t, err)
487 throwFailNow(t, AssertIs(len(user.Langs), 2))
488 throwFailNow(t, AssertIs(user.Langs[0], "zh-CN"))
489 throwFailNow(t, AssertIs(user.Langs[1], "en-US"))
490 }
491
475 func TestExpr(t *testing.T) { 492 func TestExpr(t *testing.T) {
476 user := &User{} 493 user := &User{}
477 qs := dORM.QueryTable(user) 494 qs := dORM.QueryTable(user)
...@@ -728,7 +745,7 @@ func TestValues(t *testing.T) { ...@@ -728,7 +745,7 @@ func TestValues(t *testing.T) {
728 var maps []Params 745 var maps []Params
729 qs := dORM.QueryTable("user") 746 qs := dORM.QueryTable("user")
730 747
731 num, err := qs.Values(&maps) 748 num, err := qs.OrderBy("Id").Values(&maps)
732 throwFail(t, err) 749 throwFail(t, err)
733 throwFail(t, AssertIs(num, 3)) 750 throwFail(t, AssertIs(num, 3))
734 if num == 3 { 751 if num == 3 {
...@@ -736,7 +753,7 @@ func TestValues(t *testing.T) { ...@@ -736,7 +753,7 @@ func TestValues(t *testing.T) {
736 throwFail(t, AssertIs(maps[2]["Profile"], nil)) 753 throwFail(t, AssertIs(maps[2]["Profile"], nil))
737 } 754 }
738 755
739 num, err = qs.Values(&maps, "UserName", "Profile__Age") 756 num, err = qs.OrderBy("Id").Values(&maps, "UserName", "Profile__Age")
740 throwFail(t, err) 757 throwFail(t, err)
741 throwFail(t, AssertIs(num, 3)) 758 throwFail(t, AssertIs(num, 3))
742 if num == 3 { 759 if num == 3 {
...@@ -750,7 +767,7 @@ func TestValuesList(t *testing.T) { ...@@ -750,7 +767,7 @@ func TestValuesList(t *testing.T) {
750 var list []ParamsList 767 var list []ParamsList
751 qs := dORM.QueryTable("user") 768 qs := dORM.QueryTable("user")
752 769
753 num, err := qs.ValuesList(&list) 770 num, err := qs.OrderBy("Id").ValuesList(&list)
754 throwFail(t, err) 771 throwFail(t, err)
755 throwFail(t, AssertIs(num, 3)) 772 throwFail(t, AssertIs(num, 3))
756 if num == 3 { 773 if num == 3 {
...@@ -758,7 +775,7 @@ func TestValuesList(t *testing.T) { ...@@ -758,7 +775,7 @@ func TestValuesList(t *testing.T) {
758 throwFail(t, AssertIs(list[2][9], nil)) 775 throwFail(t, AssertIs(list[2][9], nil))
759 } 776 }
760 777
761 num, err = qs.ValuesList(&list, "UserName", "Profile__Age") 778 num, err = qs.OrderBy("Id").ValuesList(&list, "UserName", "Profile__Age")
762 throwFail(t, err) 779 throwFail(t, err)
763 throwFail(t, AssertIs(num, 3)) 780 throwFail(t, AssertIs(num, 3))
764 if num == 3 { 781 if num == 3 {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!