|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "golang-rest-api-template/pkg/cache" |
| 11 | + "golang-rest-api-template/pkg/models" |
| 12 | + |
| 13 | + "github.com/go-redis/redis/v8" |
| 14 | + "github.com/golang/mock/gomock" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +type fakeBookStore struct { |
| 19 | + listFn func(offset, limit int) ([]models.Book, error) |
| 20 | + createFn func(book *models.Book) error |
| 21 | + firstFn func(id uint) (*models.Book, error) |
| 22 | + updateFn func(id uint, title, author string) (*models.Book, error) |
| 23 | + deleteFn func(id uint) error |
| 24 | +} |
| 25 | + |
| 26 | +func (f *fakeBookStore) List(offset, limit int) ([]models.Book, error) { |
| 27 | + if f.listFn != nil { |
| 28 | + return f.listFn(offset, limit) |
| 29 | + } |
| 30 | + return nil, nil |
| 31 | +} |
| 32 | + |
| 33 | +func (f *fakeBookStore) Create(book *models.Book) error { |
| 34 | + if f.createFn != nil { |
| 35 | + return f.createFn(book) |
| 36 | + } |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +func (f *fakeBookStore) FirstByID(id uint) (*models.Book, error) { |
| 41 | + if f.firstFn != nil { |
| 42 | + return f.firstFn(id) |
| 43 | + } |
| 44 | + return nil, nil |
| 45 | +} |
| 46 | + |
| 47 | +func (f *fakeBookStore) UpdateFields(id uint, title, author string) (*models.Book, error) { |
| 48 | + if f.updateFn != nil { |
| 49 | + return f.updateFn(id, title, author) |
| 50 | + } |
| 51 | + return nil, nil |
| 52 | +} |
| 53 | + |
| 54 | +func (f *fakeBookStore) DeleteByID(id uint) error { |
| 55 | + if f.deleteFn != nil { |
| 56 | + return f.deleteFn(id) |
| 57 | + } |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +func TestBooksListDataCacheKey(t *testing.T) { |
| 62 | + assert.Equal(t, "books_g2_offset_5_limit_10", BooksListDataCacheKey(2, 5, 10)) |
| 63 | +} |
| 64 | + |
| 65 | +func TestListBooksNilRedisUsesStore(t *testing.T) { |
| 66 | + want := []models.Book{{ID: 1, Title: "a", Author: "b"}} |
| 67 | + store := &fakeBookStore{ |
| 68 | + listFn: func(offset, limit int) ([]models.Book, error) { |
| 69 | + assert.Equal(t, 3, offset) |
| 70 | + assert.Equal(t, 7, limit) |
| 71 | + return want, nil |
| 72 | + }, |
| 73 | + } |
| 74 | + svc := NewBookService(store, nil) |
| 75 | + got, err := svc.ListBooks(context.Background(), 3, 7) |
| 76 | + assert.NoError(t, err) |
| 77 | + assert.Equal(t, want, got) |
| 78 | +} |
| 79 | + |
| 80 | +func TestListBooksCacheHit(t *testing.T) { |
| 81 | + ctrl := gomock.NewController(t) |
| 82 | + defer ctrl.Finish() |
| 83 | + mockRedis := cache.NewMockCache(ctrl) |
| 84 | + |
| 85 | + want := []models.Book{{ID: 9, Title: "cached", Author: "x"}} |
| 86 | + payload, err := json.Marshal(want) |
| 87 | + assert.NoError(t, err) |
| 88 | + dataKey := BooksListDataCacheKey(0, 0, 10) |
| 89 | + |
| 90 | + gomock.InOrder( |
| 91 | + mockRedis.EXPECT().Get(gomock.Any(), BooksListCacheGenKey).Return(redis.NewStringResult("", redis.Nil)), |
| 92 | + mockRedis.EXPECT().Get(gomock.Any(), dataKey).Return(redis.NewStringResult(string(payload), nil)), |
| 93 | + ) |
| 94 | + |
| 95 | + store := &fakeBookStore{ |
| 96 | + listFn: func(offset, limit int) ([]models.Book, error) { |
| 97 | + t.Fatal("store.List should not run on cache hit") |
| 98 | + return nil, nil |
| 99 | + }, |
| 100 | + } |
| 101 | + svc := NewBookService(store, mockRedis) |
| 102 | + got, err := svc.ListBooks(context.Background(), 0, 10) |
| 103 | + assert.NoError(t, err) |
| 104 | + assert.Equal(t, want, got) |
| 105 | +} |
| 106 | + |
| 107 | +func TestListBooksCacheUnmarshalError(t *testing.T) { |
| 108 | + ctrl := gomock.NewController(t) |
| 109 | + defer ctrl.Finish() |
| 110 | + mockRedis := cache.NewMockCache(ctrl) |
| 111 | + dataKey := BooksListDataCacheKey(0, 1, 5) |
| 112 | + |
| 113 | + gomock.InOrder( |
| 114 | + mockRedis.EXPECT().Get(gomock.Any(), BooksListCacheGenKey).Return(redis.NewStringResult("0", nil)), |
| 115 | + mockRedis.EXPECT().Get(gomock.Any(), dataKey).Return(redis.NewStringResult("not-json", nil)), |
| 116 | + ) |
| 117 | + |
| 118 | + svc := NewBookService(&fakeBookStore{}, mockRedis) |
| 119 | + _, err := svc.ListBooks(context.Background(), 1, 5) |
| 120 | + assert.Error(t, err) |
| 121 | + assert.ErrorIs(t, err, ErrListBooksUnmarshal) |
| 122 | +} |
| 123 | + |
| 124 | +func TestListBooksStoreError(t *testing.T) { |
| 125 | + ctrl := gomock.NewController(t) |
| 126 | + defer ctrl.Finish() |
| 127 | + mockRedis := cache.NewMockCache(ctrl) |
| 128 | + dataKey := BooksListDataCacheKey(0, 0, 10) |
| 129 | + dbErr := errors.New("db down") |
| 130 | + |
| 131 | + gomock.InOrder( |
| 132 | + mockRedis.EXPECT().Get(gomock.Any(), BooksListCacheGenKey).Return(redis.NewStringResult("", redis.Nil)), |
| 133 | + mockRedis.EXPECT().Get(gomock.Any(), dataKey).Return(redis.NewStringResult("", redis.Nil)), |
| 134 | + ) |
| 135 | + |
| 136 | + store := &fakeBookStore{ |
| 137 | + listFn: func(offset, limit int) ([]models.Book, error) { |
| 138 | + return nil, dbErr |
| 139 | + }, |
| 140 | + } |
| 141 | + svc := NewBookService(store, mockRedis) |
| 142 | + _, err := svc.ListBooks(context.Background(), 0, 10) |
| 143 | + assert.Error(t, err) |
| 144 | + assert.ErrorIs(t, err, ErrListBooksDB) |
| 145 | +} |
| 146 | + |
| 147 | +func TestListBooksRedisSetError(t *testing.T) { |
| 148 | + ctrl := gomock.NewController(t) |
| 149 | + defer ctrl.Finish() |
| 150 | + mockRedis := cache.NewMockCache(ctrl) |
| 151 | + dataKey := BooksListDataCacheKey(0, 0, 10) |
| 152 | + want := []models.Book{{Title: "t", Author: "a"}} |
| 153 | + |
| 154 | + gomock.InOrder( |
| 155 | + mockRedis.EXPECT().Get(gomock.Any(), BooksListCacheGenKey).Return(redis.NewStringResult("", redis.Nil)), |
| 156 | + mockRedis.EXPECT().Get(gomock.Any(), dataKey).Return(redis.NewStringResult("", redis.Nil)), |
| 157 | + ) |
| 158 | + mockRedis.EXPECT().Set(gomock.Any(), dataKey, gomock.Any(), time.Minute).Return(redis.NewStatusResult("", errors.New("set failed"))) |
| 159 | + |
| 160 | + store := &fakeBookStore{ |
| 161 | + listFn: func(offset, limit int) ([]models.Book, error) { |
| 162 | + return want, nil |
| 163 | + }, |
| 164 | + } |
| 165 | + svc := NewBookService(store, mockRedis) |
| 166 | + _, err := svc.ListBooks(context.Background(), 0, 10) |
| 167 | + assert.Error(t, err) |
| 168 | + assert.ErrorIs(t, err, ErrListBooksRedis) |
| 169 | +} |
| 170 | + |
| 171 | +func TestCreateBookBumpsGenerationWhenRedis(t *testing.T) { |
| 172 | + ctrl := gomock.NewController(t) |
| 173 | + defer ctrl.Finish() |
| 174 | + mockRedis := cache.NewMockCache(ctrl) |
| 175 | + mockRedis.EXPECT().Incr(gomock.Any(), BooksListCacheGenKey).Return(redis.NewIntResult(1, nil)).Times(1) |
| 176 | + |
| 177 | + store := &fakeBookStore{ |
| 178 | + createFn: func(book *models.Book) error { |
| 179 | + book.ID = 42 |
| 180 | + return nil |
| 181 | + }, |
| 182 | + } |
| 183 | + svc := NewBookService(store, mockRedis) |
| 184 | + book, err := svc.CreateBook(context.Background(), "t", "a") |
| 185 | + assert.NoError(t, err) |
| 186 | + assert.NotNil(t, book) |
| 187 | + assert.Equal(t, uint(42), book.ID) |
| 188 | +} |
| 189 | + |
| 190 | +func TestCreateBookNoIncrWhenNilRedis(t *testing.T) { |
| 191 | + store := &fakeBookStore{ |
| 192 | + createFn: func(book *models.Book) error { |
| 193 | + return nil |
| 194 | + }, |
| 195 | + } |
| 196 | + svc := NewBookService(store, nil) |
| 197 | + _, err := svc.CreateBook(context.Background(), "t", "a") |
| 198 | + assert.NoError(t, err) |
| 199 | +} |
| 200 | + |
| 201 | +func TestGetBookDelegates(t *testing.T) { |
| 202 | + want := &models.Book{ID: 3, Title: "x", Author: "y"} |
| 203 | + store := &fakeBookStore{ |
| 204 | + firstFn: func(id uint) (*models.Book, error) { |
| 205 | + assert.Equal(t, uint(3), id) |
| 206 | + return want, nil |
| 207 | + }, |
| 208 | + } |
| 209 | + svc := NewBookService(store, nil) |
| 210 | + got, err := svc.GetBook(context.Background(), 3) |
| 211 | + assert.NoError(t, err) |
| 212 | + assert.Equal(t, want, got) |
| 213 | +} |
| 214 | + |
| 215 | +func TestUpdateBookBumpsGeneration(t *testing.T) { |
| 216 | + ctrl := gomock.NewController(t) |
| 217 | + defer ctrl.Finish() |
| 218 | + mockRedis := cache.NewMockCache(ctrl) |
| 219 | + mockRedis.EXPECT().Incr(gomock.Any(), BooksListCacheGenKey).Return(redis.NewIntResult(2, nil)).Times(1) |
| 220 | + |
| 221 | + updated := &models.Book{ID: 1, Title: "n", Author: "m"} |
| 222 | + store := &fakeBookStore{ |
| 223 | + updateFn: func(id uint, title, author string) (*models.Book, error) { |
| 224 | + assert.Equal(t, uint(1), id) |
| 225 | + assert.Equal(t, "n", title) |
| 226 | + assert.Equal(t, "m", author) |
| 227 | + return updated, nil |
| 228 | + }, |
| 229 | + } |
| 230 | + svc := NewBookService(store, mockRedis) |
| 231 | + got, err := svc.UpdateBook(context.Background(), 1, "n", "m") |
| 232 | + assert.NoError(t, err) |
| 233 | + assert.Equal(t, updated, got) |
| 234 | +} |
| 235 | + |
| 236 | +func TestDeleteBookBumpsGeneration(t *testing.T) { |
| 237 | + ctrl := gomock.NewController(t) |
| 238 | + defer ctrl.Finish() |
| 239 | + mockRedis := cache.NewMockCache(ctrl) |
| 240 | + mockRedis.EXPECT().Incr(gomock.Any(), BooksListCacheGenKey).Return(redis.NewIntResult(1, nil)).Times(1) |
| 241 | + |
| 242 | + store := &fakeBookStore{ |
| 243 | + deleteFn: func(id uint) error { |
| 244 | + assert.Equal(t, uint(9), id) |
| 245 | + return nil |
| 246 | + }, |
| 247 | + } |
| 248 | + svc := NewBookService(store, mockRedis) |
| 249 | + assert.NoError(t, svc.DeleteBook(context.Background(), 9)) |
| 250 | +} |
0 commit comments