-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_test.go
461 lines (392 loc) · 14.5 KB
/
app_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
package app
import (
"context"
"fmt"
"github.com/DATA-DOG/go-sqlmock"
rd "github.com/redis/go-redis/v9"
"github.com/stretchr/testify/suite"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/modules/redis"
"github.com/testcontainers/testcontainers-go/wait"
pg "gorm.io/driver/postgres"
"gorm.io/gorm"
"strconv"
"testing"
"time"
)
// NoteRepoTestSuite represents the note repository test suite.
type NoteRepoTestSuite struct {
suite.Suite
ctx context.Context
db *gorm.DB
pgContainer *postgres.PostgresContainer
pgConnectionString string
rdContainer *redis.RedisContainer
rdConnectionString string
rdClient *rd.Client
}
func (suite *NoteRepoTestSuite) SetupSuite() {
suite.ctx = context.Background()
pgContainer, err := postgres.RunContainer(
suite.ctx,
testcontainers.WithImage("postgres:15.3-alpine"),
postgres.WithDatabase("notesdb"),
postgres.WithUsername("postgres"),
postgres.WithPassword("postgres"),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).WithStartupTimeout(5*time.Second)),
)
suite.NoError(err)
connStr, err := pgContainer.ConnectionString(suite.ctx, "sslmode=disable")
suite.NoError(err)
db, err := gorm.Open(pg.Open(connStr), &gorm.Config{})
suite.NoError(err)
suite.pgContainer = pgContainer
suite.pgConnectionString = connStr
suite.db = db
sqlDB, err := suite.db.DB()
suite.NoError(err)
err = sqlDB.Ping()
suite.NoError(err)
redisContainer, err := redis.RunContainer(suite.ctx, testcontainers.WithImage("redis:6"))
suite.NoError(err)
rdConnStr, err := redisContainer.ConnectionString(suite.ctx)
suite.NoError(err)
rdConnOptions, err := rd.ParseURL(rdConnStr)
suite.NoError(err)
rdClient := rd.NewClient(rdConnOptions)
suite.rdContainer = redisContainer
suite.rdConnectionString = connStr
suite.rdClient = rdClient
err = suite.rdClient.Ping(suite.ctx).Err()
suite.NoError(err)
}
func (suite *NoteRepoTestSuite) TearDownSuite() {
err := suite.pgContainer.Terminate(suite.ctx)
suite.NoError(err)
err = suite.rdContainer.Terminate(suite.ctx)
suite.NoError(err)
}
func (suite *NoteRepoTestSuite) SetupTest() {
err := suite.db.AutoMigrate(&Note{})
suite.NoError(err)
}
func (suite *NoteRepoTestSuite) TearDownTest() {
suite.db.Exec("DROP TABLE IF EXISTS notes CASCADE;")
suite.rdClient.FlushAll(suite.ctx)
}
func (suite *NoteRepoTestSuite) BeforeTest(_ string, testName string) {
if testName == "TestSaveUpdatedNote" || testName == "TestDeleteNote" {
note := Note{Title: "Test Update", Content: "This note will be inserted now"}
result := suite.db.Save(¬e)
suite.NoError(result.Error)
idKey := fmt.Sprintf("notes:%d", note.ID)
titleKey := fmt.Sprintf("notes:%s", note.Title)
err := suite.rdClient.HSet(suite.ctx, idKey, "id", note.ID).Err()
suite.NoError(err)
err = suite.rdClient.HSet(suite.ctx, idKey, "title", note.Title).Err()
suite.NoError(err)
err = suite.rdClient.HSet(suite.ctx, idKey, "content", note.Content).Err()
suite.NoError(err)
err = suite.rdClient.HSet(suite.ctx, idKey, "created_at", note.CreatedAt).Err()
suite.NoError(err)
err = suite.rdClient.HSet(suite.ctx, idKey, "updated_at", note.UpdatedAt).Err()
suite.NoError(err)
err = suite.rdClient.HSet(suite.ctx, titleKey, "id", note.ID).Err()
suite.NoError(err)
err = suite.rdClient.HSet(suite.ctx, titleKey, "title", note.Title).Err()
suite.NoError(err)
err = suite.rdClient.HSet(suite.ctx, titleKey, "content", note.Content).Err()
suite.NoError(err)
err = suite.rdClient.HSet(suite.ctx, titleKey, "created_at", note.CreatedAt).Err()
suite.NoError(err)
err = suite.rdClient.HSet(suite.ctx, titleKey, "updated_at", note.UpdatedAt).Err()
suite.NoError(err)
}
}
func (suite *NoteRepoTestSuite) TestSaveNewNote() {
// ensure that the cache is empty
keys, err := suite.rdClient.Keys(suite.ctx, "*").Result()
suite.NoError(err)
suite.Empty(keys)
// ensure that the postgres database is empty
var notes []Note
result := suite.db.Find(¬es)
suite.NoError(result.Error)
suite.Empty(notes)
// create repository and save new note
repo := NewNoteRepository(suite.db, suite.rdClient)
newNote := Note{Title: "Testing 123", Content: "This note was just inserted"}
err = repo.SaveNote(&newNote)
suite.NoError(err)
// ensure the cache is still empty
keys, err = suite.rdClient.Keys(suite.ctx, "*").Result()
suite.NoError(err)
suite.Empty(keys)
// ensure that we have a new note in the database
result = suite.db.Find(¬es)
suite.NoError(result.Error)
suite.Equal(1, len(notes))
suite.Equal(newNote.ID, notes[0].ID)
suite.Equal(newNote.Title, notes[0].Title)
suite.Equal(newNote.Content, notes[0].Content)
}
func (suite *NoteRepoTestSuite) TestSaveUpdatedNote() {
// ensure that we have a note in the database
var note Note
result := suite.db.First(¬e)
suite.NoError(result.Error)
suite.NotZero(note)
idKey := fmt.Sprintf("notes:%d", note.ID)
titleKey := fmt.Sprintf("notes:%s", note.Title)
// ensure that we have the note cached under its id
res, err := suite.rdClient.Exists(suite.ctx, idKey).Result()
suite.NoError(err)
suite.Greater(res, int64(0))
// ensure that we have the note cached under its title
res, err = suite.rdClient.Exists(suite.ctx, titleKey).Result()
suite.NoError(err)
suite.Greater(res, int64(0))
// update the note and save it
repo := NewNoteRepository(suite.db, suite.rdClient)
note.Content = "This is the updated note"
err = repo.SaveNote(¬e)
suite.NoError(err)
// ensure the cache is invalidated
res, err = suite.rdClient.Exists(suite.ctx, idKey).Result()
suite.NoError(err)
suite.Equal(int64(0), res)
res, err = suite.rdClient.Exists(suite.ctx, titleKey).Result()
suite.NoError(err)
suite.Equal(int64(0), res)
// ensure the note has been updated in the database.
var notes []Note
result = suite.db.Find(¬es)
suite.NoError(result.Error)
suite.Equal(1, len(notes))
suite.Equal(note.ID, notes[0].ID)
suite.Equal(note.Title, notes[0].Title)
suite.Equal(note.Content, notes[0].Content)
}
func (suite *NoteRepoTestSuite) TestDeleteNote() {
// ensure we have a note in the database
var note Note
result := suite.db.First(¬e)
suite.NoError(result.Error)
suite.NotZero(note)
idKey := fmt.Sprintf("notes:%d", note.ID)
titleKey := fmt.Sprintf("notes:%s", note.Title)
// ensure we have the note cached
res, err := suite.rdClient.Exists(suite.ctx, idKey).Result()
suite.NoError(err)
suite.Greater(res, int64(0))
res, err = suite.rdClient.Exists(suite.ctx, titleKey).Result()
suite.NoError(err)
suite.Greater(res, int64(0))
// delete the note
repo := NewNoteRepository(suite.db, suite.rdClient)
err = repo.DeleteNote(int(note.ID))
suite.NoError(err)
// ensure that the cache has been cleared
res, err = suite.rdClient.Exists(suite.ctx, idKey).Result()
suite.NoError(err)
suite.Equal(int64(0), res)
res, err = suite.rdClient.Exists(suite.ctx, titleKey).Result()
suite.NoError(err)
suite.Equal(int64(0), res)
// ensure that the note has been deleted in postgres
var notes []Note
result = suite.db.Find(¬es)
suite.NoError(result.Error)
suite.Empty(notes)
}
func (suite *NoteRepoTestSuite) TestGetNote() {
suite.Run("Get note when note does not exist in cache", func() {
// empty the notes table and flush the cache
suite.T().Cleanup(func() {
suite.db.Exec("DELETE FROM notes;")
suite.rdClient.FlushAll(suite.ctx)
})
// insert a new note in the database
dbNote := Note{
Title: "Testing 123",
Content: "This is a test content",
}
result := suite.db.Save(&dbNote)
suite.NoError(result.Error)
// ensure that the cache is empty
res, err := suite.rdClient.Exists(suite.ctx, fmt.Sprintf("notes:%d", dbNote.ID)).Result()
suite.NoError(err)
suite.Equal(int64(0), res)
res, err = suite.rdClient.Exists(suite.ctx, "notes:Testing 123").Result()
suite.NoError(err)
suite.Equal(int64(0), res)
// get a note by its id
repo := NewNoteRepository(suite.db, suite.rdClient)
note := repo.GetNoteById(int(dbNote.ID))
suite.NotNil(note)
// ensure that the note is now cached
res, err = suite.rdClient.Exists(suite.ctx, fmt.Sprintf("notes:%d", dbNote.ID)).Result()
suite.NoError(err)
suite.Greater(res, int64(0))
res, err = suite.rdClient.Exists(suite.ctx, "notes:Testing 123").Result()
suite.NoError(err)
suite.Greater(res, int64(0))
noteMap, err := suite.rdClient.HGetAll(suite.ctx, fmt.Sprintf("notes:%d", dbNote.ID)).Result()
suite.NoError(err)
suite.Equal(strconv.Itoa(int(dbNote.ID)), noteMap["id"])
suite.Equal("Testing 123", noteMap["title"])
suite.Equal("This is a test content", noteMap["content"])
noteMap, err = suite.rdClient.HGetAll(suite.ctx, "notes:Testing 123").Result()
suite.NoError(err)
suite.Equal(strconv.Itoa(int(dbNote.ID)), noteMap["id"])
suite.Equal("Testing 123", noteMap["title"])
suite.Equal("This is a test content", noteMap["content"])
})
suite.Run("Get note when note exists in cache", func() {
// empty the notes table and flush the cache
suite.T().Cleanup(func() {
suite.db.Exec("DELETE FROM notes;")
suite.rdClient.FlushAll(suite.ctx)
})
// insert note in database
dbNote := Note{
Title: "Testing 123",
Content: "This is a test content",
}
result := suite.db.Save(&dbNote)
suite.NoError(result.Error)
idKey := fmt.Sprintf("notes:%d", dbNote.ID)
titleKey := fmt.Sprintf("notes:%s", dbNote.Title)
// cache the note
suite.rdClient.HSet(suite.ctx, idKey, "id", dbNote.ID)
suite.rdClient.HSet(suite.ctx, idKey, "title", dbNote.Title)
suite.rdClient.HSet(suite.ctx, idKey, "content", dbNote.Content)
suite.rdClient.HSet(suite.ctx, idKey, "created_at", dbNote.CreatedAt)
suite.rdClient.HSet(suite.ctx, idKey, "updated_at", dbNote.UpdatedAt)
suite.rdClient.HSet(suite.ctx, titleKey, "id", dbNote.ID)
suite.rdClient.HSet(suite.ctx, titleKey, "title", dbNote.Title)
suite.rdClient.HSet(suite.ctx, titleKey, "content", dbNote.Content)
suite.rdClient.HSet(suite.ctx, titleKey, "created_at", dbNote.CreatedAt)
suite.rdClient.HSet(suite.ctx, titleKey, "updated_at", dbNote.UpdatedAt)
// construct mock sql database
mockDb, mock, err := sqlmock.New()
suite.NoError(err)
suite.T().Cleanup(func() {
mockDb.Close()
})
dialector := pg.New(pg.Config{
Conn: mockDb,
DriverName: "postgres",
})
db, err := gorm.Open(dialector, &gorm.Config{})
suite.NoError(err)
// get the note by id and ensure the note was successfully retrieved
repo := NewNoteRepository(db, suite.rdClient)
note := repo.GetNoteById(int(dbNote.ID))
suite.NotNil(note)
suite.Equal(dbNote.ID, note.ID)
suite.Equal(dbNote.Title, note.Title)
suite.Equal(dbNote.Content, note.Content)
// ensure all query expectations were met
// since we didn't set any expectations
// this would only pass if no query was executed on the database
// indicating that our repository didn't query the database to get
// the note.
err = mock.ExpectationsWereMet()
suite.NoError(err)
})
suite.Run("Get note by title when note does not exist in cache", func() {
// empty the notes table and flush the cache
suite.T().Cleanup(func() {
suite.db.Exec("DELETE FROM notes;")
suite.rdClient.FlushAll(suite.ctx)
})
// insert note in db
dbNote := Note{
Title: "Testing 1234",
Content: "This is a test content",
}
result := suite.db.Save(&dbNote)
suite.NoError(result.Error)
// ensure note is not cached
res, err := suite.rdClient.Exists(suite.ctx, fmt.Sprintf("notes:%d", dbNote.ID)).Result()
suite.NoError(err)
suite.Equal(int64(0), res)
res, err = suite.rdClient.Exists(suite.ctx, "notes:Testing 1234").Result()
suite.NoError(err)
suite.Equal(int64(0), res)
// get a note by its title
repo := NewNoteRepository(suite.db, suite.rdClient)
note := repo.GetNoteByTitle(dbNote.Title)
suite.NotNil(note)
// ensure the note is now cached
res, err = suite.rdClient.Exists(suite.ctx, fmt.Sprintf("notes:%d", dbNote.ID)).Result()
suite.NoError(err)
suite.Greater(res, int64(0))
res, err = suite.rdClient.Exists(suite.ctx, "notes:Testing 1234").Result()
suite.NoError(err)
suite.Greater(res, int64(0))
noteMap, err := suite.rdClient.HGetAll(suite.ctx, fmt.Sprintf("notes:%d", dbNote.ID)).Result()
suite.NoError(err)
suite.Equal(strconv.Itoa(int(dbNote.ID)), noteMap["id"])
suite.Equal("Testing 1234", noteMap["title"])
suite.Equal("This is a test content", noteMap["content"])
noteMap, err = suite.rdClient.HGetAll(suite.ctx, "notes:Testing 1234").Result()
suite.NoError(err)
suite.Equal(strconv.Itoa(int(dbNote.ID)), noteMap["id"])
suite.Equal("Testing 1234", noteMap["title"])
suite.Equal("This is a test content", noteMap["content"])
})
suite.Run("Get note by title when note exists in cache", func() {
// empty the notes table and flush the cache
suite.T().Cleanup(func() {
suite.db.Exec("DELETE FROM notes;")
suite.rdClient.FlushAll(suite.ctx)
})
dbNote := Note{
Title: "Testing 123",
Content: "This is a test content",
}
result := suite.db.Save(&dbNote)
suite.NoError(result.Error)
idKey := fmt.Sprintf("notes:%d", dbNote.ID)
titleKey := fmt.Sprintf("notes:%s", dbNote.Title)
// store note in cache
suite.rdClient.HSet(suite.ctx, idKey, "id", dbNote.ID)
suite.rdClient.HSet(suite.ctx, idKey, "title", dbNote.Title)
suite.rdClient.HSet(suite.ctx, idKey, "content", dbNote.Content)
suite.rdClient.HSet(suite.ctx, idKey, "created_at", dbNote.CreatedAt)
suite.rdClient.HSet(suite.ctx, idKey, "updated_at", dbNote.UpdatedAt)
suite.rdClient.HSet(suite.ctx, titleKey, "id", dbNote.ID)
suite.rdClient.HSet(suite.ctx, titleKey, "title", dbNote.Title)
suite.rdClient.HSet(suite.ctx, titleKey, "content", dbNote.Content)
suite.rdClient.HSet(suite.ctx, titleKey, "created_at", dbNote.CreatedAt)
suite.rdClient.HSet(suite.ctx, titleKey, "updated_at", dbNote.UpdatedAt)
// setup database mock
mockDb, mock, err := sqlmock.New()
suite.NoError(err)
suite.T().Cleanup(func() {
mockDb.Close()
})
dialector := pg.New(pg.Config{
Conn: mockDb,
DriverName: "postgres",
})
db, err := gorm.Open(dialector, &gorm.Config{})
suite.NoError(err)
repo := NewNoteRepository(db, suite.rdClient)
note := repo.GetNoteByTitle(dbNote.Title)
suite.NotNil(note)
suite.Equal(dbNote.ID, note.ID)
suite.Equal(dbNote.Title, note.Title)
suite.Equal(dbNote.Content, note.Content)
err = mock.ExpectationsWereMet()
suite.NoError(err)
})
}
func TestNoteRepository(t *testing.T) {
suite.Run(t, new(NoteRepoTestSuite))
}