-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrivers_test.go
195 lines (171 loc) · 4.54 KB
/
rivers_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package rivers_test
import (
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/qba73/rivers"
)
func TestLoadCSV_LoadsExistingFile(t *testing.T) {
t.Parallel()
got, err := rivers.LoadWaterLevelCSV("testdata/data.csv")
if err != nil {
t.Fatal(err)
}
gotRows := len(got)
wantRows := 96
if wantRows != gotRows {
t.Errorf("want %d, got %v", wantRows, gotRows)
}
}
func TestLoadCSV_FailsOnNotExistingFile(t *testing.T) {
t.Parallel()
_, err := rivers.LoadWaterLevelCSV("testdata/notexisting.csv")
if err == nil {
t.Error("want error on not existing file")
}
}
func TestReadCSV(t *testing.T) {
got, err := rivers.ReadWaterLevelCSV(strings.NewReader(stationData))
if err != nil {
t.Fatal(err)
}
wantLen := 5
if wantLen != len(got) {
t.Errorf("want %d items, got %d", wantLen, len(got))
}
wantTimestamp, err := time.Parse(time.RFC3339, "2021-02-10T13:00:00Z")
if err != nil {
t.Fatal(err)
}
wantValue := 1772
want := rivers.WaterLevelReading{
Timestamp: wantTimestamp,
Value: wantValue,
}
if !cmp.Equal(want, got[0]) {
t.Errorf(cmp.Diff(want, got[0]))
}
}
func TestParseStationGroup_ErrorsOnEmptyInput(t *testing.T) {
t.Parallel()
_, err := rivers.ReadGroupCSV(strings.NewReader(invalidGroupInputNoData))
if err == nil {
t.Fatal("want err on empty input")
}
}
func TestParseStationGroup_ErrorsOnHeaderOnlyCSV(t *testing.T) {
t.Parallel()
_, err := rivers.ReadGroupCSV(strings.NewReader(invalidGroupInputHeaderOnly))
if err == nil {
t.Fatal("want err on empty input")
}
}
func TestParseStationGroup_ErrorsOnNoStationInCSV(t *testing.T) {
t.Parallel()
_, err := rivers.ReadGroupCSV(strings.NewReader(invalidGroupInputOneColumnOnly))
if err == nil {
t.Fatal("want err on a record without station")
}
}
func TestParseStationGroup_ParsesSingleRecord(t *testing.T) {
t.Parallel()
want := []rivers.WaterLevelReading{
{
Name: "John's Bridge Nore",
Timestamp: time.Date(2021, time.June, 15, 22, 00, 00, 00, time.UTC),
Value: 466,
},
{
Name: "Dinin Bridge",
Timestamp: time.Date(2021, time.June, 15, 22, 00, 00, 00, time.UTC),
Value: 53,
},
{
Name: "Brownsbarn",
Timestamp: time.Date(2021, time.June, 15, 22, 00, 00, 00, time.UTC),
Value: 413,
},
{
Name: "Mount Juliet",
Timestamp: time.Date(2021, time.June, 15, 22, 00, 00, 00, time.UTC),
Value: 451,
},
}
got, err := rivers.ReadGroupCSV(strings.NewReader(validGroupInputSingleRecord))
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(want, got) {
t.Fatal(cmp.Diff(want, got))
}
}
func TestParseStationGroup_ParsesMultipleRecords(t *testing.T) {
t.Parallel()
want := []rivers.WaterLevelReading{
{
Name: "John's Bridge Nore",
Timestamp: time.Date(2021, time.June, 15, 22, 00, 00, 00, time.UTC),
Value: 466,
},
{
Name: "Dinin Bridge",
Timestamp: time.Date(2021, time.June, 15, 22, 00, 00, 00, time.UTC),
Value: 53,
},
{
Name: "Brownsbarn",
Timestamp: time.Date(2021, time.June, 15, 22, 00, 00, 00, time.UTC),
Value: 413,
},
{
Name: "Mount Juliet",
Timestamp: time.Date(2021, time.June, 15, 22, 00, 00, 00, time.UTC),
Value: 451,
},
{
Name: "John's Bridge Nore",
Timestamp: time.Date(2021, time.June, 15, 22, 15, 00, 00, time.UTC),
Value: 400,
},
{
Name: "Dinin Bridge",
Timestamp: time.Date(2021, time.June, 15, 22, 15, 00, 00, time.UTC),
Value: 500,
},
{
Name: "Brownsbarn",
Timestamp: time.Date(2021, time.June, 15, 22, 15, 00, 00, time.UTC),
Value: 400,
},
{
Name: "Mount Juliet",
Timestamp: time.Date(2021, time.June, 15, 22, 15, 00, 00, time.UTC),
Value: 400,
},
}
got, err := rivers.ReadGroupCSV(strings.NewReader(validGroupInputMultipleRecords))
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(want, got) {
t.Fatal(cmp.Diff(want, got))
}
}
var (
validGroupInputSingleRecord = `Datetime,John's Bridge Nore,Dinin Bridge,Brownsbarn,Mount Juliet
2021-06-15 22:00,0.466,0.053,0.413,0.451`
validGroupInputMultipleRecords = `Datetime,John's Bridge Nore,Dinin Bridge,Brownsbarn,Mount Juliet
2021-06-15 22:00,0.466,0.053,0.413,0.451
2021-06-15 22:15,0.400,0.500,0.400,0.400`
invalidGroupInputNoData = ``
invalidGroupInputHeaderOnly = `Datetime,John's Bridge Nore,Dinin Bridge,Brownsbarn,Mount Juliet`
invalidGroupInputOneColumnOnly = `Datetime
2021-06-15 22:00`
stationData = `datetime,value
2021-02-10 13:00,1.772
2021-02-10 13:15,1.771
2021-02-10 13:30,1.769
2021-02-10 13:45,1.769
2021-02-10 14:00,1.768`
)