Skip to content

Instantly share code, notes, and snippets.

@anatoliyfedorenko
Created July 30, 2018 05:49
Show Gist options
  • Select an option

  • Save anatoliyfedorenko/7fb16b2f30378ede4d89e658cb3b38de to your computer and use it in GitHub Desktop.

Select an option

Save anatoliyfedorenko/7fb16b2f30378ede4d89e658cb3b38de to your computer and use it in GitHub Desktop.
Go table tests practice
func TestHandleUserCommands(t *testing.T) {
AddUser := "command=/comedianadd&text=<@userid|test>&channel_id=chanid&channel_name=channame"
AddEmptyText := "command=/comedianadd&text="
AddUserEmptyChannelID := "command=/comedianadd&text=test&channel_id=&channel_name=channame"
AddUserEmptyChannelName := "command=/comedianadd&text=test&channel_id=chanid&channel_name="
DelUser := "command=/comedianremove&text=@test&channel_id=chanid"
ListUsers := "command=/comedianlist&channel_id=chanid"
c, err := config.Get()
rest, err := NewRESTAPI(c)
assert.NoError(t, err)
testCases := []struct {
title string
command string
statusCode int
responseBody string
}{
{"empty channel ID", AddUserEmptyChannelID, http.StatusBadRequest, "`channel_id` cannot be empty"},
{"empty channel name", AddUserEmptyChannelName, http.StatusBadRequest, "`channel_name` cannot be empty"},
{"empty text", AddEmptyText, http.StatusBadRequest, "`text` cannot be empty"},
{"add user no standup time", AddUser, http.StatusOK, "<@test> added, but there is no standup time for this channel"},
{"list users", ListUsers, http.StatusOK, "Standupers in this channel: <@test>"},
{"add user with user exist", AddUser, http.StatusOK, "User already exists!"},
{"delete user", DelUser, http.StatusOK, "<@test> deleted"},
}
for _, tt := range testCases {
context, rec := getContext(tt.command)
err := rest.handleCommands(context)
if err != nil {
logrus.Errorf("TestHandleUserCommands: %s failed. Error: %v\n", tt.title, err)
}
assert.Equal(t, tt.statusCode, rec.Code)
assert.Equal(t, tt.responseBody, rec.Body.String())
}
st, err := rest.db.CreateStandupTime(model.StandupTime{
ChannelID: "chanid",
Channel: "channame",
Time: int64(12),
})
testCases = []struct {
title string
command string
statusCode int
responseBody string
}{
{"add user with standup time", AddUser, http.StatusOK, "<@test> added"},
{"delete user", DelUser, http.StatusOK, "<@test> deleted"},
{"list no users", ListUsers, http.StatusOK, "No standupers in this channel! To add one, please, use /comedianadd slash command"},
}
for _, tt := range testCases {
context, rec := getContext(tt.command)
err := rest.handleCommands(context)
if err != nil {
logrus.Errorf("TestHandleUserCommands: %s failed. Error: %v\n", tt.title, err)
}
assert.Equal(t, tt.statusCode, rec.Code)
assert.Equal(t, tt.responseBody, rec.Body.String())
}
assert.NoError(t, rest.db.DeleteStandupTime(st.ChannelID))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment