forked from pegnet/pegnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasource_test.go
66 lines (52 loc) · 1.59 KB
/
datasource_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
package polling_test
import (
"net/http"
"testing"
"github.com/pegnet/pegnet/common"
"github.com/pegnet/pegnet/polling"
"github.com/pegnet/pegnet/testutils"
"github.com/zpatrick/go-config"
)
// FixedDataSourceTest will test the parsing of the data source using the fixed response
func FixedDataSourceTest(t *testing.T, source string, fixed []byte) {
defer func() { http.DefaultClient = &http.Client{} }() // Don't leave http broken
c := config.NewConfig([]config.Provider{common.NewUnitTestConfigProvider()})
// Set default http client to return what we expect from apilayer
cl := testutils.GetClientWithFixedResp(fixed)
http.DefaultClient = cl
polling.NewHTTPClient = func() *http.Client {
return testutils.GetClientWithFixedResp(fixed)
}
s, err := polling.NewDataSource(source, c)
if err != nil {
t.Error(err)
}
testDataSource(t, s)
}
// ActualDataSourceTest actually fetches the resp over the internet
func ActualDataSourceTest(t *testing.T, source string) {
defer func() { http.DefaultClient = &http.Client{} }() // Don't leave http broken
c := config.NewConfig([]config.Provider{common.NewUnitTestConfigProvider()})
http.DefaultClient = &http.Client{}
s, err := polling.NewDataSource(source, c)
if err != nil {
t.Error(err)
}
testDataSource(t, s)
}
func testDataSource(t *testing.T, s polling.IDataSource) {
pegs, err := s.FetchPegPrices()
if err != nil {
t.Error(err)
}
for _, asset := range s.SupportedPegs() {
r, ok := pegs[asset]
if !ok {
t.Errorf("Missing %s", asset)
}
err := testutils.PriceCheck(asset, r.Value)
if err != nil {
t.Error(err)
}
}
}