]> git.example.dev Git - binbsis50-sm.git/commitdiff
updated: finished basic tests for roomsapi
author2weiEmu <saalbach.robert@outlook.de>
Wed, 20 May 2026 15:25:39 +0000 (17:25 +0200)
committer2weiEmu <saalbach.robert@outlook.de>
Wed, 20 May 2026 15:25:39 +0000 (17:25 +0200)
main.go
pkg/dbhandling/roomsapi.go
pkg/dbhandling/roomsapi_test.go

diff --git a/main.go b/main.go
index 7f931b7d2b6eb480720917b7764f0eff0dacf5a3..16473cae2f853a3ff88c33dbae14d8f0f9cd596b 100644 (file)
--- 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
index 63769bb7065ec5e22a38d4ac7c62f7873f3bfc07..6359890e5f8cc0b05c526186bed922bd98198c5a 100644 (file)
@@ -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
index 0e604448dab6198cd439bc245ff5df3e7263fb40..e21ef742e33a2907ff9797e7fef261e32ecb91b6 100644 (file)
@@ -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'") }
+       })
+}