From 5d0907fe661a74698848232d95c190b4df481084 Mon Sep 17 00:00:00 2001 From: Bruno Lombardi Date: Fri, 26 Apr 2024 22:48:45 -0300 Subject: [PATCH] refactor: change tests to root folders --- .../users/create_user_controller_test.go | 34 ++++++++++------- .../users/get_user_controller_test.go | 38 ++++++++++++------- .../users/update_user_controller_test.go | 33 +++++++++------- .../authenticated_middleware_test.go | 9 ++--- .../pkg/services/auth_service_test.go | 9 ++--- .../pkg/services/user_service_test.go | 9 ++--- .../controllers/users/setup_test.go | 24 ------------ 7 files changed, 76 insertions(+), 80 deletions(-) rename {test => internal}/app/presentation/controllers/users/create_user_controller_test.go (79%) rename {test => internal}/app/presentation/controllers/users/get_user_controller_test.go (69%) rename {test => internal}/app/presentation/controllers/users/update_user_controller_test.go (77%) rename {test => internal}/app/presentation/middlewares/authenticated_middleware_test.go (92%) rename {test => internal}/pkg/services/auth_service_test.go (94%) rename {test => internal}/pkg/services/user_service_test.go (96%) delete mode 100644 test/app/presentation/controllers/users/setup_test.go diff --git a/test/app/presentation/controllers/users/create_user_controller_test.go b/internal/app/presentation/controllers/users/create_user_controller_test.go similarity index 79% rename from test/app/presentation/controllers/users/create_user_controller_test.go rename to internal/app/presentation/controllers/users/create_user_controller_test.go index 8cb04b1..ee394f2 100644 --- a/test/app/presentation/controllers/users/create_user_controller_test.go +++ b/internal/app/presentation/controllers/users/create_user_controller_test.go @@ -1,20 +1,26 @@ -package user_controllers_test +package controllers import ( "fmt" "net/http" "testing" - controllers "vanir/internal/app/presentation/controllers/users" + "vanir/internal/pkg/config" + "vanir/internal/pkg/data/db" "vanir/internal/pkg/data/models" "vanir/internal/pkg/helpers" "vanir/internal/pkg/protocols" - _ "vanir/test" + mocks "vanir/test/mocks" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) func TestRunCreateUserControllerTestCases(t *testing.T) { + config.Setup() + db.SetupDB() + + userServiceMock := &mocks.UserServiceMock{} + controllerTestCases := []ControllerTestCase{ { Name: "Should create user with valid data", @@ -33,10 +39,10 @@ func TestRunCreateUserControllerTestCases(t *testing.T) { }, ExpectResponse: func(t *testing.T, response *protocols.HttpResponse) error { user, ok := response.Body.(*models.User) - assert.True(t, ok) - assert.NotNil(t, user.ID) - assert.Equal(t, http.StatusCreated, response.StatusCode) - assert.Equal(t, "bruno@email.com", user.Email) + require.True(t, ok) + require.NotNil(t, user.ID) + require.Equal(t, http.StatusCreated, response.StatusCode) + require.Equal(t, "bruno@email.com", user.Email) return nil }, AfterTest: func() error { @@ -66,7 +72,7 @@ func TestRunCreateUserControllerTestCases(t *testing.T) { return nil }, ExpectResponse: func(t *testing.T, response *protocols.HttpResponse) error { - assert.Equal(t, http.StatusInternalServerError, response.StatusCode) + require.Equal(t, http.StatusInternalServerError, response.StatusCode) return nil }, AfterTest: func() error { @@ -85,18 +91,18 @@ func TestRunCreateUserControllerTestCases(t *testing.T) { for _, testCase := range controllerTestCases { t.Run(testCase.Name, func(t *testing.T) { - assert.NoError(t, testCase.BeforeTest()) + require.NoError(t, testCase.BeforeTest()) - createUserController := controllers.NewCreateUserController( + createUserController := NewCreateUserController( userServiceMock, ) request, ok := testCase.WhenRequest.(*protocols.HttpRequest) - assert.True(t, ok) + require.True(t, ok) response, _ := createUserController.Handle(request) - assert.NoError(t, testCase.ExpectResponse(t, response)) - assert.NoError(t, testCase.AfterTest()) + require.NoError(t, testCase.ExpectResponse(t, response)) + require.NoError(t, testCase.AfterTest()) }) } } diff --git a/test/app/presentation/controllers/users/get_user_controller_test.go b/internal/app/presentation/controllers/users/get_user_controller_test.go similarity index 69% rename from test/app/presentation/controllers/users/get_user_controller_test.go rename to internal/app/presentation/controllers/users/get_user_controller_test.go index c6ca113..5cbe494 100644 --- a/test/app/presentation/controllers/users/get_user_controller_test.go +++ b/internal/app/presentation/controllers/users/get_user_controller_test.go @@ -1,19 +1,29 @@ -package user_controllers_test +package controllers import ( "fmt" "net/http" "testing" - controllers "vanir/internal/app/presentation/controllers/users" + "vanir/internal/pkg/config" + "vanir/internal/pkg/data/db" "vanir/internal/pkg/data/models" "vanir/internal/pkg/helpers" "vanir/internal/pkg/protocols" + data_test "vanir/test/data" + mocks "vanir/test/mocks" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) -func TestRunGetUserControllerTestCases(t *testing.T) { +type ControllerTestCase data_test.ControllerTestCase + +func TestGetUserControllerTestCases(t *testing.T) { + config.Setup() + db.SetupDB() + + userServiceMock := &mocks.UserServiceMock{} + controllerTestCases := []ControllerTestCase{ { Name: "Should return user when service returns the user with valid id", @@ -29,10 +39,10 @@ func TestRunGetUserControllerTestCases(t *testing.T) { }, ExpectResponse: func(t *testing.T, response *protocols.HttpResponse) error { user, ok := response.Body.(*models.User) - assert.True(t, ok) - assert.NotNil(t, user.ID) - assert.Equal(t, http.StatusOK, response.StatusCode) - assert.Equal(t, "bruno@email.com", user.Email) + require.True(t, ok) + require.NotNil(t, user.ID) + require.Equal(t, http.StatusOK, response.StatusCode) + require.Equal(t, "bruno@email.com", user.Email) return nil }, AfterTest: func() error { @@ -54,7 +64,7 @@ func TestRunGetUserControllerTestCases(t *testing.T) { return nil }, ExpectResponse: func(t *testing.T, response *protocols.HttpResponse) error { - assert.Equal(t, http.StatusInternalServerError, response.StatusCode) + require.Equal(t, http.StatusInternalServerError, response.StatusCode) return nil }, AfterTest: func() error { @@ -68,18 +78,18 @@ func TestRunGetUserControllerTestCases(t *testing.T) { for _, testCase := range controllerTestCases { t.Run(testCase.Name, func(t *testing.T) { - assert.NoError(t, testCase.BeforeTest()) + require.NoError(t, testCase.BeforeTest()) - updateUserController := controllers.NewGetUserController( + updateUserController := NewGetUserController( userServiceMock, ) request, ok := testCase.WhenRequest.(*protocols.HttpRequest) - assert.True(t, ok) + require.True(t, ok) response, _ := updateUserController.Handle(request) - assert.NoError(t, testCase.ExpectResponse(t, response)) - assert.NoError(t, testCase.AfterTest()) + require.NoError(t, testCase.ExpectResponse(t, response)) + require.NoError(t, testCase.AfterTest()) }) } } diff --git a/test/app/presentation/controllers/users/update_user_controller_test.go b/internal/app/presentation/controllers/users/update_user_controller_test.go similarity index 77% rename from test/app/presentation/controllers/users/update_user_controller_test.go rename to internal/app/presentation/controllers/users/update_user_controller_test.go index 26d2613..504a7a6 100644 --- a/test/app/presentation/controllers/users/update_user_controller_test.go +++ b/internal/app/presentation/controllers/users/update_user_controller_test.go @@ -1,19 +1,26 @@ -package user_controllers_test +package controllers import ( "fmt" "net/http" "testing" - controllers "vanir/internal/app/presentation/controllers/users" + "vanir/internal/pkg/config" + "vanir/internal/pkg/data/db" "vanir/internal/pkg/data/models" "vanir/internal/pkg/helpers" "vanir/internal/pkg/protocols" + mocks "vanir/test/mocks" - "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" ) func TestRunUpdateUserControllerTestCases(t *testing.T) { + config.Setup() + db.SetupDB() + + userServiceMock := &mocks.UserServiceMock{} + controllerTestCases := []ControllerTestCase{ { Name: "Should update user with valid data", @@ -33,10 +40,10 @@ func TestRunUpdateUserControllerTestCases(t *testing.T) { }, ExpectResponse: func(t *testing.T, response *protocols.HttpResponse) error { user, ok := response.Body.(*models.User) - assert.True(t, ok) - assert.NotNil(t, user.ID) - assert.Equal(t, http.StatusOK, response.StatusCode) - assert.Equal(t, "bruno@email.com", user.Email) + require.True(t, ok) + require.NotNil(t, user.ID) + require.Equal(t, http.StatusOK, response.StatusCode) + require.Equal(t, "bruno@email.com", user.Email) return nil }, AfterTest: func() error { @@ -62,7 +69,7 @@ func TestRunUpdateUserControllerTestCases(t *testing.T) { return nil }, ExpectResponse: func(t *testing.T, response *protocols.HttpResponse) error { - assert.Equal(t, http.StatusInternalServerError, response.StatusCode) + require.Equal(t, http.StatusInternalServerError, response.StatusCode) return nil }, AfterTest: func() error { @@ -76,18 +83,18 @@ func TestRunUpdateUserControllerTestCases(t *testing.T) { for _, testCase := range controllerTestCases { t.Run(testCase.Name, func(t *testing.T) { - assert.NoError(t, testCase.BeforeTest()) + require.NoError(t, testCase.BeforeTest()) - updateUserController := controllers.NewUpdateUserController( + updateUserController := NewUpdateUserController( userServiceMock, ) request, ok := testCase.WhenRequest.(*protocols.HttpRequest) - assert.True(t, ok) + require.True(t, ok) response, _ := updateUserController.Handle(request) - assert.NoError(t, testCase.ExpectResponse(t, response)) - assert.NoError(t, testCase.AfterTest()) + require.NoError(t, testCase.ExpectResponse(t, response)) + require.NoError(t, testCase.AfterTest()) }) } } diff --git a/test/app/presentation/middlewares/authenticated_middleware_test.go b/internal/app/presentation/middlewares/authenticated_middleware_test.go similarity index 92% rename from test/app/presentation/middlewares/authenticated_middleware_test.go rename to internal/app/presentation/middlewares/authenticated_middleware_test.go index 54de223..a94f8ab 100644 --- a/test/app/presentation/middlewares/authenticated_middleware_test.go +++ b/internal/app/presentation/middlewares/authenticated_middleware_test.go @@ -1,10 +1,9 @@ -package middlewares_test +package middlewares import ( "fmt" "net/http" "testing" - "vanir/internal/app/presentation/middlewares" "vanir/internal/pkg/data/models" "vanir/internal/pkg/protocols" "vanir/test/mocks" @@ -15,7 +14,7 @@ import ( type AuthMiddlewareSuite struct { suite.Suite - authMiddleware *middlewares.AuthenticatedMiddleware + authMiddleware *AuthenticatedMiddleware encrypter *mocks.EncrypterMock userService *mocks.UserServiceMock } @@ -24,7 +23,7 @@ func (sut *AuthMiddlewareSuite) BeforeTest(_, _ string) { sut.userService = &mocks.UserServiceMock{} sut.encrypter = &mocks.EncrypterMock{} - sut.authMiddleware = middlewares.NewAuthenticatedMiddleware(sut.encrypter, sut.userService) + sut.authMiddleware = NewAuthenticatedMiddleware(sut.encrypter, sut.userService) } func (sut *AuthMiddlewareSuite) AfterTest(_, _ string) { @@ -33,7 +32,7 @@ func (sut *AuthMiddlewareSuite) AfterTest(_, _ string) { } func (sut *AuthMiddlewareSuite) TestSmokeTest() { - sut.NotNil(middlewares.GetAuthenticatedMiddleware(&mocks.EncrypterMock{}, &mocks.UserServiceMock{})) + sut.NotNil(GetAuthenticatedMiddleware(&mocks.EncrypterMock{}, &mocks.UserServiceMock{})) } func (sut *AuthMiddlewareSuite) TestShouldNotReturnErrorIfAuthorizationTokenIsValid() { diff --git a/test/pkg/services/auth_service_test.go b/internal/pkg/services/auth_service_test.go similarity index 94% rename from test/pkg/services/auth_service_test.go rename to internal/pkg/services/auth_service_test.go index de4ce5c..a2299b1 100644 --- a/test/pkg/services/auth_service_test.go +++ b/internal/pkg/services/auth_service_test.go @@ -1,4 +1,4 @@ -package services_test +package services import ( "fmt" @@ -7,7 +7,6 @@ import ( "vanir/internal/pkg/data/repositories" "vanir/internal/pkg/helpers" "vanir/internal/pkg/protocols" - "vanir/internal/pkg/services" "vanir/test/mocks" "github.com/stretchr/testify/mock" @@ -16,7 +15,7 @@ import ( type AuthServiceSuite struct { suite.Suite - authService *services.AuthServiceImpl + authService *AuthServiceImpl userRepository *mocks.UserRepositoryMock hasher *mocks.HasherMock encrypter *mocks.EncrypterMock @@ -27,7 +26,7 @@ func (sut *AuthServiceSuite) BeforeTest(_, _ string) { sut.hasher = &mocks.HasherMock{} sut.encrypter = &mocks.EncrypterMock{} - sut.authService = services.NewAuthServiceImpl(sut.userRepository, sut.hasher, sut.encrypter) + sut.authService = NewAuthServiceImpl(sut.userRepository, sut.hasher, sut.encrypter) } func (sut *AuthServiceSuite) AfterTest(_, _ string) { @@ -37,7 +36,7 @@ func (sut *AuthServiceSuite) AfterTest(_, _ string) { } func (sut *AuthServiceSuite) TestSmokeAuthService() { - sut.NotNil(services.GetAuthService(&mocks.UserRepositoryMock{}, &mocks.HasherMock{}, &mocks.EncrypterMock{})) + sut.NotNil(GetAuthService(&mocks.UserRepositoryMock{}, &mocks.HasherMock{}, &mocks.EncrypterMock{})) } func (sut *AuthServiceSuite) TestShouldReturnTokenWhenValidCredentials() { diff --git a/test/pkg/services/user_service_test.go b/internal/pkg/services/user_service_test.go similarity index 96% rename from test/pkg/services/user_service_test.go rename to internal/pkg/services/user_service_test.go index 7ef35b9..a62dc1e 100644 --- a/test/pkg/services/user_service_test.go +++ b/internal/pkg/services/user_service_test.go @@ -1,4 +1,4 @@ -package services_test +package services import ( "fmt" @@ -6,7 +6,6 @@ import ( "vanir/internal/pkg/data/models" "vanir/internal/pkg/data/repositories" "vanir/internal/pkg/helpers" - "vanir/internal/pkg/services" "vanir/test/mocks" "github.com/stretchr/testify/mock" @@ -15,7 +14,7 @@ import ( type UserServiceSuite struct { suite.Suite - userService *services.UserServiceImpl + userService *UserServiceImpl userRepository *mocks.UserRepositoryMock hasher *mocks.HasherMock } @@ -24,7 +23,7 @@ func (sut *UserServiceSuite) BeforeTest(_, _ string) { sut.userRepository = &mocks.UserRepositoryMock{} sut.hasher = &mocks.HasherMock{} - sut.userService = services.NewUserServiceImpl(sut.userRepository, sut.hasher) + sut.userService = NewUserServiceImpl(sut.userRepository, sut.hasher) } func (sut *UserServiceSuite) AfterTest(_, _ string) { @@ -35,7 +34,7 @@ func (sut *UserServiceSuite) AfterTest(_, _ string) { } func (sut *UserServiceSuite) TestSmokeTest() { - sut.NotNil(services.GetUserService(&mocks.UserRepositoryMock{}, &mocks.HasherMock{})) + sut.NotNil(GetUserService(&mocks.UserRepositoryMock{}, &mocks.HasherMock{})) } func (sut *UserServiceSuite) TestShouldCreateUserWhenValidData() { diff --git a/test/app/presentation/controllers/users/setup_test.go b/test/app/presentation/controllers/users/setup_test.go deleted file mode 100644 index f7801b5..0000000 --- a/test/app/presentation/controllers/users/setup_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package user_controllers_test - -import ( - "os" - "testing" - "vanir/internal/pkg/config" - "vanir/internal/pkg/data/db" - data_test "vanir/test/data" - "vanir/test/mocks" -) - -type ControllerTestCase = data_test.ControllerTestCase - -var userServiceMock *mocks.UserServiceMock - -func TestMain(m *testing.M) { - config.Setup() - db.SetupDB() - userServiceMock = &mocks.UserServiceMock{} - - code := m.Run() - - os.Exit(code) -}