Skip to content

Commit

Permalink
unit tests for use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
josepdcs committed Jan 13, 2025
1 parent f50fb4d commit 469bbfa
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 8 deletions.
9 changes: 3 additions & 6 deletions internal/application/usecase/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ func NewUserFinderByID(user repository.User) usecase.UserFinderByID {

// Find returns a user by ID or an error if something goes wrong
func (u *UserFinderByID) Find(ctx context.Context, id uint) (entity.User, error) {
user, err := u.user.FindByID(ctx, id)
return user, err
return u.user.FindByID(ctx, id)
}

// UserCreator defines the use case for creating a user
Expand All @@ -58,8 +57,7 @@ func NewUserCreator(user repository.User) usecase.UserCreator {

// Create creates a user and returns the created user or an error if something goes wrong
func (u *UserCreator) Create(ctx context.Context, user entity.User) (entity.User, error) {
user, err := u.user.Create(ctx, user)
return user, err
return u.user.Create(ctx, user)
}

// UserModifier defines the use case for modifying a user
Expand Down Expand Up @@ -93,6 +91,5 @@ func NewUserDeleter(user repository.User) usecase.UserDeleter {

// Delete deletes a user and returns an error if something goes wrong
func (u *UserDeleter) Delete(ctx context.Context, user entity.User) error {
err := u.user.Delete(ctx, user)
return err
return u.user.Delete(ctx, user)
}
223 changes: 221 additions & 2 deletions internal/application/usecase/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestUserFinderAll_Find(t *testing.T) {
{ID: 2, Name: "Jane", Surname: "Doe"},
{ID: 3, Name: "Alice", Surname: "Smith"},
}
m.On("FindAll", mock.Anything).Return(users, nil)
m.On("FindAll", context.Background()).Return(users, nil)
return m
},
when: func(userMock *repository.UserMock) ([]entity.User, error) {
Expand All @@ -56,7 +56,7 @@ func TestUserFinderAll_Find(t *testing.T) {
given: func() *repository.UserMock {
m := repository.NewUserMock()
var users []entity.User
m.On("FindAll", mock.Anything).Return(users, errors.New("not found"))
m.On("FindAll", context.Background()).Return(users, errors.New("not found"))
return m
},
when: func(userMock *repository.UserMock) ([]entity.User, error) {
Expand All @@ -81,3 +81,222 @@ func TestUserFinderAll_Find(t *testing.T) {
})
}
}

func TestUserFinderByID_Find(t *testing.T) {
tests := []struct {
name string
given func() *repository.UserMock
when func(userMock *repository.UserMock) (entity.User, error)
then func(entity.User, error)
}{
{
name: "should find user by ID",
given: func() *repository.UserMock {
m := repository.NewUserMock()
user := entity.User{ID: 1, Name: "John", Surname: "Doe"}
m.On("FindByID", context.Background(), user.ID).Return(user, nil)
return m
},
when: func(userMock *repository.UserMock) (entity.User, error) {
return NewUserFinderByID(userMock).Find(context.Background(), 1)
},
then: func(user entity.User, err error) {
assert.NoError(t, err)
assert.NotNil(t, user)
assert.Equal(t, "John", user.Name)
assert.Equal(t, "Doe", user.Surname)
},
},
{
name: "should not find user by ID",
given: func() *repository.UserMock {
m := repository.NewUserMock()
m.On("FindByID", context.Background(), mock.Anything).Return(entity.User{}, errors.New("not found"))
return m
},
when: func(userMock *repository.UserMock) (entity.User, error) {
return NewUserFinderByID(userMock).Find(context.Background(), 1)
},
then: func(user entity.User, err error) {
assert.Error(t, err)
assert.Equal(t, entity.User{}, user)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Given
userMock := tt.given()

// When
user, err := tt.when(userMock)

// Then
tt.then(user, err)
})
}
}

func TestUserCreator_Create(t *testing.T) {
tests := []struct {
name string
given func() *repository.UserMock
when func(userMock *repository.UserMock) (entity.User, error)
then func(entity.User, error)
}{
{
name: "should create user",
given: func() *repository.UserMock {
m := repository.NewUserMock()
user := entity.User{Name: "John", Surname: "Doe"}
created := entity.User{ID: 1, Name: "John", Surname: "Doe"}
m.On("save", context.Background(), user).Return(created, nil)
return m
},
when: func(userMock *repository.UserMock) (entity.User, error) {
return NewUserCreator(userMock).Create(context.Background(), entity.User{Name: "John", Surname: "Doe"})
},
then: func(user entity.User, err error) {
assert.NoError(t, err)
assert.NotNil(t, user)
assert.Equal(t, uint(1), user.ID)
assert.Equal(t, "John", user.Name)
assert.Equal(t, "Doe", user.Surname)
},
},
{
name: "should not create user",
given: func() *repository.UserMock {
m := repository.NewUserMock()
m.On("save", context.Background(), mock.Anything).Return(entity.User{}, errors.New("not created"))
return m
},
when: func(userMock *repository.UserMock) (entity.User, error) {
return NewUserCreator(userMock).Create(context.Background(), entity.User{})
},
then: func(user entity.User, err error) {
assert.Error(t, err)
assert.Equal(t, entity.User{}, user)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Given
userMock := tt.given()

// When
user, err := tt.when(userMock)

// Then
tt.then(user, err)
})
}
}

func TestUserModifier_Modify(t *testing.T) {
tests := []struct {
name string
given func() *repository.UserMock
when func(userMock *repository.UserMock) (entity.User, error)
then func(entity.User, error)
}{
{
name: "should modify user",
given: func() *repository.UserMock {
m := repository.NewUserMock()
user := entity.User{ID: 1, Name: "John", Surname: "Doe"}
m.On("save", context.Background(), user).Return(user, nil)
return m
},
when: func(userMock *repository.UserMock) (entity.User, error) {
return NewUserModifier(userMock).Modify(context.Background(), entity.User{ID: 1, Name: "John", Surname: "Doe"})
},
then: func(user entity.User, err error) {
assert.NoError(t, err)
assert.NotNil(t, user)
assert.Equal(t, uint(1), user.ID)
assert.Equal(t, "John", user.Name)
assert.Equal(t, "Doe", user.Surname)
},
},
{
name: "should not modify user",
given: func() *repository.UserMock {
m := repository.NewUserMock()
m.On("save", context.Background(), mock.Anything).Return(entity.User{}, errors.New("not modified"))
return m
},
when: func(userMock *repository.UserMock) (entity.User, error) {
return NewUserModifier(userMock).Modify(context.Background(), entity.User{})
},
then: func(user entity.User, err error) {
assert.Error(t, err)
assert.Equal(t, entity.User{}, user)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Given
userMock := tt.given()

// When
user, err := tt.when(userMock)

// Then
tt.then(user, err)
})
}
}

func TestUserDeleter_Delete(t *testing.T) {
tests := []struct {
name string
given func() *repository.UserMock
when func(userMock *repository.UserMock) error
then func(error)
}{
{
name: "should delete user",
given: func() *repository.UserMock {
m := repository.NewUserMock()
user := entity.User{ID: 1, Name: "John", Surname: "Doe"}
m.On("Delete", context.Background(), user).Return(nil)
return m
},
when: func(userMock *repository.UserMock) error {
return NewUserDeleter(userMock).Delete(context.Background(), entity.User{ID: 1, Name: "John", Surname: "Doe"})
},
then: func(err error) {
assert.NoError(t, err)
},
},
{
name: "should not delete user",
given: func() *repository.UserMock {
m := repository.NewUserMock()
m.On("Delete", context.Background(), mock.Anything).Return(errors.New("not deleted"))
return m
},
when: func(userMock *repository.UserMock) error {
return NewUserDeleter(userMock).Delete(context.Background(), entity.User{})
},
then: func(err error) {
assert.Error(t, err)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Given
userMock := tt.given()

// When
err := tt.when(userMock)

// Then
tt.then(err)
})
}
}

0 comments on commit 469bbfa

Please sign in to comment.