Skip to content

Commit

Permalink
Add altMessage to AssertEqualError (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry committed May 4, 2022
1 parent 285d938 commit 1191d9b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
22 changes: 22 additions & 0 deletions integration/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,28 @@ func AssertEqualError(t testing.TB, expected mongo.CommandError, actual error) b
return assert.Equal(t, expected, a)
}

// AssertEqualAltError asserts that expected error is the same as actual
// using altMessage for CommandError.Message if set and ignoring the Raw part.
func AssertEqualAltError(t testing.TB, expected mongo.CommandError, altMessage string, actual error) bool {
t.Helper()

a, ok := actual.(mongo.CommandError)
if !ok {
return assert.Equal(t, expected, actual)
}

// raw part might be useful if assertion fails
require.Nil(t, expected.Raw)
expected.Raw = a.Raw

if assert.ObjectsAreEqual(expected, a) {
return true
}

expected.Message = altMessage
return assert.Equal(t, expected, a)
}

// CollectIDs returns all _id values from given documents.
//
// The order is preserved.
Expand Down
21 changes: 18 additions & 3 deletions integration/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,23 @@ func TestQueryBadSortType(t *testing.T) {
ctx, collection := setup(t, shareddata.Scalars, shareddata.Composites)

for name, tc := range map[string]struct {
command bson.D
err *mongo.CommandError
command bson.D
err *mongo.CommandError
altMessage string
}{
"BadSortTypeDouble": {
command: bson.D{
{"find", collection.Name()},
{"projection", bson.D{{"value", "some"}}},
{"sort", 42.13},
},
err: &mongo.CommandError{
Code: 14,
Name: "TypeMismatch",
Message: "Expected field sortto be of type object",
},
altMessage: "Expected field sort to be of type object",
},
"BadSortType": {
command: bson.D{
{"find", collection.Name()},
Expand All @@ -266,6 +280,7 @@ func TestQueryBadSortType(t *testing.T) {
Name: "TypeMismatch",
Message: "Expected field sortto be of type object",
},
altMessage: "Expected field sort to be of type object",
},
} {
name, tc := name, tc
Expand All @@ -275,7 +290,7 @@ func TestQueryBadSortType(t *testing.T) {
var actual bson.D
err := collection.Database().RunCommand(ctx, tc.command).Decode(&actual)
require.Error(t, err)
AssertEqualError(t, *tc.err, err)
AssertEqualAltError(t, *tc.err, tc.altMessage, err)
})
}
}
2 changes: 1 addition & 1 deletion internal/handlers/pg/msg_find.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (h *Handler) MsgFind(ctx context.Context, msg *wire.OpMsg) (*wire.OpMsg, er
return nil, err
}
if sort, err = common.GetOptionalParam(document, "sort", sort); err != nil {
return nil, common.NewErrorMsg(common.ErrTypeMismatch, "Expected field sortto be of type object")
return nil, common.NewErrorMsg(common.ErrTypeMismatch, "Expected field sort to be of type object")
}
if projection, err = common.GetOptionalParam(document, "projection", projection); err != nil {
return nil, err
Expand Down

0 comments on commit 1191d9b

Please sign in to comment.