test: assert to require for errors

This commit is contained in:
Demian 2020-09-26 14:29:47 +03:00
parent 7a47b1a206
commit 551df7ebce
4 changed files with 39 additions and 27 deletions

View File

@ -10,6 +10,7 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
const ( const (
@ -68,7 +69,7 @@ func TestNewBot(t *testing.T) {
pref.offline = true pref.offline = true
b, err = NewBot(pref) b, err = NewBot(pref)
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, client, b.client) assert.Equal(t, client, b.client)
assert.Equal(t, pref.URL, b.URL) assert.Equal(t, pref.URL, b.URL)
assert.Equal(t, pref.Poller, b.Poller) 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 // remove webhook to be sure that bot can poll
assert.NoError(t, b.RemoveWebhook()) require.NoError(t, b.RemoveWebhook())
go b.Start() go b.Start()
b.Stop() b.Stop()
@ -127,7 +128,7 @@ func TestBotStart(t *testing.T) {
}() }()
b, err = NewBot(pref) b, err = NewBot(pref)
assert.NoError(t, err) require.NoError(t, err)
b.Poller = tp b.Poller = tp
var ok bool var ok bool
@ -384,7 +385,7 @@ func TestBot(t *testing.T) {
t.Run("Send(what=Sendable)", func(t *testing.T) { t.Run("Send(what=Sendable)", func(t *testing.T) {
msg, err = b.Send(to, photo) msg, err = b.Send(to, photo)
assert.NoError(t, err) require.NoError(t, err)
assert.NotNil(t, msg.Photo) assert.NotNil(t, msg.Photo)
assert.Equal(t, photo.Caption, msg.Caption) assert.Equal(t, photo.Caption, msg.Caption)
}) })
@ -397,7 +398,7 @@ func TestBot(t *testing.T) {
assert.Error(t, err) assert.Error(t, err)
msgs, err := b.SendAlbum(to, Album{photo, photo}) msgs, err := b.SendAlbum(to, Album{photo, photo})
assert.NoError(t, err) require.NoError(t, err)
assert.Len(t, msgs, 2) assert.Len(t, msgs, 2)
assert.NotEmpty(t, msgs[0].AlbumID) assert.NotEmpty(t, msgs[0].AlbumID)
}) })
@ -406,12 +407,12 @@ func TestBot(t *testing.T) {
b.parseMode = ModeHTML b.parseMode = ModeHTML
edited, err := b.EditCaption(msg, "<b>new caption with html</b>") 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, "new caption with html", edited.Caption)
assert.Equal(t, EntityBold, edited.CaptionEntities[0].Type) assert.Equal(t, EntityBold, edited.CaptionEntities[0].Type)
edited, err = b.EditCaption(msg, "*new caption with markdown*", ModeMarkdown) 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, "new caption with markdown", edited.Caption)
assert.Equal(t, EntityBold, edited.CaptionEntities[0].Type) 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) { t.Run("Edit(what=InputMedia)", func(t *testing.T) {
edited, err := b.Edit(msg, photo) edited, err := b.Edit(msg, photo)
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, edited.Photo.UniqueID, photo.UniqueID) assert.Equal(t, edited.Photo.UniqueID, photo.UniqueID)
}) })
t.Run("Send(what=string)", func(t *testing.T) { t.Run("Send(what=string)", func(t *testing.T) {
msg, err = b.Send(to, t.Name()) msg, err = b.Send(to, t.Name())
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, t.Name(), msg.Text) assert.Equal(t, t.Name(), msg.Text)
rpl, err := b.Reply(msg, t.Name()) rpl, err := b.Reply(msg, t.Name())
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, rpl.Text, msg.Text) assert.Equal(t, rpl.Text, msg.Text)
assert.NotNil(t, rpl.ReplyTo) assert.NotNil(t, rpl.ReplyTo)
assert.Equal(t, rpl.ReplyTo, msg) assert.Equal(t, rpl.ReplyTo, msg)
assert.True(t, rpl.IsReply()) assert.True(t, rpl.IsReply())
fwd, err := b.Forward(to, msg) fwd, err := b.Forward(to, msg)
assert.NoError(t, err) require.NoError(t, err)
assert.NotNil(t, msg, fwd) assert.NotNil(t, msg, fwd)
assert.True(t, fwd.IsForwarded()) assert.True(t, fwd.IsForwarded())
@ -448,7 +449,7 @@ func TestBot(t *testing.T) {
t.Run("Edit(what=string)", func(t *testing.T) { t.Run("Edit(what=string)", func(t *testing.T) {
msg, err = b.Edit(msg, t.Name()) msg, err = b.Edit(msg, t.Name())
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, t.Name(), msg.Text) assert.Equal(t, t.Name(), msg.Text)
_, err = b.Edit(msg, msg.Text) _, err = b.Edit(msg, msg.Text)
@ -474,11 +475,11 @@ func TestBot(t *testing.T) {
} }
edited, err := b.Edit(msg, good) edited, err := b.Edit(msg, good)
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, edited.ReplyMarkup.InlineKeyboard, good.InlineKeyboard) assert.Equal(t, edited.ReplyMarkup.InlineKeyboard, good.InlineKeyboard)
edited, err = b.EditReplyMarkup(edited, nil) edited, err = b.EditReplyMarkup(edited, nil)
assert.NoError(t, err) require.NoError(t, err)
assert.Nil(t, edited.ReplyMarkup.InlineKeyboard) assert.Nil(t, edited.ReplyMarkup.InlineKeyboard)
_, err = b.Edit(edited, bad) _, err = b.Edit(edited, bad)
@ -488,23 +489,23 @@ func TestBot(t *testing.T) {
t.Run("Edit(what=Location)", func(t *testing.T) { t.Run("Edit(what=Location)", func(t *testing.T) {
loc := &Location{Lat: 42, Lng: 69, LivePeriod: 60} loc := &Location{Lat: 42, Lng: 69, LivePeriod: 60}
edited, err := b.Send(to, loc) edited, err := b.Send(to, loc)
assert.NoError(t, err) require.NoError(t, err)
assert.NotNil(t, edited.Location) assert.NotNil(t, edited.Location)
loc = &Location{Lat: loc.Lng, Lng: loc.Lat} loc = &Location{Lat: loc.Lng, Lng: loc.Lat}
edited, err = b.Edit(edited, *loc) edited, err = b.Edit(edited, *loc)
assert.NoError(t, err) require.NoError(t, err)
assert.NotNil(t, edited.Location) assert.NotNil(t, edited.Location)
}) })
// should be the last // should be the last
t.Run("Delete()", func(t *testing.T) { 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) { t.Run("Notify()", func(t *testing.T) {
assert.Equal(t, ErrBadRecipient, b.Notify(nil, Typing)) 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) { t.Run("Answer()", func(t *testing.T) {
@ -535,10 +536,10 @@ func TestBot(t *testing.T) {
Text: "test", Text: "test",
Description: "test command", Description: "test command",
}} }}
assert.NoError(t, b.SetCommands(orig)) require.NoError(t, b.SetCommands(orig))
cmds, err := b.Commands() cmds, err := b.Commands()
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, orig, cmds) assert.Equal(t, orig, cmds)
}) })
} }

View File

@ -4,6 +4,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestBtn(t *testing.T) { func TestBtn(t *testing.T) {
@ -58,6 +59,6 @@ func TestOptions(t *testing.T) {
assert.Equal(t, o.copy(), o) assert.Equal(t, o.copy(), o)
data, err := PollQuiz.MarshalJSON() data, err := PollQuiz.MarshalJSON()
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, []byte(`{"type":"quiz"}`), data) assert.Equal(t, []byte(`{"type":"quiz"}`), data)
} }

View File

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestPoll(t *testing.T) { func TestPoll(t *testing.T) {
@ -18,8 +19,8 @@ func TestPoll(t *testing.T) {
} }
func TestPollSend(t *testing.T) { func TestPollSend(t *testing.T) {
if token == "" { if b == nil {
t.Skip("TELEBOT_SECRET is required") t.Skip("Cached bot instance is bad (probably wrong or empty TELEBOT_SECRET)")
} }
if userID == 0 { if userID == 0 {
t.Skip("USER_ID is required for Poll methods test") t.Skip("USER_ID is required for Poll methods test")
@ -37,7 +38,7 @@ func TestPollSend(t *testing.T) {
poll.AddOptions("1", "2") poll.AddOptions("1", "2")
msg, err := b.Send(user, poll) 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.Type, msg.Poll.Type)
assert.Equal(t, poll.Question, msg.Poll.Question) assert.Equal(t, poll.Question, msg.Poll.Question)
assert.Equal(t, poll.Options, msg.Poll.Options) 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()) assert.Equal(t, poll.CloseDate(), msg.Poll.CloseDate())
p, err := b.StopPoll(msg) p, err := b.StopPoll(msg)
assert.NoError(t, err) require.NoError(t, err)
assert.Equal(t, poll.Options, p.Options) assert.Equal(t, poll.Options, p.Options)
assert.Equal(t, 0, p.VoterCount) assert.Equal(t, 0, p.VoterCount)
} }

View File

@ -4,11 +4,12 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
) )
func TestExtractOk(t *testing.T) { func TestExtractOk(t *testing.T) {
data := []byte(`{"ok":true,"result":{"foo":"bar"}}`) 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}}`) data = []byte(`{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 8","parameters":{"retry_after":8}}`)
assert.Error(t, extractOk(data)) assert.Error(t, extractOk(data))
@ -24,7 +25,7 @@ func TestExtractMessage(t *testing.T) {
data = []byte(`{"ok":true,"result":{"foo":"bar"}}`) data = []byte(`{"ok":true,"result":{"foo":"bar"}}`)
_, err = extractMessage(data) _, err = extractMessage(data)
assert.NoError(t, err) require.NoError(t, err)
} }
func TestEmbedRights(t *testing.T) { func TestEmbedRights(t *testing.T) {
@ -44,6 +45,14 @@ func TestEmbedRights(t *testing.T) {
"can_send_polls": true, "can_send_polls": true,
"can_send_other_messages": true, "can_send_other_messages": true,
"can_add_web_page_previews": 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) assert.Equal(t, expected, params)
} }