-
Notifications
You must be signed in to change notification settings - Fork 27
/
record_description_test.go
207 lines (181 loc) · 6.54 KB
/
record_description_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package godb
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
)
type typeToDescribe struct {
ID int `db:"id"`
}
type otherTypeToDescribe struct {
ID int `db:"id"`
}
func (*otherTypeToDescribe) TableName() string {
return "others"
}
func TestBuildRecordDescription(t *testing.T) {
Convey("Given a single instance pointer", t, func() {
instance := &typeToDescribe{}
Convey("extratType will extract the type information", func() {
recordDesc, err := buildRecordDescription(instance)
So(err, ShouldBeNil)
So(recordDesc, ShouldNotBeNil)
So(recordDesc.instanceType.Name(), ShouldEqual, "typeToDescribe")
So(recordDesc.isSlice, ShouldBeFalse)
So(recordDesc.isSliceOfPointers, ShouldBeFalse)
So(recordDesc.structMapping.Name, ShouldEndWith, "typeToDescribe")
})
})
Convey("Given a slice pointer", t, func() {
slice := make([]typeToDescribe, 0)
Convey("extratType will extract the type information", func() {
recordDesc, err := buildRecordDescription(&slice)
So(err, ShouldBeNil)
So(recordDesc, ShouldNotBeNil)
So(recordDesc.instanceType.Name(), ShouldEqual, "typeToDescribe")
So(recordDesc.isSlice, ShouldBeTrue)
So(recordDesc.isSliceOfPointers, ShouldBeFalse)
So(recordDesc.structMapping.Name, ShouldEndWith, "typeToDescribe")
})
})
Convey("Given a slice pointer of pointers", t, func() {
slice := make([]*typeToDescribe, 0)
Convey("extratType will extract the type information", func() {
recordDesc, err := buildRecordDescription(&slice)
So(err, ShouldBeNil)
So(recordDesc, ShouldNotBeNil)
So(recordDesc.instanceType.Name(), ShouldEqual, "typeToDescribe")
So(recordDesc.isSlice, ShouldBeTrue)
So(recordDesc.isSliceOfPointers, ShouldBeTrue)
So(recordDesc.structMapping.Name, ShouldEndWith, "typeToDescribe")
})
})
}
func TestFillRecord(t *testing.T) {
Convey("Given a single instance descriptor ", t, func() {
instancePtr := &typeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("fillRecord call the given func with the instance pointer", func() {
recordDesc.fillRecord(func(record interface{}) error {
So(record, ShouldEqual, instancePtr)
record.(*typeToDescribe).ID = 123
return nil
})
So(instancePtr.ID, ShouldEqual, 123)
})
})
Convey("Given a slice descriptor ", t, func() {
slice := make([]typeToDescribe, 0)
recordDesc, _ := buildRecordDescription(&slice)
Convey("fillRecord call the given func with a new instance pointer", func() {
recordDesc.fillRecord(func(record interface{}) error {
So(record, ShouldHaveSameTypeAs, &typeToDescribe{})
record.(*typeToDescribe).ID = 123
return nil
})
So(len(slice), ShouldEqual, 1)
So(slice[0], ShouldHaveSameTypeAs, typeToDescribe{})
So(slice[0].ID, ShouldEqual, 123)
})
})
Convey("Given a slice of pointers descriptor ", t, func() {
slice := make([]*typeToDescribe, 0)
recordDesc, _ := buildRecordDescription(&slice)
Convey("fillRecord call the given func with a new instance pointer", func() {
recordDesc.fillRecord(func(record interface{}) error {
So(record, ShouldHaveSameTypeAs, &typeToDescribe{})
record.(*typeToDescribe).ID = 123
return nil
})
So(len(slice), ShouldEqual, 1)
So(slice[0], ShouldHaveSameTypeAs, &typeToDescribe{})
So((*slice[0]).ID, ShouldEqual, 123)
})
})
}
func TestGetOneInstancePointer(t *testing.T) {
Convey("Given a single instance descriptor ", t, func() {
instancePtr := &typeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getOneInstancePointer returns a pointer to the instance", func() {
p := recordDesc.getOneInstancePointer()
So(p, ShouldEqual, instancePtr)
})
})
Convey("Given a slice descriptor ", t, func() {
slice := make([]typeToDescribe, 0)
recordDesc, _ := buildRecordDescription(&slice)
Convey("getOneInstancePointer returns a pointer to the instance", func() {
p := recordDesc.getOneInstancePointer()
So(p, ShouldHaveSameTypeAs, &typeToDescribe{})
})
})
}
func TestLen(t *testing.T) {
Convey("Given a single instance descriptor", t, func() {
instancePtr := &typeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("Len returns 1", func() {
So(recordDesc.len(), ShouldEqual, 1)
})
})
Convey("Given a slice descriptor", t, func() {
slice := make([]typeToDescribe, 0)
slice = append(slice, typeToDescribe{})
slice = append(slice, typeToDescribe{})
recordDesc, _ := buildRecordDescription(&slice)
Convey("Len returns the len of the slice", func() {
So(recordDesc.len(), ShouldEqual, 2)
})
})
}
func TestIndex(t *testing.T) {
Convey("Given a single instance descriptor", t, func() {
instancePtr := &typeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("Index returns the pointer to the instance", func() {
So(recordDesc.index(0), ShouldEqual, instancePtr)
})
})
Convey("Given a slice descriptor", t, func() {
slice := make([]typeToDescribe, 0)
first := typeToDescribe{ID: 123}
second := typeToDescribe{ID: 456}
slice = append(slice, first, second)
recordDesc, _ := buildRecordDescription(&slice)
Convey("Index returns the pointer to the instance at the given index", func() {
So(recordDesc.index(0).(*typeToDescribe).ID, ShouldEqual, 123)
So(recordDesc.index(1).(*typeToDescribe).ID, ShouldEqual, 456)
recordDesc.index(0).(*typeToDescribe).ID = 1234
So(recordDesc.index(0).(*typeToDescribe).ID, ShouldEqual, 1234)
})
})
Convey("Given a slice of pointers descriptor", t, func() {
slice := make([]*typeToDescribe, 0)
first := typeToDescribe{ID: 123}
second := typeToDescribe{ID: 456}
slice = append(slice, &first, &second)
recordDesc, _ := buildRecordDescription(&slice)
Convey("Index returns the pointer to the instance at the given index", func() {
So(recordDesc.index(0), ShouldEqual, &first)
So(recordDesc.index(1), ShouldEqual, &second)
})
})
}
func TestTableName(t *testing.T) {
Convey("Given a record descriptor", t, func() {
instancePtr := &typeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getTableName returns by default the struct name a table name", func() {
tableName, _ := recordDesc.getTableName()
So(tableName, ShouldEqual, "typeToDescribe")
})
})
Convey("Given a record descriptor of type implmenting tableNamer interface", t, func() {
instancePtr := &otherTypeToDescribe{}
recordDesc, _ := buildRecordDescription(instancePtr)
Convey("getTableName returns the string given by TableName()", func() {
tableName, _ := recordDesc.getTableName()
So(tableName, ShouldEqual, "others")
})
})
}