Skip to content

Commit

Permalink
Allow negative values to invert transaction (#229)
Browse files Browse the repository at this point in the history
* Allow negative values to invert transaction

Solves #228

* Update Go dependencies
  • Loading branch information
LucaBernstein authored Feb 4, 2024
1 parent 9ed2412 commit c2c58bc
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 31 deletions.
4 changes: 4 additions & 0 deletions bot/botTest/mockBot.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ func (c *MockContext) Respond(resp ...*tb.CallbackResponse) error {
func (c *MockContext) Get(key string) interface{} { return nil }
func (c *MockContext) Set(key string, val interface{}) {}
func (c *MockContext) Entities() tb.Entities { return nil }
func (c *MockContext) Topic() *tb.Topic { return nil }

// Test type matching
var _ tb.Context = &MockContext{}
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
31 changes: 30 additions & 1 deletion bot/transactionBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestHandleFloat(t *testing.T) {

handledFloat, err = bot.HandleFloat(&tb.Message{Text: "-5.678"})
helpers.TestExpect(t, err, nil, "Should not throw an error for -5.678")
helpers.TestExpect(t, handledFloat, bot.FORMATTER_PLACEHOLDER+"5.678", "Should use absolute value")
helpers.TestExpect(t, handledFloat, bot.FORMATTER_PLACEHOLDER+"-5.678", "Should keep negative value")
}

func TestHandleFloatSimpleCalculations(t *testing.T) {
Expand Down 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
53 changes: 27 additions & 26 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,65 @@ go 1.17

require (
github.com/fatih/structs v1.1.0
github.com/go-co-op/gocron v1.30.1
gopkg.in/telebot.v3 v3.1.3
github.com/go-co-op/gocron v1.37.0
gopkg.in/telebot.v3 v3.2.1
)

require (
github.com/bytedance/sonic v1.10.0-rc3 // indirect
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/gin-gonic/contrib v0.0.0-20221130124618-7e01895a63f2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.1 // indirect
github.com/go-playground/validator/v10 v10.17.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.11.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.3.0 // indirect
modernc.org/cc/v3 v3.41.0 // indirect
modernc.org/ccgo/v3 v3.16.14 // indirect
modernc.org/libc v1.24.1 // indirect
modernc.org/ccgo/v3 v3.16.15 // indirect
modernc.org/libc v1.40.18 // indirect
modernc.org/mathutil v1.6.0 // indirect
modernc.org/memory v1.6.0 // indirect
modernc.org/memory v1.7.2 // indirect
modernc.org/opt v0.1.3 // indirect
modernc.org/strutil v1.1.3 // indirect
modernc.org/strutil v1.2.0 // indirect
modernc.org/token v1.1.0 // indirect
)

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/gin-contrib/cors v1.4.0
github.com/gin-contrib/cors v1.5.0
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.3.0
github.com/google/uuid v1.6.0
github.com/lib/pq v1.10.9
github.com/mandrigin/gin-spa v0.0.0-20200212133200-790d0c0c7335
github.com/stretchr/testify v1.8.4
modernc.org/sqlite v1.24.0
modernc.org/sqlite v1.28.0
)
Loading

0 comments on commit c2c58bc

Please sign in to comment.