forked from blockfrost/blockfrost-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_addresses_test.go
154 lines (131 loc) · 4.15 KB
/
api_addresses_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package blockfrost_test
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"github.com/blockfrost/blockfrost-go"
)
var (
update = flag.Bool("update", false, "update .golden files")
generate = flag.Bool("gen", false, "generate .golden files")
)
const testdata = "testdata"
func TestAddressUnMarshall(t *testing.T) {
want := blockfrost.Address{
Address: "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz",
Amount: []blockfrost.AddressAmount{{Unit: "lovelace", Quantity: "0"}},
StakeAddress: "stake1ux3u6x5cs388djqz6awnyuvez2f6n8jzjhqq59s4yxhm8jskeh0t9",
Type: "shelley",
Script: false,
}
fp := filepath.Join(testdata, "json", "address", "address.json")
got := blockfrost.Address{}
testStructGotWant(t, fp, &got, &want)
}
func TestAddressDetailsUnMarshall(t *testing.T) {
want := blockfrost.AddressDetails{
Address: "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz",
ReceivedSum: []blockfrost.AddressAmount{
{Unit: "lovelace", Quantity: "3324671000"},
},
SentSum: []blockfrost.AddressAmount{
{Unit: "lovelace", Quantity: "3324671000"},
},
TxCount: 2,
}
fp := filepath.Join(testdata, "json", "address", "address_details.json")
got := blockfrost.AddressDetails{}
testStructGotWant(t, fp, &got, &want)
}
func WriteGoldenFile(t *testing.T, path string, bytes []byte) {
t.Helper()
err := os.MkdirAll(filepath.Dir(path), 0777)
if err != nil {
t.Fatal(err)
}
err = ioutil.WriteFile(path, bytes, 0666)
if err != nil {
t.Fatal(err)
}
}
func ReadOrGenerateGoldenFile(t *testing.T, path string, v interface{}) []byte {
t.Helper()
b, err := ioutil.ReadFile(path)
switch {
case errors.Is(err, os.ErrNotExist):
if *generate {
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
err := enc.Encode(v)
if err != nil {
t.Fatal("Failed to marshal to json")
}
WriteGoldenFile(t, path, buf.Bytes())
return buf.Bytes()
}
t.Fatalf("Missing golden file. Run `go test -args -gen` to generate it.")
case err != nil:
t.Fatal(err)
}
return b
}
func TestResourceAddress(t *testing.T) {
addr := "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz"
api := blockfrost.NewAPIClient(blockfrost.APIClientOptions{})
got, err := api.Address(context.TODO(), addr)
if err != nil {
t.Fatal(err)
}
fp := filepath.Join(testdata, strings.ToLower(strings.TrimLeft(t.Name(), "Test"))+".golden")
want := blockfrost.Address{}
testIntUtil(t, fp, &got, &want)
}
func TestResourceAddressDetails(t *testing.T) {
addr := "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz"
api := blockfrost.NewAPIClient(blockfrost.APIClientOptions{})
got, err := api.AddressDetails(context.TODO(), addr)
if err != nil {
t.Fatal(err)
}
fp := filepath.Join(testdata, strings.ToLower(strings.TrimLeft(t.Name(), "Test"))+".golden")
want := blockfrost.AddressDetails{}
testIntUtil(t, fp, &got, &want)
}
func TestResourceAddressTransactions(t *testing.T) {
addr := "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz"
api := blockfrost.NewAPIClient(blockfrost.APIClientOptions{})
got, err := api.AddressTransactions(
context.TODO(),
addr,
blockfrost.APIQueryParams{},
)
if err != nil {
t.Fatal(err)
}
fp := filepath.Join(testdata, strings.ToLower(strings.TrimLeft(t.Name(), "Test"))+".golden")
var want []blockfrost.AddressTransactions
testIntUtil(t, fp, &got, &want)
}
func TestAddressUTXOs(t *testing.T) {
addr := "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz"
api := blockfrost.NewAPIClient(blockfrost.APIClientOptions{})
got, err := api.AddressUTXOs(
context.TODO(),
addr,
blockfrost.APIQueryParams{},
)
if err != nil {
t.Fatal(err)
}
fp := filepath.Join(testdata, strings.ToLower(strings.TrimLeft(t.Name(), "Test"))+".golden")
var want []blockfrost.AddressUTXO
testIntUtil(t, fp, &got, &want)
}