-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherrors_test.go
88 lines (84 loc) · 4.49 KB
/
errors_test.go
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
package neoql
import (
"errors"
"gopkg.in/neoql.v1/types"
"gopkg.in/packstream.v1"
"testing"
)
func TestGetMessageError(t *testing.T) {
if err := getMessageError(packstream.NewStructure(byteSuccess)); err != nil {
t.Error(err)
}
if err := getMessageError(packstream.NewStructure(byteFailure)); err == nil {
t.Error("error should not be nil when byte failure is set and there is no other fields.")
} else if err != types.ErrProtocol {
t.Error("error should be a ProtocolError when byte failure is set and there is no other fields.")
}
if err := getMessageError(packstream.NewStructure(byteFailure, 42)); err == nil {
t.Error("error should not be nil when byte failure is set and second field is not a map.")
} else if err != types.ErrProtocol {
t.Error("error should be a ProtocolError when byte failure is set and second field is not a map.")
}
if err := getMessageError(packstream.NewStructure(byteFailure, map[string]interface{}{})); err == nil {
t.Error("error should not be nil when byte failure is set and second field is an empty map.")
} else if err != types.ErrProtocol {
t.Error("error should be a ProtocolError when byte failure is set and second field is an empty map.")
}
if err := getMessageError(packstream.NewStructure(byteFailure, map[string]interface{}{"code": 4})); err == nil {
t.Error("error should not be nil when code is an integer.")
} else if err != types.ErrProtocol {
t.Error("error should be a ProtocolError when code is an integer.")
}
if err := getMessageError(packstream.NewStructure(byteFailure, map[string]interface{}{"code": "string"})); err == nil {
t.Error("error should not be nil when code is valid.")
} else if cErr, ok := err.(*types.CypherError); !ok {
t.Error("error should be a CypherError when code is valid.")
} else if cErr.Code != "string" {
t.Errorf("error code should be %v, got %v", "string", cErr.Code)
}
if err := getMessageError(packstream.NewStructure(byteFailure, map[string]interface{}{"code": "string", "message": 42})); err == nil {
t.Error("error should not be nil when code is valid and message is an int.")
} else if cErr, ok := err.(*types.CypherError); !ok {
t.Error("error should be a CypherError when code is valid and message is an int.")
} else if cErr.Code != "string" {
t.Errorf("error code should be %v, got %v", "string", cErr.Code)
}
if err := getMessageError(packstream.NewStructure(byteFailure, map[string]interface{}{"message": 42})); err == nil {
t.Error("error should not be nil when message is an int.")
} else if err != types.ErrProtocol {
t.Error("error should be a ProtocolError when message is an int.")
}
if err := getMessageError(packstream.NewStructure(byteFailure, map[string]interface{}{"message": "string"})); err == nil {
t.Error("error should not be nil when message is valid.")
} else if cErr, ok := err.(*types.CypherError); !ok {
t.Error("error should be a CypherError when message is valid.")
} else if cErr.Message != "string" {
t.Errorf("error message should be %v, got %v", "string", cErr.Code)
}
if err := getMessageError(packstream.NewStructure(byteFailure, map[string]interface{}{"code": "42", "message": "hello"})); err == nil {
t.Error("error should not be nil when message and code are valid.")
} else if cErr, ok := err.(*types.CypherError); !ok {
t.Error("error should be a CypherError when message and core are valid.")
} else if cErr.Message != "hello" {
t.Errorf("error message should be %v, got %v", "hello", cErr.Code)
} else if cErr.Code != "42" {
t.Errorf("error code should be %v, got %v", "42", cErr.Code)
}
if err := getMessageError(packstream.NewStructure(byteFailure, map[string]interface{}{"code": unauthorizedCode, "message": "hello"})); err == nil {
t.Error("error should not be nil when error is an valid UnauthorizedError.")
} else if err != types.ErrUnauthorized {
t.Error("error should be an UnauthorizedError when error is valid .")
}
}
func TestMessageError(t *testing.T) {
if err := messageError(packstream.NewStructure(byteSuccess), errors.New("default")); err == nil {
t.Error("error should not be nil when calling message error")
} else if err.Error() != "default" {
t.Error("error should equals the second parameter when structure signature is byteSuccess.")
}
if err := messageError(packstream.NewStructure(byteFailure), errors.New("default")); err == nil {
t.Error("error should not be nil when byte failure is set.")
} else if err != types.ErrProtocol {
t.Error("error should be a ProtocolError when byte failure is set and there is no other fields.")
}
}