Skip to content

Commit

Permalink
✨ add tags api and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codermuss committed Jul 29, 2024
1 parent 1cd432c commit 73435b5
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 0 deletions.
4 changes: 4 additions & 0 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ func (server *Server) setupRouter() {
{
categoryRoutes.GET("/index", server.GetCategories)
}
tagRoutes := router.Group("/v1/tags").Use(authMiddleware(server.tokenMaker))
{
tagRoutes.GET("/index", server.GetTags)
}

// Serve the bundled static files
statikFS, err := fs.New()
Expand Down
27 changes: 27 additions & 0 deletions api/tag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package api

import (
"net/http"

"github.com/gin-gonic/gin"
)

func (server *Server) GetTags(ctx *gin.Context) {

tags, err := server.store.GetTags(ctx)
if err != nil {
BuildResponse(ctx, BaseResponse{
Code: http.StatusInternalServerError,
Message: ResponseMessage{
Type: ERROR,
Content: "Internal error: " + err.Error(),
},
})
return
}

BuildResponse(ctx, BaseResponse{
Code: http.StatusOK,
Data: tags,
})
}
104 changes: 104 additions & 0 deletions api/tag_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package api

import (
"bytes"
"database/sql"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/golang/mock/gomock"
mockdb "github.com/mustafayilmazdev/musarchive/db/mock"
db "github.com/mustafayilmazdev/musarchive/db/sqlc"
"github.com/mustafayilmazdev/musarchive/token"
"github.com/stretchr/testify/require"
)

func TestGetTags(t *testing.T) {
user, _ := RandomUser(t)

tags := []db.Tag{
{ID: 1, Name: "Tag 1"},
{ID: 2, Name: "Tag 2"},
}

testCases := []struct {
name string
setupAuth func(t *testing.T, request *http.Request, tokenMaker token.Maker)
buildStubs func(store *mockdb.MockStore)
checkResponse func(recorder *httptest.ResponseRecorder)
}{
{
name: "OK",
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, int(user.ID), user.Role, time.Minute)
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetTags(gomock.Any()).
Times(1).
Return(tags, nil)
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusOK, recorder.Code)
requireBodyMatchTags(t, recorder.Body, tags)
},
},
{
name: "InternalError",
setupAuth: func(t *testing.T, request *http.Request, tokenMaker token.Maker) {
addAuthorization(t, request, tokenMaker, authorizationTypeBearer, int(user.ID), user.Role, time.Minute)
},
buildStubs: func(store *mockdb.MockStore) {
store.EXPECT().
GetTags(gomock.Any()).
Times(1).Return([]db.Tag{}, sql.ErrConnDone)
},
checkResponse: func(recorder *httptest.ResponseRecorder) {
require.Equal(t, http.StatusInternalServerError, recorder.Code)
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

store := mockdb.NewMockStore(ctrl)
tc.buildStubs(store)

server := newTestServer(t, store)
recorder := httptest.NewRecorder()

url := "/v1/tags/index"
request, err := http.NewRequest(http.MethodGet, url, nil)
require.NoError(t, err)

tc.setupAuth(t, request, server.tokenMaker)
server.Router.ServeHTTP(recorder, request)
tc.checkResponse(recorder)
})
}
}

func requireBodyMatchTags(t *testing.T, body *bytes.Buffer, Tags []db.Tag) {
data, err := io.ReadAll(body)
require.NoError(t, err)

var response BaseResponse
err = json.Unmarshal(data, &response)
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.Code)

TagsJSON, err := json.Marshal(Tags)
require.NoError(t, err)

responseDataJSON, err := json.Marshal(response.Data)
require.NoError(t, err)

require.JSONEq(t, string(TagsJSON), string(responseDataJSON))
}
15 changes: 15 additions & 0 deletions db/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions db/query/tags.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ SELECT id, name
FROM tags
WHERE id = $1;

-- name: GetTags :many
SELECT id, name
FROM tags;

-- name: UpdateTag :one
UPDATE tags
SET name = $1
Expand Down
1 change: 1 addition & 0 deletions db/sqlc/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions db/sqlc/tags.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 73435b5

Please sign in to comment.