@@ -20,12 +20,15 @@ import (
2020 "context"
2121 "testing"
2222
23+ "go.mongodb.org/mongo-driver/bson/primitive"
24+
25+ "github.com/stretchr/testify/require"
26+
2327 "github.com/chenmingyong0423/go-mongox/bsonx"
2428
2529 "github.com/chenmingyong0423/go-mongox/builder/aggregation"
2630 "github.com/chenmingyong0423/go-mongox/builder/query"
2731 "github.com/chenmingyong0423/go-mongox/types"
28- "github.com/stretchr/testify/assert"
2932 "go.mongodb.org/mongo-driver/mongo"
3033 "go.mongodb.org/mongo-driver/mongo/options"
3134 "go.mongodb.org/mongo-driver/mongo/readpref"
@@ -37,8 +40,8 @@ func getCollection(t *testing.T) *mongo.Collection {
3740 Password : "test" ,
3841 AuthSource : "db-test" ,
3942 }))
40- assert .NoError (t , err )
41- assert .NoError (t , client .Ping (context .Background (), readpref .Primary ()))
43+ require .NoError (t , err )
44+ require .NoError (t , client .Ping (context .Background (), readpref .Primary ()))
4245
4346 return client .Database ("db-test" ).Collection ("test_user" )
4447}
@@ -47,8 +50,8 @@ func TestAggregator_e2e_New(t *testing.T) {
4750 collection := getCollection (t )
4851
4952 result := NewAggregator [types.TestUser ](collection )
50- assert .NotNil (t , result , "Expected non-nil Aggregator" )
51- assert .Equal (t , collection , result .collection , "Expected collection field to be initialized correctly" )
53+ require .NotNil (t , result , "Expected non-nil Aggregator" )
54+ require .Equal (t , collection , result .collection , "Expected collection field to be initialized correctly" )
5255}
5356
5457func TestAggregator_e2e_Aggregation (t * testing.T ) {
@@ -65,7 +68,7 @@ func TestAggregator_e2e_Aggregation(t *testing.T) {
6568
6669 ctx context.Context
6770 want []* types.TestUser
68- wantErr assert .ErrorAssertionFunc
71+ wantErr require .ErrorAssertionFunc
6972 }{
7073 {
7174 name : "got error when pipeline is nil" ,
@@ -77,114 +80,144 @@ func TestAggregator_e2e_Aggregation(t *testing.T) {
7780
7881 ctx : context .Background (),
7982 want : nil ,
80- wantErr : assert .Error ,
83+ wantErr : require .Error ,
8184 },
8285 {
8386 name : "decode error" ,
8487 before : func (ctx context.Context , t * testing.T ) {
8588 insertManyResult , err := collection .InsertMany (ctx , []any {
8689 & types.IllegalUser {
87- Id : "1" , Name : "cmy " , Age : "24" ,
90+ Name : "chenmingyong " , Age : "24" ,
8891 },
8992 & types.IllegalUser {
90- Id : "2" , Name : "gopher" , Age : "20" ,
93+ Name : "gopher" , Age : "20" ,
9194 },
9295 })
93- assert .NoError (t , err )
94- assert . ElementsMatch (t , [] string { "1" , "2" }, insertManyResult .InsertedIDs )
96+ require .NoError (t , err )
97+ require . Len (t , insertManyResult .InsertedIDs , 2 )
9598 },
9699 after : func (ctx context.Context , t * testing.T ) {
97- deleteResult , err := collection .DeleteMany (ctx , query .BsonBuilder (). InString ( "_id " , "1 " , "2" ). Build ( ))
98- assert .NoError (t , err )
99- assert .Equal (t , int64 (2 ), deleteResult .DeletedCount )
100+ deleteResult , err := collection .DeleteMany (ctx , query .In ( "name " , "chenmingyong " , "gopher" ))
101+ require .NoError (t , err )
102+ require .Equal (t , int64 (2 ), deleteResult .DeletedCount )
100103 },
101104 pipeline : mongo.Pipeline {},
102105 aggregationOptions : nil ,
103106 want : []* types.TestUser {},
104107 ctx : context .Background (),
105- wantErr : assert .Error ,
108+ wantErr : require .Error ,
106109 },
107110 {
108111 name : "got result when pipeline is empty" ,
109112 before : func (ctx context.Context , t * testing.T ) {
110113 insertManyResult , err := collection .InsertMany (ctx , []any {
111- types.TestUser {Id : "1" , Name : "cmy" , Age : 24 },
112- types.TestUser {Id : "2" , Name : "gopher" , Age : 20 },
114+ & types.TestUser {
115+ Name : "chenmingyong" , Age : 24 ,
116+ },
117+ & types.TestUser {
118+ Name : "gopher" , Age : 20 ,
119+ },
113120 })
114- assert .NoError (t , err )
115- assert . ElementsMatch (t , [] any { "1" , "2" }, insertManyResult .InsertedIDs )
121+ require .NoError (t , err )
122+ require . Len (t , insertManyResult .InsertedIDs , 2 )
116123 },
117124 after : func (ctx context.Context , t * testing.T ) {
118- deleteResult , err := collection .DeleteMany (ctx , query .BsonBuilder (). InString ( "_id " , [] string { "1 " , "2" } ... ). Build ( ))
119- assert .NoError (t , err )
120- assert .Equal (t , int64 (2 ), deleteResult .DeletedCount )
125+ deleteResult , err := collection .DeleteMany (ctx , query .In ( "name " , "chenmingyong " , "gopher" ))
126+ require .NoError (t , err )
127+ require .Equal (t , int64 (2 ), deleteResult .DeletedCount )
121128 },
122129 pipeline : mongo.Pipeline {},
123130 aggregationOptions : nil ,
124131 want : []* types.TestUser {
125- {Id : "2" , Name : "gopher" , Age : 20 },
126- {Id : "1" , Name : "cmy" , Age : 24 },
132+ {
133+ Name : "chenmingyong" , Age : 24 ,
134+ },
135+ {
136+ Name : "gopher" , Age : 20 ,
137+ },
127138 },
128139 ctx : context .Background (),
129- wantErr : assert .NoError ,
140+ wantErr : require .NoError ,
130141 },
131142 {
132143 name : "got result by pipeline with match stage" ,
133144 before : func (ctx context.Context , t * testing.T ) {
134145 insertManyResult , err := collection .InsertMany (ctx , []any {
135- types.TestUser {Id : "2" , Name : "gopher" , Age : 20 },
136- types.TestUser {Id : "1" , Name : "cmy" , Age : 24 },
146+ & types.TestUser {
147+ Name : "chenmingyong" , Age : 24 ,
148+ },
149+ & types.TestUser {
150+ Name : "gopher" , Age : 20 ,
151+ },
137152 })
138- assert .NoError (t , err )
139- assert . ElementsMatch (t , [] any { "1" , "2" }, insertManyResult .InsertedIDs )
153+ require .NoError (t , err )
154+ require . Len (t , insertManyResult .InsertedIDs , 2 )
140155 },
141156 after : func (ctx context.Context , t * testing.T ) {
142- deleteResult , err := collection .DeleteMany (ctx , query .BsonBuilder (). InString ( "_id " , [] string { "1 " , "2" } ... ). Build ( ))
143- assert .NoError (t , err )
144- assert .Equal (t , int64 (2 ), deleteResult .DeletedCount )
157+ deleteResult , err := collection .DeleteMany (ctx , query .In ( "name " , "chenmingyong " , "gopher" ))
158+ require .NoError (t , err )
159+ require .Equal (t , int64 (2 ), deleteResult .DeletedCount )
145160 },
146161 pipeline : aggregation .StageBsonBuilder ().Sort (bsonx .M ("age" , - 1 )).Build (),
147162 want : []* types.TestUser {
148- {Id : "1" , Name : "cmy" , Age : 24 },
149- {Id : "2" , Name : "gopher" , Age : 20 },
163+ {
164+ Name : "chenmingyong" , Age : 24 ,
165+ },
166+ {
167+ Name : "gopher" , Age : 20 ,
168+ },
150169 },
151170 ctx : context .Background (),
152- wantErr : assert .NoError ,
171+ wantErr : require .NoError ,
153172 },
154173 {
155174 name : "got result with aggregation options" ,
156175 before : func (ctx context.Context , t * testing.T ) {
157176 insertManyResult , err := collection .InsertMany (ctx , []any {
158- types.TestUser {Id : "2" , Name : "gopher" , Age : 20 },
159- types.TestUser {Id : "1" , Name : "cmy" , Age : 24 },
177+ & types.TestUser {
178+ Name : "chenmingyong" , Age : 24 ,
179+ },
180+ & types.TestUser {
181+ Name : "gopher" , Age : 20 ,
182+ },
160183 })
161- assert .NoError (t , err )
162- assert . ElementsMatch (t , [] any { "1" , "2" }, insertManyResult .InsertedIDs )
184+ require .NoError (t , err )
185+ require . Len (t , insertManyResult .InsertedIDs , 2 )
163186 },
164187 after : func (ctx context.Context , t * testing.T ) {
165- deleteResult , err := collection .DeleteMany (ctx , query .BsonBuilder (). InString ( "_id " , [] string { "1 " , "2" } ... ). Build ( ))
166- assert .NoError (t , err )
167- assert .Equal (t , int64 (2 ), deleteResult .DeletedCount )
188+ deleteResult , err := collection .DeleteMany (ctx , query .In ( "name " , "chenmingyong " , "gopher" ))
189+ require .NoError (t , err )
190+ require .Equal (t , int64 (2 ), deleteResult .DeletedCount )
168191 },
169192 pipeline : aggregation .StageBsonBuilder ().Sort (bsonx .M ("age" , 1 )).Build (),
170193 aggregationOptions : []* options.AggregateOptions {
171194 options .Aggregate ().SetCollation (& options.Collation {Locale : "en" , Strength : 2 }),
172195 },
173196 want : []* types.TestUser {
174- {Id : "1" , Name : "cmy" , Age : 24 },
175- {Id : "2" , Name : "gopher" , Age : 20 },
197+ {
198+ Name : "chenmingyong" , Age : 24 ,
199+ },
200+ {
201+ Name : "gopher" , Age : 20 ,
202+ },
176203 },
177204 ctx : context .Background (),
178- wantErr : assert .NoError ,
205+ wantErr : require .NoError ,
179206 },
180207 }
181208 for _ , tc := range testCases {
182209 t .Run (tc .name , func (t * testing.T ) {
183210 tc .before (tc .ctx , t )
184211 testUsers , err := aggregator .Pipeline (tc .pipeline ).Aggregate (tc .ctx , tc .aggregationOptions ... )
185212 tc .after (tc .ctx , t )
186- if tc .wantErr (t , err ) {
187- assert .ElementsMatch (t , tc .want , testUsers )
213+ tc .wantErr (t , err )
214+ if err == nil {
215+ require .Equal (t , len (tc .want ), len (testUsers ))
216+ for _ , tu := range testUsers {
217+ var zero primitive.ObjectID
218+ tu .ID = zero
219+ }
220+ require .ElementsMatch (t , tc .want , testUsers )
188221 }
189222 })
190223 }
@@ -210,7 +243,7 @@ func TestAggregator_e2e_AggregateWithParse(t *testing.T) {
210243 ctx context.Context
211244 result any
212245 want []* User
213- wantErr assert .ErrorAssertionFunc
246+ wantErr require .ErrorAssertionFunc
214247 }{
215248 {
216249 name : "got error when pipeline is nil" ,
@@ -221,22 +254,22 @@ func TestAggregator_e2e_AggregateWithParse(t *testing.T) {
221254 aggregationOptions : nil ,
222255 ctx : context .Background (),
223256 want : nil ,
224- wantErr : assert .Error ,
257+ wantErr : require .Error ,
225258 },
226259 {
227260 name : "got result by pipeline with match stage" ,
228261 before : func (ctx context.Context , t * testing.T ) {
229262 insertManyResult , err := collection .InsertMany (ctx , []any {
230- types.TestUser {Id : "2" , Name : "gopher" , Age : 20 },
231- types.TestUser {Id : "1" , Name : "cmy" , Age : 24 },
263+ types.TestTempUser {Id : "2" , Name : "gopher" , Age : 20 },
264+ types.TestTempUser {Id : "1" , Name : "cmy" , Age : 24 },
232265 })
233- assert .NoError (t , err )
234- assert .ElementsMatch (t , []any {"1" , "2" }, insertManyResult .InsertedIDs )
266+ require .NoError (t , err )
267+ require .ElementsMatch (t , []any {"1" , "2" }, insertManyResult .InsertedIDs )
235268 },
236269 after : func (ctx context.Context , t * testing.T ) {
237270 deleteResult , err := collection .DeleteMany (ctx , query .BsonBuilder ().InString ("_id" , []string {"1" , "2" }... ).Build ())
238- assert .NoError (t , err )
239- assert .Equal (t , int64 (2 ), deleteResult .DeletedCount )
271+ require .NoError (t , err )
272+ require .Equal (t , int64 (2 ), deleteResult .DeletedCount )
240273 },
241274 pipeline : aggregation .StageBsonBuilder ().Set (bsonx .M ("is_programmer" , true )).Build (),
242275 result : make ([]* User , 0 , 4 ),
@@ -245,22 +278,22 @@ func TestAggregator_e2e_AggregateWithParse(t *testing.T) {
245278 {Id : "2" , Name : "gopher" , Age : 20 , IsProgrammer : true },
246279 },
247280 ctx : context .Background (),
248- wantErr : assert .NoError ,
281+ wantErr : require .NoError ,
249282 },
250283 {
251284 name : "got result with aggregation options" ,
252285 before : func (ctx context.Context , t * testing.T ) {
253286 insertManyResult , err := collection .InsertMany (ctx , []any {
254- types.TestUser {Id : "2" , Name : "gopher" , Age : 20 },
255- types.TestUser {Id : "1" , Name : "cmy" , Age : 24 },
287+ types.TestTempUser {Id : "2" , Name : "gopher" , Age : 20 },
288+ types.TestTempUser {Id : "1" , Name : "cmy" , Age : 24 },
256289 })
257- assert .NoError (t , err )
258- assert .ElementsMatch (t , []any {"1" , "2" }, insertManyResult .InsertedIDs )
290+ require .NoError (t , err )
291+ require .ElementsMatch (t , []any {"1" , "2" }, insertManyResult .InsertedIDs )
259292 },
260293 after : func (ctx context.Context , t * testing.T ) {
261294 deleteResult , err := collection .DeleteMany (ctx , query .BsonBuilder ().InString ("_id" , []string {"1" , "2" }... ).Build ())
262- assert .NoError (t , err )
263- assert .Equal (t , int64 (2 ), deleteResult .DeletedCount )
295+ require .NoError (t , err )
296+ require .Equal (t , int64 (2 ), deleteResult .DeletedCount )
264297 },
265298 pipeline : aggregation .StageBsonBuilder ().Set (bsonx .M ("is_programmer" , true )).Sort (bsonx .M ("name" , 1 )).Build (),
266299 result : make ([]* User , 0 , 4 ),
@@ -270,38 +303,39 @@ func TestAggregator_e2e_AggregateWithParse(t *testing.T) {
270303 },
271304 aggregationOptions : options .Aggregate ().SetCollation (& options.Collation {Locale : "en" , Strength : 2 }),
272305 ctx : context .Background (),
273- wantErr : assert .NoError ,
306+ wantErr : require .NoError ,
274307 },
275308 {
276309 name : "got error from cursor" ,
277310 before : func (ctx context.Context , t * testing.T ) {
278311 insertManyResult , err := collection .InsertMany (ctx , []any {
279- types.TestUser {Id : "2" , Name : "gopher" , Age : 20 },
280- types.TestUser {Id : "1" , Name : "cmy" , Age : 24 },
312+ types.TestTempUser {Id : "2" , Name : "gopher" , Age : 20 },
313+ types.TestTempUser {Id : "1" , Name : "cmy" , Age : 24 },
281314 })
282- assert .NoError (t , err )
283- assert .ElementsMatch (t , []any {"1" , "2" }, insertManyResult .InsertedIDs )
315+ require .NoError (t , err )
316+ require .ElementsMatch (t , []any {"1" , "2" }, insertManyResult .InsertedIDs )
284317 },
285318 after : func (ctx context.Context , t * testing.T ) {
286319 deleteResult , err := collection .DeleteMany (ctx , query .BsonBuilder ().InString ("_id" , []string {"1" , "2" }... ).Build ())
287- assert .NoError (t , err )
288- assert .Equal (t , int64 (2 ), deleteResult .DeletedCount )
320+ require .NoError (t , err )
321+ require .Equal (t , int64 (2 ), deleteResult .DeletedCount )
289322 },
290323 pipeline : aggregation .StageBsonBuilder ().Set (bsonx .M ("is_programmer" , true )).Sort (bsonx .M ("name" , 1 )).Build (),
291324 result : []string {},
292325 want : []* User {},
293326 aggregationOptions : options .Aggregate ().SetCollation (& options.Collation {Locale : "en" , Strength : 2 }),
294327 ctx : context .Background (),
295- wantErr : assert .Error ,
328+ wantErr : require .Error ,
296329 },
297330 }
298331 for _ , tc := range testCases {
299332 t .Run (tc .name , func (t * testing.T ) {
300333 tc .before (tc .ctx , t )
301334 err := aggregator .Pipeline (tc .pipeline ).AggregateWithParse (tc .ctx , & tc .result , tc .aggregationOptions )
302335 tc .after (tc .ctx , t )
303- if tc .wantErr (t , err ) {
304- assert .ElementsMatch (t , tc .want , tc .result )
336+ tc .wantErr (t , err )
337+ if err == nil {
338+ require .ElementsMatch (t , tc .want , tc .result )
305339 }
306340 })
307341 }
0 commit comments