-
Notifications
You must be signed in to change notification settings - Fork 12
/
report_read_test.go
127 lines (120 loc) · 2.62 KB
/
report_read_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
package poplar
import (
"encoding/json"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoadReport(t *testing.T) {
data, err := ioutil.ReadFile("testdata/report.json")
require.NoError(t, err)
expectedReport := &Report{}
require.NoError(t, json.Unmarshal(data, expectedReport))
require.NoError(t, os.Setenv(APIKeyEnv, "key"))
require.NoError(t, os.Setenv(APISecretEnv, "secret"))
require.NoError(t, os.Setenv(APITokenEnv, "token"))
require.Empty(t, expectedReport.BucketConf.APIKey)
require.Empty(t, expectedReport.BucketConf.APISecret)
require.Empty(t, expectedReport.BucketConf.APIToken)
expectedReport.BucketConf.APIKey = "key"
expectedReport.BucketConf.APISecret = "secret"
expectedReport.BucketConf.APIToken = "token"
for _, test := range []struct {
name string
fn string
hasErr bool
}{
{
name: "FileDoesNotExist",
fn: "DNE",
hasErr: true,
},
{
name: "FileIsDir",
fn: "testdata",
hasErr: true,
},
{
name: "NoUnmarshaler",
fn: "testdata/csv_example.csv",
hasErr: true,
},
{
name: "MarshalsBSONCorrectly",
fn: "testdata/report.bson",
},
{
name: "MarshalsJSONCorrectly",
fn: "testdata/report.json",
},
{
name: "MarshalsYAMLCorrectly",
fn: "testdata/report.yaml",
},
} {
t.Run(test.name, func(t *testing.T) {
report, err := LoadReport(test.fn)
if test.hasErr {
assert.Nil(t, report)
assert.Error(t, err)
} else {
assert.Equal(t, expectedReport, report)
assert.NoError(t, err)
}
})
}
}
func TestLoadTests(t *testing.T) {
data, err := ioutil.ReadFile("testdata/tests.json")
require.NoError(t, err)
expectedTests := []Test{}
require.NoError(t, json.Unmarshal(data, &expectedTests))
expectedReport := &Report{Tests: expectedTests}
for _, test := range []struct {
name string
fn string
hasErr bool
}{
{
name: "FileDoesNotExist",
fn: "DNE",
hasErr: true,
},
{
name: "FileIsDir",
fn: "testdata",
hasErr: true,
},
{
name: "NoUnmarshaler",
fn: "testdata/csv_example.csv",
hasErr: true,
},
{
name: "MarshalsBSONFails",
fn: "testdata/report.bson",
hasErr: true,
},
{
name: "MarshalsJSONCorrectly",
fn: "testdata/tests.json",
},
{
name: "MarshalsYAMLCorrectly",
fn: "testdata/tests.yaml",
},
} {
t.Run(test.name, func(t *testing.T) {
report, err := LoadTests(test.fn)
if test.hasErr {
assert.Nil(t, report)
assert.Error(t, err)
} else {
assert.Equal(t, expectedReport, report)
assert.NoError(t, err)
}
})
}
}