From aaff88d32fdf83af0371d6717e744514ab5840d3 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Tue, 23 Jan 2018 00:16:42 +0300 Subject: [PATCH] Reworked http tests mocking --- http/cape_test.go | 28 ++++++++++++++-------------- http/face_test.go | 12 ++++++------ http/http_test.go | 18 +++++++++++++----- http/signed_textures_test.go | 12 ++++++------ http/skin_test.go | 29 +++++++++++++++-------------- http/textures_test.go | 32 ++++++++++++++++---------------- 6 files changed, 70 insertions(+), 61 deletions(-) diff --git a/http/cape_test.go b/http/cape_test.go index ed50c1e..1d3de99 100644 --- a/http/cape_test.go +++ b/http/cape_test.go @@ -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() diff --git a/http/face_test.go b/http/face_test.go index f61daff..8bb698d 100644 --- a/http/face_test.go +++ b/http/face_test.go @@ -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() diff --git a/http/http_test.go b/http/http_test.go index be23234..3db459d 100644 --- a/http/http_test.go +++ b/http/http_test.go @@ -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) @@ -35,6 +39,10 @@ func setupMocks(ctrl *gomock.Controller) ( return &Config{ SkinsRepo: skinsRepo, CapesRepo: capesRepo, - Logger: wd, - }, skinsRepo, capesRepo, wd + Logger: wd, + }, &mocks{ + Skins: skinsRepo, + Capes: capesRepo, + Log: wd, + } } diff --git a/http/signed_textures_test.go b/http/signed_textures_test.go index 48d728a..56465df 100644 --- a/http/signed_textures_test.go +++ b/http/signed_textures_test.go @@ -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() diff --git a/http/skin_test.go b/http/skin_test.go index 0f55cb7..c58585d 100644 --- a/http/skin_test.go +++ b/http/skin_test.go @@ -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, diff --git a/http/textures_test.go b/http/textures_test.go index 97f6ac2..9fdbe09 100644 --- a/http/textures_test.go +++ b/http/textures_test.go @@ -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) }