From e1811e100af674dd87c2e2ab43493216f05707c3 Mon Sep 17 00:00:00 2001 From: Demian Date: Tue, 9 Jun 2020 23:28:28 +0300 Subject: [PATCH] tests: implement more --- api.go | 2 +- bot.go | 28 ++++++++++++++-------------- bot_test.go | 49 ++++++++++++++++++++++++++++++++++++------------- file.go | 7 ++----- file_test.go | 24 ++++++++++++++++++++++++ message.go | 13 ++++++------- sendable.go | 19 ++++++------------- telebot.go | 2 +- 8 files changed, 90 insertions(+), 54 deletions(-) create mode 100644 file_test.go diff --git a/api.go b/api.go index b331ffd..f5f48c3 100644 --- a/api.go +++ b/api.go @@ -29,7 +29,7 @@ func (b *Bot) Raw(method string, payload interface{}) ([]byte, error) { resp, err := b.client.Post(url, "application/json", &buf) if err != nil { - return nil, errors.Wrap(err, "http.Post failed") + return nil, wrapError(err) } resp.Close = true defer resp.Body.Close() diff --git a/bot.go b/bot.go index 4b3bce8..13c6c41 100644 --- a/bot.go +++ b/bot.go @@ -544,17 +544,17 @@ func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Messag var ( repr string data []byte - f = x.MediaFile() + file = x.MediaFile() ) switch { - case f.InCloud(): - repr = f.FileID - case f.FileURL != "": - repr = f.FileURL - case f.OnDisk() || f.FileReader != nil: + case file.InCloud(): + repr = file.FileID + case file.FileURL != "": + repr = file.FileURL + case file.OnDisk() || file.FileReader != nil: repr = "attach://" + strconv.Itoa(i) - files[strconv.Itoa(i)] = *f + files[strconv.Itoa(i)] = *file default: return nil, errors.Errorf("telebot: album entry #%d does not exist", i) } @@ -1053,16 +1053,16 @@ func (b *Bot) Answer(query *Query, resp *QueryResponse) error { // bot.Respond(c) // bot.Respond(c, response) // -func (b *Bot) Respond(c *Callback, response ...*CallbackResponse) error { - var resp *CallbackResponse - if response == nil { - resp = &CallbackResponse{} +func (b *Bot) Respond(c *Callback, resp ...*CallbackResponse) error { + var r *CallbackResponse + if resp == nil { + r = &CallbackResponse{} } else { - resp = response[0] + r = resp[0] } - resp.CallbackID = c.ID - _, err := b.Raw("answerCallbackQuery", resp) + r.CallbackID = c.ID + _, err := b.Raw("answerCallbackQuery", r) return err } diff --git a/bot_test.go b/bot_test.go index aba2e21..12537aa 100644 --- a/bot_test.go +++ b/bot_test.go @@ -324,6 +324,19 @@ func TestBot(t *testing.T) { assert.Equal(t, photo.Caption, msg.Caption) }) + t.Run("SendAlbum()", func(t *testing.T) { + _, err = b.SendAlbum(nil, nil) + assert.Equal(t, ErrBadRecipient, err) + + _, err = b.SendAlbum(to, nil) + assert.Error(t, err) + + msgs, err := b.SendAlbum(to, Album{photo, photo}) + assert.NoError(t, err) + assert.Len(t, msgs, 2) + assert.NotEmpty(t, msgs[0].AlbumID) + }) + t.Run("EditCaption()+ParseMode", func(t *testing.T) { b.parseMode = ModeHTML edited, err := b.EditCaption(msg, "new caption with parse mode") @@ -415,26 +428,24 @@ func TestBot(t *testing.T) { assert.NotNil(t, edited.Location) }) + // should be the last + t.Run("Delete()", func(t *testing.T) { + assert.NoError(t, b.Delete(msg)) + }) + t.Run("Notify()", func(t *testing.T) { assert.Equal(t, ErrBadRecipient, b.Notify(nil, Typing)) assert.NoError(t, b.Notify(to, Typing)) }) - // should be the last - t.Run("Delete()", func(t *testing.T) { - assert.NoError(t, b.Delete(msg)) + t.Run("Answer()", func(t *testing.T) { + assert.Error(t, b.Answer(&Query{}, &QueryResponse{ + Results: Results{&ArticleResult{}}, + })) }) - t.Run("Commands", func(t *testing.T) { - orig := []Command{{ - Text: "test", - Description: "test command", - }} - assert.NoError(t, b.SetCommands(orig)) - - cmds, err := b.GetCommands() - assert.NoError(t, err) - assert.Equal(t, orig, cmds) + t.Run("Respond()", func(t *testing.T) { + assert.Error(t, b.Respond(&Callback{}, &CallbackResponse{})) }) t.Run("Payments", func(t *testing.T) { @@ -449,4 +460,16 @@ func TestBot(t *testing.T) { assert.Equal(t, ErrUnsupportedWhat, b.Ship(&ShippingQuery{}, 0)) }) }) + + t.Run("Commands", func(t *testing.T) { + orig := []Command{{ + Text: "test", + Description: "test command", + }} + assert.NoError(t, b.SetCommands(orig)) + + cmds, err := b.GetCommands() + assert.NoError(t, err) + assert.Equal(t, orig, cmds) + }) } diff --git a/file.go b/file.go index e0c6788..2025e77 100644 --- a/file.go +++ b/file.go @@ -80,9 +80,6 @@ func (f *File) InCloud() bool { // OnDisk will return true if file is present on disk. func (f *File) OnDisk() bool { - if _, err := os.Stat(f.FileLocal); err != nil { - return false - } - - return true + _, err := os.Stat(f.FileLocal) + return err == nil } diff --git a/file_test.go b/file_test.go new file mode 100644 index 0000000..f727828 --- /dev/null +++ b/file_test.go @@ -0,0 +1,24 @@ +package telebot + +import ( + "io" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestFile(t *testing.T) { + f := FromDisk("telebot.go") + g := FromURL("http://") + + assert.True(t, f.OnDisk()) + assert.True(t, (&File{FileID: "1"}).InCloud()) + assert.Equal(t, File{FileLocal: "telebot.go"}, f) + assert.Equal(t, File{FileURL: "http://"}, g) + assert.Equal(t, File{FileReader: io.Reader(nil)}, FromReader(io.Reader(nil))) + + g.stealRef(&f) + f.stealRef(&g) + assert.Equal(t, g.FileLocal, f.FileLocal) + assert.Equal(t, f.FileURL, g.FileURL) +} diff --git a/message.go b/message.go index e314c1a..efb2451 100644 --- a/message.go +++ b/message.go @@ -72,7 +72,7 @@ type Message struct { // For an audio recording, information about it. Audio *Audio `json:"audio"` - // For a gneral file, information about it. + // For a general file, information about it. Document *Document `json:"document"` // For a photo, all available sizes (thumbnails). @@ -156,7 +156,7 @@ type Message struct { // Sender would lead to creator of the chat. GroupCreated bool `json:"group_chat_created"` - // For a service message, true if super group has been created. + // For a service message, true if supergroup has been created. // // You would receive such a message if you are one of // initial group chat members. @@ -172,11 +172,11 @@ type Message struct { // Sender would lead to creator of the chat. ChannelCreated bool `json:"channel_chat_created"` - // For a service message, the destination (super group) you + // For a service message, the destination (supergroup) you // migrated to. // // You would receive such a message when your chat has migrated - // to a super group. + // to a supergroup. // // Sender would lead to creator of the migration. MigrateTo int64 `json:"migrate_to_chat_id"` @@ -185,7 +185,7 @@ type Message struct { // from. // // You would receive such a message when your chat has migrated - // to a super group. + // to a supergroup. // // Sender would lead to creator of the migration. MigrateFrom int64 `json:"migrate_from_chat_id"` @@ -266,8 +266,7 @@ func (m *Message) Private() bool { return m.Chat.Type == ChatPrivate } -// FromGroup returns true, if message came from a group OR -// a super group. +// FromGroup returns true, if message came from a group OR a supergroup. func (m *Message) FromGroup() bool { return m.Chat.Type == ChatGroup || m.Chat.Type == ChatSuperGroup } diff --git a/sendable.go b/sendable.go index 0ec8150..0f18abe 100644 --- a/sendable.go +++ b/sendable.go @@ -29,7 +29,6 @@ func (p *Photo) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { "chat_id": to.Recipient(), "caption": p.Caption, } - b.embedSendOptions(params, opt) msg, err := b.sendObject(&p.File, "photo", params, nil) @@ -53,13 +52,12 @@ func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { "title": a.Title, "file_name": a.FileName, } + b.embedSendOptions(params, opt) if a.Duration != 0 { params["duration"] = strconv.Itoa(a.Duration) } - b.embedSendOptions(params, opt) - msg, err := b.sendObject(&a.File, "audio", params, thumbnailToFilemap(a.Thumbnail)) if err != nil { return nil, err @@ -86,13 +84,12 @@ func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error "caption": d.Caption, "file_name": d.FileName, } + b.embedSendOptions(params, opt) if d.FileSize != 0 { params["file_size"] = strconv.Itoa(d.FileSize) } - b.embedSendOptions(params, opt) - msg, err := b.sendObject(&d.File, "document", params, thumbnailToFilemap(d.Thumbnail)) if err != nil { return nil, err @@ -130,6 +127,7 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { "caption": v.Caption, "file_name": v.FileName, } + b.embedSendOptions(params, opt) if v.Duration != 0 { params["duration"] = strconv.Itoa(v.Duration) @@ -144,8 +142,6 @@ func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { params["supports_streaming"] = "true" } - b.embedSendOptions(params, opt) - msg, err := b.sendObject(&v.File, "video", params, thumbnailToFilemap(v.Thumbnail)) if err != nil { return nil, err @@ -175,6 +171,7 @@ func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, erro "caption": a.Caption, "file_name": a.FileName, } + b.embedSendOptions(params, opt) if a.Duration != 0 { params["duration"] = strconv.Itoa(a.Duration) @@ -191,8 +188,6 @@ func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, erro params["file_name"] = filepath.Base(a.File.FileLocal) } - b.embedSendOptions(params, opt) - msg, err := b.sendObject(&a.File, "animation", params, nil) if err != nil { return nil, err @@ -210,13 +205,12 @@ func (v *Voice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { params := map[string]string{ "chat_id": to.Recipient(), } + b.embedSendOptions(params, opt) if v.Duration != 0 { params["duration"] = strconv.Itoa(v.Duration) } - b.embedSendOptions(params, opt) - msg, err := b.sendObject(&v.File, "voice", params, nil) if err != nil { return nil, err @@ -233,6 +227,7 @@ func (v *VideoNote) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, erro params := map[string]string{ "chat_id": to.Recipient(), } + b.embedSendOptions(params, opt) if v.Duration != 0 { params["duration"] = strconv.Itoa(v.Duration) @@ -241,8 +236,6 @@ func (v *VideoNote) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, erro params["length"] = strconv.Itoa(v.Length) } - b.embedSendOptions(params, opt) - msg, err := b.sendObject(&v.File, "videoNote", params, thumbnailToFilemap(v.Thumbnail)) if err != nil { return nil, err diff --git a/telebot.go b/telebot.go index c5a1ceb..ff357e6 100644 --- a/telebot.go +++ b/telebot.go @@ -78,7 +78,7 @@ const ( OnGroupPhotoDeleted = "\achat_photo_del" // Migration happens when group switches to - // a super group. You might want to update + // a supergroup. You might want to update // your internal references to this chat // upon switching as its ID will change. //