-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added testcase for more functions in server/command.go #846
Open
Kshitij-Katiyar
wants to merge
1
commit into
add_server/command.go_testcases
Choose a base branch
from
add_api/command.go_testcases
base: add_server/command.go_testcases
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -655,3 +655,353 @@ func TestHandleUnmuteAll(t *testing.T) { | |||||||||
}) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func TestHandleMuteCommand(t *testing.T) { | ||||||||||
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t) | ||||||||||
p := getPluginTest(mockAPI, mockKvStore) | ||||||||||
userInfo, err := GetMockGHUserInfo(p) | ||||||||||
assert.NoError(t, err) | ||||||||||
|
||||||||||
tests := []struct { | ||||||||||
name string | ||||||||||
parameters []string | ||||||||||
setup func() | ||||||||||
assertions func(*testing.T, string) | ||||||||||
}{ | ||||||||||
{ | ||||||||||
name: "Success - list muted users", | ||||||||||
parameters: []string{"list"}, | ||||||||||
setup: func() { | ||||||||||
mutedUsernames := []byte("user1,user2,user3") | ||||||||||
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error { | ||||||||||
*value = mutedUsernames | ||||||||||
return nil | ||||||||||
}).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, response string) { | ||||||||||
assert.Equal(t, "Your muted users:\n- user1\n- user2\n- user3\n", response) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Success - add new muted user", | ||||||||||
parameters: []string{"add", "newUser"}, | ||||||||||
setup: func() { | ||||||||||
mockKvStore.EXPECT().Get(userInfo.UserID+"-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error { | ||||||||||
*value = []byte("existingUser") | ||||||||||
return nil | ||||||||||
}).Times(1) | ||||||||||
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("existingUser,newUser")).Return(true, nil).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, response string) { | ||||||||||
assert.Equal(t, "`newUser` is now muted. You'll no longer receive notifications for comments in your PRs and issues.", response) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Error - invalid number of parameters for add", | ||||||||||
parameters: []string{"add"}, | ||||||||||
setup: func() {}, | ||||||||||
assertions: func(t *testing.T, response string) { | ||||||||||
assert.Equal(t, "Invalid number of parameters supplied to add", response) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Success - delete muted user", | ||||||||||
parameters: []string{"delete", "user1"}, | ||||||||||
setup: func() { | ||||||||||
mutedUsernames := []byte("user1,user2,user3") | ||||||||||
mockKvStore.EXPECT().Get("mockUserID-muted-users", gomock.Any()).DoAndReturn(func(key string, value *[]byte) error { | ||||||||||
*value = mutedUsernames | ||||||||||
return nil | ||||||||||
}).Times(1) | ||||||||||
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", gomock.Any()).Return(true, nil).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, response string) { | ||||||||||
assert.Equal(t, "`user1` is no longer muted", response) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Error - invalid number of parameters for delete", | ||||||||||
parameters: []string{"delete"}, | ||||||||||
setup: func() {}, | ||||||||||
assertions: func(t *testing.T, response string) { | ||||||||||
assert.Equal(t, "Invalid number of parameters supplied to delete", response) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Success - delete all muted users", | ||||||||||
parameters: []string{"delete-all"}, | ||||||||||
setup: func() { | ||||||||||
mockKvStore.EXPECT().Set(userInfo.UserID+"-muted-users", []byte("")).Return(true, nil).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, response string) { | ||||||||||
assert.Equal(t, "Unmuted all users", response) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Error - unknown subcommand", | ||||||||||
parameters: []string{"unknown"}, | ||||||||||
setup: func() {}, | ||||||||||
assertions: func(t *testing.T, response string) { | ||||||||||
assert.Equal(t, "Unknown subcommand unknown", response) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Error - no parameters provided", | ||||||||||
parameters: []string{}, | ||||||||||
setup: func() {}, | ||||||||||
assertions: func(t *testing.T, response string) { | ||||||||||
assert.Equal(t, "Invalid mute command. Available commands are 'list', 'add' and 'delete'.", response) | ||||||||||
}, | ||||||||||
}, | ||||||||||
} | ||||||||||
for _, tc := range tests { | ||||||||||
t.Run(tc.name, func(t *testing.T) { | ||||||||||
tc.setup() | ||||||||||
result := p.handleMuteCommand(nil, nil, tc.parameters, userInfo) | ||||||||||
tc.assertions(t, result) | ||||||||||
}) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func TestArrayDifference(t *testing.T) { | ||||||||||
tests := []struct { | ||||||||||
name string | ||||||||||
a []string | ||||||||||
b []string | ||||||||||
Comment on lines
+769
to
+770
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
expected []string | ||||||||||
}{ | ||||||||||
{ | ||||||||||
name: "No difference - all elements in a are in b", | ||||||||||
a: []string{"apple", "banana", "cherry"}, | ||||||||||
b: []string{"apple", "banana", "cherry"}, | ||||||||||
expected: []string{}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Difference - some elements in a are not in b", | ||||||||||
a: []string{"apple", "banana", "cherry", "date"}, | ||||||||||
b: []string{"apple", "banana"}, | ||||||||||
expected: []string{"cherry", "date"}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "All elements different - no elements in a are in b", | ||||||||||
a: []string{"apple", "banana"}, | ||||||||||
b: []string{"cherry", "date"}, | ||||||||||
expected: []string{"apple", "banana"}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Empty a - no elements to compare", | ||||||||||
a: []string{}, | ||||||||||
b: []string{"apple", "banana"}, | ||||||||||
expected: []string{}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Empty b - all elements in a should be returned", | ||||||||||
a: []string{"apple", "banana"}, | ||||||||||
b: []string{}, | ||||||||||
expected: []string{"apple", "banana"}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Both a and b empty - no elements to compare", | ||||||||||
a: []string{}, | ||||||||||
b: []string{}, | ||||||||||
expected: []string{}, | ||||||||||
}, | ||||||||||
} | ||||||||||
for _, tc := range tests { | ||||||||||
t.Run(tc.name, func(t *testing.T) { | ||||||||||
result := arrayDifference(tc.a, tc.b) | ||||||||||
assert.ElementsMatch(t, tc.expected, result) | ||||||||||
}) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func TestHandleSubscriptionsList(t *testing.T) { | ||||||||||
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t) | ||||||||||
p := getPluginTest(mockAPI, mockKvStore) | ||||||||||
|
||||||||||
tests := []struct { | ||||||||||
name string | ||||||||||
channelID string | ||||||||||
setup func() | ||||||||||
assertions func(t *testing.T, result string) | ||||||||||
}{ | ||||||||||
{ | ||||||||||
name: "Error retrieving subscriptions", | ||||||||||
channelID: "channel1", | ||||||||||
setup: func() { | ||||||||||
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).Return(errors.New("store error")).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, result string) { | ||||||||||
assert.Contains(t, result, "could not get subscriptions from KVStore: store error") | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "No subscriptions in the channel", | ||||||||||
channelID: "channel2", | ||||||||||
setup: func() { | ||||||||||
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error { | ||||||||||
*value = &Subscriptions{Repositories: map[string][]*Subscription{}} | ||||||||||
return nil | ||||||||||
}).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, result string) { | ||||||||||
assert.Equal(t, "Currently there are no subscriptions in this channel", result) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Multiple subscriptions in the channel", | ||||||||||
channelID: "channel3", | ||||||||||
setup: func() { | ||||||||||
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error { | ||||||||||
*value = &Subscriptions{ | ||||||||||
Repositories: map[string][]*Subscription{ | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create a util for this, a similar thing is used below as well |
||||||||||
"repo1": { | ||||||||||
{ | ||||||||||
ChannelID: "channel3", | ||||||||||
Repository: "repo1", | ||||||||||
}, | ||||||||||
{ | ||||||||||
ChannelID: "channel4", | ||||||||||
Repository: "repo1", | ||||||||||
}, | ||||||||||
}, | ||||||||||
"repo2": { | ||||||||||
{ | ||||||||||
ChannelID: "channel3", | ||||||||||
Repository: "repo2", | ||||||||||
}, | ||||||||||
}, | ||||||||||
}, | ||||||||||
} | ||||||||||
return nil | ||||||||||
}).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, result string) { | ||||||||||
expected := "### Subscriptions in this channel\n" + | ||||||||||
"* `repo1` - \n" + | ||||||||||
"* `repo2` - \n" | ||||||||||
assert.Equal(t, expected, result) | ||||||||||
}, | ||||||||||
}, | ||||||||||
} | ||||||||||
|
||||||||||
for _, tc := range tests { | ||||||||||
t.Run(tc.name, func(t *testing.T) { | ||||||||||
tc.setup() | ||||||||||
result := p.handleSubscriptionsList(nil, &model.CommandArgs{ChannelId: tc.channelID}, nil, nil) | ||||||||||
tc.assertions(t, result) | ||||||||||
}) | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
func TestGetSubscribedFeatures(t *testing.T) { | ||||||||||
mockKvStore, mockAPI, _, _, _ := GetTestSetup(t) | ||||||||||
p := getPluginTest(mockAPI, mockKvStore) | ||||||||||
|
||||||||||
tests := []struct { | ||||||||||
name string | ||||||||||
channelID string | ||||||||||
owner string | ||||||||||
repo string | ||||||||||
setup func() | ||||||||||
assertions func(t *testing.T, features Features, err error) | ||||||||||
}{ | ||||||||||
{ | ||||||||||
name: "Error retrieving subscriptions", | ||||||||||
channelID: "channel1", | ||||||||||
owner: "owner1", | ||||||||||
repo: "repo1", | ||||||||||
setup: func() { | ||||||||||
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).Return(errors.New("store error")).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, features Features, err error) { | ||||||||||
assert.Error(t, err) | ||||||||||
assert.ErrorContains(t, err, "store error") | ||||||||||
assert.Empty(t, features) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "No subscriptions in the channel", | ||||||||||
channelID: "channel2", | ||||||||||
owner: "owner2", | ||||||||||
repo: "repo2", | ||||||||||
setup: func() { | ||||||||||
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error { | ||||||||||
*value = &Subscriptions{Repositories: map[string][]*Subscription{}} | ||||||||||
return nil | ||||||||||
}).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, features Features, err error) { | ||||||||||
assert.NoError(t, err) | ||||||||||
assert.Empty(t, features) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Subscribed features found for repo", | ||||||||||
channelID: "channel3", | ||||||||||
owner: "owner3", | ||||||||||
repo: "repo3", | ||||||||||
setup: func() { | ||||||||||
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error { | ||||||||||
*value = &Subscriptions{ | ||||||||||
Repositories: map[string][]*Subscription{ | ||||||||||
"owner3/repo3": { | ||||||||||
{ | ||||||||||
ChannelID: "channel3", | ||||||||||
Repository: "owner3/repo3", | ||||||||||
Features: Features("FeatureA"), | ||||||||||
}, | ||||||||||
}, | ||||||||||
"owner4/repo4": { | ||||||||||
{ | ||||||||||
ChannelID: "channel4", | ||||||||||
Repository: "owner4/repo4", | ||||||||||
Features: Features("FeatureB"), | ||||||||||
}, | ||||||||||
}, | ||||||||||
}, | ||||||||||
} | ||||||||||
return nil | ||||||||||
}).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, features Features, err error) { | ||||||||||
assert.NoError(t, err) | ||||||||||
expectedFeatures := Features("FeatureA") | ||||||||||
assert.Equal(t, expectedFeatures, features) | ||||||||||
}, | ||||||||||
}, | ||||||||||
{ | ||||||||||
name: "Subscribed features not found for repo", | ||||||||||
channelID: "channel4", | ||||||||||
owner: "owner5", | ||||||||||
repo: "repo5", | ||||||||||
setup: func() { | ||||||||||
mockKvStore.EXPECT().Get(SubscriptionsKey, gomock.Any()).DoAndReturn(func(key string, value **Subscriptions) error { | ||||||||||
*value = &Subscriptions{ | ||||||||||
Repositories: map[string][]*Subscription{ | ||||||||||
"owner6/repo6": { | ||||||||||
{ | ||||||||||
ChannelID: "channel4", | ||||||||||
Repository: "owner6/repo6", | ||||||||||
Features: Features("FeatureC"), | ||||||||||
}, | ||||||||||
}, | ||||||||||
}, | ||||||||||
} | ||||||||||
return nil | ||||||||||
}).Times(1) | ||||||||||
}, | ||||||||||
assertions: func(t *testing.T, features Features, err error) { | ||||||||||
assert.NoError(t, err) | ||||||||||
assert.Empty(t, features) | ||||||||||
}, | ||||||||||
}, | ||||||||||
} | ||||||||||
for _, tc := range tests { | ||||||||||
t.Run(tc.name, func(t *testing.T) { | ||||||||||
tc.setup() | ||||||||||
features, err := p.getSubscribedFeatures(tc.channelID, tc.owner, tc.repo) | ||||||||||
tc.assertions(t, features, err) | ||||||||||
}) | ||||||||||
} | ||||||||||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mockAPI is never used