-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from solumD/Task-4
Task 4
- Loading branch information
Showing
23 changed files
with
3,521 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.idea | ||
/bin/ | ||
/bin//coverage.out | ||
/coverage.out |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/solumD/chat-server/internal/api/chat" | ||
"github.com/solumD/chat-server/internal/api/chat/errors" | ||
"github.com/solumD/chat-server/internal/model" | ||
"github.com/solumD/chat-server/internal/service" | ||
serviceMocks "github.com/solumD/chat-server/internal/service/mocks" | ||
desc "github.com/solumD/chat-server/pkg/chat_v1" | ||
|
||
"github.com/brianvoe/gofakeit/v7" | ||
"github.com/gojuno/minimock/v3" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCreateChat(t *testing.T) { | ||
t.Parallel() | ||
|
||
type chatServiceMockFunc func(mc *minimock.Controller) service.ChatService | ||
|
||
type args struct { | ||
ctx context.Context | ||
req *desc.CreateChatRequest | ||
} | ||
|
||
var ( | ||
ctx = context.Background() | ||
mc = minimock.NewController(t) | ||
|
||
name = gofakeit.Name() | ||
usernames = []string{gofakeit.Name(), gofakeit.Name(), gofakeit.Name(), gofakeit.Name()} | ||
id = gofakeit.Int64() | ||
|
||
serviceErr = fmt.Errorf("service err") | ||
|
||
req = &desc.CreateChatRequest{ | ||
Name: name, | ||
Usernames: usernames, | ||
} | ||
|
||
info = &model.Chat{ | ||
Name: name, | ||
Usernames: usernames, | ||
} | ||
|
||
res = &desc.CreateChatResponse{ | ||
Id: id, | ||
} | ||
) | ||
defer t.Cleanup(mc.Finish) | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want *desc.CreateChatResponse | ||
err error | ||
chatServiceMock chatServiceMockFunc | ||
}{ | ||
{ | ||
name: "success case", | ||
args: args{ | ||
ctx: ctx, | ||
req: req, | ||
}, | ||
want: res, | ||
err: nil, | ||
chatServiceMock: func(mc *minimock.Controller) service.ChatService { | ||
mock := serviceMocks.NewChatServiceMock(mc) | ||
mock.CreateChatMock.Expect(ctx, info).Return(id, nil) | ||
return mock | ||
}, | ||
}, | ||
{ | ||
name: "error case", | ||
args: args{ | ||
ctx: ctx, | ||
req: req, | ||
}, | ||
want: nil, | ||
err: serviceErr, | ||
chatServiceMock: func(mc *minimock.Controller) service.ChatService { | ||
mock := serviceMocks.NewChatServiceMock(mc) | ||
mock.CreateChatMock.Expect(ctx, info).Return(0, serviceErr) | ||
return mock | ||
}, | ||
}, | ||
{ | ||
name: "error req is nil", | ||
args: args{ | ||
ctx: ctx, | ||
req: nil, | ||
}, | ||
want: nil, | ||
err: errors.ErrDescChatIsNil, | ||
chatServiceMock: func(mc *minimock.Controller) service.ChatService { | ||
mock := serviceMocks.NewChatServiceMock(mc) | ||
return mock | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
chatServiceMock := tt.chatServiceMock(mc) | ||
api := chat.NewAPI(chatServiceMock) | ||
|
||
res, err := api.CreateChat(tt.args.ctx, tt.args.req) | ||
require.Equal(t, tt.err, err) | ||
require.Equal(t, tt.want, res) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package tests | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/solumD/chat-server/internal/api/chat" | ||
"github.com/solumD/chat-server/internal/service" | ||
serviceMocks "github.com/solumD/chat-server/internal/service/mocks" | ||
desc "github.com/solumD/chat-server/pkg/chat_v1" | ||
|
||
"github.com/brianvoe/gofakeit/v7" | ||
"github.com/gojuno/minimock/v3" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/types/known/emptypb" | ||
) | ||
|
||
func TestDeleteChat(t *testing.T) { | ||
t.Parallel() | ||
|
||
type chatServiceMockFunc func(mc *minimock.Controller) service.ChatService | ||
|
||
type args struct { | ||
ctx context.Context | ||
req *desc.DeleteChatRequest | ||
} | ||
|
||
var ( | ||
ctx = context.Background() | ||
mc = minimock.NewController(t) | ||
|
||
id = gofakeit.Int64() | ||
|
||
serviceErr = fmt.Errorf("service err") | ||
reqIsNilErr = fmt.Errorf("req is nil") | ||
|
||
req = &desc.DeleteChatRequest{ | ||
Id: id, | ||
} | ||
|
||
res = &emptypb.Empty{} | ||
) | ||
defer t.Cleanup(mc.Finish) | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want *emptypb.Empty | ||
err error | ||
chatServiceMock chatServiceMockFunc | ||
}{ | ||
{ | ||
name: "success case", | ||
args: args{ | ||
ctx: ctx, | ||
req: req, | ||
}, | ||
want: res, | ||
err: nil, | ||
chatServiceMock: func(mc *minimock.Controller) service.ChatService { | ||
mock := serviceMocks.NewChatServiceMock(mc) | ||
mock.DeleteChatMock.Expect(ctx, id).Return(&emptypb.Empty{}, nil) | ||
return mock | ||
}, | ||
}, | ||
{ | ||
name: "service error", | ||
args: args{ | ||
ctx: ctx, | ||
req: req, | ||
}, | ||
want: nil, | ||
err: serviceErr, | ||
chatServiceMock: func(mc *minimock.Controller) service.ChatService { | ||
mock := serviceMocks.NewChatServiceMock(mc) | ||
mock.DeleteChatMock.Expect(ctx, id).Return(nil, serviceErr) | ||
return mock | ||
}, | ||
}, | ||
{ | ||
name: "error req is nil", | ||
args: args{ | ||
ctx: ctx, | ||
req: nil, | ||
}, | ||
want: nil, | ||
err: reqIsNilErr, | ||
chatServiceMock: func(mc *minimock.Controller) service.ChatService { | ||
mock := serviceMocks.NewChatServiceMock(mc) | ||
return mock | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
chatServiceMock := tt.chatServiceMock(mc) | ||
api := chat.NewAPI(chatServiceMock) | ||
|
||
res, err := api.DeleteChat(tt.args.ctx, tt.args.req) | ||
require.Equal(t, tt.err, err) | ||
require.Equal(t, tt.want, res) | ||
}) | ||
} | ||
} |
Oops, something went wrong.