Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
138 lines (118 loc) Β· 4.36 KB
/
Copy patherrors_test.go
File metadata and controls
138 lines (118 loc) Β· 4.36 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package whatsonchain
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
// TestErrorConstants tests all error constant definitions
func TestErrorConstants(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
err error
expectedMsg string
}{
{"ErrAddressNotFound", ErrAddressNotFound, "address not found"},
{"ErrBlockNotFound", ErrBlockNotFound, "block not found"},
{"ErrChainInfoNotFound", ErrChainInfoNotFound, "chain info not found"},
{"ErrChainTipsNotFound", ErrChainTipsNotFound, "chain tips not found"},
{"ErrPeerInfoNotFound", ErrPeerInfoNotFound, "peer info not found"},
{"ErrExchangeRateNotFound", ErrExchangeRateNotFound, "exchange rate not found"},
{"ErrMempoolInfoNotFound", ErrMempoolInfoNotFound, "mempool info not found"},
{"ErrHeadersNotFound", ErrHeadersNotFound, "headers not found"},
{"ErrScriptNotFound", ErrScriptNotFound, "script not found"},
{"ErrTransactionNotFound", ErrTransactionNotFound, "transaction not found"},
{"ErrMaxAddressesExceeded", ErrMaxAddressesExceeded, "max limit of addresses exceeded"},
{"ErrMaxScriptsExceeded", ErrMaxScriptsExceeded, "max limit of scripts exceeded"},
{"ErrBroadcastFailed", ErrBroadcastFailed, "error broadcasting transaction"},
{"ErrMaxTransactionsExceeded", ErrMaxTransactionsExceeded, "max transactions limit exceeded"},
{"ErrMaxPayloadSizeExceeded", ErrMaxPayloadSizeExceeded, "max overall payload size exceeded"},
{"ErrMaxTransactionSizeExceeded", ErrMaxTransactionSizeExceeded, "max transaction size exceeded"},
{"ErrMaxUTXOsExceeded", ErrMaxUTXOsExceeded, "max limit of UTXOs exceeded"},
{"ErrMaxRawTransactionsExceeded", ErrMaxRawTransactionsExceeded, "max limit of raw transactions exceeded"},
{"ErrMissingRequest", ErrMissingRequest, "missing request"},
{"ErrBadRequest", ErrBadRequest, "bad request"},
{"ErrTokenNotFound", ErrTokenNotFound, "token not found"},
{"ErrInvalidNetwork", ErrInvalidNetwork, "invalid network type: must be one of: main, test, stn"},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Test that error implements error interface
assert.Implements(t, (*error)(nil), tc.err)
// Test error message
assert.Equal(t, tc.expectedMsg, tc.err.Error())
// Test that error is not nil
assert.Error(t, tc.err)
})
}
}
// TestErrorComparison tests error equality and comparison
func TestErrorComparison(t *testing.T) {
t.Parallel()
// Test that different errors are not equal
assert.NotEqual(t, ErrAddressNotFound, ErrBlockNotFound)
// Test error identity with errors.Is
assert.NotErrorIs(t, ErrAddressNotFound, ErrBlockNotFound)
}
// TestNotFoundErrors tests all "not found" type errors
func TestNotFoundErrors(t *testing.T) {
t.Parallel()
notFoundErrors := []error{
ErrAddressNotFound,
ErrBlockNotFound,
ErrChainInfoNotFound,
ErrChainTipsNotFound,
ErrPeerInfoNotFound,
ErrExchangeRateNotFound,
ErrMempoolInfoNotFound,
ErrHeadersNotFound,
ErrScriptNotFound,
ErrTransactionNotFound,
ErrTokenNotFound,
}
for _, err := range notFoundErrors {
assert.Contains(t, err.Error(), "not found")
}
}
// TestLimitExceededErrors tests all limit exceeded errors
func TestLimitExceededErrors(t *testing.T) {
t.Parallel()
limitErrors := []error{
ErrMaxAddressesExceeded,
ErrMaxScriptsExceeded,
ErrMaxTransactionsExceeded,
ErrMaxPayloadSizeExceeded,
ErrMaxTransactionSizeExceeded,
ErrMaxUTXOsExceeded,
ErrMaxRawTransactionsExceeded,
}
for _, err := range limitErrors {
errMsg := err.Error()
assert.True(t,
contains(errMsg, "max") || contains(errMsg, "limit") || contains(errMsg, "exceeded"),
"Expected error message to contain 'max', 'limit', or 'exceeded': %s", errMsg)
}
}
// TestErrorWrapping tests error wrapping behavior
func TestErrorWrapping(t *testing.T) {
t.Parallel()
// Test custom wrapping
customErr := fmt.Errorf("custom: %w", ErrBlockNotFound)
if !errors.Is(customErr, ErrBlockNotFound) {
t.Errorf("expected error to wrap ErrBlockNotFound")
}
}
// contains is a helper function to check if a string contains a substring
func contains(s, substr string) bool {
return len(s) >= len(substr) && (s == substr || len(substr) == 0 ||
(len(substr) > 0 && containsHelper(s, substr)))
}
func containsHelper(s, substr string) bool {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return true
}
}
return false
}