generated from yandex-praktikum/go-musthave-diploma-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Добавил unit тесты, моки, интерфейсы
- Loading branch information
Пешков Дмитрий
committed
Sep 24, 2024
1 parent
fca4882
commit 4b5dd29
Showing
27 changed files
with
805 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package handlers | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"github.com/golang/mock/gomock" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"github.com/spqrcor/gofermart/internal/services/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestAddOrdersHandler(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
order := mocks.NewMockOrder(mockCtrl) | ||
order.EXPECT().Add(context.Background(), "333").Return(services.ErrOrderInvalidFormat).AnyTimes() | ||
order.EXPECT().Add(context.Background(), "9278923470").Return(nil).MaxTimes(1) | ||
order.EXPECT().Add(context.Background(), "9278923470").Return(services.ErrOrderUserExists).MinTimes(1) | ||
order.EXPECT().Add(context.Background(), "12345678903").Return(services.ErrOrderAnotherUserExists).AnyTimes() | ||
|
||
tests := []struct { | ||
name string | ||
contentType string | ||
body []byte | ||
statusCode int | ||
}{ | ||
{ | ||
name: "content type error", | ||
contentType: "application/json", | ||
body: []byte(`<num>3333</num>`), | ||
statusCode: http.StatusBadRequest, | ||
}, | ||
{ | ||
name: "format number error", | ||
contentType: "text/plain", | ||
body: []byte(`333`), | ||
statusCode: http.StatusUnprocessableEntity, | ||
}, | ||
{ | ||
name: "success", | ||
contentType: "text/plain", | ||
body: []byte(`9278923470`), | ||
statusCode: http.StatusAccepted, | ||
}, | ||
{ | ||
name: "order user exists", | ||
contentType: "text/plain", | ||
body: []byte(`9278923470`), | ||
statusCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "order another user exists", | ||
contentType: "text/plain", | ||
body: []byte(`12345678903`), | ||
statusCode: http.StatusConflict, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rw := httptest.NewRecorder() | ||
req := httptest.NewRequest("POST", "/api/user/orders", bytes.NewReader(tt.body)) | ||
req.Header.Add("Content-Type", tt.contentType) | ||
AddOrdersHandler(order)(rw, req) | ||
|
||
resp := rw.Result() | ||
assert.Equal(t, tt.statusCode, resp.StatusCode, "Error http status code") | ||
_ = req.Body.Close() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package handlers | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"github.com/golang/mock/gomock" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"github.com/spqrcor/gofermart/internal/services/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestAddWithdrawalHandler(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
withdrawal := mocks.NewMockWithdrawal(mockCtrl) | ||
withdrawal.EXPECT().Add(context.Background(), services.InputWithdrawal{OrderNum: "333", Sum: 100}).Return(services.ErrOrderInvalidFormat).AnyTimes() | ||
withdrawal.EXPECT().Add(context.Background(), services.InputWithdrawal{OrderNum: "9278923470", Sum: 100}).Return(nil).AnyTimes() | ||
withdrawal.EXPECT().Add(context.Background(), services.InputWithdrawal{OrderNum: "12345678903", Sum: 100}).Return(services.ErrBalance).AnyTimes() | ||
|
||
tests := []struct { | ||
name string | ||
contentType string | ||
body []byte | ||
statusCode int | ||
}{ | ||
{ | ||
name: "content type error", | ||
contentType: "text/plain", | ||
body: []byte(`1111`), | ||
statusCode: http.StatusInternalServerError, | ||
}, | ||
{ | ||
name: "format order error", | ||
contentType: "application/json", | ||
body: []byte(`{"order":"333","sum":100}`), | ||
statusCode: http.StatusUnprocessableEntity, | ||
}, | ||
{ | ||
name: "success", | ||
contentType: "application/json", | ||
body: []byte(`{"order":"9278923470","sum":100}`), | ||
statusCode: http.StatusOK, | ||
}, | ||
{ | ||
name: "balance error", | ||
contentType: "application/json", | ||
body: []byte(`{"order":"12345678903","sum":100}`), | ||
statusCode: http.StatusPaymentRequired, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rw := httptest.NewRecorder() | ||
req := httptest.NewRequest("POST", "/api/user/balance/withdraw", bytes.NewReader(tt.body)) | ||
req.Header.Add("Content-Type", tt.contentType) | ||
AddWithdrawalHandler(withdrawal)(rw, req) | ||
|
||
resp := rw.Result() | ||
assert.Equal(t, tt.statusCode, resp.StatusCode, "Error http status code") | ||
_ = req.Body.Close() | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package handlers | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/golang/mock/gomock" | ||
"github.com/spqrcor/gofermart/internal/services" | ||
"github.com/spqrcor/gofermart/internal/services/mocks" | ||
"github.com/stretchr/testify/assert" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestGetBalanceHandler(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
withdrawal := mocks.NewMockWithdrawal(mockCtrl) | ||
withdrawal.EXPECT().GetBalance(context.Background()).Return(services.BalanceInfo{}, errors.New("user not found")).MaxTimes(1) | ||
withdrawal.EXPECT().GetBalance(context.Background()).Return(services.BalanceInfo{Current: 100, Withdrawn: 100}, nil).MinTimes(1) | ||
tests := []struct { | ||
name string | ||
statusCode int | ||
}{ | ||
{ | ||
name: "no content error", | ||
statusCode: http.StatusInternalServerError, | ||
}, | ||
{ | ||
name: "success", | ||
statusCode: http.StatusOK, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
rw := httptest.NewRecorder() | ||
req := httptest.NewRequest("GET", "/api/user/balance", nil) | ||
GetBalanceHandler(withdrawal)(rw, req) | ||
|
||
resp := rw.Result() | ||
assert.Equal(t, tt.statusCode, resp.StatusCode, "Error http status code") | ||
_ = req.Body.Close() | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.