-
Notifications
You must be signed in to change notification settings - Fork 0
/
readings_repo_test.go
197 lines (186 loc) · 5.05 KB
/
readings_repo_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
196
197
package rivers_test
import (
"errors"
"testing"
"time"
"database/sql"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
_ "github.com/mattn/go-sqlite3" // DB diver for SQLite3
"github.com/qba73/rivers"
)
func TestListGetsAllWaterLevelReadingsFromDatabase(t *testing.T) {
t.Parallel()
db := newTestDB(stmtRetrieveLastReadingForOneStation, t)
readings := rivers.ReadingsRepo{
Store: &rivers.SQLiteStore{
DB: db,
},
}
got, err := readings.List()
if err != nil {
t.Fatal(err)
}
want := []rivers.StationWaterLevelReading{
{
StationID: 1042,
Name: "Sandy Millss",
Readtime: time.Date(2022, 06, 28, 04, 45, 00, 00, time.UTC),
WaterLevel: 383,
},
{
StationID: 1043,
Name: "Ballybofey",
Readtime: time.Date(2022, 06, 28, 04, 15, 00, 00, time.UTC),
WaterLevel: 679,
},
{
StationID: 1043,
Name: "Ballybofey",
Readtime: time.Date(2022, 06, 29, 05, 15, 00, 00, time.UTC),
WaterLevel: 779,
},
{
StationID: 1043,
Name: "Ballybofey",
Readtime: time.Date(2022, 06, 30, 04, 15, 00, 00, time.UTC),
WaterLevel: 879,
},
{
StationID: 3055,
Name: "Glaslough",
Readtime: time.Date(2022, 06, 28, 04, 45, 00, 00, time.UTC),
WaterLevel: 478,
},
}
if !cmp.Equal(want, got) {
t.Errorf(cmp.Diff(want, got))
}
}
func TestRetrieveLastReadingForOneStation(t *testing.T) {
t.Parallel()
db := newTestDB(stmtRetrieveLastReadingForOneStation, t)
readings := rivers.ReadingsRepo{
Store: &rivers.SQLiteStore{
DB: db,
},
}
got, err := readings.GetLastReadingForStationID(1043)
if err != nil {
t.Fatal(err)
}
want := rivers.StationWaterLevelReading{
StationID: 1043,
Name: "Ballybofey",
Readtime: time.Date(2022, 06, 30, 04, 15, 00, 00, time.UTC),
WaterLevel: 879,
}
if !cmp.Equal(want, got) {
t.Error(cmp.Diff(want, got))
}
}
func TestAddSingleReadingToTheStore(t *testing.T) {
t.Parallel()
db := newTestDB(stmtEmptyDB, t)
readings := rivers.ReadingsRepo{
Store: &rivers.SQLiteStore{
DB: db,
},
}
want := rivers.StationWaterLevelReading{
StationID: 3055,
Name: "Glaslough",
Readtime: time.Now(),
WaterLevel: 991,
}
err := readings.Add(want)
if err != nil {
t.Fatal(err)
}
got, err := readings.GetLastReadingForStationID(3055)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(want, got, cmpopts.IgnoreFields(rivers.StationWaterLevelReading{}, "Readtime")) {
t.Error(cmp.Diff(want, got))
}
}
func TestAdd_DoesNotAddDuplicateReadings(t *testing.T) {
t.Parallel()
db := newTestDB(stmtEmptyDB, t)
readings := rivers.ReadingsRepo{
Store: &rivers.SQLiteStore{
DB: db,
},
}
want := rivers.StationWaterLevelReading{
StationID: 3055,
Readtime: time.Date(2022, 06, 28, 04, 15, 00, 00, time.UTC),
}
err := readings.Add(want)
if err != nil {
t.Fatal(err)
}
got, err := readings.GetLastReadingForStationID(3055)
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(want, got, cmpopts.IgnoreFields(rivers.StationWaterLevelReading{}, "Readtime")) {
t.Error(cmp.Diff(want, got))
}
// Try to add the same reading second time.
err = readings.Add(want)
if !errors.Is(err, rivers.ErrReadingExists) {
t.Fatalf("want ErrReadingExists, got %v", err)
}
gotReadings, err := readings.List()
if err != nil {
t.Error(err)
}
if len(gotReadings) != 1 {
t.Error("it's not filtering out duplicates")
}
}
func newTestDB(stmtPopulateData string, t *testing.T) *sql.DB {
db, err := sql.Open("sqlite3", ":memory:")
if err != nil {
t.Fatal(err)
}
if err = db.Ping(); err != nil {
t.Fatal(err)
}
_, err = db.Exec(waterLevelSchema)
if err != nil {
t.Fatal(err)
}
if stmtPopulateData != "" {
_, err = db.Exec(stmtPopulateData)
if err != nil {
t.Fatal(err)
}
}
// Call the func to clean database after each test.
// This way we don't need to pullute test logic with `defer`.
t.Cleanup(func() {
if _, err := db.Exec(`DROP TABLE waterlevel_readings`); err != nil {
t.Fatalf("error cleaning up test database: %#v", err)
}
})
return db
}
var (
waterLevelSchema = `DROP TABLE IF EXISTS waterlevel_readings;
CREATE TABLE waterlevel_readings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
station_id INT NOT NULL,
station_name CHAR(50) NOT NULL,
datetime TEXT NOT NULL,
value INTEGER);`
// DB statements for populating data
stmtRetrieveLastReadingForOneStation = `INSERT INTO "waterlevel_readings" (station_id, station_name, datetime, value) VALUES (1042,'Sandy Millss',datetime('2022-06-28 04:45:00-00:00'),383);
INSERT INTO "waterlevel_readings" (station_id, station_name, datetime, value) VALUES(1043,'Ballybofey',datetime('2022-06-28 04:15:00-00:00'),679);
INSERT INTO "waterlevel_readings" (station_id, station_name, datetime, value) VALUES(1043,'Ballybofey',datetime('2022-06-29 05:15:00-00:00'),779);
INSERT INTO "waterlevel_readings" (station_id, station_name, datetime, value) VALUES(1043,'Ballybofey',datetime('2022-06-30 04:15:00-00:00'),879);
INSERT INTO "waterlevel_readings" (station_id, station_name, datetime, value) VALUES(3055,'Glaslough',datetime('2022-06-28 04:45:00-00:00'),478);`
stmtEmptyDB = ``
)