-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
model_test.go
216 lines (170 loc) · 4.71 KB
/
model_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
208
209
210
211
212
213
214
215
216
package pop
import (
"context"
"fmt"
"testing"
"time"
"github.com/gobuffalo/pop/v6/testdata/models/a"
"github.com/gobuffalo/pop/v6/testdata/models/ac"
"github.com/gobuffalo/pop/v6/testdata/models/b"
"github.com/gobuffalo/pop/v6/testdata/models/bc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_Model_TableName(t *testing.T) {
for k, v := range []interface{}{
User{},
&User{},
&Users{},
Users{},
[]*User{},
&[]*User{},
[]User{},
&[]User{},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
r := require.New(t)
m := Model{Value: v}
r.Equal("users", m.TableName())
})
}
}
type tn struct{}
func (tn) TableName() string {
return "this is my table name"
}
type tnc struct{}
func (tnc) TableName(ctx context.Context) string {
return ctx.Value("name").(string)
}
// A failing test case for #477
func Test_TableNameCache(t *testing.T) {
r := assert.New(t)
r.Equal("usera", (&Model{Value: a.User{}}).TableName())
r.Equal("userb", (&Model{Value: b.User{}}).TableName())
r.Equal("usera", (&Model{Value: []a.User{}}).TableName())
r.Equal("userb", (&Model{Value: []b.User{}}).TableName())
}
// A failing test case for #477
func Test_TableNameContextCache(t *testing.T) {
ctx := context.WithValue(context.Background(), "name", "context_table")
r := assert.New(t)
r.Equal("context_table_useras", (&Model{Value: ac.User{}, ctx: ctx}).TableName())
r.Equal("context_table_userbs", (&Model{Value: bc.User{}, ctx: ctx}).TableName())
r.Equal("context_table_useras", (&Model{Value: []ac.User{}, ctx: ctx}).TableName())
r.Equal("context_table_userbs", (&Model{Value: []bc.User{}, ctx: ctx}).TableName())
}
func Test_TableName(t *testing.T) {
r := require.New(t)
cases := []interface{}{
tn{},
&tn{},
[]tn{},
&[]tn{},
[]*tn{},
&[]*tn{},
}
for _, tc := range cases {
m := Model{Value: tc}
r.Equal("this is my table name", m.TableName())
}
}
func Test_TableNameContext(t *testing.T) {
r := require.New(t)
tn := "context_table_names"
ctx := context.WithValue(context.Background(), "name", tn)
cases := []interface{}{
tnc{},
[]tnc{},
}
for _, tc := range cases {
m := Model{Value: tc, ctx: ctx}
r.Equal(tn, m.TableName())
}
}
type TimeTimestamp struct {
ID int `db:"id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type UnixTimestamp struct {
ID int `db:"id"`
CreatedAt int `db:"created_at"`
UpdatedAt int `db:"updated_at"`
}
func Test_Touch_Time_Timestamp(t *testing.T) {
r := require.New(t)
m := NewModel(&TimeTimestamp{}, context.Background())
// Override time.Now()
t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
m.setCreatedAt(t0)
m.setUpdatedAt(t0)
v := m.Value.(*TimeTimestamp)
r.Equal(t0, v.CreatedAt)
r.Equal(t0, v.UpdatedAt)
}
func Test_Touch_Time_Timestamp_With_Existing_Value(t *testing.T) {
r := require.New(t)
// Override time.Now()
t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
createdAt := nowFunc().Add(-36 * time.Hour)
m := NewModel(&TimeTimestamp{CreatedAt: createdAt}, context.Background())
m.setCreatedAt(t0)
m.setUpdatedAt(t0)
v := m.Value.(*TimeTimestamp)
r.Equal(createdAt, v.CreatedAt)
r.Equal(t0, v.UpdatedAt)
}
func Test_Touch_Unix_Timestamp(t *testing.T) {
r := require.New(t)
m := NewModel(&UnixTimestamp{}, context.Background())
// Override time.Now()
t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
m.setCreatedAt(t0)
m.setUpdatedAt(t0)
v := m.Value.(*UnixTimestamp)
r.Equal(int(t0.Unix()), v.CreatedAt)
r.Equal(int(t0.Unix()), v.UpdatedAt)
}
func Test_Touch_Unix_Timestamp_With_Existing_Value(t *testing.T) {
r := require.New(t)
// Override time.Now()
t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
createdAt := int(time.Now().Add(-36 * time.Hour).Unix())
m := NewModel(&UnixTimestamp{CreatedAt: createdAt}, context.Background())
m.setCreatedAt(t0)
m.setUpdatedAt(t0)
v := m.Value.(*UnixTimestamp)
r.Equal(createdAt, v.CreatedAt)
r.Equal(int(t0.Unix()), v.UpdatedAt)
}
func Test_IDField(t *testing.T) {
r := require.New(t)
type testCustomID struct {
ID int `db:"custom_id"`
}
m := Model{Value: &testCustomID{ID: 1}}
r.Equal("custom_id", m.IDField())
type testNormalID struct {
ID int
}
m = Model{Value: &testNormalID{ID: 1}}
r.Equal("id", m.IDField())
}
type testPrefixID struct {
ID int `db:"custom_id"`
}
func (t testPrefixID) TableName() string {
return "foo.bar"
}
func Test_WhereID(t *testing.T) {
r := require.New(t)
m := Model{Value: &testPrefixID{ID: 1}}
r.Equal("foo_bar.custom_id = ?", m.WhereID())
r.Equal("foo_bar.custom_id = :custom_id", m.WhereNamedID())
type testNormalID struct {
ID int
}
m = Model{Value: &testNormalID{ID: 1}}
r.Equal("id", m.IDField())
}