Skip to content

Commit

Permalink
fix: better use of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomMoulard committed May 3, 2024
1 parent 59358e9 commit 642d314
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 47 deletions.
8 changes: 4 additions & 4 deletions pkg/channel/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
func TestCreateGroupChannel(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
channel := NewChannel(client)

createChannelRequest := CreateGroupChannelRequest{
UserIDs: []string{"42", "43"},
Name: "name",
Expand All @@ -37,7 +34,10 @@ func TestCreateGroupChannel(t *testing.T) {
Name: "name",
}

client.OnPost("/group_channels", createChannelRequest, &CreateGroupChannelResponse{}).Return(createChannelResponse, nil)
client := client.NewClientMock(t).
OnPost("/group_channels", createChannelRequest, &CreateGroupChannelResponse{}).TypedReturns(createChannelResponse, nil).Once().
Parent
channel := NewChannel(client)

cur, err := channel.CreateGroupChannel(context.Background(), createChannelRequest)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/channel/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ func TestPtr(t *testing.T) {
func TestListGroupChannels(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
channel := NewChannel(client)

url := "/group_channels"
url += "?channel_urls=channel-url1%2Cchannel-url2"
url += "&created_after=43"
Expand Down Expand Up @@ -111,7 +108,10 @@ func TestListGroupChannels(t *testing.T) {
}},
}

client.OnGet(url, nil, &ListGroupChannelResponse{}).Return(listChannelsResponse, nil)
client := client.NewClientMock(t).
OnGet(url, nil, &ListGroupChannelResponse{}).TypedReturns(listChannelsResponse, nil).Once().
Parent
channel := NewChannel(client)

cur, err := channel.ListGroupChannels(context.Background(), listChannelsRequest)
require.NoError(t, err)
Expand Down
6 changes: 3 additions & 3 deletions pkg/channel/mark_all_messages_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import (
func TestMarkAsRead(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
client := client.NewClientMock(t).
OnPut("/group_channels/channel-url/messages/mark_as_read", markAsReadRequest{UserID: "user-id"}, nil).TypedReturns(nil, nil).Once().
Parent
channel := NewChannel(client)

client.OnPut("/group_channels/channel-url/messages/mark_as_read", markAsReadRequest{UserID: "user-id"}, nil).Return(nil, nil)

err := channel.MarkAsRead(context.Background(), "channel-url", "user-id")
require.NoError(t, err)
}
12 changes: 6 additions & 6 deletions pkg/channel/typing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ import (
func TestStartTyping(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
client := client.NewClientMock(t).
OnPost("/group_channels/channel-url/typing", typingRequest{UserIDs: []string{"user-id"}}, nil).TypedReturns(nil, nil).Once().
Parent
channel := NewChannel(client)

client.OnPost("/group_channels/channel-url/typing", typingRequest{UserIDs: []string{"user-id"}}, nil).Return(nil, nil)

err := channel.StartTyping(context.Background(), "channel-url", []string{"user-id"})
require.NoError(t, err)
}

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

client := client.NewClientMock(t)
client := client.NewClientMock(t).
OnDelete("/group_channels/channel-url/typing", typingRequest{UserIDs: []string{"user-id"}}, nil).TypedReturns(nil, nil).Once().
Parent
channel := NewChannel(client)

client.OnDelete("/group_channels/channel-url/typing", typingRequest{UserIDs: []string{"user-id"}}, nil).Return(nil, nil)

err := channel.StopTyping(context.Background(), "channel-url", []string{"user-id"})
require.NoError(t, err)
}
8 changes: 4 additions & 4 deletions pkg/channel/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
func TestUpdateGroupChannel(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
channel := NewChannel(client)

updateChannelRequest := UpdateGroupChannelRequest{
Name: "channel-name",
CoverURL: "cover-url",
Expand All @@ -31,7 +28,10 @@ func TestUpdateGroupChannel(t *testing.T) {
Name: "channel-name",
}

client.OnPut("/group_channels/channel-url", updateChannelRequest, &UpdateGroupChannelResponse{}).Return(updateChannelResponse, nil)
client := client.NewClientMock(t).
OnPut("/group_channels/channel-url", updateChannelRequest, &UpdateGroupChannelResponse{}).TypedReturns(updateChannelResponse, nil).Once().
Parent
channel := NewChannel(client)

cur, err := channel.UpdateGroupChannel(context.Background(), "channel-url", updateChannelRequest)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/message/list_messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
func TestListMessages(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
message := NewMessage(client)

url := "/group_channels/url/messages"
url += "?custom_types=%2A"
url += "&include=true"
Expand Down Expand Up @@ -66,7 +63,10 @@ func TestListMessages(t *testing.T) {
}},
}

client.OnGet(url, nil, &ListMessagesResponse{}).Return(listMessagesResponse, nil)
client := client.NewClientMock(t).
OnGet(url, nil, &ListMessagesResponse{}).TypedReturns(listMessagesResponse, nil).Once().
Parent
message := NewMessage(client)

cur, err := message.ListMessages(context.Background(), ChannelTypeGroup, "url", listMessagesRequest)
require.NoError(t, err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/message/send_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ func TestSendMessage(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
client := client.NewClientMock(t).
OnPost("/group_channels/url/messages", test.smrq, &SendMessageResponse{}).TypedReturns(&test.smrp, nil).Once().
Parent
message := NewMessage(client)
client.OnPost("/group_channels/url/messages", test.smrq, &SendMessageResponse{}).Return(&test.smrp, nil)

cur, err := message.SendMessage(context.Background(), ChannelTypeGroup, "url", test.smrq)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/user/create_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
func TestCreateUser(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
user := NewUser(client)

createUserRequest := CreateUserRequest{
UserID: "user-id",
Nickname: "nickname",
Expand Down Expand Up @@ -45,7 +42,10 @@ func TestCreateUser(t *testing.T) {
},
}

client.OnPost("/users", createUserRequest, &CreateUserResponse{}).Return(createUserResponse, nil)
client := client.NewClientMock(t).
OnPost("/users", createUserRequest, &CreateUserResponse{}).TypedReturns(createUserResponse, nil).Once().
Parent
user := NewUser(client)

cur, err := user.CreateUser(context.Background(), createUserRequest)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/user/joined_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
func TestGetGroupChannelCount(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
user := NewUser(client)

getGroupChannelCountRequest := GetGroupChannelCountRequest{
CustomTypes: []string{"custom-type"},
HiddenMode: ModeAll,
Expand All @@ -26,7 +23,10 @@ func TestGetGroupChannelCount(t *testing.T) {
GroupChannelCount: 42,
}

client.OnGet("/users/42/group_channel_count", getGroupChannelCountRequest, &GetGroupChannelCountResponse{}).Return(getGroupChannelCountResponse, nil)
client := client.NewClientMock(t).
OnGet("/users/42/group_channel_count", getGroupChannelCountRequest, &GetGroupChannelCountResponse{}).TypedReturns(getGroupChannelCountResponse, nil).Once().
Parent
user := NewUser(client)

cur, err := user.GetGroupChannelCount(context.Background(), "42", getGroupChannelCountRequest)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/user/session_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
func TestGetSessionToken(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
user := NewUser(client)

getSessionTokenRequest := GetSessionTokenRequest{
ExpiresAt: 0,
}
Expand All @@ -24,7 +21,10 @@ func TestGetSessionToken(t *testing.T) {
ExpiresAt: 0,
}

client.OnPost("/users/42/token", getSessionTokenRequest, &GetSessionTokenResponse{}).Return(getSessionTokenResponse, nil)
client := client.NewClientMock(t).
OnPost("/users/42/token", getSessionTokenRequest, &GetSessionTokenResponse{}).TypedReturns(getSessionTokenResponse, nil).Once().
Parent
user := NewUser(client)

cur, err := user.GetSessionToken(context.Background(), "42", getSessionTokenRequest)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/user/unread_messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
func TestGetUnreadMessagesCount(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
user := NewUser(client)

getUnreadMessagesCountRequest := GetUnreadMessagesCountRequest{
CustomTypes: []string{"custom-type"},
SuperMode: SuperModeSuper,
Expand All @@ -24,7 +21,10 @@ func TestGetUnreadMessagesCount(t *testing.T) {
UnreadCount: 42,
}

client.OnGet("/users/42/unread_message_count", getUnreadMessagesCountRequest, &GetUnreadMessagesCountResponse{}).Return(getUnreadMessagesCountResponse, nil)
client := client.NewClientMock(t).
OnGet("/users/42/unread_message_count", getUnreadMessagesCountRequest, &GetUnreadMessagesCountResponse{}).TypedReturns(getUnreadMessagesCountResponse, nil).Once().
Parent
user := NewUser(client)

cur, err := user.GetUnreadMessagesCount(context.Background(), "42", getUnreadMessagesCountRequest)
require.NoError(t, err)
Expand Down
8 changes: 4 additions & 4 deletions pkg/user/update_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ import (
func TestUpdateUser(t *testing.T) {
t.Parallel()

client := client.NewClientMock(t)
user := NewUser(client)

updateUserRequest := UpdateUserRequest{
Nickname: "nickname",
ProfileURL: "profile-url",
Expand Down Expand Up @@ -45,7 +42,10 @@ func TestUpdateUser(t *testing.T) {
},
}

client.OnPut("/users/42", updateUserRequest, &UpdateUserResponse{}).Return(updateUserResponse, nil)
client := client.NewClientMock(t).
OnPut("/users/42", updateUserRequest, &UpdateUserResponse{}).TypedReturns(updateUserResponse, nil).Once().
Parent
user := NewUser(client)

cur, err := user.UpdateUser(context.Background(), "42", updateUserRequest)
require.NoError(t, err)
Expand Down

0 comments on commit 642d314

Please sign in to comment.