Skip to content

Commit

Permalink
Add some tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
onrik committed Jul 28, 2016
1 parent 7634d22 commit 0c9dac8
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ func (bot *Bot) GetChatAdministrators(chatId int64) ([]ChatMember, error) {
}

administrators := []ChatMember{}
err := bot.get("getChatAdministrators", params, administrators)
err := bot.get("getChatAdministrators", params, &administrators)

return administrators, err
}
Expand Down
121 changes: 120 additions & 1 deletion bot_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package micha

import (
"fmt"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/suite"
"net/url"
"testing"
)

Expand All @@ -13,7 +15,6 @@ type BotTestSuite struct {

func (s *BotTestSuite) SetupSuite() {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

httpmock.RegisterResponder("GET", "https://api.telegram.org/bot111/getMe",
httpmock.NewStringResponder(200, `{"ok":true,"result":{"id":1,"first_name":"Micha","username":"michabot"}}`))
Expand All @@ -27,11 +28,129 @@ func (s *BotTestSuite) SetupSuite() {
s.bot = bot
}

func (s *BotTestSuite) TearDownSuite() {
httpmock.DeactivateAndReset()
}

func (s *BotTestSuite) registerResponse(method string, params url.Values, response string) {
url := s.bot.buildUrl(method)
if params != nil {
url += fmt.Sprintf("?%s", params.Encode())
}
httpmock.RegisterResponder("GET", url, httpmock.NewStringResponder(200, response))
}

func (s *BotTestSuite) TestBuildUrl() {
url := s.bot.buildUrl("someMethod")
s.Equal(url, "https://api.telegram.org/bot111/someMethod")
}

func (s *BotTestSuite) TestGetChat() {
s.registerResponse("getChat", url.Values{"chat_id": {"123"}}, `{
"ok": true,
"result": {
"id": 123,
"type": "group",
"title": "ChatTitle",
"first_name": "fn",
"last_name": "ln",
"username": "un"
}
}`)

chat, err := s.bot.GetChat(123)
s.Equal(err, nil)
s.Equal(chat.Id, int64(123))
s.Equal(chat.Type, CHAT_TYPE_GROUP)
s.Equal(chat.Title, "ChatTitle")
s.Equal(chat.FirstName, "fn")
s.Equal(chat.LastName, "ln")
s.Equal(chat.Username, "un")
}

func (s *BotTestSuite) TestGetChatAdministrators() {
s.registerResponse("getChatAdministrators", url.Values{"chat_id": {"123"}}, `{
"ok": true,
"result": [
{
"status": "administrator",
"user": {
"id": 456,
"first_name": "John",
"last_name": "Doe",
"username": "john_doe"
}
},
{
"status": "administrator",
"user": {
"id": 789,
"first_name": "Mohammad",
"last_name": "Li",
"username": "mli"
}
}
]
}`)

administrators, err := s.bot.GetChatAdministrators(123)
s.Equal(err, nil)
s.Equal(len(administrators), 2)
s.Equal(administrators[0].User.Id, int64(456))
s.Equal(administrators[0].User.FirstName, "John")
s.Equal(administrators[0].User.LastName, "Doe")
s.Equal(administrators[0].User.Username, "john_doe")
s.Equal(administrators[0].Status, MEMBER_STATUS_ADMINISTRATOR)

s.Equal(administrators[1].User.Id, int64(789))
s.Equal(administrators[1].User.FirstName, "Mohammad")
s.Equal(administrators[1].User.LastName, "Li")
s.Equal(administrators[1].User.Username, "mli")
s.Equal(administrators[1].Status, MEMBER_STATUS_ADMINISTRATOR)
}

func (s *BotTestSuite) TestGetChatMember() {
s.registerResponse("getChatMember", url.Values{"chat_id": {"123"}, "user_id": {"456"}}, `{
"ok": true,
"result": {
"status": "creator",
"user": {
"id": 456,
"first_name": "John",
"last_name": "Doe",
"username": "john_doe"
}
}
}`)

chatMember, err := s.bot.GetChatMember(123, 456)
s.Equal(err, nil)
s.Equal(chatMember.User.Id, int64(456))
s.Equal(chatMember.User.FirstName, "John")
s.Equal(chatMember.User.LastName, "Doe")
s.Equal(chatMember.User.Username, "john_doe")
s.Equal(chatMember.Status, MEMBER_STATUS_CREATOR)

}

func (s *BotTestSuite) TestGetChatMembersCount() {
s.registerResponse("getChatMembersCount", url.Values{"chat_id": {"123"}}, `{"ok":true, "result": 25}`)

count, err := s.bot.GetChatMembersCount(123)
s.Equal(err, nil)
s.Equal(count, 25)
}

func (s *BotTestSuite) TestGetFile() {
s.registerResponse("getFile", url.Values{"file_id": {"222"}}, `{"ok":true,"result":{"file_id":"222","file_size":5,"file_path":"document/file_3.txt"}}`)

file, err := s.bot.GetFile("222")
s.Equal(err, nil)
s.Equal(file.FileId, "222")
s.Equal(file.FileSize, uint64(5))
s.Equal(file.FilePath, "document/file_3.txt")
}

func TestBotTestSuite(t *testing.T) {
suite.Run(t, new(BotTestSuite))
}

0 comments on commit 0c9dac8

Please sign in to comment.