Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type UpdateTestCase struct {
- inJSON string
- inUpdate domain.User
- outJSON string
- status int
- }
- var testCaseUpdate = []UpdateTestCase{
- {
- inJSON: `{"email":"newmail1@mail.ru"}` + "\n",
- inUpdate: domain.User{
- ID: 1,
- Email: "newmail1@mail.ru",
- },
- outJSON: `{"id":1,"email":"newmail1@mail.ru","username":"good_user"}`,
- status: http.StatusOK,
- },
- {
- inJSON: `{"username":"new_user1"}` + "\n",
- inUpdate: domain.User{
- ID: 1,
- Username: "new_user1",
- },
- outJSON: `{"id":1,"email":"mail1@mail.ru","username":"new_user1"}`,
- status: http.StatusOK,
- },
- {
- inJSON: `{"email":"newmail1@mail.ru","username":"new_user1"}` + "\n",
- inUpdate: domain.User{
- ID: 1,
- Email: "newmail1@mail.ru",
- Username: "new_user1",
- },
- outJSON: `{"id":1,"email":"newmail1@gmail.com","username":"new_user1"}`,
- status: http.StatusOK,
- },
- {
- inJSON: `{"email":"","username":""}` + "\n",
- inUpdate: domain.User{
- ID: 1,
- Email: "",
- Username: "",
- },
- outJSON: `{"message":"invalid update"}`,
- status: http.StatusBadRequest,
- },
- {
- inJSON: `{}` + "\n",
- inUpdate: domain.User{
- ID: 1,
- },
- outJSON: `{"error":"error while updating user"}`,
- status: http.StatusBadRequest,
- },
- }
- func TestUserHandler_Update(t *testing.T) {
- ctrl := gomock.NewController(t)
- defer ctrl.Finish()
- uCaseMock := mock.NewMockUserUsecase(ctrl)
- for _, test := range testCaseUpdate {
- if test.status == http.StatusOK {
- uCaseMock.EXPECT().Update(&test.inUpdate).Times(1).Return(nil)
- uCaseMock.EXPECT().GetById(uint(1)).Times(1).Return(usersTestData[1], nil)
- } else {
- uCaseMock.EXPECT().Update(test.inUpdate).Times(1).Return(user.InvalidUpdateError)
- }
- }
- handler := &UserHandler{
- UUsecase: uCaseMock,
- }
- for _, test := range testCaseUpdate {
- t.Run(fmt.Sprintf("IN: %v, OUT: %v, CODE: %v", test.inJSON, test.outJSON, test.status),
- func(t *testing.T) {
- test.outJSON += "\n"
- body := bytes.NewReader([]byte(test.inJSON))
- r := httptest.NewRequest("PATCH", "/api/users/1", body)
- r = mux.SetURLVars(r, map[string]string{"id": "1"})
- w := httptest.NewRecorder()
- err := session.Create(w, r, 1)
- require.NoError(t, err)
- defer session.Delete(w, r, 1)
- handler.Update(w, r)
- current := UpdateTestCase{
- inJSON: test.inJSON,
- inUpdate: test.inUpdate,
- outJSON: w.Body.String(),
- status: w.Code,
- }
- require.Equal(t, test, current)
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement