mirror of
https://github.com/tucnak/telebot
synced 2024-11-05 06:00:58 +00:00
test: assert to require for errors
This commit is contained in:
parent
7a47b1a206
commit
551df7ebce
41
bot_test.go
41
bot_test.go
@ -10,6 +10,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -68,7 +69,7 @@ func TestNewBot(t *testing.T) {
|
||||
pref.offline = true
|
||||
|
||||
b, err = NewBot(pref)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, client, b.client)
|
||||
assert.Equal(t, pref.URL, b.URL)
|
||||
assert.Equal(t, pref.Poller, b.Poller)
|
||||
@ -116,7 +117,7 @@ func TestBotStart(t *testing.T) {
|
||||
}
|
||||
|
||||
// remove webhook to be sure that bot can poll
|
||||
assert.NoError(t, b.RemoveWebhook())
|
||||
require.NoError(t, b.RemoveWebhook())
|
||||
|
||||
go b.Start()
|
||||
b.Stop()
|
||||
@ -127,7 +128,7 @@ func TestBotStart(t *testing.T) {
|
||||
}()
|
||||
|
||||
b, err = NewBot(pref)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
b.Poller = tp
|
||||
|
||||
var ok bool
|
||||
@ -384,7 +385,7 @@ func TestBot(t *testing.T) {
|
||||
|
||||
t.Run("Send(what=Sendable)", func(t *testing.T) {
|
||||
msg, err = b.Send(to, photo)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, msg.Photo)
|
||||
assert.Equal(t, photo.Caption, msg.Caption)
|
||||
})
|
||||
@ -397,7 +398,7 @@ func TestBot(t *testing.T) {
|
||||
assert.Error(t, err)
|
||||
|
||||
msgs, err := b.SendAlbum(to, Album{photo, photo})
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, msgs, 2)
|
||||
assert.NotEmpty(t, msgs[0].AlbumID)
|
||||
})
|
||||
@ -406,12 +407,12 @@ func TestBot(t *testing.T) {
|
||||
b.parseMode = ModeHTML
|
||||
|
||||
edited, err := b.EditCaption(msg, "<b>new caption with html</b>")
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "new caption with html", edited.Caption)
|
||||
assert.Equal(t, EntityBold, edited.CaptionEntities[0].Type)
|
||||
|
||||
edited, err = b.EditCaption(msg, "*new caption with markdown*", ModeMarkdown)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "new caption with markdown", edited.Caption)
|
||||
assert.Equal(t, EntityBold, edited.CaptionEntities[0].Type)
|
||||
|
||||
@ -420,24 +421,24 @@ func TestBot(t *testing.T) {
|
||||
|
||||
t.Run("Edit(what=InputMedia)", func(t *testing.T) {
|
||||
edited, err := b.Edit(msg, photo)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, edited.Photo.UniqueID, photo.UniqueID)
|
||||
})
|
||||
|
||||
t.Run("Send(what=string)", func(t *testing.T) {
|
||||
msg, err = b.Send(to, t.Name())
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, t.Name(), msg.Text)
|
||||
|
||||
rpl, err := b.Reply(msg, t.Name())
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, rpl.Text, msg.Text)
|
||||
assert.NotNil(t, rpl.ReplyTo)
|
||||
assert.Equal(t, rpl.ReplyTo, msg)
|
||||
assert.True(t, rpl.IsReply())
|
||||
|
||||
fwd, err := b.Forward(to, msg)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, msg, fwd)
|
||||
assert.True(t, fwd.IsForwarded())
|
||||
|
||||
@ -448,7 +449,7 @@ func TestBot(t *testing.T) {
|
||||
|
||||
t.Run("Edit(what=string)", func(t *testing.T) {
|
||||
msg, err = b.Edit(msg, t.Name())
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, t.Name(), msg.Text)
|
||||
|
||||
_, err = b.Edit(msg, msg.Text)
|
||||
@ -474,11 +475,11 @@ func TestBot(t *testing.T) {
|
||||
}
|
||||
|
||||
edited, err := b.Edit(msg, good)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, edited.ReplyMarkup.InlineKeyboard, good.InlineKeyboard)
|
||||
|
||||
edited, err = b.EditReplyMarkup(edited, nil)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Nil(t, edited.ReplyMarkup.InlineKeyboard)
|
||||
|
||||
_, err = b.Edit(edited, bad)
|
||||
@ -488,23 +489,23 @@ func TestBot(t *testing.T) {
|
||||
t.Run("Edit(what=Location)", func(t *testing.T) {
|
||||
loc := &Location{Lat: 42, Lng: 69, LivePeriod: 60}
|
||||
edited, err := b.Send(to, loc)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, edited.Location)
|
||||
|
||||
loc = &Location{Lat: loc.Lng, Lng: loc.Lat}
|
||||
edited, err = b.Edit(edited, *loc)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.NotNil(t, edited.Location)
|
||||
})
|
||||
|
||||
// should be the last
|
||||
t.Run("Delete()", func(t *testing.T) {
|
||||
assert.NoError(t, b.Delete(msg))
|
||||
require.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))
|
||||
require.NoError(t, b.Notify(to, Typing))
|
||||
})
|
||||
|
||||
t.Run("Answer()", func(t *testing.T) {
|
||||
@ -535,10 +536,10 @@ func TestBot(t *testing.T) {
|
||||
Text: "test",
|
||||
Description: "test command",
|
||||
}}
|
||||
assert.NoError(t, b.SetCommands(orig))
|
||||
require.NoError(t, b.SetCommands(orig))
|
||||
|
||||
cmds, err := b.Commands()
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, orig, cmds)
|
||||
})
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestBtn(t *testing.T) {
|
||||
@ -58,6 +59,6 @@ func TestOptions(t *testing.T) {
|
||||
assert.Equal(t, o.copy(), o)
|
||||
|
||||
data, err := PollQuiz.MarshalJSON()
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, []byte(`{"type":"quiz"}`), data)
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPoll(t *testing.T) {
|
||||
@ -18,8 +19,8 @@ func TestPoll(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestPollSend(t *testing.T) {
|
||||
if token == "" {
|
||||
t.Skip("TELEBOT_SECRET is required")
|
||||
if b == nil {
|
||||
t.Skip("Cached bot instance is bad (probably wrong or empty TELEBOT_SECRET)")
|
||||
}
|
||||
if userID == 0 {
|
||||
t.Skip("USER_ID is required for Poll methods test")
|
||||
@ -37,7 +38,7 @@ func TestPollSend(t *testing.T) {
|
||||
poll.AddOptions("1", "2")
|
||||
|
||||
msg, err := b.Send(user, poll)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, poll.Type, msg.Poll.Type)
|
||||
assert.Equal(t, poll.Question, msg.Poll.Question)
|
||||
assert.Equal(t, poll.Options, msg.Poll.Options)
|
||||
@ -45,7 +46,7 @@ func TestPollSend(t *testing.T) {
|
||||
assert.Equal(t, poll.CloseDate(), msg.Poll.CloseDate())
|
||||
|
||||
p, err := b.StopPoll(msg)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, poll.Options, p.Options)
|
||||
assert.Equal(t, 0, p.VoterCount)
|
||||
}
|
||||
|
13
util_test.go
13
util_test.go
@ -4,11 +4,12 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestExtractOk(t *testing.T) {
|
||||
data := []byte(`{"ok":true,"result":{"foo":"bar"}}`)
|
||||
assert.NoError(t, extractOk(data))
|
||||
require.NoError(t, extractOk(data))
|
||||
|
||||
data = []byte(`{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 8","parameters":{"retry_after":8}}`)
|
||||
assert.Error(t, extractOk(data))
|
||||
@ -24,7 +25,7 @@ func TestExtractMessage(t *testing.T) {
|
||||
|
||||
data = []byte(`{"ok":true,"result":{"foo":"bar"}}`)
|
||||
_, err = extractMessage(data)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestEmbedRights(t *testing.T) {
|
||||
@ -44,6 +45,14 @@ func TestEmbedRights(t *testing.T) {
|
||||
"can_send_polls": true,
|
||||
"can_send_other_messages": true,
|
||||
"can_add_web_page_previews": true,
|
||||
"can_change_info": false,
|
||||
"can_post_messages": false,
|
||||
"can_edit_messages": false,
|
||||
"can_delete_messages": false,
|
||||
"can_invite_users": false,
|
||||
"can_restrict_members": false,
|
||||
"can_pin_messages": false,
|
||||
"can_promote_members": false,
|
||||
}
|
||||
assert.Equal(t, expected, params)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user