Skip to content

Commit

Permalink
feat: add delete task job and list tasks job api in job unit test.
Browse files Browse the repository at this point in the history
Signed-off-by: Asklv <[email protected]>
  • Loading branch information
IRONICBo committed Aug 3, 2024
1 parent 7aee5fd commit f4dd25c
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions manager/handlers/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ var (
"user_id": 4,
"bio": "bio"
}`
mockListTasksJobReqBody = `
{
"type": "list_tasks",
"user_id": 4,
"bio": "bio"
}`
mockDeleteTaskJobReqBody = `
{
"type": "delete_task",
"user_id": 4,
"bio": "bio"
}`
mockOtherJobReqBody = `
{
"type": "others",
Expand All @@ -50,6 +62,16 @@ var (
Type: "preheat",
BIO: "bio",
}
mockListTasksCreateJobRequest = types.CreateListTasksJobRequest{
UserID: 4,
Type: "list_tasks",
BIO: "bio",
}
mockDeleteTaskCreateJobRequest = types.CreateDeleteTaskJobRequest{
UserID: 4,
Type: "delete_task",
BIO: "bio",
}
mockUpdateJobRequest = types.UpdateJobRequest{
UserID: 4,
BIO: "bio",
Expand All @@ -61,6 +83,20 @@ var (
BIO: "bio",
TaskID: "2",
}
mockListTasksJobModel = &models.Job{
BaseModel: mockBaseModel,
UserID: 4,
Type: "list_tasks",
BIO: "bio",
TaskID: "2",
}
mockDeleteTaskJobModel = &models.Job{
BaseModel: mockBaseModel,
UserID: 4,
Type: "delete_task",
BIO: "bio",
TaskID: "2",
}
)

func mockJobRouter(h *Handlers) *gin.Engine {
Expand Down Expand Up @@ -115,6 +151,36 @@ func TestHandlers_CreateJob(t *testing.T) {
assert.Equal(mockPreheatJobModel, &job)
},
},
{
name: "success",
req: httptest.NewRequest(http.MethodPost, "/oapi/v1/jobs", strings.NewReader(mockListTasksJobReqBody)),
mock: func(ms *mocks.MockServiceMockRecorder) {
ms.CreateListTasksJob(gomock.Any(), gomock.Eq(mockListTasksCreateJobRequest)).Return(mockListTasksJobModel, nil).Times(1)
},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
job := models.Job{}
err := json.Unmarshal(w.Body.Bytes(), &job)
assert.NoError(err)
assert.Equal(mockListTasksJobModel, &job)
},
},
{
name: "success",
req: httptest.NewRequest(http.MethodPost, "/oapi/v1/jobs", strings.NewReader(mockDeleteTaskJobReqBody)),
mock: func(ms *mocks.MockServiceMockRecorder) {
ms.CreateDeleteTaskJob(gomock.Any(), gomock.Eq(mockDeleteTaskCreateJobRequest)).Return(mockDeleteTaskJobModel, nil).Times(1)
},
expect: func(t *testing.T, w *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(http.StatusOK, w.Code)
job := models.Job{}
err := json.Unmarshal(w.Body.Bytes(), &job)
assert.NoError(err)
assert.Equal(mockDeleteTaskJobModel, &job)
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
Expand Down

0 comments on commit f4dd25c

Please sign in to comment.