From 51f36a6c8ee1814681626dd40cf6529285793128 Mon Sep 17 00:00:00 2001 From: Paul Nicolas Date: Tue, 17 Oct 2023 12:42:02 +0200 Subject: [PATCH] fix(balances): missing delimiters for regexp --- pkg/storage/sqlstorage/balances.go | 2 +- pkg/storage/sqlstorage/balances_test.go | 24 +++++++++- pkg/storage/sqlstorage/store_ledger_test.go | 49 +++++++++++++++++++++ 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/pkg/storage/sqlstorage/balances.go b/pkg/storage/sqlstorage/balances.go index 6756ac5bd..a5ac692ce 100644 --- a/pkg/storage/sqlstorage/balances.go +++ b/pkg/storage/sqlstorage/balances.go @@ -131,7 +131,7 @@ func (s *Store) GetBalances(ctx context.Context, q ledger.BalancesQuery) (api.Cu continue } - arg := sb.Args.Add(strings.ReplaceAll(segment, "\\", "\\\\")) + arg := sb.Args.Add("^" + strings.ReplaceAll(segment, "\\", "\\\\") + "$") sb.Where(fmt.Sprintf("account_json @@ ('$[%d] like_regex \"' || %s::text || '\"')::jsonpath", i, arg)) } } else { diff --git a/pkg/storage/sqlstorage/balances_test.go b/pkg/storage/sqlstorage/balances_test.go index 956cbfefa..517abc444 100644 --- a/pkg/storage/sqlstorage/balances_test.go +++ b/pkg/storage/sqlstorage/balances_test.go @@ -15,9 +15,31 @@ import ( ) func testGetBalances(t *testing.T, store *sqlstorage.Store) { - err := store.Commit(context.Background(), tx1, tx2, tx3) + err := store.Commit(context.Background(), tx1, tx2, tx3, tx4) require.NoError(t, err) + t.Run("on 1 accounts", func(t *testing.T) { + cursor, err := store.GetBalances(context.Background(), + ledger.BalancesQuery{ + Filters: ledger.BalancesQueryFilters{ + AddressRegexp: []string{"users:1"}, + }, + PageSize: 10, + }) + assert.NoError(t, err) + assert.Equal(t, 10, cursor.PageSize) + assert.Equal(t, false, cursor.HasMore) + assert.Equal(t, "", cursor.Previous) + assert.Equal(t, "", cursor.Next) + assert.Equal(t, []core.AccountsBalances{ + { + "users:1": core.AssetsBalances{ + "USD": core.NewMonetaryInt(1), + }, + }, + }, cursor.Data) + }) + t.Run("all accounts", func(t *testing.T) { cursor, err := store.GetBalances(context.Background(), ledger.BalancesQuery{ diff --git a/pkg/storage/sqlstorage/store_ledger_test.go b/pkg/storage/sqlstorage/store_ledger_test.go index e6e440d83..a01548016 100644 --- a/pkg/storage/sqlstorage/store_ledger_test.go +++ b/pkg/storage/sqlstorage/store_ledger_test.go @@ -229,6 +229,55 @@ var tx3 = core.ExpandedTransaction{ }, } +var tx4 = core.ExpandedTransaction{ + Transaction: core.Transaction{ + ID: 3, + TransactionData: core.TransactionData{ + Postings: []core.Posting{ + { + Source: "central_bank", + Destination: "users:11", + Amount: core.NewMonetaryInt(1), + Asset: "USD", + }, + }, + Reference: "tx4", + Metadata: core.Metadata{ + "priority": json.RawMessage(`"high"`), + }, + Timestamp: now.Add(-1 * time.Hour), + }, + }, + PreCommitVolumes: core.AccountsAssetsVolumes{ + "central_bank": { + "USD": { + Input: core.NewMonetaryInt(200), + Output: core.NewMonetaryInt(0), + }, + }, + "users:11": { + "USD": { + Input: core.NewMonetaryInt(0), + Output: core.NewMonetaryInt(0), + }, + }, + }, + PostCommitVolumes: core.AccountsAssetsVolumes{ + "central_bank": { + "USD": { + Input: core.NewMonetaryInt(200), + Output: core.NewMonetaryInt(1), + }, + }, + "users:11": { + "USD": { + Input: core.NewMonetaryInt(1), + Output: core.NewMonetaryInt(0), + }, + }, + }, +} + func testCommit(t *testing.T, store *sqlstorage.Store) { tx := core.ExpandedTransaction{ Transaction: core.Transaction{