Skip to content

Commit c2c58bc

Browse files
Allow negative values to invert transaction (#229)
* Allow negative values to invert transaction Solves #228 * Update Go dependencies
1 parent 9ed2412 commit c2c58bc

File tree

5 files changed

+116
-31
lines changed

5 files changed

+116
-31
lines changed

bot/botTest/mockBot.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ func (c *MockContext) Respond(resp ...*tb.CallbackResponse) error {
7676
func (c *MockContext) Get(key string) interface{} { return nil }
7777
func (c *MockContext) Set(key string, val interface{}) {}
7878
func (c *MockContext) Entities() tb.Entities { return nil }
79+
func (c *MockContext) Topic() *tb.Topic { return nil }
80+
81+
// Test type matching
82+
var _ tb.Context = &MockContext{}

bot/transactionBuilder.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ func HandleFloat(m *tb.Message) (string, error) {
6767
if err != nil {
6868
return "", fmt.Errorf("parsing failed at value '%s': %s", value, err.Error())
6969
}
70-
if v < 0 {
71-
c.LogLocalf(INFO, nil, "Got negative value. Inverting.")
72-
v *= -1
73-
}
7470
c.LogLocalf(TRACE, nil, "Handled float: '%s' -> %f", amount, v)
7571
values = append(values, v)
7672
}

bot/transactionBuilder_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestHandleFloat(t *testing.T) {
5555

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

6161
func TestHandleFloatSimpleCalculations(t *testing.T) {
@@ -193,6 +193,35 @@ func TestTransactionBuilding(t *testing.T) {
193193
`, "Templated string should be filled with variables as expected.")
194194
}
195195

196+
func TestTransactionBuildingWithNegativeAmount(t *testing.T) {
197+
const twoWayTemplate = `${date} * "${description}"${tag}
198+
${account:from:the money came *from*} ${-amount}
199+
${account:to:the money went *to*} ${amount}`
200+
201+
tx, err := bot.CreateSimpleTx("", twoWayTemplate)
202+
if err != nil {
203+
t.Errorf("Error creating simple tx: %s", err.Error())
204+
}
205+
tx.Input(&tb.Message{Text: "-34"}) // amount
206+
tx.Input(&tb.Message{Text: "Birthday gift from grandma"}) // description
207+
tx.Input(&tb.Message{Text: "Assets:Wallet"}) // from
208+
tx.Input(&tb.Message{Text: "Income:Gifts"}) // to
209+
210+
if !tx.IsDone() {
211+
t.Errorf("With given input transaction data should be complete for SimpleTx")
212+
}
213+
214+
templated, err := tx.FillTemplate("USD", "", 0)
215+
if err != nil {
216+
t.Errorf("There should be no error raised during templating: %s", err.Error())
217+
}
218+
today := time.Now().Format(helpers.BEANCOUNT_DATE_FORMAT)
219+
helpers.TestExpect(t, templated, today+` * "Birthday gift from grandma"
220+
Assets:Wallet 34.00 USD
221+
Income:Gifts -34.00 USD
222+
`, "Templated string should be filled with variables as expected.")
223+
}
224+
196225
func TestTransactionBuildingCustomCurrencyInAmount(t *testing.T) {
197226
tx, err := bot.CreateSimpleTx("", bot.TEMPLATE_SIMPLE_DEFAULT)
198227
if err != nil {

go.mod

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,65 @@ go 1.17
44

55
require (
66
github.com/fatih/structs v1.1.0
7-
github.com/go-co-op/gocron v1.30.1
8-
gopkg.in/telebot.v3 v3.1.3
7+
github.com/go-co-op/gocron v1.37.0
8+
gopkg.in/telebot.v3 v3.2.1
99
)
1010

1111
require (
12-
github.com/bytedance/sonic v1.10.0-rc3 // indirect
12+
github.com/bytedance/sonic v1.10.2 // indirect
1313
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
14-
github.com/chenzhuoyu/iasm v0.9.0 // indirect
14+
github.com/chenzhuoyu/iasm v0.9.1 // indirect
1515
github.com/davecgh/go-spew v1.1.1 // indirect
1616
github.com/dustin/go-humanize v1.0.1 // indirect
17-
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
17+
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
1818
github.com/gin-contrib/sse v0.1.0 // indirect
1919
github.com/gin-gonic/contrib v0.0.0-20221130124618-7e01895a63f2 // indirect
2020
github.com/go-playground/locales v0.14.1 // indirect
2121
github.com/go-playground/universal-translator v0.18.1 // indirect
22-
github.com/go-playground/validator/v10 v10.14.1 // indirect
22+
github.com/go-playground/validator/v10 v10.17.0 // indirect
2323
github.com/goccy/go-json v0.10.2 // indirect
2424
github.com/json-iterator/go v1.1.12 // indirect
2525
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
26-
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
27-
github.com/leodido/go-urn v1.2.4 // indirect
28-
github.com/mattn/go-isatty v0.0.19 // indirect
26+
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
27+
github.com/leodido/go-urn v1.4.0 // indirect
28+
github.com/mattn/go-isatty v0.0.20 // indirect
2929
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3030
github.com/modern-go/reflect2 v1.0.2 // indirect
31-
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
31+
github.com/ncruces/go-strftime v0.1.9 // indirect
32+
github.com/pelletier/go-toml/v2 v2.1.1 // indirect
3233
github.com/pmezard/go-difflib v1.0.0 // indirect
3334
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
3435
github.com/robfig/cron/v3 v3.0.1 // indirect
3536
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
36-
github.com/ugorji/go/codec v1.2.11 // indirect
37+
github.com/ugorji/go/codec v1.2.12 // indirect
3738
go.uber.org/atomic v1.11.0 // indirect
38-
golang.org/x/arch v0.4.0 // indirect
39-
golang.org/x/crypto v0.14.0 // indirect
40-
golang.org/x/mod v0.12.0 // indirect
41-
golang.org/x/net v0.17.0 // indirect
42-
golang.org/x/sys v0.13.0 // indirect
43-
golang.org/x/text v0.13.0 // indirect
44-
golang.org/x/tools v0.11.0 // indirect
45-
google.golang.org/protobuf v1.31.0 // indirect
39+
golang.org/x/arch v0.7.0 // indirect
40+
golang.org/x/crypto v0.18.0 // indirect
41+
golang.org/x/mod v0.14.0 // indirect
42+
golang.org/x/net v0.20.0 // indirect
43+
golang.org/x/sys v0.16.0 // indirect
44+
golang.org/x/text v0.14.0 // indirect
45+
golang.org/x/tools v0.17.0 // indirect
46+
google.golang.org/protobuf v1.32.0 // indirect
4647
gopkg.in/yaml.v3 v3.0.1 // indirect
4748
lukechampine.com/uint128 v1.3.0 // indirect
4849
modernc.org/cc/v3 v3.41.0 // indirect
49-
modernc.org/ccgo/v3 v3.16.14 // indirect
50-
modernc.org/libc v1.24.1 // indirect
50+
modernc.org/ccgo/v3 v3.16.15 // indirect
51+
modernc.org/libc v1.40.18 // indirect
5152
modernc.org/mathutil v1.6.0 // indirect
52-
modernc.org/memory v1.6.0 // indirect
53+
modernc.org/memory v1.7.2 // indirect
5354
modernc.org/opt v0.1.3 // indirect
54-
modernc.org/strutil v1.1.3 // indirect
55+
modernc.org/strutil v1.2.0 // indirect
5556
modernc.org/token v1.1.0 // indirect
5657
)
5758

5859
require (
5960
github.com/DATA-DOG/go-sqlmock v1.5.0
60-
github.com/gin-contrib/cors v1.4.0
61+
github.com/gin-contrib/cors v1.5.0
6162
github.com/gin-gonic/gin v1.9.1
62-
github.com/google/uuid v1.3.0
63+
github.com/google/uuid v1.6.0
6364
github.com/lib/pq v1.10.9
6465
github.com/mandrigin/gin-spa v0.0.0-20200212133200-790d0c0c7335
6566
github.com/stretchr/testify v1.8.4
66-
modernc.org/sqlite v1.24.0
67+
modernc.org/sqlite v1.28.0
6768
)

0 commit comments

Comments
 (0)