-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors_test.go
More file actions
123 lines (103 loc) · 3.16 KB
/
Copy patherrors_test.go
File metadata and controls
123 lines (103 loc) · 3.16 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
package reolink
import (
"errors"
"testing"
)
func TestAPIError_Error(t *testing.T) {
err := NewAPIError("GetDevInfo", 0, ErrCodeLoginRequired, "please login first")
expected := "reolink api error: cmd=GetDevInfo code=0 rspCode=-6 detail=please login first"
if err.Error() != expected {
t.Errorf("expected error message %q, got %q", expected, err.Error())
}
}
func TestAPIError_ErrorWithoutDetail(t *testing.T) {
err := NewAPIError("GetDevInfo", 0, ErrCodeLoginRequired, "")
if err.Error() == "" {
t.Error("expected non-empty error message")
}
// Should include the error code description
if err.Error() != "reolink api error: cmd=GetDevInfo code=0 rspCode=-6 (login required)" {
t.Errorf("unexpected error message: %s", err.Error())
}
}
func TestAPIError_Is(t *testing.T) {
err1 := NewAPIError("GetDevInfo", 0, ErrCodeLoginRequired, "please login first")
err2 := NewAPIError("GetTime", 0, ErrCodeLoginRequired, "login required")
err3 := NewAPIError("GetDevInfo", 0, ErrCodeParametersError, "param error")
if !errors.Is(err1, err2) {
t.Error("expected errors with same RspCode to match")
}
if errors.Is(err1, err3) {
t.Error("expected errors with different RspCode to not match")
}
}
func TestErrorCodeToString(t *testing.T) {
tests := []struct {
code int
expected string
}{
{ErrCodeSuccess, "success"},
{ErrCodeLoginRequired, "login required"},
{ErrCodeParametersError, "parameters error"},
{ErrCodeNotSupported, "not supported"},
{ErrCodeTokenError, "token error"},
{ErrCodeInvalidUser, "invalid user"},
{ErrCodeDeviceOffline, "device offline"},
{ErrCodeUpgradeCheckFailed, "upgrade checking firmware failed"},
{ErrCodeVideoNotExist, "the video file does not exist"},
{ErrCodeFTPConnectFailed, "cannot connect FTP server"},
{ErrCodeEmailAuthFailed, "email auth user failed"},
{ErrCodeInvalidPassword, "invalid password"},
{-9999, "unknown error code: -9999"},
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
result := errorCodeToString(tt.code)
if result != tt.expected {
t.Errorf("expected '%s', got '%s'", tt.expected, result)
}
})
}
}
func TestResponse_ToAPIError(t *testing.T) {
// Test response with error detail
resp := Response{
Cmd: "GetDevInfo",
Code: 0,
Error: &ErrorDetail{
RspCode: ErrCodeLoginRequired,
Detail: "please login first",
},
}
apiErr := resp.ToAPIError()
if apiErr == nil {
t.Fatal("expected APIError, got nil")
}
if apiErr.RspCode != ErrCodeLoginRequired {
t.Errorf("expected RspCode %d, got %d", ErrCodeLoginRequired, apiErr.RspCode)
}
if apiErr.Detail != "please login first" {
t.Errorf("expected detail 'please login first', got %q", apiErr.Detail)
}
// Test response with non-zero code but no error detail
resp2 := Response{
Cmd: "GetDevInfo",
Code: -1,
}
apiErr2 := resp2.ToAPIError()
if apiErr2 == nil {
t.Fatal("expected APIError, got nil")
}
if apiErr2.Code != -1 {
t.Errorf("expected Code -1, got %d", apiErr2.Code)
}
// Test successful response
resp3 := Response{
Cmd: "GetDevInfo",
Code: 0,
}
apiErr3 := resp3.ToAPIError()
if apiErr3 != nil {
t.Errorf("expected nil for successful response, got %v", apiErr3)
}
}