-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_test.go
114 lines (103 loc) · 2.9 KB
/
api_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
package portfolio_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/portfoliotree/portfolio"
"github.com/portfoliotree/portfolio/portfoliotest"
)
func TestMain(m *testing.M) {
server := httptest.NewServer(testdataAssetReturns(portfoliotest.ComponentReturnsProvider()))
http.DefaultClient = server.Client()
_ = os.Setenv(portfolio.ServerURLEnvironmentVariableName, server.URL)
os.Exit(func() int {
defer server.Close()
return m.Run()
}())
}
func testdataAssetReturns(crp portfolio.ComponentReturnsProvider) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
assets, err := portfolio.ParseComponentsFromURL(req.URL.Query(), "asset")
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
table, err := crp.ComponentReturnsTable(req.Context(), assets...)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
writeJSONResponse(res, table)
}
}
func writeJSONResponse(res http.ResponseWriter, data any) {
buf, err := json.Marshal(data)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
res.WriteHeader(http.StatusOK)
_, _ = res.Write(buf)
}
func Test_APIEndpoints(t *testing.T) {
if value, found := os.LookupEnv("CI"); !found || value != "true" {
t.Skip("Skipping test in CI environment")
}
t.Run("returns", func(t *testing.T) {
pf := portfolio.Specification{
Assets: []portfolio.Component{
{ID: "AAPL"},
{ID: "GOOG"},
},
}
table, err := pf.AssetReturns(context.Background())
assert.NoError(t, err)
if table.NumberOfColumns() != 2 {
t.Errorf("Expected 2 columns, got %d", table.NumberOfColumns())
}
if table.NumberOfRows() < 10 {
t.Errorf("Expected at least 10 rows, got %d", table.NumberOfRows())
}
})
}
func TestSpecification_AssetReturns(t *testing.T) {
for _, tt := range []struct {
Name string
ctx context.Context
pf portfolio.Specification
ErrorStringContains string
}{
{
Name: "nil context",
pf: portfolio.Specification{Assets: []portfolio.Component{{ID: "AAPL"}}},
ErrorStringContains: "Context",
},
{
Name: "no assets",
pf: portfolio.Specification{Assets: []portfolio.Component{}},
},
{
Name: "nil assets",
pf: portfolio.Specification{Assets: nil},
},
} {
t.Run(tt.Name, func(t *testing.T) {
_, err := tt.pf.AssetReturns(tt.ctx)
if tt.ErrorStringContains == "" {
assert.NoError(t, err)
} else {
assert.ErrorContains(t, err, tt.ErrorStringContains)
}
})
}
}
func Test_Specification_AssetReturns_bad_URL(t *testing.T) {
t.Setenv(portfolio.ServerURLEnvironmentVariableName, ":lemon:")
pf := portfolio.Specification{Assets: []portfolio.Component{{ID: "AAPL"}}}
_, err := pf.AssetReturns(context.Background())
assert.ErrorContains(t, err, "lemon")
}