update tests

This commit is contained in:
nquidox 2026-03-15 23:44:56 +03:00
parent a15b53bf74
commit ec4fe5151e

View file

@ -3,9 +3,11 @@ package merch
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"io"
"merch-api/pkg/utils"
"net/http"
"net/http/httptest"
@ -14,6 +16,7 @@ import (
const userId int64 = 789
const validUuid string = "d28f5a94-ccf9-4eb6-bac3-53b065ba0d0c"
const invalidUuid string = "invalid-uuid"
func setupTestRouter(mockRepo Repository) *gin.Engine {
ut := utils.New()
@ -471,3 +474,84 @@ func TestGetManySuccess(t *testing.T) {
})
}
}
func TestUpdateMerch(t *testing.T) {
type testCase struct {
name string
pathUuid string
payload *updateMerchDTO
updateResponse *merchDTO
expectCode int
}
cases := []testCase{
{
name: "Success",
pathUuid: validUuid,
payload: &updateMerchDTO{MerchUuid: validUuid, Name: "merch1"},
updateResponse: &merchDTO{MerchUuid: validUuid, Name: "merch1"},
expectCode: 200,
},
{
name: "Bad request - empty payload",
pathUuid: validUuid,
expectCode: 400,
},
{
name: "Bad request - no path uuid",
payload: &updateMerchDTO{MerchUuid: validUuid, Name: "merch1"},
pathUuid: invalidUuid,
expectCode: 400,
},
{
name: "Bad request - no payload and no path uuid",
payload: nil,
pathUuid: invalidUuid,
expectCode: 400,
},
}
for _, tt := range cases {
t.Run(tt.name, func(t *testing.T) {
mockRepo := new(MockRepository)
var input io.Reader = nil
if tt.expectCode == 200 {
mockRepo.
On("updateMerch", mock.Anything, userId, tt.payload).
Return(tt.updateResponse, nil)
payload, err := json.Marshal(tt.payload)
if err != nil {
t.Fatal(err)
}
input = bytes.NewReader(payload)
}
router := setupTestRouter(mockRepo)
url := fmt.Sprintf("/merch/%s", tt.pathUuid)
req, _ := http.NewRequest("PUT", url, input)
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
assert.Equal(t, tt.expectCode, w.Code)
if tt.expectCode == 200 {
var response merchDTO
err := json.Unmarshal(w.Body.Bytes(), &response)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, tt.updateResponse, &response)
}
mockRepo.AssertExpectations(t)
})
}
}