-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgovega_test.go
111 lines (101 loc) · 2.65 KB
/
govega_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
/*************************************************************************
* Copyright 2022 Gravwell, Inc. All rights reserved.
* Contact: <[email protected]>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package govega
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
)
const dataFileName = `data.json`
const badSpec = `examples/garbage.json`
var testFiles = []string{
`examples/bar-chart.vg.json`,
`examples/grouped-bar-chart.vg.json`,
`examples/county-unemployment/county-unemployment.vg.json`,
`examples/job-voyager/job-voyager.vg.json`,
`examples/u-district-cuisine/u-district-cuisine.vg.json`,
}
func TestNew(t *testing.T) {
if _, err := New(); err != nil {
t.Error(err)
}
}
func TestBadSpec(t *testing.T) {
spec, data, err := loadSpecAndData(badSpec)
if err != nil {
t.Fatal(err)
}
vm, err := New()
if err != nil {
t.Error(err)
}
ctx, cf := context.WithTimeout(context.Background(), time.Second)
defer cf()
if svg, err := vm.Render(spec, data, ctx); err == nil {
t.Fatal(err)
} else if svg != nil {
t.Fatal("returned SVG is not nil after bad spec")
}
//make sure the context isn't what fired
if err = ctx.Err(); err != nil {
t.Fatal("Context error fired", err)
}
}
func TestGoodSpecs(t *testing.T) {
vm, err := New()
if err != nil {
t.Error(err)
}
for _, s := range testFiles {
spec, data, err := loadSpecAndData(s)
if err != nil {
t.Fatalf("Failed to load %q %v", s, err)
}
ts := time.Now()
//some of these can take some time, make the timeout large so tests don't fail on slow machines
ctx, cf := context.WithTimeout(context.Background(), 30*time.Second)
if svg, err := vm.Render(spec, data, ctx); err != nil {
t.Fatalf("Failed to render %q - %v", s, err)
} else if svg == nil {
t.Fatal("returned SVG is nil")
} else if !bytes.HasPrefix(svg, []byte(`<svg`)) {
t.Fatal("returned value not an SVG?")
}
cf()
if testing.Verbose() {
fmt.Printf("%q took %v\n", s, time.Since(ts))
}
}
}
func loadSpecAndData(sp string) (spec []byte, data map[string]interface{}, err error) {
if spec, err = ioutil.ReadFile(sp); err != nil {
return
}
//check if there is a data file
datafile := filepath.Join(filepath.Dir(sp), dataFileName)
var fin *os.File
if fin, err = os.Open(datafile); err != nil {
//no data file, clear error
if os.IsNotExist(err) {
err = nil
}
return
}
if err = json.NewDecoder(fin).Decode(&data); err != nil {
fin.Close()
return
}
err = fin.Close()
return
}