Skip to content

Commit

Permalink
Merge pull request #96 from qnxm/develop
Browse files Browse the repository at this point in the history
fix Apply func return err
  • Loading branch information
jiangz222 authored Sep 27, 2020
2 parents 13372c8 + d9d46e0 commit 7f6139f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
14 changes: 12 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,12 @@ func (q *Query) findOneAndReplace(change Change, result interface{}) error {
opts.SetReturnDocument(options.After)
}

return q.collection.FindOneAndReplace(q.ctx, q.filter, change.Update, opts).Decode(result)
err := q.collection.FindOneAndReplace(q.ctx, q.filter, change.Update, opts).Decode(result)
if change.Upsert && !change.ReturnNew && err == mongo.ErrNoDocuments {
return nil
}

return err
}

// findOneAndUpdate
Expand All @@ -361,5 +366,10 @@ func (q *Query) findOneAndUpdate(change Change, result interface{}) error {
opts.SetReturnDocument(options.After)
}

return q.collection.FindOneAndUpdate(q.ctx, q.filter, change.Update, opts).Decode(result)
err := q.collection.FindOneAndUpdate(q.ctx, q.filter, change.Update, opts).Decode(result)
if change.Upsert && !change.ReturnNew && err == mongo.ErrNoDocuments {
return nil
}

return err
}
36 changes: 35 additions & 1 deletion query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,11 +608,25 @@ func TestQuery_Apply(t *testing.T) {
err = cli.Find(context.Background(), filter1).Apply(change1, &res1)
ast.EqualError(err, mongo.ErrNoDocuments.Error())

change1.ReturnNew = false
change1.Upsert = true
err = cli.Find(context.Background(), filter1).Apply(change1, &res1)
ast.NoError(err)
ast.Equal( "", res1.Name)
ast.Equal(0, res1.Age)

change1.Update = bson.M{
operator.Set: bson.M{
"name": "Tom",
"age": 19,
},
}
change1.ReturnNew = true
change1.Upsert = true
err = cli.Find(context.Background(), filter1).Apply(change1, &res1)
ast.NoError(err)
ast.Equal( "Tom", res1.Name)
ast.Equal(18, res1.Age)
ast.Equal(19, res1.Age)

res2 := QueryTestItem{}
filter2 := bson.M{
Expand Down Expand Up @@ -677,6 +691,10 @@ func TestQuery_Apply(t *testing.T) {
err = cli.Find(context.Background(), filter4).Apply(change4, &res4)
ast.EqualError(err, mongo.ErrNoDocuments.Error())

change4.ReturnNew = true
err = cli.Find(context.Background(), filter4).Apply(change4, &res4)
ast.EqualError(err, mongo.ErrNoDocuments.Error())

change4.Upsert = true
change4.ReturnNew = true
err = cli.Find(context.Background(), filter4).Apply(change4, &res4)
Expand All @@ -698,4 +716,20 @@ func TestQuery_Apply(t *testing.T) {
ast.NoError(err)
ast.Equal("Bob", res4.Name)
ast.Equal(23, res4.Age)


res4 = QueryTestItem{}
filter4 = bson.M{
"name": "James",
}
change4 = Change{
Replace: true,
Update: bson.M{"name": "James", "age": 26},
Upsert: true,
ReturnNew: false,
}
err = cli.Find(context.Background(), filter4).Apply(change4, &res4)
ast.NoError(err)
ast.Equal("", res4.Name)
ast.Equal(0, res4.Age)
}

0 comments on commit 7f6139f

Please sign in to comment.