-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_test.go
More file actions
217 lines (179 loc) Β· 4.93 KB
/
Copy pathmodel_test.go
File metadata and controls
217 lines (179 loc) Β· 4.93 KB
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
217
package mongoose_test
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tinh-tinh/mongoose/v2"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Models struct {
BaseSchema `bson:"inline"`
Title string `bson:"title"`
Author string `bson:"author"`
}
func (b Models) CollectionName() string {
return "models"
}
func Test_Model(t *testing.T) {
connect := mongoose.New(os.Getenv("MONGO_URI"))
connect.SetDB("test")
model := mongoose.NewModel[Models]()
model.SetConnect(connect)
err := model.DeleteMany(nil)
assert.Nil(t, err)
type CreateBook struct {
Title string `bson:"title"`
Level int `bson:"level"`
}
model.Set(&CreateBook{Title: "abc", Level: 1})
err = model.Save()
require.Nil(t, err)
firstOne, err := model.FindOne(nil)
require.Nil(t, err)
require.NotNil(t, firstOne)
require.Equal(t, "abc", firstOne.Title)
type UpdateBook struct {
ID primitive.ObjectID `bson:"_id"`
Title string `bson:"title"`
Level int `bson:"level"`
}
model.Set(&UpdateBook{ID: firstOne.ID, Title: "xyz", Level: 1})
err = model.Save()
require.Nil(t, err)
reFirst, err := model.FindOne(nil)
require.Nil(t, err)
require.Equal(t, "xyz", reFirst.Title)
}
type Address struct {
Line string `bson:"line"`
State string `bson:"state"`
City string `bson:"city"`
Country string `bson:"country"`
}
type Location struct {
BaseSchema `bson:"inline"`
Longitude float64 `bson:"longitude"`
Latitude float64 `bson:"latitude"`
Address *Address `bson:"address"`
}
func (l Location) CollectionName() string {
return "recursive"
}
func Test_Recusive(t *testing.T) {
connect := mongoose.New(os.Getenv("MONGO_URI"))
connect.SetDB("test")
model := mongoose.NewModel[Location]()
model.SetConnect(connect)
err := model.DeleteMany(nil)
assert.Nil(t, err)
_, err = model.Create(&Location{
Longitude: 1,
Latitude: 2,
Address: &Address{
Line: "line",
State: "state",
City: "city",
Country: "country",
},
})
require.Nil(t, err)
firstOne, err := model.FindOne(nil)
require.Nil(t, err)
require.NotNil(t, firstOne)
require.Equal(t, float64(1), firstOne.Longitude)
require.Equal(t, float64(2), firstOne.Latitude)
require.Equal(t, "line", firstOne.Address.Line)
require.Equal(t, "state", firstOne.Address.State)
require.Equal(t, "city", firstOne.Address.City)
require.Equal(t, "country", firstOne.Address.Country)
err = model.Update(nil, &Location{
Longitude: 2,
Latitude: 1,
Address: &Address{
Line: "line2",
State: "state2",
City: "city2",
Country: "country2",
},
})
require.Nil(t, err)
reFirst, err := model.FindOne(nil)
require.Nil(t, err)
require.Equal(t, float64(2), reFirst.Longitude)
require.Equal(t, float64(1), reFirst.Latitude)
require.Equal(t, "line2", reFirst.Address.Line)
require.Equal(t, "state2", reFirst.Address.State)
require.Equal(t, "city2", reFirst.Address.City)
require.Equal(t, "country2", reFirst.Address.Country)
}
type User struct {
BaseSchema `bson:"inline"`
Email string `bson:"email"`
Name string `bson:"name"`
}
func (u User) CollectionName() string {
return "indexes"
}
func TestIndex(t *testing.T) {
userModel := mongoose.NewModel[User]()
userModel.Index(bson.D{{Key: "email", Value: 1}}, options.Index().SetUnique(true))
connect := mongoose.New(os.Getenv("MONGO_URI"))
connect.SetDB("test")
userModel.SetConnect(connect)
}
func Test_ToDoc(t *testing.T) {
_, err := mongoose.ToDoc("nil")
assert.NotNil(t, err)
}
type Student struct {
BaseTimestamp `bson:"inline"`
FirstName string `bson:"firstName" validate:"isAlpha"`
LastName string `bson:"lastName" validate:"isAlpha"`
Email string `bson:"email" validate:"isEmail"`
}
func (s Student) CollectionName() string {
return "students"
}
func Test_Validator(t *testing.T) {
connect := mongoose.New(os.Getenv("MONGO_URI"))
connect.SetDB("test")
model := mongoose.NewModel[Student]()
model.SetConnect(connect)
err := model.DeleteMany(nil)
assert.Nil(t, err)
_, err = model.Create(&Student{
FirstName: "12",
LastName: "222",
Email: "111",
})
require.NotNil(t, err)
_, err = model.Create(&Student{
FirstName: "John",
LastName: "Dpe",
Email: "john@gmail.com",
})
require.Nil(t, err)
_, err = model.FindOneAndUpdate(nil, &Student{Email: "120"})
assert.NotNil(t, err)
_, err = model.FindOneAndReplace(nil, &Student{Email: "120"})
assert.NotNil(t, err)
_, err = model.CreateMany([]*Student{
{FirstName: "2"},
})
assert.NotNil(t, err)
_, err = model.CreateMany([]*Student{
{
FirstName: "Ricardo",
LastName: "Kaka",
Email: "kaka@gmail.com",
},
})
require.Nil(t, err)
err = model.Update(map[string]any{}, &Student{FirstName: "$##$$#"})
assert.NotNil(t, err)
err = model.UpdateMany(map[string]any{}, &Student{FirstName: "$##$$#"})
assert.NotNil(t, err)
}