Skip to content

Commit

Permalink
Fix tests, add go-sqlmock (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaBernstein committed Nov 4, 2021
1 parent 34124e8 commit a86825a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
69 changes: 43 additions & 26 deletions bot/controller_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package bot

import (
"database/sql"
"fmt"
"log"
"strings"
"testing"

tb "gopkg.in/tucnak/telebot.v2"

"github.com/DATA-DOG/go-sqlmock"
)

type MockBot struct {
Expand All @@ -23,62 +25,77 @@ func (b *MockBot) Me() *tb.User {
return &tb.User{Username: "Test bot"}
}

type MockDBFillCache struct {
}

func (db *MockDBFillCache) Ping() error { return nil }
func (db *MockDBFillCache) Close() error { return nil }
func (db *MockDBFillCache) Exec(query string, args ...interface{}) (sql.Result, error) {
return nil, nil
}
func (db *MockDBFillCache) Query(query string, args ...interface{}) (*sql.Rows, error) {
// fill cache
// return error as it does not matter, whether this function works for this test
return nil, fmt.Errorf("Testerror")
}

// GitHub-Issue #16: Panic if plain message without state arrives
func TestTextHandlingWithoutPriorState(t *testing.T) {
// create test dependencies
db := &MockDBFillCache{}
chat := &tb.Chat{ID: 12345}
db, mock, err := sqlmock.New()
if err != nil {
log.Fatal(err)
}
defer db.Close()
mock.
ExpectQuery(`SELECT "currency" FROM "auth::user" WHERE "tgChatId" = ?`).
WithArgs(chat.ID).
WillReturnRows(sqlmock.NewRows([]string{"TEST_CURRENCY"}))
mock.
ExpectExec(`INSERT INTO "bot::transaction"`).
WithArgs(chat.ID, sqlmock.AnyArg()).
WillReturnResult(sqlmock.NewResult(1, 1))

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

// Create simple tx and fill it completely
bc.commandCreateSimpleTx(&tb.Message{Chat: &tb.Chat{ID: 12345}})
bc.commandCreateSimpleTx(&tb.Message{Chat: chat})
tx := bc.State.states[12345]
tx.Input(&tb.Message{Text: "17.34"}) // amount
tx.Input(&tb.Message{Text: "Assets:Wallet"}) // from
tx.Input(&tb.Message{Text: "Expenses:Groceries"}) // to
tx.Input(&tb.Message{Text: "Buy something in the grocery store"}) // description
bc.handleTextState(&tb.Message{Chat: &tb.Chat{ID: 12345}, Text: "today"}) // date (via handleTextState)
tx.Input(&tb.Message{Text: "17.34"}) // amount
tx.Input(&tb.Message{Text: "Assets:Wallet"}) // from
tx.Input(&tb.Message{Text: "Expenses:Groceries"}) // to
tx.Input(&tb.Message{Text: "Buy something in the grocery store"}) // description
bc.handleTextState(&tb.Message{Chat: chat, Text: "today"}) // date (via handleTextState)

// After the first tx is done, send some command
m := &tb.Message{Chat: &tb.Chat{ID: 12345}}
m := &tb.Message{Chat: chat}
bc.handleTextState(m)

// should catch and send help instead of fail
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "you might need to start a transaction first") {
t.Errorf("String did not contain substring as expected (was: '%s')", bot.LastSentWhat)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}

// GitHub-Issue #16: Panic if plain message without state arrives
func TestTransactionDeletion(t *testing.T) {
// create test dependencies
db := &MockDBFillCache{}
chat := &tb.Chat{ID: 12345}
db, mock, err := sqlmock.New()
if err != nil {
log.Fatal(err)
}
mock.
ExpectExec(`DELETE FROM "bot::transaction" WHERE "tgChatId" = ?`).
WithArgs(chat.ID).
WillReturnResult(sqlmock.NewResult(1, 1))

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

bc.commandDeleteTransactions(&tb.Message{Chat: &tb.Chat{ID: 12345}, Text: "/deleteAll"})
bc.commandDeleteTransactions(&tb.Message{Chat: chat, Text: "/deleteAll"})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "to confirm the deletion of your transactions") {
t.Errorf("Deletion should require 'yes' confirmation. Got: %s", bot.LastSentWhat)
}

bc.commandDeleteTransactions(&tb.Message{Chat: &tb.Chat{ID: 12345}, Text: "/deleteAll YeS"})
bc.commandDeleteTransactions(&tb.Message{Chat: chat, Text: "/deleteAll YeS"})
if !strings.Contains(fmt.Sprintf("%v", bot.LastSentWhat), "Permanently deleted all your transactions") {
t.Errorf("Deletion should work with confirmation. Got: %s", bot.LastSentWhat)
}
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled expectations: %s", err)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require gopkg.in/tucnak/telebot.v2 v2.4.0

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/lib/pq v1.10.3
github.com/pkg/errors v0.9.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
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/lib/pq v1.10.3 h1:v9QZf2Sn6AmjXtQeFpdoq/eaNtYP6IN+7lcrygsIAtg=
Expand Down

0 comments on commit a86825a

Please sign in to comment.