diff --git a/api_test.go b/api_test.go index cf220b4..81895c8 100644 --- a/api_test.go +++ b/api_test.go @@ -1,7 +1,15 @@ package telebot -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/assert" +) func TestRaw(t *testing.T) { - // b := NewBot() + b, err := newTestBot() + assert.NoError(t, err) + + _, err = b.Raw("BAD METHOD", nil) + assert.EqualError(t, err, ErrNotFound.Error()) } diff --git a/bot_test.go b/bot_test.go index 6812348..2a64956 100644 --- a/bot_test.go +++ b/bot_test.go @@ -1 +1,53 @@ package telebot + +import ( + "net/http" + "os" + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func defaultSettings() Settings { + return Settings{Token: os.Getenv("TELEBOT_SECRET")} +} + +func newTestBot() (*Bot, error) { + return NewBot(defaultSettings()) +} + +func TestNewBot(t *testing.T) { + var pref Settings + _, err := NewBot(pref) + assert.Error(t, err) + + pref.Token = "BAD TOKEN" + _, err = NewBot(pref) + assert.Error(t, err) + + pref.URL = "BAD URL" + _, err = NewBot(pref) + assert.Error(t, err) + + b, err := newTestBot() + assert.NoError(t, err) + assert.NotNil(t, b.Me) + assert.Equal(t, DefaultApiURL, b.URL) + assert.Equal(t, http.DefaultClient, b.client) + assert.Equal(t, 100, cap(b.Updates)) + + pref = defaultSettings() + client := &http.Client{Timeout: time.Minute} + pref.URL = "http://api.telegram.org" // not https + pref.Client = client + pref.Poller = &LongPoller{Timeout: time.Second} + pref.Updates = 50 + + b, err = NewBot(pref) + assert.NoError(t, err) + assert.Equal(t, client, b.client) + assert.Equal(t, pref.URL, b.URL) + assert.Equal(t, pref.Poller, b.Poller) + assert.Equal(t, 50, cap(b.Updates)) +} diff --git a/go.mod b/go.mod index 4f243d9..0613748 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module gopkg.in/tucnak/telebot.v2 go 1.11 -require github.com/pkg/errors v0.8.1 +require ( + github.com/pkg/errors v0.8.1 + github.com/stretchr/testify v1.5.1 +) diff --git a/go.sum b/go.sum index f29ab35..81a2641 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,12 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/telebot_test.go b/telebot_test.go index 91d5876..6785ed6 100644 --- a/telebot_test.go +++ b/telebot_test.go @@ -1,50 +1,9 @@ package telebot import ( - "fmt" - "os" "testing" ) -func TestBot(t *testing.T) { - if testing.Short() { - t.Skip("skipping test in short mode.") - } - - token := os.Getenv("TELEBOT_SECRET") - if token == "" { - fmt.Println("ERROR: " + - "In order to test telebot functionality, you need to set up " + - "TELEBOT_SECRET environmental variable, which represents an API " + - "key to a Telegram bot.\n") - t.Fatal("Could't find TELEBOT_SECRET, aborting.") - } - - _, err := NewBot(Settings{Token: token}) - if err != nil { - t.Fatal("couldn't create bot:", err) - } -} - -func TestRecipient(t *testing.T) { - token := os.Getenv("TELEBOT_SECRET") - if token == "" { - fmt.Println("ERROR: " + - "In order to test telebot functionality, you need to set up " + - "TELEBOT_SECRET environmental variable, which represents an API " + - "key to a Telegram bot.\n") - t.Fatal("Could't find TELEBOT_SECRET, aborting.") - } - - bot, err := NewBot(Settings{Token: token}) - if err != nil { - t.Fatal("couldn't create bot:", err) - } - - bot.Send(&User{}, "") - bot.Send(&Chat{}, "") -} - func TestFile(t *testing.T) { file := FromDisk("telebot.go") @@ -58,25 +17,3 @@ func TestFile(t *testing.T) { t.Fatal("File doesn't preserve its original filename.") } } - -func TestCustomURL(t *testing.T) { - token := os.Getenv("TELEBOT_SECRET") - if token == "" { - fmt.Println("ERROR: " + - "In order to test telebot functionality, you need to set up " + - "TELEBOT_SECRET environmental variable, which represents an API " + - "key to a Telegram bot.\n") - t.Fatal("Could't find TELEBOT_SECRET, aborting.") - } - - customURL := "http://api.telegram.org" - - bot, err := NewBot(Settings{Token: token, URL: customURL}) - if err != nil { - t.Fatal("couldn't create bot:", err) - } - - if bot.URL != customURL { - t.Fatal("custom api url is not set") - } -}