Repository navigation
Expand file tree
/
Copy pathcontract_test.go
More file actions
122 lines (119 loc) · 4.58 KB
/
Copy pathcontract_test.go
File metadata and controls
122 lines (119 loc) · 4.58 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
package glassnode
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"testing"
"time"
)
func TestRecordedAPIContracts(t *testing.T) {
for _, tt := range []struct {
file string
call func(*Client) error
}{
{"asset-metadata.json", func(c *Client) error { _, err := c.ListAssets(context.Background(), ""); return err }},
{"metric-list.json", func(c *Client) error { _, err := c.ListMetrics(context.Background(), nil); return err }},
{"metric-metadata-price-usd-close.json", func(c *Client) error {
_, err := c.GetMetricMetadata(context.Background(), "market/price_usd_close", nil)
return err
}},
{"metric-metadata-us-spot-etf-balances-all.json", func(c *Client) error {
_, err := c.GetMetricMetadata(context.Background(), "institutions/us_spot_etf_balances_all", nil)
return err
}},
{"metric-metadata-balance-exchanges-btc.json", func(c *Client) error {
metadata, err := c.GetMetricMetadata(context.Background(), "distribution/balance_exchanges", &MetricParams{Asset: "BTC"})
if err != nil {
return err
}
defaults := metadata.ParametersDefaults["e"]
if len(defaults) != 1 || defaults[0] != "aggregated" {
return fmt.Errorf("lost recorded exchange defaults: %v", defaults)
}
return nil
}},
{"metric-stats-active-count-btc.json", func(c *Client) error {
_, err := c.GetMetricStats(context.Background(), "addresses/active_count", &MetricParams{Asset: "BTC"})
return err
}},
{"metric-stats-us-spot-etf-balances-all.json", func(c *Client) error {
_, err := c.GetMetricStats(context.Background(), "institutions/us_spot_etf_balances_all", nil)
return err
}},
{"timeseries-price-usd-close-btc.json", func(c *Client) error {
_, err := c.GetTimeSeries(context.Background(), "market/price_usd_close", nil)
return err
}},
{"timeseries-active-count-pit-btc.json", func(c *Client) error {
points, err := c.GetTimeSeries(context.Background(), "addresses/active_count_pit", &MetricParams{Asset: "BTC"})
if err != nil {
return err
}
if points[0].ComputedAt == nil || *points[0].ComputedAt != 1790985731 || *points[0].Value != 729981 {
return fmt.Errorf("lost computed_at: %+v", points[0])
}
return nil
}},
{"timeseries-ssr-pit-btc.json", func(c *Client) error {
points, err := c.GetObjectTimeSeries(context.Background(), "indicators/ssr_pit", &MetricParams{Asset: "BTC"})
if err != nil {
return err
}
if points[0].ComputedAt == nil || *points[0].ComputedAt != 1790985823 || points[0].Object["h"] == nil {
return fmt.Errorf("lost computed_at: %+v", points[0])
}
return nil
}},
{"timeseries-price-usd-ohlc-btc.json", func(c *Client) error {
_, err := c.GetObjectTimeSeries(context.Background(), "market/price_usd_ohlc", nil)
return err
}},
{"bulk-marketcap-usd.json", func(c *Client) error {
bulk, err := c.GetBulkMetric(context.Background(), "market/marketcap_usd", &MetricParams{Since: time.Unix(1735689600, 0)})
if err != nil {
return err
}
if eth := bulk[0].Bulk[1]; eth.Asset() != "ETH" || eth.Network() != "eth" || eth.Params["network"] != "eth" {
return fmt.Errorf("lost recorded bulk selectors: %+v", eth)
}
return nil
}},
} {
t.Run(tt.file, func(t *testing.T) {
body, err := os.ReadFile(filepath.Join("testdata", "contract", tt.file))
if err != nil {
t.Fatal(err)
}
client, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) { w.Write(body) })
if err := tt.call(client); err != nil {
t.Fatal(err)
}
})
}
}
func TestMalformedMetadataAndUsage(t *testing.T) {
for _, tt := range []struct {
name, body string
call func(*Client) error
}{
{"missing asset ID", `{"data":[{"name":"Bitcoin"}]}`, func(c *Client) error { _, err := c.ListAssets(context.Background(), ""); return err }},
{"invalid metric path", `["/../escape"]`, func(c *Client) error { _, err := c.ListMetrics(context.Background(), nil); return err }},
{"missing lag unit", `{"lag":[{"resolution":{}}]}`, func(c *Client) error {
_, err := c.GetMetricStats(context.Background(), "market/price", nil)
return err
}},
{"missing credits", `{"apiAddons":[]}`, func(c *Client) error { _, err := c.GetAPIUsage(context.Background()); return err }},
{"non-array usage", `[]`, func(c *Client) error { _, err := c.GetAPIUsage(context.Background()); return err }},
} {
t.Run(tt.name, func(t *testing.T) {
client, _ := testClient(t, func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(tt.body)) })
var decode *DecodeError
if err := tt.call(client); !errors.As(err, &decode) {
t.Fatalf("expected DecodeError, got %v", err)
}
})
}
}