-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_test.go
171 lines (127 loc) · 3.6 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
package friendlymongo_test
import (
"testing"
"time"
"github.com/pmatteo/friendlymongo"
"github.com/stretchr/testify/assert"
"go.mongodb.org/mongo-driver/bson/primitive"
)
func TestBaseModel_OnCreate(t *testing.T) {
t.Parallel()
model := &friendlymongo.BaseModel{}
assert.True(t, model.ID.IsZero())
assert.Empty(t, model.CreatedAt)
assert.Empty(t, model.UpdatedAt)
model.OnCreate()
assert.False(t, model.ID.IsZero())
assert.NotEmpty(t, model.CreatedAt)
assert.NotEmpty(t, model.UpdatedAt)
}
func TestBaseModel_OnUpdate(t *testing.T) {
t.Parallel()
tm := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
id := primitive.NewObjectID()
model := &friendlymongo.BaseModel{
ID: id,
CreatedAt: tm,
UpdatedAt: tm,
}
assert.Equal(t, id, model.ID)
assert.Equal(t, tm, model.CreatedAt)
assert.Equal(t, tm, model.UpdatedAt)
model.OnUpdate()
assert.NotEqual(t, primitive.NilObjectID, model.ID)
assert.Equal(t, id, model.ID)
assert.Equal(t, tm, model.CreatedAt)
assert.NotEqual(t, tm, model.UpdatedAt)
}
func TestBaseModel_OnReplace(t *testing.T) {
t.Parallel()
tm := time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC)
id := primitive.NewObjectID()
model := &friendlymongo.BaseModel{
ID: id,
CreatedAt: tm,
UpdatedAt: tm,
}
assert.Equal(t, id, model.ID)
assert.Equal(t, tm, model.CreatedAt)
assert.Equal(t, tm, model.UpdatedAt)
model.OnReplace()
assert.Equal(t, id, model.ID)
assert.Equal(t, tm, model.CreatedAt)
assert.NotEqual(t, tm, model.UpdatedAt)
}
type address struct {
Street string `bson:"street"`
Number int `bson:"number"`
City string `bson:"city"`
}
type customModel struct {
friendlymongo.BaseModel `bson:",inline"`
Name string `bson:"name"`
Email string `bson:"email"`
Active bool `bson:"active"`
ActivationDate *time.Time `bson:"activationDate"`
Address *address `bson:"address"`
}
type otherModel struct {
friendlymongo.BaseModel
FK primitive.ObjectID `bson:"fk"`
Group string `bson:"group"`
Permissions []string `bson:"permissions"`
}
type artwork struct {
friendlymongo.BaseModel `bson:",inline"`
Title string `bson:"title"`
Artist string `bson:"artist"`
Year int `bson:"year,omitempty"`
Price float32 `bson:"price"`
Tags []string `bson:"tags"`
}
func newCustomModel(name, email string, active bool, a *address) *customModel {
m := &customModel{
Name: name,
Email: email,
Active: active,
Address: a,
}
if active {
now := time.Now()
m.ActivationDate = &now
}
return m
}
func TestCustomModel_OnCreate(t *testing.T) {
t.Parallel()
model := newCustomModel("John Doe", "test@test", true, basicAddress)
assert.True(t, model.ID.IsZero())
assert.Empty(t, model.CreatedAt)
assert.Empty(t, model.UpdatedAt)
model.OnCreate()
assert.NotEqual(t, primitive.NilObjectID, model.ID)
assert.NotEmpty(t, model.CreatedAt)
assert.NotEmpty(t, model.UpdatedAt)
}
func TestCustomModel_OnUpdate(t *testing.T) {
t.Parallel()
model := newCustomModel("John Doe", "test@test", true, basicAddress)
model.OnCreate()
createdAt := model.CreatedAt
updatedAt := model.UpdatedAt
model.OnUpdate()
assert.False(t, model.ID.IsZero())
assert.Equal(t, createdAt, model.CreatedAt)
assert.NotEqual(t, updatedAt, model.UpdatedAt)
}
func TestCustomModel_OnReplace(t *testing.T) {
t.Parallel()
model := newCustomModel("John Doe", "test@test", true, basicAddress)
model.OnCreate()
createdAt := model.CreatedAt
updatedAt := model.UpdatedAt
model.OnReplace()
assert.False(t, model.ID.IsZero())
assert.Equal(t, createdAt, model.CreatedAt)
assert.NotEqual(t, updatedAt, model.UpdatedAt)
}