-
Notifications
You must be signed in to change notification settings - Fork 0
/
stom_complex_test.go
196 lines (168 loc) · 4.35 KB
/
stom_complex_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
package stom_test
import (
"database/sql"
"testing"
"time"
"github.com/elgris/stom"
"github.com/go-sql-driver/mysql"
)
type ComplexItem struct {
SomeItem
*BasicItem
AnotherBasicItem `db:"-"`
Author sql.NullString `db:"author"`
Generation uint32
Meta Metainfo `db:"meta"`
}
type ParentItem struct {
Base string `db:"base"`
}
type BasicItem struct {
*ParentItem
Posted mysql.NullTime `db:"basic_posted"`
}
type AnotherBasicItem struct {
AnotherBase string `db:"another_base"`
}
type Metainfo struct {
Tag string
Value string
MaybeValue sql.NullInt64
SomeFlag bool
Additional map[string]interface{}
}
// ToMap implements ToMappable interface to be used by SToM
func (this Metainfo) ToMap() (map[string]interface{}, error) {
return map[string]interface{}{
"tag": this.Tag,
"value": this.Value,
"add": this.Additional,
}, nil
}
func TestComplexItem_TagValues(t *testing.T) {
s := stom.MustNewStom(ComplexItem{}).SetTag("db")
expectedTagValues := map[string]interface{}{
"id": nil,
"name": nil,
"number": nil,
"created": nil,
"updated": nil,
"discount": nil,
"price": nil,
"reserved": nil,
"points": nil,
"rating": nil,
"visible": nil,
"base": nil,
"basic_posted": nil,
"author": nil,
"meta": nil,
}
tagValues := s.TagValues()
if len(expectedTagValues) != len(tagValues) {
t.Fatalf("number of expected tag values %d does not match number of actual tag values %d",
len(expectedTagValues),
len(tagValues))
}
for _, v := range tagValues {
if _, ok := expectedTagValues[v]; !ok {
t.Fatalf("could not find tag value %s in list of expected tagValues", v)
}
}
}
func TestComplexItem_DefaultPolicy(t *testing.T) {
stom.SetTag("db")
stom.SetDefault("DEFAULT")
stom.SetPolicy(stom.PolicyUseDefault)
expected := map[string]interface{}{
"id": 1,
"name": "item_1",
"number": 11,
"created": time.Unix(10000, 0),
"updated": mysql.NullTime{time.Unix(11000, 0), true},
"discount": 111.0,
"price": 1111.0,
"reserved": sql.NullBool{true, true},
"points": "DEFAULT",
"rating": sql.NullFloat64{1.0, true},
"visible": true,
"base": "base",
"basic_posted": "DEFAULT",
"author": "DEFAULT",
"meta": map[string]interface{}{
"tag": "metatag",
"value": "valvalval",
"add": map[string]interface{}{
"foo": 12,
"bar": sql.NullBool{true, false},
},
},
}
doTest(t, stom.ToMapperFunc(stom.ConvertToMap), getTestComplexItem(), expected)
}
func TestComplexItem_ExcludePolicy(t *testing.T) {
stom.SetTag("db")
stom.SetDefault("DEFAULT")
stom.SetPolicy(stom.PolicyExclude)
expected := map[string]interface{}{
"id": 1,
"name": "item_1",
"number": 11,
"created": time.Unix(10000, 0),
"updated": mysql.NullTime{time.Unix(11000, 0), true},
"discount": 111.0,
"price": 1111.0,
"reserved": sql.NullBool{true, true},
"rating": sql.NullFloat64{1.0, true},
"visible": true,
"base": "base",
"meta": map[string]interface{}{
"tag": "metatag",
"value": "valvalval",
"add": map[string]interface{}{
"foo": 12,
"bar": sql.NullBool{true, false},
},
},
}
doTest(t, stom.ToMapperFunc(stom.ConvertToMap), getTestComplexItem(), expected)
}
func getTestComplexItem() ComplexItem {
discount := 111.0
item := ComplexItem{
SomeItem: SomeItem{
ID: 1,
Name: "item_1",
Number: 11,
Checksum: 111,
Created: time.Unix(10000, 0),
Updated: mysql.NullTime{time.Unix(11000, 0), true},
Price: 1111.0,
Discount: &discount,
IsReserved: sql.NullBool{true, true},
Points: sql.NullInt64{int64(11), false},
Rating: sql.NullFloat64{1.0, true},
IsVisible: true,
Notes: "foo",
SomeIgnoreField: 10,
},
BasicItem: &BasicItem{
ParentItem: &ParentItem{Base: "base"},
Posted: mysql.NullTime{time.Now(), false},
},
AnotherBasicItem: AnotherBasicItem{
AnotherBase: "anotherBase",
},
Author: sql.NullString{"invalid_author", false},
Generation: 123,
}
item.Meta.Tag = "metatag"
item.Meta.Value = "valvalval"
item.Meta.MaybeValue = sql.NullInt64{int64(11), true}
item.Meta.SomeFlag = true
item.Meta.Additional = map[string]interface{}{
"foo": 12,
"bar": sql.NullBool{true, false},
}
return item
}