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

View File

@ -16,10 +16,10 @@ func TestConfig_Face(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl)
config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
wd.EXPECT().IncCounter("faces.request", int64(1))
mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
mocks.Log.EXPECT().IncCounter("faces.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user/face.png", nil)
w := httptest.NewRecorder()
@ -37,10 +37,10 @@ func TestConfig_Face2(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl)
config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{"mock_user"})
wd.EXPECT().IncCounter("faces.request", int64(1))
mocks.Skins.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{"mock_user"})
mocks.Log.EXPECT().IncCounter("faces.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/skins/mock_user/face.png", nil)
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.")
}
type mocks struct {
Skins *mock_interfaces.MockSkinsRepository
Capes *mock_interfaces.MockCapesRepository
Log *mock_wd.MockWatchdog
}
func setupMocks(ctrl *gomock.Controller) (
*Config,
*mock_interfaces.MockSkinsRepository,
*mock_interfaces.MockCapesRepository,
*mock_wd.MockWatchdog,
*mocks,
) {
skinsRepo := mock_interfaces.NewMockSkinsRepository(ctrl)
capesRepo := mock_interfaces.NewMockCapesRepository(ctrl)
@ -36,5 +40,9 @@ func setupMocks(ctrl *gomock.Controller) (
SkinsRepo: skinsRepo,
CapesRepo: capesRepo,
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)
defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl)
config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
wd.EXPECT().IncCounter("signed_textures.request", int64(1))
mocks.Skins.EXPECT().FindByUsername("mock_user").Return(createSkinModel("mock_user", false), nil)
mocks.Log.EXPECT().IncCounter("signed_textures.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/signed/mock_user", nil)
w := httptest.NewRecorder()
@ -54,10 +54,10 @@ func TestConfig_SignedTextures2(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config, skinsRepo, _, wd := setupMocks(ctrl)
config, mocks := setupMocks(ctrl)
skinsRepo.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{})
wd.EXPECT().IncCounter("signed_textures.request", int64(1))
mocks.Skins.EXPECT().FindByUsername("mock_user").Return(nil, &db.SkinNotFoundError{})
mocks.Log.EXPECT().IncCounter("signed_textures.request", int64(1))
req := httptest.NewRequest("GET", "http://skinsystem.ely.by/textures/signed/mock_user", nil)
w := httptest.NewRecorder()

View File

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

View File

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