-
Notifications
You must be signed in to change notification settings - Fork 2
/
fixture.go
231 lines (213 loc) · 4.88 KB
/
fixture.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package mongotest
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"gopkg.in/yaml.v2"
)
// UseFixtureWithContext apply fixture data to MongoDB with context.Context.
// If multi names are given, fixture data will be merged.(overwriting by after dataset)
func UseFixtureWithContext(ctx context.Context, names ...string) error {
if err := validateConfig(); err != nil {
return err
}
files, err := toFilePaths(names...)
if err != nil {
return err
}
ds, err := loadDataSet(files...)
if err != nil {
return err
}
for cn, cd := range ds {
vs, err := toValues(cn, cd)
if err != nil {
return err
}
err = resetCollection(ctx, cn, vs)
if err != nil {
return err
}
}
return nil
}
// UseFixture apply fixture data to MongoDB.
// If multi names are given, fixture data will be merged.(overwriting by after dataset)
func UseFixture(names ...string) error {
return UseFixtureWithContext(context.Background(), names...)
}
func toFilePaths(names ...string) ([]string, error) {
files := make([]string, len(names))
for i, name := range names {
file, err := findFixtureFilePath(name)
if err != nil {
return nil, err
}
files[i] = file
}
return files, nil
}
func findFixtureFilePath(name string) (string, error) {
dir, base := fixturePath(name)
fis, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}
for _, fi := range fis {
if fi.Name() == base+filepath.Ext(fi.Name()) && !fi.IsDir() {
return filepath.Join(dir, fi.Name()), nil
}
}
return "", fmt.Errorf("DataSet %q not found in %s", name, conf.fixtureRootDirAbs)
}
func fixturePath(name string) (dir string, base string) {
paths := splitDataSetName(name)
if len(paths) == 1 {
return conf.fixtureRootDirAbs, name
}
dirPaths := []string{conf.fixtureRootDirAbs}
dirPaths = append(dirPaths, paths[0:len(paths)-1]...)
return filepath.Join(dirPaths...), paths[len(paths)-1]
}
func splitDataSetName(name string) []string {
if strings.Contains(name, "/") {
return strings.Split(name, "/")
}
return []string{name}
}
func loadDataSet(files ...string) (DataSet, error) {
dss, err := toDataSets(files...)
if err != nil {
return nil, err
}
return mergeDataSet(dss), nil
}
func toDataSets(files ...string) ([]DataSet, error) {
dss := make([]DataSet, len(files))
for i, file := range files {
ds, err := readFixtureFile(file)
if err != nil {
return nil, err
}
dss[i] = ds
}
return dss, nil
}
func readFixtureFile(file string) (DataSet, error) {
format, err := fixtureFormat(file)
if err != nil {
return nil, err
}
bs, err := ioutil.ReadFile(file)
if err != nil {
return nil, err
}
ds := make(DataSet)
if format == FixtureFormatYAML {
err = yaml.Unmarshal(bs, &ds)
} else if format == FixtureFormatJSON {
err = json.Unmarshal(bs, &ds)
}
if err != nil {
return nil, err
}
return ds, nil
}
func fixtureFormat(file string) (FixtureFormatType, error) {
if conf.FixtureFormat != FixtureFormatAuto {
return conf.FixtureFormat, nil
}
ext := strings.ToLower(filepath.Ext(file))
switch ext {
case ".json":
return FixtureFormatJSON, nil
case ".yaml", ".yml":
return FixtureFormatYAML, nil
default:
return fixtureFormatUnknown, errors.New("unknown format")
}
}
func mergeDataSet(dss []DataSet) DataSet {
merged := make(DataSet)
for _, ds := range dss {
for k, v := range ds {
if coll, ok := merged[k]; ok {
merged[k] = mergeCollData(coll, v)
} else {
merged[k] = v
}
}
}
return merged
}
func mergeCollData(coll1, coll2 CollectionData) CollectionData {
merged := make(CollectionData)
for k, v := range coll1 {
merged[k] = v
}
for k, v := range coll2 {
if doc, ok := merged[k]; ok {
merged[k] = mergeDocData(doc, v)
} else {
merged[k] = v
}
}
return merged
}
func mergeDocData(doc1, doc2 DocData) DocData {
merged := make(DocData)
for k, v := range doc1 {
merged[k] = v
}
for k, v := range doc2 {
merged[k] = v
}
return merged
}
func toValues(collectionName string, coll CollectionData) ([]interface{}, error) {
values := make([]interface{}, 0, len(coll))
for id, doc := range coll {
newDoc := make(DocData)
for k, v := range doc {
newDoc[k] = v
}
newDoc["_id"] = id
v, err := applyPreFuncs(collectionName, newDoc)
if err != nil {
return nil, err
}
values = append(values, v)
}
return values, nil
}
func applyPreFuncs(collName string, value DocData) (DocData, error) {
if conf.PreInsertFuncs == nil {
return value, nil
}
v := value
var err error
for _, fn := range conf.PreInsertFuncs {
v, err = fn(collName, v)
if err != nil {
return nil, err
}
}
return v, nil
}
func resetCollection(ctx context.Context, name string, values []interface{}) error {
ctx, collection, cancel, err := connectCollection(ctx, name)
if err != nil {
return err
}
defer cancel()
err = collection.Drop(ctx)
if err != nil {
return err
}
_, err = collection.InsertMany(ctx, values)
return err
}