Skip to content

Commit

Permalink
Merge pull request #46 from LucaBernstein/increase-test-cov-fix-tests
Browse files Browse the repository at this point in the history
Fix tests and increase test coverage
  • Loading branch information
LucaBernstein authored Dec 1, 2021
2 parents 2d79ea2 + 981fd9c commit 8b70409
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@

app
beancount-bot-tg

coverage.out
6 changes: 5 additions & 1 deletion bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ func (bc *BotController) configHandleTag(m *tb.Message, params ...string) {
}
// SET tag
tag := strings.TrimPrefix(params[0], "#")
bc.Repo.UserSetTag(m, tag)
err := bc.Repo.UserSetTag(m, tag)
if err != nil {
bc.Bot.Send(m.Sender, "An error ocurred saving the tag: "+err.Error())
return
}
bc.Bot.Send(m.Sender, fmt.Sprintf("From now on all new transactions automatically get the tag #%s added (vacation mode enabled)", tag))
}

Expand Down
64 changes: 63 additions & 1 deletion bot/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestConfigCurrency(t *testing.T) {
bc := NewBotController(db)

bot := &MockBot{}
bc.ConfigureAndAttachBot(bot)
bc.AddBotAndStart(bot)

bc.commandConfig(&tb.Message{Text: "/config", Chat: chat})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Usage help for /config") {
Expand Down Expand Up @@ -74,3 +74,65 @@ func TestConfigCurrency(t *testing.T) {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}

func TestConfigTag(t *testing.T) {
// Test dependencies
chat := &tb.Chat{ID: 12345}
db, mock, err := sqlmock.New()
if err != nil {
log.Fatal(err)
}

mock. // SET
ExpectExec(`UPDATE "auth::user" SET "tag" = ?`).
WithArgs(chat.ID, "vacation2021").
WillReturnResult(sqlmock.NewResult(1, 1))
mock. // GET
ExpectQuery(`SELECT "tag" FROM "auth::user" WHERE "tgChatId" = ?`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{"tag"}).AddRow("vacation2021"))
mock. // DELETE
ExpectExec(`UPDATE "auth::user" SET "tag" = NULL WHERE "tgChatId" = ?`).
WithArgs(chat.ID).
WillReturnResult(sqlmock.NewResult(1, 1))

bc := NewBotController(db)
bot := &MockBot{}
bc.AddBotAndStart(bot)

bc.commandConfig(&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)
}

// SET tag
bc.commandConfig(&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)
}
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "get the tag #vacation2021 added") {
t.Errorf("/config tag vacation2021 response did not contain set tag: %s", bot.LastSentWhat)
}

// GET tag
bc.commandConfig(&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)
}
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "get the tag #vacation2021 added") {
t.Errorf("/config tag vacation2021 response did not contain set tag: %s", bot.LastSentWhat)
}

// DELETE tag
bc.commandConfig(&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)
}
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Disabled") {
t.Errorf("/config tag off response did not contain 'Disabled': %s", bot.LastSentWhat)
}

if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
21 changes: 13 additions & 8 deletions bot/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ type BotController struct {
CronScheduler *gocron.Scheduler
}

func (bc *BotController) ConfigureAndAttachBot(b IBot) {
func (bc *BotController) ConfigureCronScheduler() *BotController {
s := gocron.NewScheduler(time.UTC)
s.Every(1).Hour().Do(bc.cronNotifications)
bc.CronScheduler = s
return bc
}

func (bc *BotController) AddBotAndStart(b IBot) {
bc.Bot = b

mappings := bc.commandMappings()
Expand All @@ -47,13 +54,11 @@ func (bc *BotController) ConfigureAndAttachBot(b IBot) {

log.Printf("Starting bot '%s'", b.Me().Username)

// Add CRON scheduler
s := gocron.NewScheduler(time.UTC)
s.Every(1).Hour().Do(bc.cronNotifications)
bc.CronScheduler = s
s.StartAsync()
log.Print(bc.cronInfo())

if bc.CronScheduler != nil {
bc.CronScheduler.StartAsync()
} else {
log.Print("Warning: No cron scheduler has been attached!")
}
b.Start() // Blocking
}

Expand Down
4 changes: 2 additions & 2 deletions bot/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestTextHandlingWithoutPriorState(t *testing.T) {

bc := NewBotController(db)
bot := &MockBot{}
bc.ConfigureAndAttachBot(bot)
bc.AddBotAndStart(bot)

// Create simple tx and fill it completely
bc.commandCreateSimpleTx(&tb.Message{Chat: chat})
Expand Down Expand Up @@ -93,7 +93,7 @@ func TestTransactionDeletion(t *testing.T) {

bc := NewBotController(db)
bot := &MockBot{}
bc.ConfigureAndAttachBot(bot)
bc.AddBotAndStart(bot)

bc.commandDeleteTransactions(&tb.Message{Chat: chat, Text: "/deleteAll"})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "to confirm the deletion of your transactions") {
Expand Down
2 changes: 1 addition & 1 deletion bot/suggestions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestSuggestionsHandlingWithSpaces(t *testing.T) {
bc := NewBotController(db)

bot := &MockBot{}
bc.ConfigureAndAttachBot(bot)
bc.AddBotAndStart(bot)

// missing subcommand
bc.commandSuggestions(&tb.Message{Text: "/suggestions", Chat: chat})
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ func main() {
defer db.Close()

bc := bot.NewBotController(db)
bc.ConfigureCronScheduler()

bot := bot.CreateBot(bc)
bc.ConfigureAndAttachBot(bot)
bc.AddBotAndStart(bot)
}

0 comments on commit 8b70409

Please sign in to comment.