Reworked http tests mocking

This commit is contained in:
ErickSkrauch 2018-01-23 00:16:42 +03:00
parent b8c3cc6cf8
commit aaff88d32f
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
6 changed files with 70 additions and 61 deletions

View File

@ -21,14 +21,14 @@ func TestConfig_Cape(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, _, capesRepo, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
cape := createCape() cape := createCape()
capesRepo.EXPECT().FindByUsername("mocked_username").Return(&model.Cape{ mocks.Capes.EXPECT().FindByUsername("mocked_username").Return(&model.Cape{
File: bytes.NewReader(cape), File: bytes.NewReader(cape),
}, nil) }, nil)
wd.EXPECT().IncCounter("capes.request", int64(1)) mocks.Log.EXPECT().IncCounter("capes.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/cloaks/mocked_username", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/cloaks/mocked_username", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -48,10 +48,10 @@ func TestConfig_Cape2(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, _, capesRepo, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
capesRepo.EXPECT().FindByUsername("notch").Return(nil, &db.CapeNotFoundError{"notch"}) mocks.Capes.EXPECT().FindByUsername("notch").Return(nil, &db.CapeNotFoundError{"notch"})
wd.EXPECT().IncCounter("capes.request", int64(1)) mocks.Log.EXPECT().IncCounter("capes.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/cloaks/notch", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/cloaks/notch", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -69,15 +69,15 @@ func TestConfig_CapeGET(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, _, capesRepo, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
cape := createCape() cape := createCape()
capesRepo.EXPECT().FindByUsername("mocked_username").Return(&model.Cape{ mocks.Capes.EXPECT().FindByUsername("mocked_username").Return(&model.Cape{
File: bytes.NewReader(cape), File: bytes.NewReader(cape),
}, nil) }, nil)
wd.EXPECT().IncCounter("capes.request", int64(1)).Times(0) mocks.Log.EXPECT().IncCounter("capes.request", int64(1)).Times(0)
wd.EXPECT().IncCounter("capes.get_request", int64(1)) mocks.Log.EXPECT().IncCounter("capes.get_request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/cloaks?name=mocked_username", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/cloaks?name=mocked_username", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -97,11 +97,11 @@ func TestConfig_CapeGET2(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, _, capesRepo, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
capesRepo.EXPECT().FindByUsername("notch").Return(nil, &db.CapeNotFoundError{"notch"}) mocks.Capes.EXPECT().FindByUsername("notch").Return(nil, &db.CapeNotFoundError{"notch"})
wd.EXPECT().IncCounter("capes.request", int64(1)).Times(0) mocks.Log.EXPECT().IncCounter("capes.request", int64(1)).Times(0)
wd.EXPECT().IncCounter("capes.get_request", int64(1)) mocks.Log.EXPECT().IncCounter("capes.get_request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/cloaks?name=notch", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/cloaks?name=notch", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@ -16,10 +16,10 @@ func TestConfig_Face(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil) mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
wd.EXPECT().IncCounter("faces.request", int64(1)) mocks.Log.EXPECT().IncCounter("faces.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user/face.png", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user/face.png", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -37,10 +37,10 @@ func TestConfig_Face2(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{"mock_user"}) mocks.Skins.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{"mock_user"})
wd.EXPECT().IncCounter("faces.request", int64(1)) mocks.Log.EXPECT().IncCounter("faces.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user/face.png", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user/face.png", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@ -22,11 +22,15 @@ func TestBuildElyUrl(t *testing.T) {
assert.Equal("http://ely.by/test/route", buildElyUrl("http://ely.by/test/route"), "Function should do not add prefix to the provided prefixed url.") assert.Equal("http://ely.by/test/route", buildElyUrl("http://ely.by/test/route"), "Function should do not add prefix to the provided prefixed url.")
} }
type mocks struct {
Skins *mock_interfaces.MockSkinsRepository
Capes *mock_interfaces.MockCapesRepository
Log *mock_wd.MockWatchdog
}
func setupMocks(ctrl *gomock.Controller) ( func setupMocks(ctrl *gomock.Controller) (
*Config, *Config,
*mock_interfaces.MockSkinsRepository, *mocks,
*mock_interfaces.MockCapesRepository,
*mock_wd.MockWatchdog,
) { ) {
skinsRepo := mock_interfaces.NewMockSkinsRepository(ctrl) skinsRepo := mock_interfaces.NewMockSkinsRepository(ctrl)
capesRepo := mock_interfaces.NewMockCapesRepository(ctrl) capesRepo := mock_interfaces.NewMockCapesRepository(ctrl)
@ -36,5 +40,9 @@ func setupMocks(ctrl *gomock.Controller) (
SkinsRepo: skinsRepo, SkinsRepo: skinsRepo,
CapesRepo: capesRepo, CapesRepo: capesRepo,
Logger: wd, Logger: wd,
}, skinsRepo, capesRepo, wd }, &mocks{
Skins: skinsRepo,
Capes: capesRepo,
Log: wd,
}
} }

View File

@ -17,10 +17,10 @@ func TestConfig_SignedTextures(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil) mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
wd.EXPECT().IncCounter("signed_textures.request", int64(1)) mocks.Log.EXPECT().IncCounter("signed_textures.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/signed/mock_user", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/signed/mock_user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -54,10 +54,10 @@ func TestConfig_SignedTextures2(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{}) mocks.Skins.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{})
wd.EXPECT().IncCounter("signed_textures.request", int64(1)) mocks.Log.EXPECT().IncCounter("signed_textures.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/signed/mock_user", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/signed/mock_user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()

View File

@ -17,10 +17,10 @@ func TestConfig_Skin(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil) mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
wd.EXPECT().IncCounter("skins.request", int64(1)) mocks.Log.EXPECT().IncCounter("skins.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -38,10 +38,10 @@ func TestConfig_Skin2(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("notch").Return(nil, &db.SkinNotFoundError{"notch"}) mocks.Skins.EXPECT().FindByUsername("notch").Return(nil, &db.SkinNotFoundError{"notch"})
wd.EXPECT().IncCounter("skins.request", int64(1)) mocks.Log.EXPECT().IncCounter("skins.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/notch", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/notch", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -59,11 +59,11 @@ func TestConfig_SkinGET(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil) mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
wd.EXPECT().IncCounter("skins.get_request", int64(1)) mocks.Log.EXPECT().IncCounter("skins.get_request", int64(1))
wd.EXPECT().IncCounter("skins.request", int64(1)).Times(0) mocks.Log.EXPECT().IncCounter("skins.request", int64(1)).Times(0)
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins?name=mock_user", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins?name=mock_user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -81,11 +81,11 @@ func TestConfig_SkinGET2(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("notch").Return(nil, &db.SkinNotFoundError{"notch"}) mocks.Skins.EXPECT().FindByUsername("notch").Return(nil, &db.SkinNotFoundError{"notch"})
wd.EXPECT().IncCounter("skins.get_request", int64(1)) mocks.Log.EXPECT().IncCounter("skins.get_request", int64(1))
wd.EXPECT().IncCounter("skins.request", int64(1)).Times(0) mocks.Log.EXPECT().IncCounter("skins.request", int64(1)).Times(0)
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins?name=notch", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins?name=notch", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -112,6 +112,7 @@ func TestConfig_SkinGET3(t *testing.T) {
func createSkinModel(username string, isSlim bool) *model.Skin { func createSkinModel(username string, isSlim bool) *model.Skin {
return &model.Skin{ return &model.Skin{
UserId: 1,
Username: username, Username: username,
Uuid: "0f657aa8-bfbe-415d-b700-5750090d3af3", Uuid: "0f657aa8-bfbe-415d-b700-5750090d3af3",
SkinId: 1, SkinId: 1,

View File

@ -20,11 +20,11 @@ func TestConfig_Textures(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, capesRepo, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil) mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
capesRepo.EXPECT().FindByUsername("mock_user").Return(nil, &db.CapeNotFoundError{"mock_user"}) mocks.Capes.EXPECT().FindByUsername("mock_user").Return(nil, &db.CapeNotFoundError{"mock_user"})
wd.EXPECT().IncCounter("textures.request", int64(1)) mocks.Log.EXPECT().IncCounter("textures.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/mock_user", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/mock_user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -49,11 +49,11 @@ func TestConfig_Textures2(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, capesRepo, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", true), nil) mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", true), nil)
capesRepo.EXPECT().FindByUsername("mock_user").Return(nil, &db.CapeNotFoundError{"mock_user"}) mocks.Capes.EXPECT().FindByUsername("mock_user").Return(nil, &db.CapeNotFoundError{"mock_user"})
wd.EXPECT().IncCounter("textures.request", int64(1)) mocks.Log.EXPECT().IncCounter("textures.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/mock_user", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/mock_user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -81,13 +81,13 @@ func TestConfig_Textures3(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, capesRepo, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil) mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
capesRepo.EXPECT().FindByUsername("mock_user").Return(&model.Cape{ mocks.Capes.EXPECT().FindByUsername("mock_user").Return(&model.Cape{
File: bytes.NewReader(createCape()), File: bytes.NewReader(createCape()),
}, nil) }, nil)
wd.EXPECT().IncCounter("textures.request", int64(1)) mocks.Log.EXPECT().IncCounter("textures.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/mock_user", nil) req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/mock_user", nil)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@ -116,11 +116,11 @@ func TestConfig_Textures4(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
config, skinsRepo, capesRepo, wd := setupMocks(ctrl) config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("notch").Return(nil, &db.SkinNotFoundError{}) mocks.Skins.EXPECT().FindByUsername("notch").Return(nil, &db.SkinNotFoundError{})
capesRepo.EXPECT().FindByUsername("notch").Return(nil, &db.CapeNotFoundError{}) mocks.Capes.EXPECT().FindByUsername("notch").Return(nil, &db.CapeNotFoundError{})
wd.EXPECT().IncCounter("textures.request", int64(1)) mocks.Log.EXPECT().IncCounter("textures.request", int64(1))
timeNow = func() time.Time { timeNow = func() time.Time {
return time.Date(2017, time.August, 20, 0, 15, 54, 0, time.UTC) return time.Date(2017, time.August, 20, 0, 15, 54, 0, time.UTC)
} }