-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbandwidth_test.go
132 lines (125 loc) · 4.07 KB
/
bandwidth_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
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
package gonion_test
import (
"encoding/json"
"net/http"
"testing"
"github.com/R4yGM/gonion"
"github.com/stretchr/testify/assert"
)
func TestGetBandwidth(t *testing.T) {
t.Parallel()
tests := map[string]struct {
Client gonion.HTTPClient
Params gonion.Params
ExpectedBandwidth *gonion.Bandwidth
ExpectedErr error
}{
"nil-client": {
Client: nil,
Params: gonion.Params{},
ExpectedBandwidth: nil,
ExpectedErr: gonion.ErrNilClient,
},
"failing-client": {
Client: newFakeHTTPClient(``, 0, errFake),
Params: gonion.Params{},
ExpectedBandwidth: nil,
ExpectedErr: errFake,
},
"unexpected-statuscode": {
Client: newFakeHTTPClient(``, 0, nil),
Params: gonion.Params{},
ExpectedBandwidth: nil,
ExpectedErr: &gonion.ErrUnexpectedStatus{
StatusCode: 0,
Body: []byte(``),
},
},
"failing-unmarshal": {
Client: newFakeHTTPClient(`{[}]`, http.StatusOK, nil),
Params: gonion.Params{},
ExpectedBandwidth: nil,
ExpectedErr: &json.SyntaxError{
Offset: 2,
},
},
"valid-call": {
Client: newFakeHTTPClient(`{"version":"8.0","build_revision":"a0fbbe2","relays_published":"2021-09-30 13:00:00","relays":[{"fingerprint":"000853C5B75A25D6960A0910D40A5F2B210B7D3A","write_history":{"1_month":{"first":"2021-08-30 12:00:00","last":"2021-09-29 12:00:00","interval":86400,"factor":2723.6236232700335,"count":31,"values":[999]},"6_months":{"first":"2021-08-22 12:00:00","last":"2021-09-29 12:00:00","interval":86400,"factor":2882.4591997800267,"count":39,"values":[1,1,28,320]}},"read_history":{"1_month":{"first":"2021-08-30 12:00:00","last":"2021-09-29 12:00:00","interval":86400,"factor":2697.803559480341,"count":31,"values":[999,564]},"6_months":{"first":"2021-08-22 12:00:00","last":"2021-09-29 12:00:00","interval":86400,"factor":2858.2600114872075,"count":39,"values":[2]}}}],"relays_truncated":7291,"bridges_published":"2021-09-30 12:41:52","bridges":[],"bridges_truncated":1454}`, http.StatusOK, nil),
Params: gonion.Params{
Limit: i(4),
Fields: &gonion.CommaSepList{"test1", "test2"},
},
ExpectedBandwidth: &gonion.Bandwidth{
Version: "8.0",
BuildRevision: "a0fbbe2",
RelaysPublished: "2021-09-30 13:00:00",
Relays: []gonion.BandwidthNode{
{
Fingerprint: "000853C5B75A25D6960A0910D40A5F2B210B7D3A",
WriteHistory: &gonion.History{
OneMonth: &gonion.HistoryEntry{
First: "2021-08-30 12:00:00",
Last: "2021-09-29 12:00:00",
Interval: 86400,
Factor: 2723.6236232700335,
Count: 31,
Values: []*int{
i(999),
},
},
SixMonths: &gonion.HistoryEntry{
First: "2021-08-22 12:00:00",
Last: "2021-09-29 12:00:00",
Interval: 86400,
Factor: 2882.4591997800267,
Count: 39,
Values: []*int{
i(1),
i(1),
i(28),
i(320),
},
},
},
ReadHistory: &gonion.History{
OneMonth: &gonion.HistoryEntry{
First: "2021-08-30 12:00:00",
Last: "2021-09-29 12:00:00",
Interval: 86400,
Factor: 2697.803559480341,
Count: 31,
Values: []*int{
i(999),
i(564),
},
},
SixMonths: &gonion.HistoryEntry{
First: "2021-08-22 12:00:00",
Last: "2021-09-29 12:00:00",
Interval: 86400,
Factor: 2858.2600114872075,
Count: 39,
Values: []*int{
i(2),
},
},
},
},
},
RelaysTruncated: i(7291),
BridgesPublished: "2021-09-30 12:41:52",
Bridges: []gonion.BandwidthNode{},
BridgesTruncated: i(1454),
},
ExpectedErr: nil,
},
}
for testname, tt := range tests {
t.Run(testname, func(t *testing.T) {
assert := assert.New(t)
bandwidth, err := gonion.GetBandwidth(tt.Client, tt.Params)
assert.Equal(tt.ExpectedBandwidth, bandwidth)
checkErr(err, tt.ExpectedErr, t)
})
}
}