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
|
package database
import (
"fmt"
"testing"
"git.adyxax.org/adyxax/trains/pkg/model"
"github.com/DATA-DOG/go-sqlmock"
"github.com/stretchr/testify/require"
)
func TestCountStops(t *testing.T) {
stops := []model.Stop{
model.Stop{Id: "id1", Name: "name1"},
model.Stop{Id: "id2", Name: "name2"},
}
// test db setup
db, err := InitDB("sqlite3", "file::memory:?_foreign_keys=on")
require.NoError(t, err)
// check sql error
i, err := db.CountStops()
require.Error(t, err)
requireErrorTypeMatch(t, err, QueryError{})
// normal check
err = db.Migrate()
require.NoError(t, err)
err = db.ReplaceAndImportStops(stops)
i, err = db.CountStops()
require.NoError(t, err)
require.Equal(t, i, len(stops))
}
func TestGetStop(t *testing.T) {
stops := []model.Stop{
model.Stop{Id: "id1", Name: "name1"},
model.Stop{Id: "id2", Name: "name2"},
}
// test db setup
db, err := InitDB("sqlite3", "file::memory:?_foreign_keys=on")
require.NoError(t, err)
err = db.Migrate()
require.NoError(t, err)
err = db.ReplaceAndImportStops(stops)
// normal check
stop, err := db.GetStop("id1")
require.NoError(t, err)
require.Equal(t, stop, &stops[0])
// error check
stop, err = db.GetStop("non_existent")
require.Error(t, err)
requireErrorTypeMatch(t, err, QueryError{})
}
func TestGetStops(t *testing.T) {
stops := []model.Stop{
model.Stop{Id: "id1", Name: "name1"},
model.Stop{Id: "id2", Name: "name2"},
}
// test db setup
db, err := InitDB("sqlite3", "file::memory:?_foreign_keys=on")
require.NoError(t, err)
// error check
res, err := db.GetStops()
require.Error(t, err)
requireErrorTypeMatch(t, err, QueryError{})
// normal check
err = db.Migrate()
require.NoError(t, err)
err = db.ReplaceAndImportStops(stops)
res, err = db.GetStops()
require.NoError(t, err)
require.Equal(t, res, stops)
}
func TestGetStopsWithSQLMock(t *testing.T) {
// Transaction commit error
db, mock, err := sqlmock.New()
require.NoError(t, err, "an error '%s' was not expected when opening a stub database connection", err)
mock.ExpectQuery(`SELECT id, name FROM stops;`).WillReturnRows(sqlmock.NewRows([]string{"id", "name"}).AddRow(nil, "b").RowError(1, fmt.Errorf("row error")))
_, err = (&DBEnv{db: db}).GetStops()
require.Error(t, err)
requireErrorTypeMatch(t, err, QueryError{})
}
func TestReplaceAndImportStops(t *testing.T) {
// test db setup
db, err := InitDB("sqlite3", "file::memory:?_foreign_keys=on")
require.NoError(t, err)
err = db.Migrate()
require.NoError(t, err)
// datasets
data1 := []model.Stop{
model.Stop{Id: "first", Name: "firstName"},
model.Stop{Id: "second", Name: "secondName"},
}
data2 := []model.Stop{
model.Stop{Id: "first", Name: "firstTest"},
model.Stop{Id: "secondTest", Name: "secondTest"},
model.Stop{Id: "thirdTest", Name: "thirdTest"},
}
testCases := []struct {
name string
input []model.Stop
expectedError error
}{
{"Normal insert", data1, nil},
{"Normal insert overwrite", data2, nil},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := db.ReplaceAndImportStops(tc.input)
if tc.expectedError != nil {
require.Error(t, err)
requireErrorTypeMatch(t, err, tc.expectedError)
} else {
require.NoError(t, err)
}
})
}
}
func TestReplaceAndImportStopsWithSQLMock(t *testing.T) {
// datasets
data1 := []model.Stop{
model.Stop{Id: "first", Name: "firstName"},
model.Stop{Id: "second", Name: "secondName"},
}
// Transaction begin error
dbBeginError, _, err := sqlmock.New()
require.NoError(t, err, "an error '%s' was not expected when opening a stub database connection", err)
defer dbBeginError.Close()
// Query error cannot delete from
dbCannotDeleteFrom, mockCannotDeleteFrom, err := sqlmock.New()
require.NoError(t, err, "an error '%s' was not expected when opening a stub database connection", err)
defer dbCannotDeleteFrom.Close()
mockCannotDeleteFrom.ExpectBegin()
// Transaction commit error
dbCannotInsertError, mockCannotInsertError, err := sqlmock.New()
require.NoError(t, err, "an error '%s' was not expected when opening a stub database connection", err)
defer dbCannotInsertError.Close()
mockCannotInsertError.ExpectBegin()
mockCannotInsertError.ExpectExec(`DELETE FROM`).WillReturnResult(sqlmock.NewResult(1, 1))
// Transaction commit error
dbCommitError, mockCommitError, err := sqlmock.New()
require.NoError(t, err, "an error '%s' was not expected when opening a stub database connection", err)
defer dbCommitError.Close()
mockCommitError.ExpectBegin()
mockCommitError.ExpectExec(`DELETE FROM`).WillReturnResult(sqlmock.NewResult(1, 1))
mockCommitError.ExpectExec(`INSERT INTO`).WillReturnResult(sqlmock.NewResult(1, 1))
mockCommitError.ExpectExec(`INSERT INTO`).WillReturnResult(sqlmock.NewResult(1, 1))
// Test cases
testCases := []struct {
name string
db *DBEnv
expectedError error
}{
{"begin transaction error", &DBEnv{db: dbBeginError}, TransactionError{}},
{"query error cannot delete from", &DBEnv{db: dbCannotDeleteFrom}, QueryError{}},
{"query error cannot insert into", &DBEnv{db: dbCannotInsertError}, QueryError{}},
{"commit transaction error", &DBEnv{db: dbCommitError}, TransactionError{}},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := tc.db.ReplaceAndImportStops(data1)
if tc.expectedError != nil {
require.Error(t, err)
requireErrorTypeMatch(t, err, tc.expectedError)
} else {
require.NoError(t, err)
}
})
}
}
|