@@ -19,15 +19,22 @@ import (
1919// expectedError represents an error that is expected to occur during a test. This type ignores the "isError" field in
2020// test files because it is always true if it is specified, so the runner can simply assert that an error occurred.
2121type expectedError struct {
22- IsClientError * bool `bson:"isClientError"`
23- IsTimeoutError * bool `bson:"isTimeoutError"`
24- ErrorSubstring * string `bson:"errorContains"`
25- Code * int32 `bson:"errorCode"`
26- CodeName * string `bson:"errorCodeName"`
27- IncludedLabels []string `bson:"errorLabelsContain"`
28- OmittedLabels []string `bson:"errorLabelsOmit"`
29- ExpectedResult * bson.RawValue `bson:"expectResult"`
30- ErrorResponse * bson.Raw `bson:"errorResponse"`
22+ IsClientError * bool `bson:"isClientError"`
23+ IsTimeoutError * bool `bson:"isTimeoutError"`
24+ ErrorSubstring * string `bson:"errorContains"`
25+ Code * int32 `bson:"errorCode"`
26+ CodeName * string `bson:"errorCodeName"`
27+ IncludedLabels []string `bson:"errorLabelsContain"`
28+ OmittedLabels []string `bson:"errorLabelsOmit"`
29+ ExpectedResult * bson.RawValue `bson:"expectResult"`
30+ ErrorResponse * bson.Raw `bson:"errorResponse"`
31+ WriteErrors map [int ]clientBulkWriteException `bson:"writeErrors"`
32+ WriteConcernErrors []clientBulkWriteException `bson:"writeConcernErrors"`
33+ }
34+
35+ type clientBulkWriteException struct {
36+ Code * int `bson:"code"`
37+ Message * string `bson:"message"`
3138}
3239
3340// verifyOperationError compares the expected error to the actual operation result. If the expected parameter is nil,
@@ -140,6 +147,40 @@ func verifyOperationError(ctx context.Context, expected *expectedError, result *
140147 return fmt .Errorf ("error response comparison error: %w" , err )
141148 }
142149 }
150+ if expected .WriteErrors != nil {
151+ var exception mongo.ClientBulkWriteException
152+ if ! errors .As (result .Err , & exception ) {
153+ return fmt .Errorf ("expected a ClientBulkWriteException, got %T" , result .Err )
154+ }
155+ if len (expected .WriteErrors ) != len (exception .WriteErrors ) {
156+ return fmt .Errorf ("expected errors: %v, got: %v" , expected .WriteErrors , exception .WriteErrors )
157+ }
158+ for k , e := range expected .WriteErrors {
159+ if e .Code != nil && * e .Code != exception .WriteErrors [k ].Code {
160+ return fmt .Errorf ("expected errors: %v, got: %v" , expected .WriteConcernErrors , exception .WriteConcernErrors )
161+ }
162+ if e .Message != nil && * e .Message != exception .WriteErrors [k ].Message {
163+ return fmt .Errorf ("expected errors: %v, got: %v" , expected .WriteConcernErrors , exception .WriteConcernErrors )
164+ }
165+ }
166+ }
167+ if expected .WriteConcernErrors != nil {
168+ var exception mongo.ClientBulkWriteException
169+ if ! errors .As (result .Err , & exception ) {
170+ return fmt .Errorf ("expected a ClientBulkWriteException, got %T" , result .Err )
171+ }
172+ if len (expected .WriteConcernErrors ) != len (exception .WriteConcernErrors ) {
173+ return fmt .Errorf ("expected errors: %v, got: %v" , expected .WriteConcernErrors , exception .WriteConcernErrors )
174+ }
175+ for i , e := range expected .WriteConcernErrors {
176+ if e .Code != nil && * e .Code != exception .WriteConcernErrors [i ].Code {
177+ return fmt .Errorf ("expected errors: %v, got: %v" , expected .WriteConcernErrors , exception .WriteConcernErrors )
178+ }
179+ if e .Message != nil && * e .Message != exception .WriteConcernErrors [i ].Message {
180+ return fmt .Errorf ("expected errors: %v, got: %v" , expected .WriteConcernErrors , exception .WriteConcernErrors )
181+ }
182+ }
183+ }
143184 return nil
144185}
145186
0 commit comments