-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnavitia_test.go
181 lines (153 loc) · 3.97 KB
/
navitia_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
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
package navitia
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/pkg/errors"
)
var (
testDataPathFlag = flag.String("path", "./testdata", "Directory of test data")
testDataPath string
)
func init() {
flag.Parse()
// If the given path is absolute, then use it as-is
if filepath.IsAbs(*testDataPathFlag) {
testDataPath = *testDataPathFlag
} else {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
testDataPath = filepath.Join(wd, *testDataPathFlag)
}
err := load()
if err != nil {
panic(err)
}
}
type testPair struct {
raw []byte
correct interface{}
}
// Convert testing mechanism to known compare + corpus runs
type typeTestData struct {
known map[string]testPair // Hardcoded map of pairs of JSON data / the Go representation they should have
correct map[string][]byte // Map of known correct files to be parsed (they should _not_ return an error)
incorrect map[string][]byte // Map of known incorrect files to be parsed (they _should_ return an error)
bench map[string][]byte // Descriptions -> File
}
// testData stores a map which maps each category to their data
var testData = make(map[string]typeTestData, len(typesList))
// this is the list of potential types
// must be lower case
var typesList = []string{
"journeys",
"coverage",
"places",
"connections",
}
// listCategoryDirs retrieves the subdirectories under the main testdata directory
func listCategoryDirs(path string) ([]os.FileInfo, error) {
mainSubdirs, err := ioutil.ReadDir(path)
if err != nil {
return nil, errors.Wrapf(err, "Error while reading %s's files", path)
}
var subDirsInfo []os.FileInfo
// Iterate through the subdirs
for _, dinfo := range mainSubdirs {
// We have a category !
if dinfo.IsDir() {
subDirsInfo = append(subDirsInfo, dinfo)
}
}
return subDirsInfo, nil
}
// extractCorpus extracts a corpus
func extractCorpus(path string) (map[string][]byte, error) {
// List the files
files, err := ioutil.ReadDir(path)
if err != nil {
return nil, err
}
// Create the data
corpus := make(map[string][]byte, len(files))
// For each of them, populate testpairs
for _, finfo := range files {
// Get the name
name := finfo.Name()
// Build the path
filePath := filepath.Join(path, name)
// Open the file
file, err := os.Open(filePath)
if err != nil {
return corpus, err
}
// Read it all
read, err := ioutil.ReadAll(file)
if err != nil {
return corpus, err
}
// Assign it
corpus[name] = read
}
return corpus, nil
}
// getPertinentSubdirs, given a dir in a category subdirectory, returns the awaited values
func getCategory(path string) (typeTestData, error) {
// Create the data
data := typeTestData{}
// List the subdirs
subdirs, err := ioutil.ReadDir(path)
if err != nil {
return data, err
}
// Iterate through the subdirs
for _, dinfo := range subdirs {
if dinfo.IsDir() {
switch dinfo.Name() {
case "correct":
correctPath := filepath.Join(path, "correct")
data.correct, err = extractCorpus(correctPath)
if err != nil {
return data, err
}
case "incorrect":
incorrectPath := filepath.Join(path, "incorrect")
data.incorrect, err = extractCorpus(incorrectPath)
if err != nil {
return data, err
}
case "bench":
benchPath := filepath.Join(path, "bench")
data.bench, err = extractCorpus(benchPath)
if err != nil {
return data, err
}
}
}
}
// return
return data, nil
}
// load loads the file structing into the testData
func load() error {
subDirsInfo, err := listCategoryDirs(testDataPath)
if err != nil {
return err
}
// For each of them, call getCategory
for _, dinfo := range subDirsInfo {
name := dinfo.Name()
path := filepath.Join(testDataPath, name)
data, err := getCategory(path)
if err != nil {
return err
}
fmt.Printf("For %s we have:\n\tCorrect:\t%d\n\tIncorrect:\t%d\n\tBench:\t%d\n", path, len(data.correct), len(data.incorrect), len(data.bench))
testData[name] = data
}
return err
}