Skip to content

Commit

Permalink
update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
qingyang-hu committed Aug 30, 2024
1 parent 686ef6d commit 4897d97
Show file tree
Hide file tree
Showing 18 changed files with 1,124 additions and 406 deletions.
7 changes: 5 additions & 2 deletions mongo/bulk_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,6 @@ func (bw *bulkWrite) runUpdate(ctx context.Context, batch bulkWriteBatch) (opera
filter: converted.Filter,
update: converted.Replacement,
hint: converted.Hint,
sort: converted.Sort,
collation: converted.Collation,
upsert: converted.Upsert,
}.marshal(bw.collection.bsonOpts, bw.collection.registry)
Expand Down Expand Up @@ -445,7 +444,11 @@ func (doc updateDoc) marshal(bsonOpts *options.BSONOptions, registry *bsoncodec.

if doc.multi {
updateDoc = bsoncore.AppendBooleanElement(updateDoc, "multi", doc.multi)
} else if doc.sort != nil {
}
if doc.sort != nil {
if isUnorderedMap(doc.sort) {
return nil, ErrMapForOrderedArgument{"sort"}
}
s, err := marshal(doc.sort, bsonOpts, registry)
if err != nil {
return nil, err
Expand Down
8 changes: 7 additions & 1 deletion mongo/bulk_write_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ type ReplaceOneModel struct {
Filter interface{}
Replacement interface{}
Hint interface{}
Sort interface{}
}

// NewReplaceOneModel creates a new ReplaceOneModel.
Expand Down Expand Up @@ -240,6 +239,13 @@ func (uom *UpdateOneModel) SetUpsert(upsert bool) *UpdateOneModel {
return uom
}

// SetSort specifies which document the operation updates if the query matches multiple documents.
// The first document matched by the sort order will be updated.
func (uom *UpdateOneModel) SetSort(sort interface{}) *UpdateOneModel {
uom.Sort = sort
return uom
}

func (*UpdateOneModel) writeModel() {}

// UpdateManyModel is used to update multiple documents in a BulkWrite operation.
Expand Down
1 change: 0 additions & 1 deletion mongo/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,6 @@ func (coll *Collection) ReplaceOne(ctx context.Context, filter interface{},
uOpts.Upsert = opt.Upsert
uOpts.Hint = opt.Hint
uOpts.Let = opt.Let
uOpts.Sort = opt.Sort
uOpts.Comment = opt.Comment
updateOptions = append(updateOptions, uOpts)
}
Expand Down
5 changes: 3 additions & 2 deletions mongo/integration/crud_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,8 +1000,6 @@ func executeReplaceOne(mt *mtest.T, sess mongo.Session, args bson.Raw) (*mongo.U
opts = opts.SetCollation(createCollation(mt, val.Document()))
case "hint":
opts = opts.SetHint(createHint(mt, val))
case "sort":
opts = opts.SetSort(createSort(mt, val))
case "session":
default:
mt.Fatalf("unrecognized replaceOne option: %v", key)
Expand Down Expand Up @@ -1142,6 +1140,9 @@ func createBulkWriteModel(mt *mtest.T, rawModel bson.Raw) mongo.WriteModel {
if hintVal, err := args.LookupErr("hint"); err == nil {
uom.SetHint(createHint(mt, hintVal))
}
if sortVal, err := args.LookupErr("sort"); err == nil {
uom.SetSort(createSort(mt, sortVal))
}
if uom.Upsert == nil {
uom.SetUpsert(false)
}
Expand Down
6 changes: 6 additions & 0 deletions mongo/integration/unified/bulkwrite_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ func createBulkWriteModel(rawModel bson.Raw) (mongo.WriteModel, error) {
return nil, fmt.Errorf("error creating hint: %w", err)
}
uom.SetHint(hint)
case "sort":
sort, err := createSort(val)
if err != nil {
return nil, fmt.Errorf("error creating sort: %w", err)
}
uom.SetSort(sort)
case "update":
update, err = createUpdateValue(val)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions mongo/integration/unified/crud_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@ func createHint(val bson.RawValue) (interface{}, error) {
return hint, nil
}

func createSort(val bson.RawValue) (interface{}, error) {
var sort interface{}

switch val.Type {
case bsontype.EmbeddedDocument:
sort = val.Document()
default:
return nil, fmt.Errorf("unrecognized sort value type %s", val.Type)
}
return sort, nil
}

func createCommentString(val bson.RawValue) (string, error) {
switch val.Type {
case bsontype.String:
Expand Down
15 changes: 0 additions & 15 deletions mongo/options/replaceoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@ type ReplaceOptions struct {
// Values must be constant or closed expressions that do not reference document fields. Parameters can then be
// accessed as variables in an aggregate expression context (e.g. "$$var").
Let interface{}

// A document specifying which document should be replaced if the filter used by the operation matches multiple
// documents in the collection. If set, the first document in the sorted order will be updated. This option is
// only valid for MongoDB versions >= 8.0. The driver will return an error if the sort parameter is a multi-key
// map. The default value is nil.
Sort interface{}
}

// Replace creates a new ReplaceOptions instance.
Expand Down Expand Up @@ -89,12 +83,6 @@ func (ro *ReplaceOptions) SetLet(l interface{}) *ReplaceOptions {
return ro
}

// SetSort sets the value for the Sort field.
func (ro *ReplaceOptions) SetSort(s interface{}) *ReplaceOptions {
ro.Sort = s
return ro
}

// MergeReplaceOptions combines the given ReplaceOptions instances into a single ReplaceOptions in a last-one-wins
// fashion.
//
Expand Down Expand Up @@ -124,9 +112,6 @@ func MergeReplaceOptions(opts ...*ReplaceOptions) *ReplaceOptions {
if ro.Let != nil {
rOpts.Let = ro.Let
}
if ro.Sort != nil {
rOpts.Sort = ro.Sort
}
}

return rOpts
Expand Down
125 changes: 0 additions & 125 deletions testdata/command-monitoring/updateMany-sort.json

This file was deleted.

57 changes: 0 additions & 57 deletions testdata/command-monitoring/updateMany-sort.yml

This file was deleted.

Loading

0 comments on commit 4897d97

Please sign in to comment.