-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfs_test.go
110 lines (104 loc) · 2.89 KB
/
fs_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
package portfolio_test
import (
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/portfoliotree/portfolio"
"github.com/portfoliotree/portfolio/allocation"
)
func TestParseSpecificationFile(t *testing.T) {
tmp := t.TempDir()
badPortfolioSpecFilepath := filepath.Join(tmp, "invalid_portfolio.yml")
// language=yaml
require.NoError(t, os.WriteFile(badPortfolioSpecFilepath, []byte(`type: Banana`), 0o666))
for _, tt := range []struct {
Name string
FilePath string
ErrorStringContains string
Portfolios []portfolio.Document
}{
{
Name: "asset ids with policy weights",
FilePath: filepath.Join("examples", "60-40_portfolio.yml"),
Portfolios: []portfolio.Document{
{
Type: "Portfolio",
Metadata: portfolio.Metadata{
Name: "60/40",
Benchmark: portfolio.Component{ID: "BIGPX"},
},
Spec: portfolio.Specification{
Assets: []portfolio.Component{
{ID: "ACWI"},
{ID: "AGG"},
},
Policy: portfolio.Policy{
Weights: []float64{60, 40},
WeightsAlgorithm: allocation.ConstantWeightsAlgorithmName,
RebalancingInterval: "Quarterly",
},
},
},
},
},
{
Name: "mixed asset spec node type and weight algorithm",
FilePath: filepath.Join("examples", "maang_portfolio.yml"),
Portfolios: []portfolio.Document{
{
Type: "Portfolio",
Metadata: portfolio.Metadata{
Name: "MAANG",
Benchmark: portfolio.Component{ID: "SPY"},
},
Spec: portfolio.Specification{
Assets: []portfolio.Component{
{ID: "META"},
{ID: "AMZN"},
{ID: "AAPL"},
{ID: "NFLX"},
{ID: "GOOG"},
},
Policy: portfolio.Policy{
RebalancingInterval: "Quarterly",
WeightsAlgorithm: allocation.EqualWeightsAlgorithmName,
},
},
},
},
},
{
Name: "file does not exist",
FilePath: "missing_portfolio.yml",
ErrorStringContains: "file",
},
{
Name: "not a yaml file",
FilePath: "lemon.png",
ErrorStringContains: "it must have a _portfolio.yml file name suffix",
},
{
Name: "not a valid portfolio specification",
FilePath: badPortfolioSpecFilepath,
ErrorStringContains: "incorrect specification type",
},
} {
t.Run(tt.Name, func(t *testing.T) {
portfolios, err := portfolio.ParseSpecificationFile(tt.FilePath)
if tt.ErrorStringContains == "" {
assert.NoError(t, err)
} else {
assert.Error(t, err)
assert.ErrorContains(t, err, tt.ErrorStringContains)
}
assert.Equal(t, tt.Portfolios, portfolios)
})
}
}
func TestLoadPortfolios(t *testing.T) {
specs, err := portfolio.WalkDirectoryAndParseSpecificationFiles(os.DirFS("examples"))
assert.NoError(t, err)
assert.Len(t, specs, 2)
}