Skip to content

Commit

Permalink
add some api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
philipphomberger committed Mar 30, 2024
1 parent ba2c289 commit a644051
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
5 changes: 0 additions & 5 deletions models/album.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,3 @@ type Album struct {
Artist string `json:"artist,omitempty" validate:"required"`
Price float64 `json:"price,omitempty" validate:"required"`
}

func getAlbumStruct() Album {
var album Album
return album
}
28 changes: 13 additions & 15 deletions routes/albums_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
package routes

import (
"ginapi/models"
"github.com/mjarkk/mongomock"
"fmt"
"github.com/stretchr/testify/assert"
"log"
"net/http"
"net/http/httptest"
"testing"
)

func TestAlbumsRoute(t *testing.T) {
db := mongomock.NewDB()
collection := db.Collection("Albums")
err := collection.Insert(models.Album{
Title: "Let it Be",
Artist: "The Beatles",
Price: 12.99,
})
if err != nil {
log.Fatal(err)
}
func TestGetAlbumsRoute(t *testing.T) {
router := SetupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/albums", nil)
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
//assert.Equal(t, "pong", w.Body.String())
fmt.Println(w.Body.String())
}

func TestGetAlbumRoute(t *testing.T) {
router := SetupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/albums/66053fd1e9647c9e6a7d8a25", nil)
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
assert.Equal(t, `{"status":200,"message":"success","data":{"data":{"_id":"66053fd1e9647c9e6a7d8a25","artist":"Beatles","id":"66053fd1e9647c9e6a7d8a24","price":12.99,"title":"Abbey Road"}}}`, w.Body.String())
}
29 changes: 29 additions & 0 deletions routes/artist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package routes

import (
"fmt"
"github.com/stretchr/testify/assert"
"net/http"
"net/http/httptest"
"testing"
)

func TestGetArtistsRoute(t *testing.T) {
router := SetupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/artists", nil)
router.ServeHTTP(w, req)

assert.Equal(t, 200, w.Code)
fmt.Println(w.Body.String())
}

func TestGetArtistRoute(t *testing.T) {
router := SetupRouter()
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/artists/6606e636ea20581b675f6cdf", nil)
router.ServeHTTP(w, req)
fmt.Println(w.Body.String())
assert.Equal(t, 200, w.Code)
assert.Equal(t, `{"status":200,"message":"success","data":{"data":{"_id":"6606e636ea20581b675f6cdf","id":"6606e636ea20581b675f6cde","name":"Lennon","surname":"John"}}}`, w.Body.String())
}

0 comments on commit a644051

Please sign in to comment.