-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from LucaBernstein/no-cached-date-hints
Don't show suggestions for past dates
- Loading branch information
Showing
7 changed files
with
103 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package crud_test | ||
|
||
import ( | ||
"log" | ||
"testing" | ||
|
||
"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" | ||
) | ||
|
||
func TestCacheOnlySuggestible(t *testing.T) { | ||
// create test dependencies | ||
chat := &tb.Chat{ID: 12345} | ||
db, mock, err := sqlmock.New() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer db.Close() | ||
mock. | ||
ExpectQuery(` | ||
SELECT "type", "value" | ||
FROM "bot::cache" | ||
WHERE "tgChatId" = ?`). | ||
WithArgs(chat.ID). | ||
WillReturnRows(sqlmock.NewRows([]string{})) | ||
// Should only insert description suggestion into db cache | ||
mock. | ||
ExpectExec(`INSERT INTO "bot::cache"`). | ||
WithArgs(chat.ID, helpers.STX_DESC, "description_value"). | ||
WillReturnResult(sqlmock.NewResult(1, 1)) | ||
mock. | ||
ExpectQuery(` | ||
SELECT "type", "value" | ||
FROM "bot::cache" | ||
WHERE "tgChatId" = ?`). | ||
WithArgs(chat.ID). | ||
WillReturnRows(sqlmock.NewRows([]string{})) | ||
|
||
bc := crud.NewRepo(db) | ||
message := &tb.Message{Chat: chat} | ||
err = bc.PutCacheHints(message, map[string]string{helpers.STX_DATE: "2021-01-01", helpers.STX_AMTF: "1234", helpers.STX_DESC: "description_value"}) | ||
if err != nil { | ||
t.Errorf("PutCacheHints unexpectedly threw an error: %s", err.Error()) | ||
} | ||
|
||
if err := mock.ExpectationsWereMet(); err != nil { | ||
t.Errorf("there were unfulfilled expectations: %s", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package migrations | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
) | ||
|
||
func v3(db *sql.Tx) { | ||
v3CleanCache(db) | ||
} | ||
|
||
func v3CleanCache(db *sql.Tx) { | ||
sqlStatement := ` | ||
DELETE FROM "bot::cache" | ||
WHERE "type" NOT IN ('accTo', 'accFrom', 'txDesc') | ||
` | ||
_, err := db.Exec(sqlStatement) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package helpers | ||
|
||
const ( | ||
STX_DESC = "txDesc" | ||
STX_DATE = "txDate" | ||
STX_ACCF = "accFrom" | ||
STX_AMTF = "amountFrom" | ||
STX_ACCT = "accTo" | ||
) | ||
|
||
func AllowedSuggestionTypes() []string { | ||
return []string{ | ||
STX_ACCF, | ||
STX_ACCT, | ||
STX_DESC, | ||
} | ||
} |