Skip to content

Commit d8e36b4

Browse files
author
iwysiu
committed
add test for non-equal
1 parent 012d30b commit d8e36b4

File tree

2 files changed

+130
-61
lines changed

2 files changed

+130
-61
lines changed

mongo/gridfs/bucket.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ func (b *Bucket) findChunks(ctx context.Context, fileID interface{}) (*mongo.Cur
453453
}
454454

455455
// returns true if the 2 index documents are equal
456-
func indexDocsEqual(expected, actual bsoncore.Document) (bool, error) {
456+
func numericalIndexDocsEqual(expected, actual bsoncore.Document) (bool, error) {
457457
if bytes.Equal(expected, actual) {
458458
return true, nil
459459
}
@@ -482,10 +482,8 @@ func indexDocsEqual(expected, actual bsoncore.Document) (bool, error) {
482482
actualInt, actualOK := actualVal.AsInt64OK()
483483
expectedInt, expectedOK := expectedVal.AsInt64OK()
484484

485+
//GridFS indexes always have numeric values
485486
if !actualOK || !expectedOK {
486-
if actualVal.Equal(expectedVal) {
487-
continue
488-
}
489487
return false, nil
490488
}
491489

@@ -497,7 +495,7 @@ func indexDocsEqual(expected, actual bsoncore.Document) (bool, error) {
497495
}
498496

499497
// Create an index if it doesn't already exist
500-
func createIndexIfNotExists(ctx context.Context, iv mongo.IndexView, model mongo.IndexModel) error {
498+
func createNumericalIndexIfNotExists(ctx context.Context, iv mongo.IndexView, model mongo.IndexModel) error {
501499
c, err := iv.List(ctx)
502500
if err != nil {
503501
return err
@@ -520,7 +518,7 @@ func createIndexIfNotExists(ctx context.Context, iv mongo.IndexView, model mongo
520518

521519
keyElemDoc := keyElem.Document()
522520

523-
found, err := indexDocsEqual(modelKeysDoc, bsoncore.Document(keyElemDoc))
521+
found, err := numericalIndexDocsEqual(modelKeysDoc, bsoncore.Document(keyElemDoc))
524522
if err != nil {
525523
return err
526524
}
@@ -567,10 +565,10 @@ func (b *Bucket) createIndexes(ctx context.Context) error {
567565
Options: options.Index().SetUnique(true),
568566
}
569567

570-
if err = createIndexIfNotExists(ctx, filesIv, filesModel); err != nil {
568+
if err = createNumericalIndexIfNotExists(ctx, filesIv, filesModel); err != nil {
571569
return err
572570
}
573-
if err = createIndexIfNotExists(ctx, chunksIv, chunksModel); err != nil {
571+
if err = createNumericalIndexIfNotExists(ctx, chunksIv, chunksModel); err != nil {
574572
return err
575573
}
576574

mongo/integration/gridfs_test.go

Lines changed: 124 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -47,67 +47,138 @@ func TestGridFS(x *testing.T) {
4747
findIndex(findCtx, mt, mt.DB.Collection("fs.chunks"), true, "key", "files_id")
4848
})
4949
// should not create a new index if index is numerically the same
50-
mt.Run("indexes not created if equivalent indexes exist", func(mt *mtest.T) {
51-
// add indexes with floats to collections manually
52-
res := mt.DB.RunCommand(context.Background(),
53-
bson.D{
54-
{"createIndexes", "fs.files"},
55-
{"indexes", bson.A{
56-
bson.D{
57-
{"key", bson.D{{"filename", float64(1.0)}, {"uploadDate", float64(1.0)}}},
58-
{"name", "filename_1_uploadDate_1"},
59-
},
60-
}},
50+
mt.Run("equivalent indexes", func(mt *mtest.T) {
51+
tests := []struct {
52+
name string
53+
filesIndex bson.D
54+
chunksIndex bson.D
55+
newIndexes bool
56+
}{
57+
{
58+
"numerically equal",
59+
bson.D{
60+
{"key", bson.D{{"filename", float64(1.0)}, {"uploadDate", float64(1.0)}}},
61+
{"name", "filename_1_uploadDate_1"},
62+
},
63+
bson.D{
64+
{"key", bson.D{{"files_id", float64(1.0)}, {"n", float64(1.0)}}},
65+
{"name", "files_id_1_n_1"},
66+
{"unique", true},
67+
},
68+
false,
6169
},
62-
)
63-
assert.Nil(mt, res.Err(), "createIndexes error: %v", res.Err())
64-
65-
res = mt.DB.RunCommand(context.Background(),
66-
bson.D{
67-
{"createIndexes", "fs.chunks"},
68-
{"indexes", bson.A{
69-
bson.D{
70-
{"key", bson.D{{"files_id", float64(1.0)}, {"n", float64(1.0)}}},
71-
{"name", "files_id_1_n_1"},
72-
{"unique", true},
73-
},
74-
}},
70+
{
71+
"numerically inequal",
72+
bson.D{
73+
{"key", bson.D{{"filename", float64(-1.0)}, {"uploadDate", float64(1.0)}}},
74+
{"name", "filename_-1_uploadDate_1"},
75+
},
76+
bson.D{
77+
{"key", bson.D{{"files_id", float64(1.0)}, {"n", float64(-1.0)}}},
78+
{"name", "files_id_1_n_-1"},
79+
{"unique", true},
80+
},
81+
true,
7582
},
76-
)
77-
assert.Nil(mt, res.Err(), "createIndexes error: %v", res.Err())
83+
}
84+
for _, test := range tests {
85+
mt.Run(test.name, func(mt *mtest.T) {
86+
mt.Run("OpenUploadStream", func(mt *mtest.T) {
87+
// add indexes with floats to collections manually
88+
res := mt.DB.RunCommand(context.Background(),
89+
bson.D{
90+
{"createIndexes", "fs.files"},
91+
{"indexes", bson.A{
92+
test.filesIndex,
93+
}},
94+
},
95+
)
96+
assert.Nil(mt, res.Err(), "createIndexes error: %v", res.Err())
7897

79-
mt.ClearEvents()
80-
mt.Run("OpenUploadStream", func(mt *mtest.T) {
81-
bucket, err := gridfs.NewBucket(mt.DB)
82-
assert.Nil(mt, err, "NewBucket error: %v", err)
98+
res = mt.DB.RunCommand(context.Background(),
99+
bson.D{
100+
{"createIndexes", "fs.chunks"},
101+
{"indexes", bson.A{
102+
test.chunksIndex,
103+
}},
104+
},
105+
)
106+
assert.Nil(mt, res.Err(), "createIndexes error: %v", res.Err())
83107

84-
_, err = bucket.OpenUploadStream("filename")
85-
assert.Nil(mt, err, "OpenUploadStream error: %v", err)
108+
mt.ClearEvents()
86109

87-
mt.FilterStartedEvents(func(evt *event.CommandStartedEvent) bool {
88-
return evt.CommandName == "createIndexes"
89-
})
90-
evt := mt.GetStartedEvent()
91-
if evt != nil {
92-
mt.Fatalf("expected no createIndexes events but got %v", evt.Command)
93-
}
94-
})
95-
mt.Run("UploadFromStream", func(mt *mtest.T) {
96-
var fileContent []byte
97-
bucket, err := gridfs.NewBucket(mt.DB)
98-
assert.Nil(mt, err, "NewBucket error: %v", err)
110+
bucket, err := gridfs.NewBucket(mt.DB)
111+
assert.Nil(mt, err, "NewBucket error: %v", err)
112+
defer func() {
113+
bucket.Drop()
114+
} ()
99115

100-
_, err = bucket.UploadFromStream("filename", bytes.NewBuffer(fileContent))
101-
assert.Nil(mt, err, "UploadFromStream error: %v", err)
116+
_, err = bucket.OpenUploadStream("filename")
117+
assert.Nil(mt, err, "OpenUploadStream error: %v", err)
102118

103-
mt.FilterStartedEvents(func(evt *event.CommandStartedEvent) bool {
104-
return evt.CommandName == "createIndexes"
119+
mt.FilterStartedEvents(func(evt *event.CommandStartedEvent) bool {
120+
return evt.CommandName == "createIndexes"
121+
})
122+
evt := mt.GetStartedEvent()
123+
if test.newIndexes {
124+
if evt == nil {
125+
mt.Fatalf("expected createIndexes events but got none")
126+
}
127+
} else {
128+
if evt != nil {
129+
mt.Fatalf("expected no createIndexes events but got %v", evt.Command)
130+
}
131+
}
132+
})
133+
mt.Run("UploadFromStream", func(mt *mtest.T) {
134+
// add indexes with floats to collections manually
135+
res := mt.DB.RunCommand(context.Background(),
136+
bson.D{
137+
{"createIndexes", "fs.files"},
138+
{"indexes", bson.A{
139+
test.filesIndex,
140+
}},
141+
},
142+
)
143+
assert.Nil(mt, res.Err(), "createIndexes error: %v", res.Err())
144+
145+
res = mt.DB.RunCommand(context.Background(),
146+
bson.D{
147+
{"createIndexes", "fs.chunks"},
148+
{"indexes", bson.A{
149+
test.chunksIndex,
150+
}},
151+
},
152+
)
153+
assert.Nil(mt, res.Err(), "createIndexes error: %v", res.Err())
154+
155+
mt.ClearEvents()
156+
var fileContent []byte
157+
bucket, err := gridfs.NewBucket(mt.DB)
158+
assert.Nil(mt, err, "NewBucket error: %v", err)
159+
defer func() {
160+
bucket.Drop()
161+
} ()
162+
163+
_, err = bucket.UploadFromStream("filename", bytes.NewBuffer(fileContent))
164+
assert.Nil(mt, err, "UploadFromStream error: %v", err)
165+
166+
mt.FilterStartedEvents(func(evt *event.CommandStartedEvent) bool {
167+
return evt.CommandName == "createIndexes"
168+
})
169+
evt := mt.GetStartedEvent()
170+
if test.newIndexes {
171+
if evt == nil {
172+
mt.Fatalf("expected createIndexes events but got none")
173+
}
174+
} else {
175+
if evt != nil {
176+
mt.Fatalf("expected no createIndexes events but got %v", evt.Command)
177+
}
178+
}
179+
})
105180
})
106-
evt := mt.GetStartedEvent()
107-
if evt != nil {
108-
mt.Fatalf("expected no createIndexes events but got %v", evt.Command)
109-
}
110-
})
181+
}
111182
})
112183

113184
mt.RunOpts("round trip", mtest.NewOptions().MaxServerVersion("3.6"), func(mt *mtest.T) {

0 commit comments

Comments
 (0)