Skip to content

Commit

Permalink
Allow negative values to invert transaction
Browse files Browse the repository at this point in the history
Solves #228
  • Loading branch information
LucaBernstein committed Feb 4, 2024
1 parent 9ed2412 commit c6eb586
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
4 changes: 0 additions & 4 deletions bot/transactionBuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ func HandleFloat(m *tb.Message) (string, error) {
if err != nil {
return "", fmt.Errorf("parsing failed at value '%s': %s", value, err.Error())
}
if v < 0 {
c.LogLocalf(INFO, nil, "Got negative value. Inverting.")
v *= -1
}
c.LogLocalf(TRACE, nil, "Handled float: '%s' -> %f", amount, v)
values = append(values, v)
}
Expand Down
29 changes: 29 additions & 0 deletions bot/transactionBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,35 @@ func TestTransactionBuilding(t *testing.T) {
`, "Templated string should be filled with variables as expected.")
}

func TestTransactionBuildingWithNegativeAmount(t *testing.T) {
const twoWayTemplate = `${date} * "${description}"${tag}
${account:from:the money came *from*} ${-amount}
${account:to:the money went *to*} ${amount}`

tx, err := bot.CreateSimpleTx("", twoWayTemplate)
if err != nil {
t.Errorf("Error creating simple tx: %s", err.Error())
}
tx.Input(&tb.Message{Text: "-34"}) // amount
tx.Input(&tb.Message{Text: "Birthday gift from grandma"}) // description
tx.Input(&tb.Message{Text: "Assets:Wallet"}) // from
tx.Input(&tb.Message{Text: "Income:Gifts"}) // to

if !tx.IsDone() {
t.Errorf("With given input transaction data should be complete for SimpleTx")
}

templated, err := tx.FillTemplate("USD", "", 0)
if err != nil {
t.Errorf("There should be no error raised during templating: %s", err.Error())
}
today := time.Now().Format(helpers.BEANCOUNT_DATE_FORMAT)
helpers.TestExpect(t, templated, today+` * "Birthday gift from grandma"
Assets:Wallet 34.00 USD
Income:Gifts -34.00 USD
`, "Templated string should be filled with variables as expected.")
}

func TestTransactionBuildingCustomCurrencyInAmount(t *testing.T) {
tx, err := bot.CreateSimpleTx("", bot.TEMPLATE_SIMPLE_DEFAULT)
if err != nil {
Expand Down

0 comments on commit c6eb586

Please sign in to comment.