From 784432503e2aaf6dcd57308c3070b554addf2089 Mon Sep 17 00:00:00 2001 From: Demian Date: Thu, 16 Apr 2020 17:52:15 +0300 Subject: [PATCH] tests: implement b.Send() function test --- api_test.go | 7 ++++++- bot.go | 2 +- bot_test.go | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- poller_test.go | 1 - telebot.go | 6 ++++++ 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/api_test.go b/api_test.go index 0ee9878..e71e71c 100644 --- a/api_test.go +++ b/api_test.go @@ -44,7 +44,12 @@ func testRawServer(w http.ResponseWriter, r *http.Request) { } func TestRaw(t *testing.T) { - _, err := b.Raw("BAD METHOD", nil) + b, err := newTestBot() + if err != nil { + t.Fatal(err) + } + + _, err = b.Raw("BAD METHOD", nil) assert.EqualError(t, err, ErrNotFound.Error()) _, err = b.Raw("", &testPayload{}) diff --git a/bot.go b/bot.go index 3192112..da2b829 100644 --- a/bot.go +++ b/bot.go @@ -521,7 +521,7 @@ func (b *Bot) Send(to Recipient, what interface{}, options ...interface{}) (*Mes case Sendable: return object.Send(b, to, sendOpts) default: - return nil, errors.New("telebot: unsupported sendable") + return nil, ErrUnsupportedSendable } } diff --git a/bot_test.go b/bot_test.go index d444e58..c90243a 100644 --- a/bot_test.go +++ b/bot_test.go @@ -3,14 +3,24 @@ package telebot import ( "net/http" "os" + "strconv" "testing" "time" "github.com/stretchr/testify/assert" ) -// Cached bot instance to avoid getMe method flooding. -var b, _ = newTestBot() +const ( + photoID = "AgACAgIAAxkDAAIBV16Ybpg7l2jPgMUiiLJ3WaQOUqTrAAJorjEbh2TBSPSOinaCHfydQO_pki4AAwEAAwIAA3kAA_NQAAIYBA" +) + +var ( + // required to test send and edit methods + chatID, _ = strconv.ParseInt(os.Getenv("CHAT_ID"), 10, 64) + + b, _ = newTestBot() // cached bot instance to avoid getMe method flooding + to = &Chat{ID: chatID} // to recipient for send and edit methods +) func defaultSettings() Settings { return Settings{Token: os.Getenv("TELEBOT_SECRET")} @@ -71,8 +81,8 @@ func TestBotStart(t *testing.T) { assert.Panics(t, func() { b.Start() }) pref := defaultSettings() - pref.Poller = &LongPoller{} + b, err := NewBot(pref) assert.NoError(t, err) @@ -105,7 +115,9 @@ func TestBotStart(t *testing.T) { func TestBotIncomingUpdate(t *testing.T) { b, err := newTestBot() - assert.NoError(t, err) + if err != nil { + t.Fatal(err) + } tp := &testPoller{updates: make(chan Update, 1)} b.Poller = tp @@ -248,3 +260,33 @@ func TestBotIncomingUpdate(t *testing.T) { time.AfterFunc(100*time.Millisecond, b.Stop) b.Start() // stops after some delay } + +func TestBotSend(t *testing.T) { + if chatID == 0 { + t.Skip("CHAT_ID is required for Send method test") + } + + _, err := b.Send(to, nil) + assert.Equal(t, ErrUnsupportedSendable, err) + + _, err = b.Send(nil, t.Name()) + assert.Error(t, err) + + t.Run("what=string", func(t *testing.T) { + msg, err := b.Send(to, t.Name()) + assert.NoError(t, err) + assert.Equal(t, t.Name(), msg.Text) + }) + + t.Run("what=Sendable", func(t *testing.T) { + photo := &Photo{ + File: File{FileID: photoID}, + Caption: t.Name(), + } + + msg, err := b.Send(to, photo) + assert.NoError(t, err) + assert.NotNil(t, msg.Photo) + assert.Equal(t, photo.Caption, msg.Caption) + }) +} diff --git a/poller_test.go b/poller_test.go index ccc7a94..de0f262 100644 --- a/poller_test.go +++ b/poller_test.go @@ -5,7 +5,6 @@ type testPoller struct { } func (p *testPoller) Poll(b *Bot, updates chan Update, stop chan struct{}) { - for { select { case upd := <-p.updates: diff --git a/telebot.go b/telebot.go index 1f59031..8e91319 100644 --- a/telebot.go +++ b/telebot.go @@ -28,6 +28,12 @@ // package telebot +import "github.com/pkg/errors" + +var ( + ErrUnsupportedSendable = errors.New("telebot: unsupported sendable") +) + // These are one of the possible events Handle() can deal with. // // For convenience, all Telebot-provided endpoints start with