-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfetcher_test.go
92 lines (81 loc) · 2.26 KB
/
fetcher_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
package ticker
import (
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"testing"
"time"
"github.com/gocraft/health"
"github.com/jarcoal/httpmock"
)
func init() {
cmcQueryLimit = testCMCQueryLimit
rand.Seed(time.Now().UnixNano())
}
func TestFetch(t *testing.T) {
stream := health.NewStream()
stream.AddSink(&health.WriterSink{os.Stdout})
disableMocksFn := createHTTPMocks()
defer disableMocksFn()
// Prepare outfiles path
outfilePath := fmt.Sprintf("/tmp/ticker_proxy_test_%d", rand.Int())
err := os.Remove(outfilePath)
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
err = os.Mkdir(outfilePath, os.ModePerm)
if err != nil {
t.Fatal(err)
}
conf := Config{
BTCAVGPubkey: "pubkey",
BTCAVGPrivkey: "privkey",
CMCAPIKey: "cmc-api-key",
CMCEnv: "sandbox",
}
// Fetch data. First let it fail with missing symbol, then override to let it
// work on a second run.
err = Fetch(stream, conf, func(_ *health.Job, data []byte) error {
if string(data) != testExpectedFetchData {
t.Fatal("Fetch returned incorrect data\nGot:", string(data), "\nWanted:", testExpectedFetchData)
}
return nil
}, NewFileSystemWriter(outfilePath))
if err != errFetchMissingRequiredSymbol("EUR") {
t.Fatal(err)
}
RequiredSymbols = []string{}
err = Fetch(stream, conf, func(_ *health.Job, data []byte) error {
if string(data) != testExpectedFetchData {
t.Fatal("Fetch returned incorrect data\nGot:", string(data), "\nWanted:", testExpectedFetchData)
}
return nil
}, NewFileSystemWriter(outfilePath))
if err != nil {
t.Fatal(err)
}
// Make sure we wrote to outfiles
savedBytes, err := ioutil.ReadFile(path.Join(outfilePath, "rates"))
if err != nil {
t.Fatal(err)
}
if string(savedBytes) != testExpectedFetchData {
t.Fatal("Incorrect rates outfile contents:", string(savedBytes))
}
savedBytes, err = ioutil.ReadFile(path.Join(outfilePath, "whitelist"))
if err != nil {
t.Fatal(err)
}
if string(savedBytes) != string(PinnedSymbolsToIDsJSON()) {
t.Fatal("Incorrect whitelist outfile contents:", string(savedBytes))
}
}
func createHTTPMocks() func() {
httpmock.Activate()
for endpoint, resp := range httpMocks {
httpmock.RegisterResponder("GET", endpoint, httpmock.NewStringResponder(200, resp))
}
return httpmock.DeactivateAndReset
}