]> git.example.dev Git - binbsis50-sm.git/commitdiff
updated: first test, wanna do more testing
author2weiEmu <saalbach.robert@outlook.de>
Mon, 13 Apr 2026 14:21:38 +0000 (16:21 +0200)
committer2weiEmu <saalbach.robert@outlook.de>
Mon, 13 Apr 2026 14:21:38 +0000 (16:21 +0200)
Makefile
pkg/dbhandling/songsapi.go
pkg/dbhandling/songsapi_test.go
songmanager

index 026d301bb97728dcbc56c5182a7afba5dcbf05ba..c9c19f74763b8c011e5123b525f1f3fee100f1fc 100644 (file)
--- 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 ./...
index 58d794c04cd5cf60e55a6b296a3399f870da4044..a2e5d494817cee9a9373787dd8cc1129e6db796a 100644 (file)
@@ -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
index 524e3ca4849be8f1155f73ccda223b6b824c985b..78a0937cacd04de54d1e791a91ec4958623ac58f 100644 (file)
@@ -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")
+               }
+       })
 }
index 9a48265186039bb2cab8e934d15e263734a9fa55..dd44eb6939e703fcbda991185c27a1fdcd36a368 100755 (executable)
Binary files a/songmanager and b/songmanager differ