|
| 1 | +package http |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "musicboxapi/database" |
| 9 | + "musicboxapi/models" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/assert" |
| 15 | +) |
| 16 | + |
| 17 | +type mockPlaylistTable struct { |
| 18 | + database.IPlaylistTable |
| 19 | + fetchPlaylists func(ctx context.Context, lastKnowPlaylistId int) (playlists []models.Playlist, error error) |
| 20 | + insertPlaylist func(playlist models.Playlist) (lastInsertedId int, error error) |
| 21 | + deletePlaylist func(playlistId int) (error error) |
| 22 | +} |
| 23 | + |
| 24 | +func (m *mockPlaylistTable) FetchPlaylists(ctx context.Context, lastKnowPlaylistId int) (playlists []models.Playlist, error error) { |
| 25 | + return m.fetchPlaylists(ctx, lastKnowPlaylistId) |
| 26 | +} |
| 27 | +func (m *mockPlaylistTable) InsertPlaylist(playlist models.Playlist) (lastInsertedId int, error error) { |
| 28 | + return m.insertPlaylist(playlist) |
| 29 | +} |
| 30 | +func (m *mockPlaylistTable) DeletePlaylist(playlistId int) (error error) { |
| 31 | + return m.deletePlaylist(playlistId) |
| 32 | +} |
| 33 | + |
| 34 | +func TestFetchPlaylists(t *testing.T) { |
| 35 | + // Arrange |
| 36 | + route := "/api/v1/playlist" |
| 37 | + router := SetupTestRouter() |
| 38 | + |
| 39 | + mockTable := &mockPlaylistTable{ |
| 40 | + fetchPlaylists: func(ctx context.Context, lastKnowPlaylistId int) ([]models.Playlist, error) { |
| 41 | + return []models.Playlist{ |
| 42 | + {Id: 1, Name: "Playlist_1", Description: "Best ever", ThumbnailPath: "path/image1.png", IsPublic: false}, |
| 43 | + {Id: 2, Name: "Playlist_2", Description: "Second best", ThumbnailPath: "path/image2.png", IsPublic: true}, |
| 44 | + }, nil |
| 45 | + }, |
| 46 | + } |
| 47 | + |
| 48 | + playlistHandler := PlaylistHandler{ |
| 49 | + PlaylistTable: mockTable, |
| 50 | + } |
| 51 | + |
| 52 | + router.GET(route, playlistHandler.FetchPlaylists) |
| 53 | + |
| 54 | + recorder := httptest.NewRecorder() |
| 55 | + |
| 56 | + req, _ := http.NewRequest("GET", route, nil) |
| 57 | + |
| 58 | + // Act |
| 59 | + router.ServeHTTP(recorder, req) |
| 60 | + |
| 61 | + // Assert |
| 62 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 63 | + |
| 64 | + var rawResult map[string]any |
| 65 | + |
| 66 | + err := json.Unmarshal(recorder.Body.Bytes(), &rawResult) |
| 67 | + |
| 68 | + assert.Equal(t, nil, err) |
| 69 | + |
| 70 | + dataBytes, err := json.Marshal(rawResult["Data"]) |
| 71 | + |
| 72 | + var playlists []models.Playlist |
| 73 | + err = json.Unmarshal(dataBytes, &playlists) |
| 74 | + assert.NoError(t, err) |
| 75 | + |
| 76 | + assert.Equal(t, 2, len(playlists)) |
| 77 | + assert.Equal(t, "Playlist_1", playlists[0].Name) |
| 78 | + assert.Equal(t, "Playlist_2", playlists[1].Name) |
| 79 | +} |
| 80 | + |
| 81 | +func TestFetchPlaylistsLastKnowPlaylistId(t *testing.T) { |
| 82 | + // Arrange |
| 83 | + _lastKnowPlaylistId := 4 |
| 84 | + route := "/api/v1/playlist" |
| 85 | + router := SetupTestRouter() |
| 86 | + |
| 87 | + mockTable := &mockPlaylistTable{ |
| 88 | + fetchPlaylists: func(ctx context.Context, lastKnowPlaylistId int) ([]models.Playlist, error) { |
| 89 | + assert.Equal(t, _lastKnowPlaylistId, lastKnowPlaylistId) |
| 90 | + return []models.Playlist{ |
| 91 | + {Id: 4, Name: "Playlist_4", Description: "Second best", ThumbnailPath: "path/image4.png", IsPublic: true}, |
| 92 | + }, nil |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + playlistHandler := PlaylistHandler{ |
| 97 | + PlaylistTable: mockTable, |
| 98 | + } |
| 99 | + |
| 100 | + router.GET(route, playlistHandler.FetchPlaylists) |
| 101 | + |
| 102 | + recorder := httptest.NewRecorder() |
| 103 | + |
| 104 | + queryRoute := fmt.Sprintf("%s?lastKnowPlaylistId=%d", route, _lastKnowPlaylistId) |
| 105 | + |
| 106 | + req, _ := http.NewRequest("GET", queryRoute, nil) |
| 107 | + |
| 108 | + // Act |
| 109 | + router.ServeHTTP(recorder, req) |
| 110 | + |
| 111 | + // Assert |
| 112 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 113 | + |
| 114 | + var rawResult map[string]any |
| 115 | + |
| 116 | + err := json.Unmarshal(recorder.Body.Bytes(), &rawResult) |
| 117 | + |
| 118 | + assert.Equal(t, nil, err) |
| 119 | + |
| 120 | + dataBytes, err := json.Marshal(rawResult["Data"]) |
| 121 | + |
| 122 | + var playlists []models.Playlist |
| 123 | + err = json.Unmarshal(dataBytes, &playlists) |
| 124 | + assert.NoError(t, err) |
| 125 | + |
| 126 | + assert.Equal(t, 1, len(playlists)) |
| 127 | + assert.Equal(t, "Playlist_4", playlists[0].Name) |
| 128 | +} |
| 129 | + |
| 130 | +func TestInsertPlaylist(t *testing.T) { |
| 131 | + // Arrange |
| 132 | + route := "/api/v1/playlist" |
| 133 | + router := SetupTestRouter() |
| 134 | + |
| 135 | + mockTable := &mockPlaylistTable{ |
| 136 | + insertPlaylist: func(playlist models.Playlist) (lastInsertedId int, error error) { |
| 137 | + return 1, nil |
| 138 | + }, |
| 139 | + } |
| 140 | + |
| 141 | + playlistHandler := PlaylistHandler{ |
| 142 | + PlaylistTable: mockTable, |
| 143 | + } |
| 144 | + |
| 145 | + router.POST(route, playlistHandler.InsertPlaylist) |
| 146 | + |
| 147 | + recorder := httptest.NewRecorder() |
| 148 | + |
| 149 | + body := models.Playlist{ |
| 150 | + Description: "Cool playlist", |
| 151 | + } |
| 152 | + |
| 153 | + bodyBytes, _ := json.Marshal(body) |
| 154 | + |
| 155 | + req, _ := http.NewRequest("POST", route, bytes.NewBuffer(bodyBytes)) |
| 156 | + |
| 157 | + // Act |
| 158 | + router.ServeHTTP(recorder, req) |
| 159 | + |
| 160 | + // Assert |
| 161 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 162 | + |
| 163 | + var rawResult map[string]any |
| 164 | + |
| 165 | + err := json.Unmarshal(recorder.Body.Bytes(), &rawResult) |
| 166 | + |
| 167 | + assert.Equal(t, nil, err) |
| 168 | + |
| 169 | + dataBytes, err := json.Marshal(rawResult["Data"]) |
| 170 | + |
| 171 | + err = json.Unmarshal(dataBytes, &rawResult) |
| 172 | + |
| 173 | + assert.Equal(t, 1, int(rawResult["playlistId"].(float64))) |
| 174 | +} |
| 175 | + |
| 176 | +func TestInsertPlaylistJsonError(t *testing.T) { |
| 177 | + // Arrange |
| 178 | + route := "/api/v1/playlist" |
| 179 | + router := SetupTestRouter() |
| 180 | + |
| 181 | + mockTable := &mockPlaylistTable{ |
| 182 | + insertPlaylist: func(playlist models.Playlist) (lastInsertedId int, error error) { |
| 183 | + return 1, nil |
| 184 | + }, |
| 185 | + } |
| 186 | + |
| 187 | + playlistHandler := PlaylistHandler{ |
| 188 | + PlaylistTable: mockTable, |
| 189 | + } |
| 190 | + |
| 191 | + router.POST(route, playlistHandler.InsertPlaylist) |
| 192 | + |
| 193 | + recorder := httptest.NewRecorder() |
| 194 | + |
| 195 | + // Wrong type, will throw an error |
| 196 | + bodyBytes, _ := json.Marshal("") |
| 197 | + |
| 198 | + req, _ := http.NewRequest("POST", route, bytes.NewBuffer(bodyBytes)) |
| 199 | + |
| 200 | + // Act |
| 201 | + router.ServeHTTP(recorder, req) |
| 202 | + |
| 203 | + // Assert |
| 204 | + assert.Equal(t, http.StatusInternalServerError, recorder.Code) |
| 205 | +} |
| 206 | + |
| 207 | +func TestDeletePlaylistPlaylistIdError(t *testing.T) { |
| 208 | + // Arrange |
| 209 | + route := "/playlist/:playlistId" |
| 210 | + router := SetupTestRouter() |
| 211 | + |
| 212 | + mockTable := &mockPlaylistTable{ |
| 213 | + deletePlaylist: func(playlistId int) (error error) { |
| 214 | + return nil |
| 215 | + }, |
| 216 | + } |
| 217 | + |
| 218 | + playlistHandler := PlaylistHandler{ |
| 219 | + PlaylistTable: mockTable, |
| 220 | + } |
| 221 | + |
| 222 | + router.DELETE(route, playlistHandler.DeletePlaylist) |
| 223 | + |
| 224 | + recorder := httptest.NewRecorder() |
| 225 | + |
| 226 | + // Unable to parse to int, will throw error |
| 227 | + _route := "/playlist/sdfsd" |
| 228 | + |
| 229 | + req, _ := http.NewRequest("DELETE", _route, nil) |
| 230 | + |
| 231 | + // Act |
| 232 | + router.ServeHTTP(recorder, req) |
| 233 | + |
| 234 | + // Assert |
| 235 | + assert.Equal(t, http.StatusInternalServerError, recorder.Code) |
| 236 | +} |
| 237 | + |
| 238 | +func TestDeletePlaylistPlaylistId(t *testing.T) { |
| 239 | + // Arrange |
| 240 | + route := "/playlist/:playlistId" |
| 241 | + router := SetupTestRouter() |
| 242 | + |
| 243 | + mockTable := &mockPlaylistTable{ |
| 244 | + deletePlaylist: func(playlistId int) (error error) { |
| 245 | + return nil |
| 246 | + }, |
| 247 | + } |
| 248 | + |
| 249 | + playlistHandler := PlaylistHandler{ |
| 250 | + PlaylistTable: mockTable, |
| 251 | + } |
| 252 | + |
| 253 | + router.DELETE(route, playlistHandler.DeletePlaylist) |
| 254 | + |
| 255 | + recorder := httptest.NewRecorder() |
| 256 | + |
| 257 | + // Unable to parse to int, will throw error |
| 258 | + _route := "/playlist/1" |
| 259 | + |
| 260 | + req, _ := http.NewRequest("DELETE", _route, nil) |
| 261 | + |
| 262 | + // Act |
| 263 | + router.ServeHTTP(recorder, req) |
| 264 | + |
| 265 | + // Assert |
| 266 | + assert.Equal(t, http.StatusOK, recorder.Code) |
| 267 | +} |
0 commit comments