1
|
|
|
package repository |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"database/sql" |
5
|
|
|
"fmt" |
6
|
|
|
"testing" |
7
|
|
|
"time" |
8
|
|
|
|
9
|
|
|
"github.com/architectv/estate-task/pkg/model" |
10
|
|
|
"github.com/stretchr/testify/assert" |
11
|
|
|
sqlmock "github.com/zhashkevych/go-sqlxmock" |
12
|
|
|
) |
13
|
|
|
|
14
|
|
|
func TestBookingPostgres_Create(t *testing.T) { |
15
|
|
|
db, mock, err := sqlmock.Newx() |
16
|
|
|
if err != nil { |
17
|
|
|
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) |
18
|
|
|
} |
19
|
|
|
defer db.Close() |
20
|
|
|
|
21
|
|
|
r := NewBookingPostgres(db) |
22
|
|
|
|
23
|
|
|
type args struct { |
24
|
|
|
booking *model.Booking |
25
|
|
|
} |
26
|
|
|
type mockBehavior func(args args) |
27
|
|
|
|
28
|
|
|
tests := []struct { |
29
|
|
|
name string |
30
|
|
|
mock mockBehavior |
31
|
|
|
input args |
32
|
|
|
want int |
33
|
|
|
wantErr bool |
34
|
|
|
}{ |
35
|
|
|
{ |
36
|
|
|
name: "Ok", |
37
|
|
|
input: args{ |
38
|
|
|
booking: &model.Booking{ |
39
|
|
|
RoomId: 1, |
40
|
|
|
DateStart: time.Date(2021, time.January, 5, 0, 0, 0, 0, time.UTC), |
41
|
|
|
DateEnd: time.Date(2021, time.January, 8, 0, 0, 0, 0, time.UTC), |
42
|
|
|
}, |
43
|
|
|
}, |
44
|
|
|
mock: func(args args) { |
45
|
|
|
booking := args.booking |
46
|
|
|
rows := sqlmock.NewRows([]string{"id"}).AddRow(1) |
47
|
|
|
mock.ExpectQuery(fmt.Sprintf("INSERT INTO %s", bookingsTable)). |
48
|
|
|
WithArgs(booking.RoomId, booking.DateStart, booking.DateEnd). |
49
|
|
|
WillReturnRows(rows) |
50
|
|
|
}, |
51
|
|
|
want: 1, |
52
|
|
|
wantErr: false, |
53
|
|
|
}, |
54
|
|
|
{ |
55
|
|
|
name: "Wrong Room Id", |
56
|
|
|
input: args{ |
57
|
|
|
booking: &model.Booking{ |
58
|
|
|
RoomId: 1, |
59
|
|
|
DateStart: time.Date(2021, time.January, 5, 0, 0, 0, 0, time.UTC), |
60
|
|
|
DateEnd: time.Date(2021, time.January, 8, 0, 0, 0, 0, time.UTC), |
61
|
|
|
}, |
62
|
|
|
}, |
63
|
|
|
mock: func(args args) { |
64
|
|
|
booking := args.booking |
65
|
|
|
rows := sqlmock.NewRows([]string{"id"}) |
66
|
|
|
mock.ExpectQuery(fmt.Sprintf("INSERT INTO %s", bookingsTable)). |
67
|
|
|
WithArgs(booking.RoomId, booking.DateStart, booking.DateEnd). |
68
|
|
|
WillReturnRows(rows) |
69
|
|
|
}, |
70
|
|
|
wantErr: true, |
71
|
|
|
}, |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
for _, test := range tests { |
75
|
|
|
t.Run(test.name, func(t *testing.T) { |
76
|
|
|
test.mock(test.input) |
77
|
|
|
|
78
|
|
|
got, err := r.Create(test.input.booking) |
79
|
|
|
if test.wantErr { |
80
|
|
|
assert.Error(t, err) |
81
|
|
|
} else { |
82
|
|
|
assert.NoError(t, err) |
83
|
|
|
assert.Equal(t, test.want, got) |
84
|
|
|
} |
85
|
|
|
}) |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
func TestBookingPostgres_Delete(t *testing.T) { |
90
|
|
|
db, mock, err := sqlmock.Newx() |
91
|
|
|
if err != nil { |
92
|
|
|
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) |
93
|
|
|
} |
94
|
|
|
defer db.Close() |
95
|
|
|
|
96
|
|
|
r := NewBookingPostgres(db) |
97
|
|
|
|
98
|
|
|
type args struct { |
99
|
|
|
id int |
100
|
|
|
} |
101
|
|
|
type mockBehavior func(args args) |
102
|
|
|
|
103
|
|
|
tests := []struct { |
104
|
|
|
name string |
105
|
|
|
mock mockBehavior |
106
|
|
|
input args |
107
|
|
|
wantErr bool |
108
|
|
|
}{ |
109
|
|
|
{ |
110
|
|
|
name: "Ok", |
111
|
|
|
input: args{ |
112
|
|
|
id: 1, |
113
|
|
|
}, |
114
|
|
|
mock: func(args args) { |
115
|
|
|
mock.ExpectExec(fmt.Sprintf("DELETE FROM %s WHERE (.+)", bookingsTable)). |
116
|
|
|
WithArgs(args.id).WillReturnResult(sqlmock.NewResult(0, 1)) |
117
|
|
|
}, |
118
|
|
|
wantErr: false, |
119
|
|
|
}, |
120
|
|
|
{ |
121
|
|
|
name: "Not Found", |
122
|
|
|
input: args{ |
123
|
|
|
id: 1, |
124
|
|
|
}, |
125
|
|
|
mock: func(args args) { |
126
|
|
|
mock.ExpectExec(fmt.Sprintf("DELETE FROM %s WHERE (.+)", bookingsTable)). |
127
|
|
|
WithArgs(args.id).WillReturnError(sql.ErrNoRows) |
128
|
|
|
}, |
129
|
|
|
wantErr: true, |
130
|
|
|
}, |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
for _, test := range tests { |
134
|
|
|
t.Run(test.name, func(t *testing.T) { |
135
|
|
|
test.mock(test.input) |
136
|
|
|
|
137
|
|
|
err := r.Delete(test.input.id) |
138
|
|
|
if test.wantErr { |
139
|
|
|
assert.Error(t, err) |
140
|
|
|
} else { |
141
|
|
|
assert.NoError(t, err) |
142
|
|
|
} |
143
|
|
|
}) |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
func TestBookingPostgres_GetByRoomId(t *testing.T) { |
148
|
|
|
db, mock, err := sqlmock.Newx() |
149
|
|
|
if err != nil { |
150
|
|
|
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) |
151
|
|
|
} |
152
|
|
|
defer db.Close() |
153
|
|
|
|
154
|
|
|
r := NewBookingPostgres(db) |
155
|
|
|
|
156
|
|
|
type args struct { |
157
|
|
|
roomId int |
158
|
|
|
} |
159
|
|
|
type mockBehavior func(args args) |
160
|
|
|
|
161
|
|
|
tests := []struct { |
162
|
|
|
name string |
163
|
|
|
mock mockBehavior |
164
|
|
|
input args |
165
|
|
|
want []*model.Booking |
166
|
|
|
wantErr bool |
167
|
|
|
}{ |
168
|
|
|
{ |
169
|
|
|
name: "Ok", |
170
|
|
|
input: args{ |
171
|
|
|
roomId: 1, |
172
|
|
|
}, |
173
|
|
|
mock: func(args args) { |
174
|
|
|
dateStart1 := time.Date(2021, time.January, 5, 0, 0, 0, 0, time.UTC) |
175
|
|
|
dateEnd1 := time.Date(2021, time.January, 8, 0, 0, 0, 0, time.UTC) |
176
|
|
|
|
177
|
|
|
dateStart2 := time.Date(2021, time.January, 25, 0, 0, 0, 0, time.UTC) |
178
|
|
|
dateEnd2 := time.Date(2021, time.January, 28, 0, 0, 0, 0, time.UTC) |
179
|
|
|
|
180
|
|
|
rows := sqlmock.NewRows([]string{"id", "room_id", "date_start", "date_end"}). |
181
|
|
|
AddRow(1, 1, dateStart1, dateEnd1). |
182
|
|
|
AddRow(2, 1, dateStart2, dateEnd2) |
183
|
|
|
|
184
|
|
|
mock.ExpectQuery(fmt.Sprintf("SELECT (.+) FROM %s WHERE (.+)", bookingsTable)). |
185
|
|
|
WithArgs(args.roomId).WillReturnRows(rows) |
186
|
|
|
}, |
187
|
|
|
want: []*model.Booking{ |
188
|
|
|
{ |
189
|
|
|
Id: 1, |
190
|
|
|
RoomId: 1, |
191
|
|
|
DateStart: time.Date(2021, time.January, 5, 0, 0, 0, 0, time.UTC), |
192
|
|
|
DateEnd: time.Date(2021, time.January, 8, 0, 0, 0, 0, time.UTC), |
193
|
|
|
}, |
194
|
|
|
{ |
195
|
|
|
Id: 2, |
196
|
|
|
RoomId: 1, |
197
|
|
|
DateStart: time.Date(2021, time.January, 25, 0, 0, 0, 0, time.UTC), |
198
|
|
|
DateEnd: time.Date(2021, time.January, 28, 0, 0, 0, 0, time.UTC), |
199
|
|
|
}, |
200
|
|
|
}, |
201
|
|
|
wantErr: false, |
202
|
|
|
}, |
203
|
|
|
{ |
204
|
|
|
name: "Ok Empty List", |
205
|
|
|
input: args{ |
206
|
|
|
roomId: 1, |
207
|
|
|
}, |
208
|
|
|
mock: func(args args) { |
209
|
|
|
rows := sqlmock.NewRows([]string{"id", "room_id", "date_start", "date_end"}) |
210
|
|
|
|
211
|
|
|
mock.ExpectQuery(fmt.Sprintf("SELECT (.+) FROM %s WHERE (.+)", bookingsTable)). |
212
|
|
|
WithArgs(args.roomId).WillReturnRows(rows) |
213
|
|
|
}, |
214
|
|
|
want: nil, |
215
|
|
|
wantErr: false, |
216
|
|
|
}, |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
for _, test := range tests { |
220
|
|
|
t.Run(test.name, func(t *testing.T) { |
221
|
|
|
test.mock(test.input) |
222
|
|
|
|
223
|
|
|
got, err := r.GetByRoomId(test.input.roomId) |
224
|
|
|
if test.wantErr { |
225
|
|
|
assert.Error(t, err) |
226
|
|
|
} else { |
227
|
|
|
assert.NoError(t, err) |
228
|
|
|
assert.Equal(t, test.want, got) |
229
|
|
|
} |
230
|
|
|
}) |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
func TestBookingPostgres_GetById(t *testing.T) { |
235
|
|
|
db, mock, err := sqlmock.Newx() |
236
|
|
|
if err != nil { |
237
|
|
|
t.Fatalf("an error '%s' was not expected when opening a stub database connection", err) |
238
|
|
|
} |
239
|
|
|
defer db.Close() |
240
|
|
|
|
241
|
|
|
r := NewBookingPostgres(db) |
242
|
|
|
|
243
|
|
|
type args struct { |
244
|
|
|
id int |
245
|
|
|
} |
246
|
|
|
type mockBehavior func(args args) |
247
|
|
|
|
248
|
|
|
tests := []struct { |
249
|
|
|
name string |
250
|
|
|
mock mockBehavior |
251
|
|
|
input args |
252
|
|
|
want *model.Booking |
253
|
|
|
wantErr bool |
254
|
|
|
}{ |
255
|
|
|
{ |
256
|
|
|
name: "Ok", |
257
|
|
|
input: args{ |
258
|
|
|
id: 1, |
259
|
|
|
}, |
260
|
|
|
mock: func(args args) { |
261
|
|
|
dateStart := time.Date(2021, time.January, 5, 0, 0, 0, 0, time.UTC) |
262
|
|
|
dateEnd := time.Date(2021, time.January, 8, 0, 0, 0, 0, time.UTC) |
263
|
|
|
rows := sqlmock.NewRows([]string{"id", "room_id", "date_start", "date_end"}). |
264
|
|
|
AddRow(1, 1, dateStart, dateEnd) |
265
|
|
|
|
266
|
|
|
mock.ExpectQuery(fmt.Sprintf("SELECT (.+) FROM %s WHERE (.+)", bookingsTable)). |
267
|
|
|
WithArgs(args.id).WillReturnRows(rows) |
268
|
|
|
}, |
269
|
|
|
want: &model.Booking{ |
270
|
|
|
Id: 1, |
271
|
|
|
RoomId: 1, |
272
|
|
|
DateStart: time.Date(2021, time.January, 5, 0, 0, 0, 0, time.UTC), |
273
|
|
|
DateEnd: time.Date(2021, time.January, 8, 0, 0, 0, 0, time.UTC), |
274
|
|
|
}, |
275
|
|
|
wantErr: false, |
276
|
|
|
}, |
277
|
|
|
{ |
278
|
|
|
name: "Not Found", |
279
|
|
|
input: args{ |
280
|
|
|
id: 1, |
281
|
|
|
}, |
282
|
|
|
mock: func(args args) { |
283
|
|
|
rows := sqlmock.NewRows([]string{"id", "room_id", "date_start", "date_end"}) |
284
|
|
|
|
285
|
|
|
mock.ExpectQuery(fmt.Sprintf("SELECT (.+) FROM %s WHERE (.+)", bookingsTable)). |
286
|
|
|
WithArgs(args.id).WillReturnRows(rows) |
287
|
|
|
}, |
288
|
|
|
wantErr: true, |
289
|
|
|
}, |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
for _, test := range tests { |
293
|
|
|
t.Run(test.name, func(t *testing.T) { |
294
|
|
|
test.mock(test.input) |
295
|
|
|
|
296
|
|
|
got, err := r.GetById(test.input.id) |
297
|
|
|
if test.wantErr { |
298
|
|
|
assert.Error(t, err) |
299
|
|
|
} else { |
300
|
|
|
assert.NoError(t, err) |
301
|
|
|
assert.Equal(t, test.want, got) |
302
|
|
|
} |
303
|
|
|
}) |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|