@@ -552,3 +552,150 @@ func TestCollectionStatistics(t *testing.T) {
552552 }
553553 }
554554}
555+
556+ // TestCollectionMinReplFactCreate creates a collection with minReplicationFactor != 1
557+ func TestCollectionMinReplFactCreate (t * testing.T ) {
558+ c := createClientFromEnv (t , true )
559+ skipBelowVersion (c , "3.5" , t )
560+ db := ensureDatabase (nil , c , "collection_min_repl_test" , nil , t )
561+ name := "test_min_repl_create"
562+ minRepl := 2
563+ options := driver.CreateCollectionOptions {
564+ ReplicationFactor : minRepl ,
565+ MinReplicationFactor : minRepl ,
566+ }
567+ if _ , err := db .CreateCollection (nil , name , & options ); err != nil {
568+ t .Fatalf ("Failed to create collection '%s': %s" , name , describe (err ))
569+ }
570+
571+ // Collection must exist now
572+ if found , err := db .CollectionExists (nil , name ); err != nil {
573+ t .Errorf ("CollectionExists('%s') failed: %s" , name , describe (err ))
574+ } else if ! found {
575+ t .Errorf ("CollectionExists('%s') return false, expected true" , name )
576+ }
577+ // Check if the collection has a minReplicationFactor
578+ if col , err := db .Collection (nil , name ); err != nil {
579+ t .Errorf ("Collection('%s') failed: %s" , name , describe (err ))
580+ } else {
581+ if prop , err := col .Properties (nil ); err != nil {
582+ t .Errorf ("Properties() failed: %s" , describe (err ))
583+ } else {
584+ if prop .MinReplicationFactor != minRepl {
585+ t .Errorf ("Collection does not have the correct min replication factor value, expected `%d`, found `%d`" , minRepl , prop .MinReplicationFactor )
586+ }
587+ }
588+ }
589+ }
590+
591+ // TestCollectionMinReplFactInvalid creates a collection with minReplicationFactor > replicationFactor
592+ func TestCollectionMinReplFactInvalid (t * testing.T ) {
593+ c := createClientFromEnv (t , true )
594+ skipBelowVersion (c , "3.5" , t )
595+ skipNoCluster (c , t )
596+ db := ensureDatabase (nil , c , "collection_min_repl_test" , nil , t )
597+ name := "test_min_repl_create_invalid"
598+ minRepl := 2
599+ options := driver.CreateCollectionOptions {
600+ ReplicationFactor : minRepl ,
601+ MinReplicationFactor : minRepl + 1 ,
602+ }
603+ if _ , err := db .CreateCollection (nil , name , & options ); err == nil {
604+ t .Fatalf ("CreateCollection('%s') did not fail" , name )
605+ }
606+ // Collection must not exist now
607+ if found , err := db .CollectionExists (nil , name ); err != nil {
608+ t .Errorf ("CollectionExists('%s') failed: %s" , name , describe (err ))
609+ } else if found {
610+ t .Errorf ("Collection %s should not exist" , name )
611+ }
612+ }
613+
614+ // TestCollectionMinReplFactClusterInv tests if minReplicationFactor is forwarded to ClusterInfo
615+ func TestCollectionMinReplFactClusterInv (t * testing.T ) {
616+ c := createClientFromEnv (t , true )
617+ skipBelowVersion (c , "3.5" , t )
618+ skipNoCluster (c , t )
619+ db := ensureDatabase (nil , c , "collection_min_repl_test" , nil , t )
620+ name := "test_min_repl_cluster_invent"
621+ minRepl := 2
622+ ensureCollection (nil , db , name , & driver.CreateCollectionOptions {
623+ ReplicationFactor : minRepl ,
624+ MinReplicationFactor : minRepl ,
625+ }, t )
626+
627+ cc , err := c .Cluster (nil )
628+ if err != nil {
629+ t .Fatalf ("Failed to get Cluster: %s" , describe (err ))
630+ }
631+
632+ inv , err := cc .DatabaseInventory (nil , db )
633+ if err != nil {
634+ t .Fatalf ("Failed to get Database Inventory: %s" , describe (err ))
635+ }
636+
637+ col , found := inv .CollectionByName (name )
638+ if ! found {
639+ t .Fatalf ("Failed to get find collection: %s" , describe (err ))
640+ }
641+
642+ if col .Parameters .MinReplicationFactor != minRepl {
643+ t .Errorf ("Collection does not have the correct min replication factor value, expected `%d`, found `%d`" , minRepl , col .Parameters .MinReplicationFactor )
644+ }
645+ }
646+
647+ // TestCollectionMinReplFactSetProp updates the minimal replication factor using SetProperties
648+ func TestCollectionMinReplFactSetProp (t * testing.T ) {
649+ c := createClientFromEnv (t , true )
650+ skipBelowVersion (c , "3.5" , t )
651+ skipNoCluster (c , t )
652+ db := ensureDatabase (nil , c , "collection_min_repl_test" , nil , t )
653+ name := "test_min_repl_set_prop"
654+ minRepl := 2
655+ col := ensureCollection (nil , db , name , & driver.CreateCollectionOptions {
656+ ReplicationFactor : minRepl ,
657+ MinReplicationFactor : minRepl ,
658+ }, t )
659+
660+ if err := col .SetProperties (nil , driver.SetCollectionPropertiesOptions {
661+ MinReplicationFactor : 1 ,
662+ }); err != nil {
663+ t .Fatalf ("Failed to update properties: %s" , describe (err ))
664+ }
665+
666+ if prop , err := col .Properties (nil ); err != nil {
667+ t .Fatalf ("Failed to get properties: %s" , describe (err ))
668+ } else {
669+ if prop .MinReplicationFactor != 1 {
670+ t .Fatalf ("MinReplicationFactor not updated, expected %d, found %d" , 1 , prop .MinReplicationFactor )
671+ }
672+ }
673+ }
674+
675+ // TestCollectionMinReplFactSetPropInvalid updates the minimal replication factor to an invalid value using SetProperties
676+ func TestCollectionMinReplFactSetPropInvalid (t * testing.T ) {
677+ c := createClientFromEnv (t , true )
678+ skipBelowVersion (c , "3.5" , t )
679+ skipNoCluster (c , t )
680+ db := ensureDatabase (nil , c , "collection_min_repl_test" , nil , t )
681+ name := "test_min_repl_set_prop_inv"
682+ minRepl := 2
683+ col := ensureCollection (nil , db , name , & driver.CreateCollectionOptions {
684+ ReplicationFactor : minRepl ,
685+ MinReplicationFactor : minRepl ,
686+ }, t )
687+
688+ if err := col .SetProperties (nil , driver.SetCollectionPropertiesOptions {
689+ MinReplicationFactor : minRepl + 1 ,
690+ }); err == nil {
691+ t .Errorf ("SetProperties did not fail" )
692+ }
693+
694+ if prop , err := col .Properties (nil ); err != nil {
695+ t .Fatalf ("Failed to get properties: %s" , describe (err ))
696+ } else {
697+ if prop .MinReplicationFactor != minRepl {
698+ t .Fatalf ("MinReplicationFactor not updated, expected %d, found %d" , minRepl , prop .MinReplicationFactor )
699+ }
700+ }
701+ }
0 commit comments