/
primary_keys_test.go
49 lines (43 loc) · 1.3 KB
/
primary_keys_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package norm
import (
"github.com/picatic/norm/field"
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestPrimaryKeyer(t *testing.T) {
Convey("PrimaryKeyer", t, func() {
model := &MockModel{}
Convey("SinglePrimaryKey", func() {
pk := NewSinglePrimaryKey(field.Name("Id"))
So(pk.Fields(), ShouldResemble, field.Names{field.Name("Id")})
fields, err := model.PrimaryKey().Generator(model)
So(len(fields), ShouldEqual, 0)
So(err, ShouldBeNil)
v, _ := model.Id.Value()
So(v, ShouldBeNil)
})
Convey("MultiplePrimaryKey", func() {
pk := NewMultiplePrimaryKey(field.Names{"Id", "Org"})
So(pk.Fields(), ShouldResemble, field.Names{"Id", "Org"})
fields, err := model.PrimaryKey().Generator(model)
So(len(fields), ShouldEqual, 0)
So(err, ShouldBeNil)
v, _ := model.Id.Value()
So(v, ShouldBeNil)
v, _ = model.Org.Value()
So(v, ShouldBeNil)
})
Convey("CustomPrimaryKey", func() {
pk := NewCustomPrimaryKey(field.Names{"Id", "Org"}, func(pk PrimaryKeyer, model Model) (field.Names, error) {
f, _ := ModelGetField(model, "Id")
f.Scan("abc-123")
return field.Names{"Id"}, nil
})
fields, err := pk.Generator(model)
So(len(fields), ShouldEqual, 1)
So(err, ShouldBeNil)
v, _ := model.Id.Value()
So(v, ShouldEqual, "abc-123")
})
})
}