@@ -309,3 +309,50 @@ func TestEnsureSkipListIndex(t *testing.T) {
309
309
}
310
310
}
311
311
}
312
+
313
+ // TestEnsureTTLIndex creates a collection with a ttl index.
314
+ func TestEnsureTTLIndex (t * testing.T ) {
315
+ c := createClientFromEnv (t , true )
316
+ db := ensureDatabase (nil , c , "index_test" , nil , t )
317
+ skipBelowVersion (c , "3.5" , t )
318
+
319
+ col := ensureCollection (nil , db , "ttl_index_test" , nil , t )
320
+ idx , created , err := col .EnsureTTLIndex (nil , "createdAt" , 3600 , nil )
321
+ if err != nil {
322
+ t .Fatalf ("Failed to create new index: %s" , describe (err ))
323
+ }
324
+ if ! created {
325
+ t .Error ("Expected created to be true, got false" )
326
+ }
327
+ if idxType := idx .Type (); idxType != driver .TTLIndex {
328
+ t .Errorf ("Expected TTLIndex, found `%s`" , idxType )
329
+ }
330
+
331
+ // Index must exists now
332
+ if found , err := col .IndexExists (nil , idx .Name ()); err != nil {
333
+ t .Fatalf ("Failed to check index '%s' exists: %s" , idx .Name (), describe (err ))
334
+ } else if ! found {
335
+ t .Errorf ("Index '%s' does not exist, expected it to exist" , idx .Name ())
336
+ }
337
+
338
+ // Ensure again, created must be false now
339
+ _ , created , err = col .EnsureTTLIndex (nil , "createdAt" , 3600 , nil )
340
+ if err != nil {
341
+ t .Fatalf ("Failed to re-create index: %s" , describe (err ))
342
+ }
343
+ if created {
344
+ t .Error ("Expected created to be false, got true" )
345
+ }
346
+
347
+ // Remove index
348
+ if err := idx .Remove (nil ); err != nil {
349
+ t .Fatalf ("Failed to remove index '%s': %s" , idx .Name (), describe (err ))
350
+ }
351
+
352
+ // Index must not exists now
353
+ if found , err := col .IndexExists (nil , idx .Name ()); err != nil {
354
+ t .Fatalf ("Failed to check index '%s' exists: %s" , idx .Name (), describe (err ))
355
+ } else if found {
356
+ t .Errorf ("Index '%s' does exist, expected it not to exist" , idx .Name ())
357
+ }
358
+ }
0 commit comments