88 "github.com/samber/lo"
99 "go.mongodb.org/mongo-driver/bson"
1010 "go.mongodb.org/mongo-driver/mongo"
11+ "golang.org/x/exp/maps"
1112)
1213
1314func (suite * IntegrationTestSuite ) TestShardingMismatch () {
@@ -33,15 +34,22 @@ func (suite *IntegrationTestSuite) TestShardingMismatch() {
3334 )
3435 }
3536
36- collections := mslices .Of (
37- "idonly" ,
38- "numtype" ,
39- "id_and_foo" ,
40- "sharded_dst" ,
41- )
37+ // Create indexes as needed to ensure that we only
38+ // check for sharding mismatches.
39+ allIndexes := map [string ]map [string ]bson.D {
40+ "numtype" : {
41+ "foo_1" : {{"foo" , 1 }},
42+ },
43+ "id_and_foo" : {
44+ "foo_1__id_1" : {{"foo" , 1 }, {"_id" , 1 }},
45+ "_id_1_foo_1" : {{"_id" , 1 }, {"foo" , 1 }},
46+ },
47+ "idonly" : {},
48+ "sharded_dst" : {},
49+ }
4250
4351 for c , client := range mslices .Of (suite .srcMongoClient , suite .dstMongoClient ) {
44- for _ , collName := range collections {
52+ for collName , indexMap := range allIndexes {
4553 suite .Require ().NoError (
4654 client .Database (dbname ).CreateCollection (
4755 ctx ,
@@ -50,6 +58,27 @@ func (suite *IntegrationTestSuite) TestShardingMismatch() {
5058 "should create %#q on " + lo .Ternary (c == 0 , "source" , "destinatinon" ),
5159 collName ,
5260 )
61+
62+ if len (indexMap ) > 0 {
63+
64+ suite .Require ().NoError (
65+ client .Database (dbname ).RunCommand (
66+ ctx ,
67+ bson.D {
68+ {"createIndexes" , collName },
69+ {"indexes" , lo .Map (
70+ maps .Keys (indexMap ),
71+ func (idxName string , _ int ) bson.D {
72+ return bson.D {
73+ {"name" , idxName },
74+ {"key" , indexMap [idxName ]},
75+ }
76+ },
77+ )},
78+ },
79+ ).Err (),
80+ )
81+ }
5382 }
5483 }
5584
@@ -79,40 +108,6 @@ func (suite *IntegrationTestSuite) TestShardingMismatch() {
79108 bson.D {{"_id" , 1 }, {"foo" , 1 }},
80109 "src" ,
81110 )
82- } else {
83- suite .Require ().NoError (
84- suite .srcMongoClient .Database (dbname ).RunCommand (
85- ctx ,
86- bson.D {
87- {"createIndexes" , "numtype" },
88- {"indexes" , []bson.D {
89- {
90- {"name" , "foo_1" },
91- {"key" , bson.D {{"foo" , 1 }}},
92- },
93- }},
94- },
95- ).Err (),
96- )
97-
98- suite .Require ().NoError (
99- suite .srcMongoClient .Database (dbname ).RunCommand (
100- ctx ,
101- bson.D {
102- {"createIndexes" , "id_and_foo" },
103- {"indexes" , []bson.D {
104- {
105- {"name" , "foo_1__id_1" },
106- {"key" , bson.D {{"foo" , 1 }, {"_id" , 1 }}},
107- },
108- {
109- {"name" , "_id_1_foo_1" },
110- {"key" , bson.D {{"_id" , 1 }, {"foo" , 1 }}},
111- },
112- }},
113- },
114- ).Err (),
115- )
116111 }
117112
118113 if dstInfo .Topology == util .TopologySharded {
@@ -144,7 +139,7 @@ func (suite *IntegrationTestSuite) TestShardingMismatch() {
144139 shardCollection (
145140 suite .dstMongoClient ,
146141 "sharded_dst" ,
147- bson.D {{"foo " , 1 }},
142+ bson.D {{"_id " , 1 }},
148143 "dst" ,
149144 )
150145 } else {
@@ -186,7 +181,7 @@ func (suite *IntegrationTestSuite) TestShardingMismatch() {
186181 verifier := suite .BuildVerifier ()
187182
188183 namespaces := lo .Map (
189- collections ,
184+ maps . Keys ( allIndexes ) ,
190185 func (collName string , _ int ) string {
191186 return dbname + "." + collName
192187 },
@@ -210,7 +205,7 @@ func (suite *IntegrationTestSuite) TestShardingMismatch() {
210205 var tasks []VerificationTask
211206 suite .Require ().NoError (cursor .All (ctx , & tasks ))
212207
213- suite .Require ().Len (tasks , len (collections ))
208+ suite .Require ().Len (tasks , len (allIndexes ))
214209
215210 if srcInfo .Topology == util .TopologySharded && dstInfo .Topology == util .TopologySharded {
216211 taskMap := mslices .ToMap (
0 commit comments