Advertisement
PaulPaulAga

Untitled

Mar 25th, 2021
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.49 KB | None | 0 0
  1.  
  2. type UpdateTestCase struct {
  3.     inJSON   string
  4.     inUpdate domain.User
  5.     outJSON  string
  6.     status   int
  7. }
  8.  
  9. var testCaseUpdate = []UpdateTestCase{
  10.     {
  11.         inJSON: `{"email":"newmail1@mail.ru"}` + "\n",
  12.         inUpdate: domain.User{
  13.             ID:    1,
  14.             Email: "newmail1@mail.ru",
  15.         },
  16.         outJSON: `{"id":1,"email":"newmail1@mail.ru","username":"good_user"}`,
  17.         status:  http.StatusOK,
  18.     },
  19.     {
  20.         inJSON: `{"username":"new_user1"}` + "\n",
  21.         inUpdate: domain.User{
  22.             ID:       1,
  23.             Username: "new_user1",
  24.         },
  25.         outJSON: `{"id":1,"email":"mail1@mail.ru","username":"new_user1"}`,
  26.         status:  http.StatusOK,
  27.     },
  28.     {
  29.         inJSON: `{"email":"newmail1@mail.ru","username":"new_user1"}` + "\n",
  30.         inUpdate: domain.User{
  31.             ID:       1,
  32.             Email:    "newmail1@mail.ru",
  33.             Username: "new_user1",
  34.         },
  35.         outJSON: `{"id":1,"email":"newmail1@gmail.com","username":"new_user1"}`,
  36.         status:  http.StatusOK,
  37.     },
  38.     {
  39.         inJSON: `{"email":"","username":""}` + "\n",
  40.         inUpdate: domain.User{
  41.             ID:       1,
  42.             Email:    "",
  43.             Username: "",
  44.         },
  45.         outJSON: `{"message":"invalid update"}`,
  46.         status:  http.StatusBadRequest,
  47.     },
  48.     {
  49.         inJSON: `{}` + "\n",
  50.         inUpdate: domain.User{
  51.             ID: 1,
  52.         },
  53.         outJSON: `{"error":"error while updating user"}`,
  54.         status:  http.StatusBadRequest,
  55.     },
  56. }
  57.  
  58. func TestUserHandler_Update(t *testing.T) {
  59.     ctrl := gomock.NewController(t)
  60.     defer ctrl.Finish()
  61.     uCaseMock := mock.NewMockUserUsecase(ctrl)
  62.     for _, test := range testCaseUpdate {
  63.         if test.status == http.StatusOK {
  64.             uCaseMock.EXPECT().Update(&test.inUpdate).Times(1).Return(nil)
  65.             uCaseMock.EXPECT().GetById(uint(1)).Times(1).Return(usersTestData[1], nil)
  66.         } else {
  67.             uCaseMock.EXPECT().Update(test.inUpdate).Times(1).Return(user.InvalidUpdateError)
  68.         }
  69.     }
  70.     handler := &UserHandler{
  71.         UUsecase: uCaseMock,
  72.     }
  73.     for _, test := range testCaseUpdate {
  74.         t.Run(fmt.Sprintf("IN: %v, OUT: %v, CODE: %v", test.inJSON, test.outJSON, test.status),
  75.             func(t *testing.T) {
  76.                 test.outJSON += "\n"
  77.                 body := bytes.NewReader([]byte(test.inJSON))
  78.                 r := httptest.NewRequest("PATCH", "/api/users/1", body)
  79.                 r = mux.SetURLVars(r, map[string]string{"id": "1"})
  80.                 w := httptest.NewRecorder()
  81.  
  82.                 err := session.Create(w, r, 1)
  83.                 require.NoError(t, err)
  84.                 defer session.Delete(w, r, 1)
  85.  
  86.                 handler.Update(w, r)
  87.                 current := UpdateTestCase{
  88.                     inJSON:   test.inJSON,
  89.                     inUpdate: test.inUpdate,
  90.                     outJSON:  w.Body.String(),
  91.                     status:   w.Code,
  92.                 }
  93.                 require.Equal(t, test, current)
  94.             })
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement