-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
123 lines (108 loc) · 3.08 KB
/
Copy patherrors_test.go
File metadata and controls
123 lines (108 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) the go-ruby-mongodb/mongodb authors
//
// SPDX-License-Identifier: BSD-3-Clause
package mongodb
import (
"context"
"errors"
"testing"
"go.mongodb.org/mongo-driver/v2/mongo"
)
func TestMapErrorNil(t *testing.T) {
if mapError(nil) != nil {
t.Fatal("nil should map to nil")
}
}
func TestMapErrorAlreadyClassified(t *testing.T) {
in := &Error{Class: ErrInvalidDocument, Message: "boom"}
if mapError(in) != error(in) {
t.Fatal("a *mongodb.Error should pass through unchanged")
}
}
func TestMapErrorCommand(t *testing.T) {
in := mongo.CommandError{Code: 26, Message: "ns not found"}
e, ok := mapError(in).(*Error)
if !ok || e.Class != ErrOperationFailure || e.Code != 26 {
t.Fatalf("got %+v", e)
}
}
func TestMapErrorWriteException(t *testing.T) {
in := mongo.WriteException{
WriteErrors: mongo.WriteErrors{{Index: 2, Code: 121, Message: "doc failed validation"}},
}
e := mapError(in).(*Error)
if e.Class != ErrOperationFailure || e.Code != 121 || len(e.WriteErrors) != 1 || e.WriteErrors[0].Index != 2 {
t.Fatalf("got %+v", e)
}
}
func TestMapErrorWriteExceptionConcernOnly(t *testing.T) {
in := mongo.WriteException{
WriteConcernError: &mongo.WriteConcernError{Code: 64, Message: "wtimeout"},
}
e := mapError(in).(*Error)
if e.Class != ErrOperationFailure || e.Code != 64 {
t.Fatalf("got %+v", e)
}
}
func TestMapErrorBulkWrite(t *testing.T) {
in := mongo.BulkWriteException{
WriteErrors: []mongo.BulkWriteError{
{WriteError: mongo.WriteError{Index: 0, Code: DuplicateKeyCode, Message: "E11000 dup"}},
},
}
e := mapError(in).(*Error)
if e.Class != ErrBulkWriteError || e.Code != DuplicateKeyCode {
t.Fatalf("got %+v", e)
}
if !e.IsDuplicateKey() {
t.Fatal("should be duplicate key")
}
}
func TestMapErrorBulkWriteConcernOnly(t *testing.T) {
in := mongo.BulkWriteException{
WriteConcernError: &mongo.WriteConcernError{Code: 64, Message: "wtimeout"},
}
e := mapError(in).(*Error)
if e.Class != ErrBulkWriteError || e.Code != 64 {
t.Fatalf("got %+v", e)
}
}
func TestMapErrorTimeout(t *testing.T) {
e := mapError(context.DeadlineExceeded).(*Error)
if e.Class != ErrConnectionFailure {
t.Fatalf("got %+v", e)
}
}
func TestMapErrorDisconnected(t *testing.T) {
e := mapError(mongo.ErrClientDisconnected).(*Error)
if e.Class != ErrConnectionFailure {
t.Fatalf("got %+v", e)
}
}
func TestMapErrorBase(t *testing.T) {
e := mapError(errors.New("something odd")).(*Error)
if e.Class != ErrBase {
t.Fatalf("got %+v", e)
}
}
func TestErrorInterface(t *testing.T) {
wrapped := errors.New("inner")
e := &Error{Class: ErrOperationFailure, Message: "outer", Wrapped: wrapped}
if e.Error() != "outer" {
t.Fatalf("Error() = %q", e.Error())
}
if !errors.Is(e, wrapped) {
t.Fatal("Unwrap should expose the wrapped error")
}
}
func TestIsDuplicateKey(t *testing.T) {
if (&Error{Code: DuplicateKeyCode}).IsDuplicateKey() != true {
t.Fatal("top-level code")
}
if (&Error{WriteErrors: []WriteError{{Code: DuplicateKeyCode}}}).IsDuplicateKey() != true {
t.Fatal("write-error code")
}
if (&Error{Code: 1}).IsDuplicateKey() != false {
t.Fatal("non-dup")
}
}