label mock tests
This commit is contained in:
parent
62432a69c1
commit
44f6038b03
1 changed files with 195 additions and 3 deletions
|
|
@ -12,6 +12,8 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
const userId int64 = 789
|
||||
|
||||
func setupTestRouter(mockRepo Repository) *gin.Engine {
|
||||
ut := utils.New()
|
||||
|
||||
|
|
@ -25,8 +27,6 @@ func setupTestRouter(mockRepo Repository) *gin.Engine {
|
|||
gin.SetMode(gin.TestMode)
|
||||
router := gin.New()
|
||||
|
||||
var userId int64 = 789
|
||||
|
||||
router.Use(
|
||||
func(c *gin.Context) {
|
||||
c.Set("userId", userId)
|
||||
|
|
@ -41,7 +41,7 @@ func setupTestRouter(mockRepo Repository) *gin.Engine {
|
|||
func TestCreateLabelSuccess(t *testing.T) {
|
||||
mockRepo := new(MockRepository)
|
||||
mockRepo.
|
||||
On("createLabel", mock.Anything, mock.Anything, mock.Anything).
|
||||
On("createLabel", mock.Anything, userId, mock.Anything).
|
||||
Return(nil)
|
||||
|
||||
router := setupTestRouter(mockRepo)
|
||||
|
|
@ -66,3 +66,195 @@ func TestCreateLabelSuccess(t *testing.T) {
|
|||
assert.Equal(t, 200, w.Code)
|
||||
mockRepo.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestGetLabelsSuccess(t *testing.T) {
|
||||
expectedList := []LabelsList{
|
||||
{LabelUuid: "123", Name: "First", Color: "1", BgColor: "2"},
|
||||
{LabelUuid: "456", Name: "Second", Color: "3", BgColor: "4"},
|
||||
}
|
||||
|
||||
mockRepo := new(MockRepository)
|
||||
mockRepo.
|
||||
On("getLabels", mock.Anything, userId).
|
||||
Return(expectedList, nil)
|
||||
|
||||
router := setupTestRouter(mockRepo)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/merch/labels", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
|
||||
var response []LabelsList
|
||||
err := json.Unmarshal(w.Body.Bytes(), &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, expectedList, response)
|
||||
|
||||
mockRepo.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestUpdateLabelSuccess(t *testing.T) {
|
||||
mockRepo := new(MockRepository)
|
||||
mockRepo.
|
||||
On("updateLabel",
|
||||
mock.Anything,
|
||||
userId,
|
||||
mock.MatchedBy(func(arg any) bool {
|
||||
label, ok := arg.(*Label)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return label.Uuid == "u123" && label.Name == "Test Label1"
|
||||
}),
|
||||
).
|
||||
Return(nil)
|
||||
|
||||
router := setupTestRouter(mockRepo)
|
||||
|
||||
l := &LabelDTO{
|
||||
Name: "Test Label1",
|
||||
Color: "1",
|
||||
BgColor: "2",
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(l)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := bytes.NewBuffer(payload)
|
||||
|
||||
req, _ := http.NewRequest("PUT", "/merch/labels/u123", body)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
}
|
||||
|
||||
func TestDeleteLabelSuccess(t *testing.T) {
|
||||
mockRepo := new(MockRepository)
|
||||
|
||||
expectedUuid := mock.MatchedBy(func(arg any) bool {
|
||||
u, ok := arg.(string)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return u == "u123"
|
||||
})
|
||||
|
||||
mockRepo.
|
||||
On("deleteLabel", mock.Anything, userId, expectedUuid, mock.Anything).
|
||||
Return(nil)
|
||||
|
||||
router := setupTestRouter(mockRepo)
|
||||
|
||||
req, _ := http.NewRequest("DELETE", "/merch/labels/u123", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
}
|
||||
|
||||
func TestAttachLabelSuccess(t *testing.T) {
|
||||
mockRepo := new(MockRepository)
|
||||
|
||||
expected := mock.MatchedBy(func(arg any) bool {
|
||||
val, ok := arg.(*LabelLink)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return val.LabelUuid == "l123"
|
||||
})
|
||||
|
||||
mockRepo.
|
||||
On("attachLabel", mock.Anything, userId, expected, mock.Anything).
|
||||
Return(nil)
|
||||
|
||||
router := setupTestRouter(mockRepo)
|
||||
|
||||
l := LabelLink{
|
||||
MerchUuid: "m123",
|
||||
LabelUuid: "l123",
|
||||
}
|
||||
payload, err := json.Marshal(l)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := bytes.NewBuffer(payload)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/merch/labels/attach", body)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
mockRepo.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestDetachLabelSuccess(t *testing.T) {
|
||||
mockRepo := new(MockRepository)
|
||||
|
||||
expected := mock.MatchedBy(func(arg any) bool {
|
||||
val, ok := arg.(*LabelLink)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return val.LabelUuid == "l123"
|
||||
})
|
||||
|
||||
mockRepo.
|
||||
On("detachLabel", mock.Anything, userId, expected, mock.Anything).
|
||||
Return(nil)
|
||||
|
||||
router := setupTestRouter(mockRepo)
|
||||
|
||||
l := LabelLink{
|
||||
MerchUuid: "m123",
|
||||
LabelUuid: "l123",
|
||||
}
|
||||
payload, err := json.Marshal(l)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
body := bytes.NewBuffer(payload)
|
||||
|
||||
req, _ := http.NewRequest("POST", "/merch/labels/detach", body)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
mockRepo.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestGetMerchLabelsSuccess(t *testing.T) {
|
||||
mockRepo := new(MockRepository)
|
||||
|
||||
expected := []string{"l123", "l456", "l789"}
|
||||
mockRepo.
|
||||
On("getAttachedLabelsByUuid", mock.Anything, userId, "m123").
|
||||
Return(expected, nil)
|
||||
|
||||
router := setupTestRouter(mockRepo)
|
||||
|
||||
req, _ := http.NewRequest("GET", "/merch/labels/m123", nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
router.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(t, 200, w.Code)
|
||||
|
||||
var response []string
|
||||
err := json.Unmarshal(w.Body.Bytes(), &response)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assert.Equal(t, expected, response)
|
||||
|
||||
mockRepo.AssertExpectations(t)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue