Skip to content

Commit

Permalink
fix(balances): missing delimiters for regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Oct 17, 2023
1 parent 0a8144b commit 51f36a6
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pkg/storage/sqlstorage/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 23 additions & 1 deletion pkg/storage/sqlstorage/balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
49 changes: 49 additions & 0 deletions pkg/storage/sqlstorage/store_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 51f36a6

Please sign in to comment.