From: 2weiEmu Date: Wed, 20 May 2026 15:25:39 +0000 (+0200) Subject: updated: finished basic tests for roomsapi X-Git-Url: https://git.saalbach.dev/?a=commitdiff_plain;h=6cacd8ac9e79f633414e180b47e4b1dae1a1e4d4;p=binbsis50-sm.git updated: finished basic tests for roomsapi --- diff --git a/main.go b/main.go index 7f931b7..16473ca 100644 --- a/main.go +++ b/main.go @@ -61,7 +61,7 @@ func main() { 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("PUT /api/rooms/remove/{name}", DBwrapper.RoomsRemoveByNameAndId) mux.HandleFunc("DELETE /api/rooms/delete/{name}", DBwrapper.RoomsDeleteByName) // api/files - api related to song files diff --git a/pkg/dbhandling/roomsapi.go b/pkg/dbhandling/roomsapi.go index 63769bb..6359890 100644 --- a/pkg/dbhandling/roomsapi.go +++ b/pkg/dbhandling/roomsapi.go @@ -79,7 +79,6 @@ func (dbw *DBWrapper) RoomsAddByNameAndId(w http.ResponseWriter, r *http.Request err := json.NewDecoder(r.Body).Decode(&assignedSongKeys) if err != nil { w.WriteHeader(http.StatusInternalServerError) - fmt.Println("What went wrong,", err) return } @@ -107,22 +106,23 @@ func (dbw *DBWrapper) RoomsAddByNameAndId(w http.ResponseWriter, r *http.Request w.WriteHeader(http.StatusOK) } -/** TEST +/** * The same as adding, except that the IDs mentioned in the list will be removed from the room * (if they are present) **/ func (dbw *DBWrapper) RoomsRemoveByNameAndId(w http.ResponseWriter, r *http.Request) { - - room := DBRoom{} - err := json.NewDecoder(r.Body).Decode(&room) + var assignedSongKeys []int + err := json.NewDecoder(r.Body).Decode(&assignedSongKeys) if err != nil { w.WriteHeader(http.StatusInternalServerError) return } + name := r.PathValue("name") + // TODO: again, is there some way roll this into one command? - for _, k := range room.AssignedSongKeys { - _, err = dbw.RedisDb.ZRem(dbw.Ctx, room.Name, k).Result() + for _, k := range assignedSongKeys { + _, err = dbw.RedisDb.ZRem(dbw.Ctx, name, k).Result() if err != nil { w.WriteHeader(http.StatusInternalServerError) return @@ -132,23 +132,14 @@ func (dbw *DBWrapper) RoomsRemoveByNameAndId(w http.ResponseWriter, r *http.Requ w.WriteHeader(http.StatusOK) } -/** TEST +/** * Delete a room by giving the name as a string, this just means the room won't exist anymore + * the ids should be given as a list in the body of the request **/ func (dbw *DBWrapper) RoomsDeleteByName(w http.ResponseWriter, r *http.Request) { + name := r.PathValue("name") - - // 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) - - _, err = dbw.RedisDb.Del(dbw.Ctx, name).Result() + _, err := dbw.RedisDb.Del(dbw.Ctx, name).Result() if err != nil { w.WriteHeader(http.StatusInternalServerError) return diff --git a/pkg/dbhandling/roomsapi_test.go b/pkg/dbhandling/roomsapi_test.go index 0e60444..e21ef74 100644 --- a/pkg/dbhandling/roomsapi_test.go +++ b/pkg/dbhandling/roomsapi_test.go @@ -3,6 +3,7 @@ package dbhandling_test import ( "encoding/json" "fmt" + "net/http" "net/http/httptest" "slices" "songmanager/pkg/dbhandling" @@ -109,12 +110,12 @@ 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 := httptest.NewRequest(http.MethodPut, "/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 := httptest.NewRequest(http.MethodGet, "/api/rooms/get/hits", &EmptyReader{}) r2.SetPathValue("name", "hits") recorder2 := httptest.NewRecorder() @@ -139,3 +140,83 @@ func TestRoomsAddByNameAndId(t *testing.T) { if len(ids) != 14 { t.Error("Not the expected amount of ids", ids) } }) } + +type testRemoveReader struct {} + +var testRemoveSongsId = []int{0, 1, 2, 3} + +func (r *testRemoveReader) Read(p []byte) (int, error) { + bin, err := json.Marshal(testRemoveSongsId) + if err != nil { + return 0, err + } + + if len(bin) > len(p) { return 0, err } + + count := copy(p, bin) + return count, nil +} + +func TestRoomsRemoveByNameAndId(t *testing.T) { + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + + t.Run("RoomsRemoveByNameAndId1", func(t *testing.T) { + r := httptest.NewRequest(http.MethodPut, "/api/rooms/remove/hits", &testRemoveReader{}) + r.SetPathValue("name", "hits") + recorder := httptest.NewRecorder() + + wrapper.RoomsRemoveByNameAndId(recorder, r) + + r2 := httptest.NewRequest(http.MethodGet, "/api/rooms/get/hits", &EmptyReader{}) + r2.SetPathValue("name", "hits") + + recorder2 := httptest.NewRecorder() + wrapper.RoomsGetByName(recorder2, r2) + + rep := make([]string, 10) + + 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) != 10 { t.Error("Not the expected amount of ids", ids) } + }) +} + +func TestRoomsDeleteByName(t *testing.T) { + wrapper := dbhandling.NewDbWrapper("localhost:6379", "./testing/songs") + + t.Run("RoomsDeleteByName-1", func(t *testing.T) { + r := httptest.NewRequest(http.MethodDelete, "/api/rooms/delete/rock", &EmptyReader{}) + r.SetPathValue("name", "rock") + recorder := httptest.NewRecorder() + + wrapper.RoomsDeleteByName(recorder, r) + + if recorder.Code != 200 { t.Error("Incorrect HTTP return code") } + + r2 := httptest.NewRequest(http.MethodGet, "/api/rooms/get/all", &EmptyReader{}) + recorder2 := httptest.NewRecorder() + wrapper.RoomsGet(recorder2, r2) + + l := make([]string , 1) + + err := json.Unmarshal(recorder2.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 contain 'rock'") } + }) +}