From cafab32d9de613d3221501da25c44a2a6f434e37 Mon Sep 17 00:00:00 2001 From: 2weiEmu Date: Wed, 20 May 2026 15:38:34 +0200 Subject: [PATCH] updated: fixed the way some methods get parameters, as I forgot how I wanted to do that, and wrote some more tests --- main.go | 6 +- pkg/dbhandling/roomsapi.go | 41 ++++------- pkg/dbhandling/roomsapi_test.go | 125 +++++++++++++++++++++++++++++++- testing/dummy_testing_data.py | 1 + 4 files changed, 142 insertions(+), 31 deletions(-) diff --git a/main.go b/main.go index 6f5b502..7f931b7 100644 --- a/main.go +++ b/main.go @@ -58,9 +58,9 @@ func main() { mux.HandleFunc("DELETE /api/songs/delete/{id}", DBwrapper.SongsDeleteById) // api/rooms - api related to managaing rooms - mux.HandleFunc("GET /api/rooms/get", DBwrapper.RoomsGet) - mux.HandleFunc("POST /api/rooms/create/", DBwrapper.RoomsCreate) - mux.HandleFunc("PUT /api/rooms/add/{name}/{id}", DBwrapper.RoomsAddByNameAndId) + mux.HandleFunc("GET /api/rooms/get/{name}", DBwrapper.RoomsGetByName) + mux.HandleFunc("GET /api/rooms/get/all", DBwrapper.RoomsGet) + mux.HandleFunc("PUT /api/rooms/add/{name}", DBwrapper.RoomsAddByNameAndId) mux.HandleFunc("PUT /api/rooms/remove/{name}/{id}", DBwrapper.RoomsRemoveByNameAndId) mux.HandleFunc("DELETE /api/rooms/delete/{name}", DBwrapper.RoomsDeleteByName) diff --git a/pkg/dbhandling/roomsapi.go b/pkg/dbhandling/roomsapi.go index ca8280c..63769bb 100644 --- a/pkg/dbhandling/roomsapi.go +++ b/pkg/dbhandling/roomsapi.go @@ -2,12 +2,13 @@ package dbhandling import ( "encoding/json" + "fmt" "net/http" "github.com/redis/go-redis/v9" ) -/** TEST +/** * Get all the names of all the rooms in the database **/ func (dbw *DBWrapper) RoomsGet(w http.ResponseWriter, r *http.Request) { @@ -42,21 +43,12 @@ func (dbw *DBWrapper) RoomsGet(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) } -/** TEST +/** * Get the id's of the songs present in a room using the name of the room - * the sent JSON should just be a string with the name of the room -* what will be returned is just a list of ids in the body, in the form [1, 2, 3, 4, 5] (without song:) + * what will be returned is just a list of ids in the body, in the form [1, 2, 3, 4, 5] (without song:) **/ func (dbw *DBWrapper) RoomsGetByName(w http.ResponseWriter, r *http.Request) { - // the body should just be a string that is the name of the room - nameBytes := make([]byte, 256) - _, err := r.Body.Read(nameBytes) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return - } - - name := string(nameBytes) + name := r.PathValue("name") idList, err := dbw.RedisDb.ZRange(dbw.Ctx, name, 0, -1).Result() if err != nil { @@ -75,26 +67,25 @@ func (dbw *DBWrapper) RoomsGetByName(w http.ResponseWriter, r *http.Request) { } -/** TEST +/** * Add songs to a room using the ID of the song and the NAME of the room - * The values should be given in the following way: - * { - * name: "NAME OF ROOM", - * assignedSongKeys: [1, 2, 3, 4, 5], - * } + * The values given should just be a list of ids to be added to the room * If a room with that name does not exist, it will be created instead. **/ func (dbw *DBWrapper) RoomsAddByNameAndId(w http.ResponseWriter, r *http.Request) { + name := r.PathValue("name") + var assignedSongKeys []int - room := DBRoom{} - err := json.NewDecoder(r.Body).Decode(&room) + err := json.NewDecoder(r.Body).Decode(&assignedSongKeys) if err != nil { w.WriteHeader(http.StatusInternalServerError) + fmt.Println("What went wrong,", err) return } + fmt.Println("Gotten keys:", assignedSongKeys) // TODO: replace with logging - c, err := dbw.RedisDb.ZCount(dbw.Ctx, room.Name, "-inf", "+inf").Result() + c, err := dbw.RedisDb.ZCount(dbw.Ctx, name, "-inf", "+inf").Result() if err != nil { w.WriteHeader(http.StatusInternalServerError) return @@ -102,8 +93,8 @@ func (dbw *DBWrapper) RoomsAddByNameAndId(w http.ResponseWriter, r *http.Request // TODO: we can probably make all of this execute as like one command ideally? // so that there is less I/O? - for i, k := range room.AssignedSongKeys { - _, err = dbw.RedisDb.ZAdd(dbw.Ctx, room.Name, redis.Z{ + for i, k := range assignedSongKeys { + _, err = dbw.RedisDb.ZAdd(dbw.Ctx, name, redis.Z{ Score: float64(c + int64(i)), Member: k, }).Result() @@ -113,9 +104,7 @@ func (dbw *DBWrapper) RoomsAddByNameAndId(w http.ResponseWriter, r *http.Request return } } - w.WriteHeader(http.StatusOK) - } /** TEST diff --git a/pkg/dbhandling/roomsapi_test.go b/pkg/dbhandling/roomsapi_test.go index c92cf69..0e60444 100644 --- a/pkg/dbhandling/roomsapi_test.go +++ b/pkg/dbhandling/roomsapi_test.go @@ -1,20 +1,141 @@ package dbhandling_test import ( + "encoding/json" + "fmt" "net/http/httptest" + "slices" "songmanager/pkg/dbhandling" + "strconv" "testing" ) func TestRoomsGet(t *testing.T) { - - r := httptest.NewRequest("GET", "/api/rooms/get", &EmptyReader{}) + r := httptest.NewRequest("GET", "/api/rooms/get/all", &EmptyReader{}) wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") // subtests t.Run("RoomsGet1", func(t *testing.T) { recorder := httptest.NewRecorder() wrapper.RoomsGet(recorder, r) + + l := make([]string , 2) + + err := json.Unmarshal(recorder.Body.Bytes(), &l) + if err != nil { + t.Error("Failed to Unmarshal the Body") + } + + if !slices.Contains(l, "hits") { t.Error("Did not contain 'hits'") } + if !slices.Contains(l, "rock") { t.Error("Did not contain 'rock'") } + }) +} + +func TestRoomsGetByName(t *testing.T) { + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + + // subtests + t.Run("RoomsGet1", func(t *testing.T) { + r := httptest.NewRequest("GET", "/api/rooms/get/hits", &EmptyReader{}) + r.SetPathValue("name", "hits") + recorder := httptest.NewRecorder() + wrapper.RoomsGetByName(recorder, r) + + rep := make([]string, 10) + + err := json.Unmarshal(recorder.Body.Bytes(), &rep) + if err != nil { t.Error("Failed to Unmarshal the response") } + + fmt.Println(rep) + + var ids []int + for _, k := range rep { + if k != "" { + n, err := strconv.Atoi(k) + if err != nil { t.Error("ID was not a valid number: ", k) } + ids = append(ids, n) + } + } + + if len(ids) != 10 { t.Error("Not the expected amount of ids", ids) } + + }) + + // subtests + t.Run("RoomsGet2", func(t *testing.T) { + r := httptest.NewRequest("GET", "/api/rooms/get/rock", &EmptyReader{}) + r.SetPathValue("name", "rock") + recorder := httptest.NewRecorder() + wrapper.RoomsGetByName(recorder, r) + + rep := make([]string, 10) + + err := json.Unmarshal(recorder.Body.Bytes(), &rep) + if err != nil { t.Error("Failed to Unmarshal the response") } + + fmt.Println(rep) + + var ids []int + for _, k := range rep { + if k != "" { + n, err := strconv.Atoi(k) + if err != nil { t.Error("ID was not a valid number: ", k) } + ids = append(ids, n) + } + } + + if len(ids) != 10 { t.Error("Not the expected amount of ids", ids) } + + }) +} + +type testAddReader struct {} +var testAddSongIds = []int{10, 11, 12, 13} + +func (a *testAddReader) Read(p []byte) (int, error) { + bin, err := json.Marshal(testAddSongIds) + if err != nil { + return 0, err + } + + if len(bin) > len(p) { return 0, err } + + count := copy(p, bin) + return count, nil +} + +func TestRoomsAddByNameAndId(t *testing.T) { + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + + t.Run("RoomsAddByNameAndId1", func(t *testing.T) { + r := httptest.NewRequest("GET", "/api/rooms/add/hits", &testAddReader{}) + r.SetPathValue("name", "hits") + recorder := httptest.NewRecorder() + wrapper.RoomsAddByNameAndId(recorder, r) + + r2 := httptest.NewRequest("GET", "/api/rooms/get/hits", &EmptyReader{}) + r2.SetPathValue("name", "hits") + + recorder2 := httptest.NewRecorder() + wrapper.RoomsGetByName(recorder2, r2) + + rep := make([]string, 14) + + err := json.Unmarshal(recorder2.Body.Bytes(), &rep) + if err != nil { t.Error("Failed to Unmarshal the response") } + + fmt.Println(rep) + + var ids []int + for _, k := range rep { + if k != "" { + n, err := strconv.Atoi(k) + if err != nil { t.Error("ID was not a valid number: ", k) } + ids = append(ids, n) + } + } + + if len(ids) != 14 { t.Error("Not the expected amount of ids", ids) } }) } diff --git a/testing/dummy_testing_data.py b/testing/dummy_testing_data.py index 6d41e51..6bbdb05 100644 --- a/testing/dummy_testing_data.py +++ b/testing/dummy_testing_data.py @@ -22,6 +22,7 @@ def main(): f = open(f"./testing/songs/{line[4]}", "w+") f.close() r.zadd("hits", {i: i}) + r.zadd("rock", {i: i}) # 'song:' + i, # 'artistName', -- 2.54.0