-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathmodel_test.go
132 lines (100 loc) · 2.84 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
package pop
import (
"testing"
"time"
"github.com/stretchr/testify/require"
)
func Test_Model_TableName(t *testing.T) {
r := require.New(t)
m := Model{Value: User{}}
r.Equal(m.TableName(), "users")
m = Model{Value: &User{}}
r.Equal(m.TableName(), "users")
m = Model{Value: &Users{}}
r.Equal(m.TableName(), "users")
m = Model{Value: []User{}}
r.Equal(m.TableName(), "users")
m = Model{Value: &[]User{}}
r.Equal(m.TableName(), "users")
m = Model{Value: []*User{}}
r.Equal(m.TableName(), "users")
}
type tn struct{}
func (tn) TableName() string {
return "this is my table name"
}
func Test_TableName(t *testing.T) {
r := require.New(t)
cases := []interface{}{
tn{},
[]tn{},
}
for _, tc := range cases {
m := Model{Value: tc}
r.Equal("this is my table name", 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 := Model{Value: &TimeTimestamp{}}
// Override time.Now()
t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
nowFunc = func() time.Time { return t0 }
defer func() { nowFunc = time.Now }()
m.touchCreatedAt()
m.touchUpdatedAt()
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")
nowFunc = func() time.Time { return t0 }
defer func() { nowFunc = time.Now }()
createdAt := nowFunc().Add(-36 * time.Hour)
m := Model{Value: &TimeTimestamp{CreatedAt: createdAt}}
m.touchCreatedAt()
m.touchUpdatedAt()
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 := Model{Value: &UnixTimestamp{}}
// Override time.Now()
t0, _ := time.Parse(time.RFC3339, "2019-07-14T00:00:00Z")
nowFunc = func() time.Time { return t0 }
defer func() { nowFunc = time.Now }()
m.touchCreatedAt()
m.touchUpdatedAt()
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")
nowFunc = func() time.Time { return t0 }
defer func() { nowFunc = time.Now }()
createdAt := int(time.Now().Add(-36 * time.Hour).Unix())
m := Model{Value: &UnixTimestamp{CreatedAt: createdAt}}
m.touchCreatedAt()
m.touchUpdatedAt()
v := m.Value.(*UnixTimestamp)
r.Equal(createdAt, v.CreatedAt)
r.Equal(int(t0.Unix()), v.UpdatedAt)
}