Skip to content

Commit

Permalink
feat: add stable cursor (#655)
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag authored and flemzord committed Dec 4, 2023
1 parent 1eb9310 commit c7ce26b
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 43 deletions.
47 changes: 38 additions & 9 deletions internal/api/v2/controllers_accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http/httptest"
"net/url"
"testing"
"time"

"github.com/formancehq/ledger/internal/api/shared"

Expand Down Expand Up @@ -33,24 +34,37 @@ func TestGetAccounts(t *testing.T) {
expectStatusCode int
expectedErrorCode string
}
before := ledger.Now()

testCases := []testCase{
{
name: "nominal",
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{}).
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{
PITFilter: ledgerstore.PITFilter{
PIT: &before,
},
}).
WithPageSize(v2.DefaultPageSize),
},
{
name: "using metadata",
body: `{"$match": { "metadata[roles]": "admin" }}`,
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{}).
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{
PITFilter: ledgerstore.PITFilter{
PIT: &before,
},
}).
WithQueryBuilder(query.Match("metadata[roles]", "admin")).
WithPageSize(v2.DefaultPageSize),
},
{
name: "using address",
body: `{"$match": { "address": "foo" }}`,
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{}).
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{
PITFilter: ledgerstore.PITFilter{
PIT: &before,
},
}).
WithQueryBuilder(query.Match("address", "foo")).
WithPageSize(v2.DefaultPageSize),
},
Expand Down Expand Up @@ -82,13 +96,21 @@ func TestGetAccounts(t *testing.T) {
queryParams: url.Values{
"pageSize": []string{"1000000"},
},
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{}).
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{
PITFilter: ledgerstore.PITFilter{
PIT: &before,
},
}).
WithPageSize(v2.MaxPageSize),
},
{
name: "using balance filter",
body: `{"$lt": { "balance[USD/2]": 100 }}`,
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{}).
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilterWithVolumes{
PITFilter: ledgerstore.PITFilter{
PIT: &before,
},
}).
WithQueryBuilder(query.Lt("balance[USD/2]", float64(100))).
WithPageSize(v2.DefaultPageSize),
},
Expand Down Expand Up @@ -121,11 +143,14 @@ func TestGetAccounts(t *testing.T) {

router := v2.NewRouter(backend, nil, metrics.NewNoOpRegistry())

req := httptest.NewRequest(http.MethodGet, "/xxx/accounts", bytes.NewBufferString(testCase.body))
req := httptest.NewRequest(http.MethodGet, "/xxx/accounts?pit="+before.Format(time.RFC3339Nano), bytes.NewBufferString(testCase.body))
rec := httptest.NewRecorder()
params := url.Values{}
if testCase.queryParams != nil {
req.URL.RawQuery = testCase.queryParams.Encode()
params = testCase.queryParams
}
params.Set("pit", before.Format(time.RFC3339Nano))
req.URL.RawQuery = params.Encode()

router.ServeHTTP(rec, req)

Expand All @@ -152,14 +177,18 @@ func TestGetAccount(t *testing.T) {
},
}

now := ledger.Now()
query := ledgerstore.NewGetAccountQuery("foo")
query.PIT = &now

backend, mock := newTestingBackend(t, true)
mock.EXPECT().
GetAccountWithVolumes(gomock.Any(), ledgerstore.NewGetAccountQuery("foo")).
GetAccountWithVolumes(gomock.Any(), query).
Return(&account, nil)

router := v2.NewRouter(backend, nil, metrics.NewNoOpRegistry())

req := httptest.NewRequest(http.MethodGet, "/xxx/accounts/foo", nil)
req := httptest.NewRequest(http.MethodGet, "/xxx/accounts/foo?pit="+now.Format(time.RFC3339Nano), nil)
rec := httptest.NewRecorder()

router.ServeHTTP(rec, req)
Expand Down
15 changes: 11 additions & 4 deletions internal/api/v2/controllers_balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http/httptest"
"net/url"
"testing"
"time"

ledger "github.com/formancehq/ledger/internal"
v2 "github.com/formancehq/ledger/internal/api/v2"
Expand All @@ -28,15 +29,21 @@ func TestGetBalancesAggregated(t *testing.T) {
expectQuery ledgerstore.PaginatedQueryOptions[ledgerstore.PITFilter]
}

before := ledger.Now()

testCases := []testCase{
{
name: "nominal",
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilter{}),
name: "nominal",
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilter{
PIT: &before,
}),
},
{
name: "using address",
body: `{"$match": {"address": "foo"}}`,
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilter{}).
expectQuery: ledgerstore.NewPaginatedQueryOptions(ledgerstore.PITFilter{
PIT: &before,
}).
WithQueryBuilder(query.Match("address", "foo")),
},
}
Expand All @@ -54,7 +61,7 @@ func TestGetBalancesAggregated(t *testing.T) {

router := v2.NewRouter(backend, nil, metrics.NewNoOpRegistry())

req := httptest.NewRequest(http.MethodGet, "/xxx/aggregate/balances", bytes.NewBufferString(testCase.body))
req := httptest.NewRequest(http.MethodGet, "/xxx/aggregate/balances?pit="+before.Format(time.RFC3339Nano), bytes.NewBufferString(testCase.body))
rec := httptest.NewRecorder()
if testCase.queryParams != nil {
req.URL.RawQuery = testCase.queryParams.Encode()
Expand Down
Loading

0 comments on commit c7ce26b

Please sign in to comment.