Skip to content

Commit

Permalink
refactor: change tests to root folders
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-lombardi committed Apr 27, 2024
1 parent 08d88ed commit 5d0907f
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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, "[email protected]", user.Email)
require.True(t, ok)
require.NotNil(t, user.ID)
require.Equal(t, http.StatusCreated, response.StatusCode)
require.Equal(t, "[email protected]", user.Email)
return nil
},
AfterTest: func() error {
Expand Down Expand Up @@ -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 {
Expand All @@ -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())
})
}
}
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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, "[email protected]", user.Email)
require.True(t, ok)
require.NotNil(t, user.ID)
require.Equal(t, http.StatusOK, response.StatusCode)
require.Equal(t, "[email protected]", user.Email)
return nil
},
AfterTest: func() error {
Expand All @@ -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 {
Expand All @@ -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())
})
}
}
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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, "[email protected]", user.Email)
require.True(t, ok)
require.NotNil(t, user.ID)
require.Equal(t, http.StatusOK, response.StatusCode)
require.Equal(t, "[email protected]", user.Email)
return nil
},
AfterTest: func() error {
Expand All @@ -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 {
Expand All @@ -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())
})
}
}
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -15,7 +14,7 @@ import (

type AuthMiddlewareSuite struct {
suite.Suite
authMiddleware *middlewares.AuthenticatedMiddleware
authMiddleware *AuthenticatedMiddleware
encrypter *mocks.EncrypterMock
userService *mocks.UserServiceMock
}
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package services_test
package services

import (
"fmt"
Expand All @@ -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"
Expand All @@ -16,7 +15,7 @@ import (

type AuthServiceSuite struct {
suite.Suite
authService *services.AuthServiceImpl
authService *AuthServiceImpl
userRepository *mocks.UserRepositoryMock
hasher *mocks.HasherMock
encrypter *mocks.EncrypterMock
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package services_test
package services

import (
"fmt"
"testing"
"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"
Expand All @@ -15,7 +14,7 @@ import (

type UserServiceSuite struct {
suite.Suite
userService *services.UserServiceImpl
userService *UserServiceImpl
userRepository *mocks.UserRepositoryMock
hasher *mocks.HasherMock
}
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down
24 changes: 0 additions & 24 deletions test/app/presentation/controllers/users/setup_test.go

This file was deleted.

0 comments on commit 5d0907f

Please sign in to comment.