telebot/poll_test.go

53 lines
1.3 KiB
Go
Raw Permalink Normal View History

2020-04-24 14:52:15 +00:00
package telebot
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
2020-09-26 11:29:47 +00:00
"github.com/stretchr/testify/require"
2020-04-24 14:52:15 +00:00
)
func TestPoll(t *testing.T) {
assert.True(t, (&Poll{Type: PollRegular}).IsRegular())
assert.True(t, (&Poll{Type: PollQuiz}).IsQuiz())
p := &Poll{}
opts := []PollOption{{Text: "Option 1"}, {Text: "Option 2"}}
p.AddOptions(opts[0].Text, opts[1].Text)
assert.Equal(t, opts, p.Options)
}
func TestPollSend(t *testing.T) {
2020-09-26 11:29:47 +00:00
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")
}
2020-04-26 14:05:21 +00:00
_, err := b.Send(user, &Poll{}) // empty poll
assert.Equal(t, ErrBadPollOptions, err)
poll := &Poll{
2020-04-24 14:52:15 +00:00
Type: PollQuiz,
Question: "Test Poll",
CloseUnixdate: time.Now().Unix() + 60,
Explanation: "Explanation",
}
2020-04-26 14:05:21 +00:00
poll.AddOptions("1", "2")
2020-04-24 14:52:15 +00:00
2020-04-26 14:05:21 +00:00
msg, err := b.Send(user, poll)
2020-09-26 11:29:47 +00:00
require.NoError(t, err)
2020-04-26 14:05:21 +00:00
assert.Equal(t, poll.Type, msg.Poll.Type)
assert.Equal(t, poll.Question, msg.Poll.Question)
assert.Equal(t, poll.Options, msg.Poll.Options)
assert.Equal(t, poll.CloseUnixdate, msg.Poll.CloseUnixdate)
assert.Equal(t, poll.CloseDate(), msg.Poll.CloseDate())
2020-04-24 14:52:15 +00:00
2020-04-26 14:05:21 +00:00
p, err := b.StopPoll(msg)
2020-09-26 11:29:47 +00:00
require.NoError(t, err)
2020-04-26 14:05:21 +00:00
assert.Equal(t, poll.Options, p.Options)
assert.Equal(t, 0, p.VoterCount)
2020-04-24 14:52:15 +00:00
}