Skip to content

Commit

Permalink
test: add user service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ishanarya0 committed Apr 18, 2024
1 parent dc08908 commit feeb206
Showing 1 changed file with 195 additions and 0 deletions.
195 changes: 195 additions & 0 deletions core/user/service_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package user_test

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/goto/shield/core/user"

"github.com/goto/shield/core/mocks"
)

func TestService_Create(t *testing.T) {
t.Parallel()

tests := []struct {
name string
user user.User
setup func(t *testing.T) *user.Service
urn string
want user.User
wantErr error
}{
{
name: "CreateUserWithUpperCase",
user: user.User{
Name: "John Doe",
Email: "[email protected]",
},
setup: func(t *testing.T) *user.Service {
t.Helper()
repository := &mocks.Repository{}
repository.EXPECT().
Create(mock.Anything, user.User{
Name: "John Doe",
Email: "[email protected]"}).
Return(user.User{
Name: "John Doe",
Email: "[email protected]"}, nil).Once()
return user.NewService(repository)

Check failure on line 44 in core/user/service_test.go

View workflow job for this annotation

GitHub Actions / golangci

unnecessary trailing newline (whitespace)
},
want: user.User{
Name: "John Doe",
Email: "[email protected]",
},
wantErr: nil,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := tt.setup(t)

assert.NotNil(t, svc)

got, err := svc.Create(context.Background(), tt.user)
if tt.wantErr != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.wantErr))
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}

Check failure on line 72 in core/user/service_test.go

View workflow job for this annotation

GitHub Actions / golangci

unnecessary trailing newline (whitespace)
}

func TestService_UpdateByID(t *testing.T) {
t.Parallel()

tests := []struct {
name string
user user.User
setup func(t *testing.T) *user.Service
urn string
want user.User
wantErr error
}{
{
name: "UpdateUserWithUpperCase",
user: user.User{
ID: "1",
Name: "John Doe",
Email: "[email protected]",
},
setup: func(t *testing.T) *user.Service {
t.Helper()
repository := &mocks.Repository{}
repository.EXPECT().
UpdateByID(mock.Anything, user.User{
ID: "1",
Name: "John Doe",
Email: "[email protected]"}).
Return(user.User{
ID: "1",
Name: "John Doe",
Email: "[email protected]"}, nil).Once()
return user.NewService(repository)

},
want: user.User{
ID: "1",
Name: "John Doe",
Email: "[email protected]",
},
wantErr: nil,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := tt.setup(t)

assert.NotNil(t, svc)

got, err := svc.UpdateByID(context.Background(), tt.user)
if tt.wantErr != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.wantErr))
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}

Check failure on line 135 in core/user/service_test.go

View workflow job for this annotation

GitHub Actions / golangci

unnecessary trailing newline (whitespace)
}

func TestService_UpdateByEmail(t *testing.T) {
t.Parallel()

tests := []struct {
name string
user user.User
setup func(t *testing.T) *user.Service
urn string
want user.User
wantErr error
}{
{
name: "UpdateUserWithUpperCase",
user: user.User{
Name: "John Doe",
Email: "[email protected]",
},
setup: func(t *testing.T) *user.Service {
t.Helper()
repository := &mocks.Repository{}
repository.EXPECT().
UpdateByEmail(mock.Anything, user.User{
Name: "John Doe",
Email: "[email protected]"}).
Return(user.User{
Name: "John Doe",
Email: "[email protected]"}, nil).Once()
return user.NewService(repository)

},
want: user.User{
Name: "John Doe",
Email: "[email protected]",
},
wantErr: nil,
},
}

for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
svc := tt.setup(t)

assert.NotNil(t, svc)

got, err := svc.UpdateByEmail(context.Background(), tt.user)
if tt.wantErr != nil {
assert.Error(t, err)
assert.True(t, errors.Is(err, tt.wantErr))
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.want, got)
})
}

}

0 comments on commit feeb206

Please sign in to comment.