Skip to content

Commit

Permalink
Update telebot framework to v3 (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaBernstein authored Sep 15, 2022
1 parent 8320b1d commit 5445159
Show file tree
Hide file tree
Showing 35 changed files with 381 additions and 289 deletions.
2 changes: 1 addition & 1 deletion ACKNOWLEDGEMENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MIT license (https://github.com/go-co-op/gocron/blob/main/LICENSE).
Telebot.
https://github.com/tucnak/telebot
Copyright (c) 2015 llya Kowalewski.
MIT license (https://github.com/tucnak/telebot/blob/v2/LICENSE).
MIT license (https://github.com/tucnak/telebot/blob/v3/LICENSE).
Cron.
https://github.com/robfig/cron.
Expand Down
2 changes: 1 addition & 1 deletion bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/LucaBernstein/beancount-bot-tg/helpers"
tb "gopkg.in/tucnak/telebot.v2"
tb "gopkg.in/telebot.v3"
)

func (bc *BotController) configHandler(m *tb.Message) {
Expand Down
34 changes: 17 additions & 17 deletions bot/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/DATA-DOG/go-sqlmock"
"github.com/LucaBernstein/beancount-bot-tg/db/crud"
"github.com/LucaBernstein/beancount-bot-tg/helpers"
tb "gopkg.in/tucnak/telebot.v2"
tb "gopkg.in/telebot.v3"
)

func TestConfigCurrency(t *testing.T) {
Expand Down Expand Up @@ -44,13 +44,13 @@ func TestConfigCurrency(t *testing.T) {
bot := &MockBot{}
bc.AddBotAndStart(bot)

bc.commandConfig(&tb.Message{Text: "/config", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config", Chat: chat}})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Usage help for /config") {
t.Errorf("/config: %s", bot.LastSentWhat)
}

// Default currency
bc.commandConfig(&tb.Message{Text: "/config currency", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config currency", Chat: chat}})
if strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Usage help") {
t.Errorf("/config currency: Unexpected usage help: %s", bot.LastSentWhat)
}
Expand All @@ -59,15 +59,15 @@ func TestConfigCurrency(t *testing.T) {
}

// Currency set in db
bc.commandConfig(&tb.Message{Text: "/config currency", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config currency", Chat: chat}})
if strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Usage help") {
t.Errorf("/config currency: Unexpected usage help: %s", bot.LastSentWhat)
}
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Your current currency is set to 'SOMEEUR") {
t.Errorf("/config currency set (2): Expected currency to be retrieved from db: %s", bot.LastSentWhat)
}

bc.commandConfig(&tb.Message{Text: "/config currency USD", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config currency USD", Chat: chat}})
if strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Usage help") {
t.Errorf("/config currency USD: Unexpected usage help: %s", bot.LastSentWhat)
}
Expand All @@ -93,7 +93,7 @@ func TestConfigTag(t *testing.T) {
bot := &MockBot{}
bc.AddBotAndStart(bot)

bc.commandConfig(&tb.Message{Text: "/config tag invalid amount of parameters", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config tag invalid amount of parameters", Chat: chat}})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Usage help for /config") {
t.Errorf("/config tag invalid amount of parameters: %s", bot.LastSentWhat)
}
Expand All @@ -105,7 +105,7 @@ func TestConfigTag(t *testing.T) {
WithArgs(12345, helpers.USERSET_TAG, "vacation2021").
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
bc.commandConfig(&tb.Message{Text: "/config tag vacation2021", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config tag vacation2021", Chat: chat}})
if strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Usage help for /config") {
t.Errorf("/config tag vacation2021: %s", bot.LastSentWhat)
}
Expand All @@ -118,7 +118,7 @@ func TestConfigTag(t *testing.T) {
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TAG).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("vacation2021"))
bc.commandConfig(&tb.Message{Text: "/config tag", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config tag", Chat: chat}})
if strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Usage help for /config") {
t.Errorf("/config tag: %s", bot.LastSentWhat)
}
Expand All @@ -130,7 +130,7 @@ func TestConfigTag(t *testing.T) {
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TAG).
WillReturnRows(sqlmock.NewRows([]string{"tag"}).AddRow(nil))
bc.commandConfig(&tb.Message{Text: "/config tag", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config tag", Chat: chat}})
if strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Usage help for /config") {
t.Errorf("/config tag: %s", bot.LastSentWhat)
}
Expand All @@ -142,7 +142,7 @@ func TestConfigTag(t *testing.T) {
mock.ExpectBegin()
mock.ExpectExec(`DELETE FROM "bot::userSetting"`).WithArgs(12345, helpers.USERSET_TAG).WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()
bc.commandConfig(&tb.Message{Text: "/config tag off", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config tag off", Chat: chat}})
if strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Usage help for /config") {
t.Errorf("/config tag off: %s", bot.LastSentWhat)
}
Expand Down Expand Up @@ -177,7 +177,7 @@ func TestConfigHandleNotification(t *testing.T) {
mock.ExpectQuery(`SELECT "delayHours", "notificationHour" FROM "bot::notificationSchedule"`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{"delayHours", "notificationHour"}))
bc.commandConfig(&tb.Message{Text: "/config notify", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config notify", Chat: chat}})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Notifications are disabled for open transactions") {
t.Errorf("Notifications should be disabled: %s", bot.LastSentWhat)
}
Expand All @@ -189,7 +189,7 @@ func TestConfigHandleNotification(t *testing.T) {
mock.ExpectQuery(`SELECT "delayHours", "notificationHour" FROM "bot::notificationSchedule"`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{"delayHours", "notificationHour"}).AddRow(24, 18))
bc.commandConfig(&tb.Message{Text: "/config notify", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config notify", Chat: chat}})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat),
"The bot will notify you daily at hour 18 ("+tz+"+3) if transactions are open for more than 1 day") {
t.Errorf("Notifications should be disabled: %s", bot.LastSentWhat)
Expand All @@ -199,7 +199,7 @@ func TestConfigHandleNotification(t *testing.T) {
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TZOFF).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("14"))
bc.commandConfig(&tb.Message{Text: "/config notify 17", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config notify 17", Chat: chat}})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "invalid parameter") {
t.Errorf("Single number as param should not be allowed: %s", bot.LastSentWhat)
}
Expand All @@ -209,7 +209,7 @@ func TestConfigHandleNotification(t *testing.T) {
WithArgs(chat.ID, helpers.USERSET_TZOFF).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("0"))
mock.ExpectExec(`DELETE FROM "bot::notificationSchedule"`).WithArgs(chat.ID).WillReturnResult(sqlmock.NewResult(1, 1))
bc.commandConfig(&tb.Message{Text: "/config notify off", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config notify off", Chat: chat}})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Successfully disabled notifications") {
t.Errorf("Single param should be allowed for 'off' to disable notifications: %s", bot.LastSentWhat)
}
Expand All @@ -228,7 +228,7 @@ func TestConfigHandleNotification(t *testing.T) {
mock.ExpectQuery(`SELECT "delayHours", "notificationHour" FROM "bot::notificationSchedule"`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{"delayHours", "notificationHour"}).AddRow(4*24, 23))
bc.commandConfig(&tb.Message{Text: "/config notify 4 23", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config notify 4 23", Chat: chat}})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat),
"The bot will notify you daily at hour 23 ("+tz+"-2) if transactions are open for more than 4 days") {
t.Errorf("Should successfully set notification: %s", bot.LastSentWhat)
Expand All @@ -239,7 +239,7 @@ func TestConfigHandleNotification(t *testing.T) {
ExpectQuery(`SELECT "value" FROM "bot::userSetting"`).
WithArgs(chat.ID, helpers.USERSET_TZOFF).
WillReturnRows(sqlmock.NewRows([]string{"value"}).AddRow("1"))
bc.commandConfig(&tb.Message{Text: "/config notify 4 24", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config notify 4 24", Chat: chat}})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat),
"invalid hour (24 is out of valid range 1-23)") {
t.Errorf("Out of bounds notification hour: %s", bot.LastSentWhat)
Expand All @@ -259,7 +259,7 @@ func TestConfigAbout(t *testing.T) {
bot := &MockBot{}
bc.AddBotAndStart(bot)

bc.commandConfig(&tb.Message{Text: "/config about", Chat: chat})
bc.commandConfig(&MockContext{M: &tb.Message{Text: "/config about", Chat: chat}})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat),
"LucaBernstein/beancount\\-bot\\-tg") {
t.Errorf("Should contain repo link: %s", bot.LastSentWhat)
Expand Down
Loading

0 comments on commit 5445159

Please sign in to comment.