From cd87660d4aa23b414c116e68439f6c1918a0feeb Mon Sep 17 00:00:00 2001 From: Jules Dupas <106673437+jdupas22@users.noreply.github.com> Date: Tue, 3 Jan 2023 11:02:53 +0100 Subject: [PATCH] feat: add ability to filter all balances not equal to specified value (#399) --- go.mod | 0 go.sum | 0 pkg/api/controllers/account_controller_test.go | 13 +++++++++++++ pkg/api/controllers/swagger.yaml | 2 +- pkg/ledger/storage.go | 2 ++ pkg/storage/sqlstorage/accounts.go | 2 ++ 6 files changed, 18 insertions(+), 1 deletion(-) mode change 100644 => 100755 go.mod mode change 100644 => 100755 go.sum diff --git a/go.mod b/go.mod old mode 100644 new mode 100755 diff --git a/go.sum b/go.sum old mode 100644 new mode 100755 diff --git a/pkg/api/controllers/account_controller_test.go b/pkg/api/controllers/account_controller_test.go index bd0980e04..124cdabab 100644 --- a/pkg/api/controllers/account_controller_test.go +++ b/pkg/api/controllers/account_controller_test.go @@ -316,6 +316,19 @@ func TestGetAccounts(t *testing.T) { assert.Equal(t, cursor.Data[0].Address, "bob") }) + // test filter by balance != 100 + t.Run("filter by balance != 100", func(t *testing.T) { + rsp = internal.GetAccounts(api, url.Values{ + "balance": []string{"100"}, + "balance_operator": []string{"ne"}, + }) + assert.Equal(t, http.StatusOK, rsp.Result().StatusCode) + cursor := internal.DecodeCursorResponse[core.Account](t, rsp.Body) + assert.Len(t, cursor.Data, 2) + assert.Equal(t, cursor.Data[0].Address, "world") + assert.Equal(t, cursor.Data[1].Address, "alice") + }) + t.Run("invalid balance", func(t *testing.T) { rsp := internal.GetAccounts(api, url.Values{ "balance": []string{"toto"}, diff --git a/pkg/api/controllers/swagger.yaml b/pkg/api/controllers/swagger.yaml index ce641d974..b08729748 100644 --- a/pkg/api/controllers/swagger.yaml +++ b/pkg/api/controllers/swagger.yaml @@ -138,7 +138,7 @@ paths: description: Operator used for the filtering of balances can be greater than/equal, less than/equal, greater than, less than, or equal schema: type: string - enum: [gte, lte, gt, lt, e] + enum: [gte, lte, gt, lt, e, ne] example: gte - name: pagination_token in: query diff --git a/pkg/ledger/storage.go b/pkg/ledger/storage.go index aa534322c..36d648e2a 100644 --- a/pkg/ledger/storage.go +++ b/pkg/ledger/storage.go @@ -143,6 +143,7 @@ const ( BalanceOperatorGte BalanceOperator = "gte" BalanceOperatorLt BalanceOperator = "lt" BalanceOperatorLte BalanceOperator = "lte" + BalanceOperatorNe BalanceOperator = "ne" DefaultBalanceOperator = BalanceOperatorGte ) @@ -153,6 +154,7 @@ func (b BalanceOperator) IsValid() bool { BalanceOperatorGt, BalanceOperatorGte, BalanceOperatorLt, + BalanceOperatorNe, BalanceOperatorLte: return true } diff --git a/pkg/storage/sqlstorage/accounts.go b/pkg/storage/sqlstorage/accounts.go index 12adf000d..f18dfa14c 100644 --- a/pkg/storage/sqlstorage/accounts.go +++ b/pkg/storage/sqlstorage/accounts.go @@ -75,6 +75,8 @@ func (s *Store) buildAccountsQuery(p ledger.AccountsQuery) (*sqlbuilder.SelectBu sb.Where(sb.GreaterThan(balanceOperation, balanceValue)) case ledger.BalanceOperatorE: sb.Where(sb.Equal(balanceOperation, balanceValue)) + case ledger.BalanceOperatorNe: + sb.Where(sb.NotEqual(balanceOperation, balanceValue)) default: // parameter is validated in the controller for now panic("invalid balance_operator parameter")