From: 2weiEmu Date: Mon, 13 Apr 2026 14:21:38 +0000 (+0200) Subject: updated: first test, wanna do more testing X-Git-Url: https://git.saalbach.dev/?a=commitdiff_plain;h=7577a66d4b473eba1f9495bd0a21ff562e2ec335;p=binbsis50-sm.git updated: first test, wanna do more testing --- diff --git a/Makefile b/Makefile index 026d301..c9c19f7 100644 --- a/Makefile +++ b/Makefile @@ -17,4 +17,4 @@ test: sudo docker run -d --name redis -p 6379:6379 redis:latest python3 ./testing/dummy_testing_data.py - go test -run '' + go test -v ./... diff --git a/pkg/dbhandling/songsapi.go b/pkg/dbhandling/songsapi.go index 58d794c..a2e5d49 100644 --- a/pkg/dbhandling/songsapi.go +++ b/pkg/dbhandling/songsapi.go @@ -1,13 +1,52 @@ package dbhandling -import "net/http" +import ( + "encoding/json" + "net/http" +) -/** TODO +/** * Get all songs, and all their assosciated information from the db **/ func (dbw *DBWrapper) SongsGet(w http.ResponseWriter, r *http.Request) { // Here we should return an entire list of songs + keys, err := dbw.RedisDb.Keys(dbw.ctx, "song:*").Result() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + key_count := len(keys) + + allSongs := make([]DBSong, key_count) + for i, k := range keys { + intf, err := dbw.RedisDb.HMGet(dbw.ctx, k, "artistName", "trackName", "trackViewUrl", "previewUrl", "artworkUrl60", "artworkUrl100", "displayArtistNames", "displayTrackName").Result() + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + song := DBSong{ + Key: k, + ArtistName: intf[0].(string), + TrackName: intf[1].(string), + TrackViewURL: intf[2].(string), + PreviewURL: intf[3].(string), + ArtworkURL60: intf[4].(string), + ArtworkURL100: intf[5].(string), + DisplayArtistNames: intf[6].(string), + DisplayTrackName: intf[7].(string), + } + allSongs[i] = song + } + + b, err := json.Marshal(allSongs) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + w.Write(b) + } /** TODO diff --git a/pkg/dbhandling/songsapi_test.go b/pkg/dbhandling/songsapi_test.go index 524e3ca..78a0937 100644 --- a/pkg/dbhandling/songsapi_test.go +++ b/pkg/dbhandling/songsapi_test.go @@ -1,11 +1,37 @@ package dbhandling_test -import "testing" +import ( + "encoding/json" + "net/http/httptest" + "songmanager/pkg/dbhandling" + "testing" +) + +type EmptyReader struct {} + +func (r *EmptyReader) Read(p []byte) (int, error) { + return 0, nil +} + func TestGetSongs(t *testing.T) { // setup some fake requests + r := httptest.NewRequest("GET", "/api/songs/get", &EmptyReader{}) + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") // subtests + t.Run("SongsGet1", func(t *testing.T) { + recorder := httptest.NewRecorder() + wrapper.SongsGet(recorder, r) + + l := make([]dbhandling.DBSong, 10) + err := json.Unmarshal(recorder.Body.Bytes(), &l) + if err != nil { + t.Error("Failed unmarshling JSON body from GetSongs Response") + } - // get answers to those requests + if len(l) != 10 { + t.Error("Returned JSON is of wrong length") + } + }) } diff --git a/songmanager b/songmanager index 9a48265..dd44eb6 100755 Binary files a/songmanager and b/songmanager differ