Skip to content

Commit

Permalink
feat: add more integrations tests for ledger (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas authored and flemzord committed May 12, 2023
1 parent a945ae7 commit 8cbe59e
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 64 deletions.
28 changes: 9 additions & 19 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,6 @@ paths:
format: int64
minimum: 1
maximum: 1000
default: 15
- name: after
in: query
description: Pagination cursor, will return accounts after given address, in descending order.
schema:
type: string
example: users:003
- name: address
in: query
description: Filter accounts by address pattern (regular expression placed between ^ and $).
Expand Down Expand Up @@ -661,12 +654,16 @@ paths:
schema:
type: string
example: users:001
- name: after
- name: pageSize
in: query
description: Pagination cursor, will return accounts after given address, in descending order.
description: |
The maximum number of results to return per page.
example: 100
schema:
type: string
example: users:003
type: integer
format: int64
minimum: 1
maximum: 1000
- name: cursor
in: query
description: |
Expand Down Expand Up @@ -751,14 +748,6 @@ paths:
format: int64
minimum: 1
maximum: 1000
default: 15
- name: after
in: query
description: Pagination cursor, will return the logs after a given ID.
(in descending order).
schema:
type: string
example: "1234"
- name: startTime
in: query
description: |
Expand Down Expand Up @@ -1196,6 +1185,7 @@ components:
enum:
- NEW_TRANSACTION
- SET_METADATA
- REVERTED_TRANSACTION
data:
type: object
properties: {}
Expand Down
14 changes: 1 addition & 13 deletions pkg/api/controllers/ledger_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controllers

import (
"net/http"
"strconv"

"github.com/formancehq/ledger/pkg/api/apierrors"
"github.com/formancehq/ledger/pkg/core"
Expand Down Expand Up @@ -58,8 +57,7 @@ func GetLogs(w http.ResponseWriter, r *http.Request) {
logsQuery := storage.NewLogsQuery()

if r.URL.Query().Get(QueryKeyCursor) != "" {
if r.URL.Query().Get("after") != "" ||
r.URL.Query().Get(QueryKeyStartTime) != "" ||
if r.URL.Query().Get(QueryKeyStartTime) != "" ||
r.URL.Query().Get(QueryKeyEndTime) != "" ||
r.URL.Query().Get(QueryKeyPageSize) != "" {
apierrors.ResponseError(w, r, errorsutil.NewError(ledger.ErrValidation,
Expand All @@ -75,15 +73,6 @@ func GetLogs(w http.ResponseWriter, r *http.Request) {
}
} else {
var err error
var afterIDParsed uint64
if r.URL.Query().Get("after") != "" {
afterIDParsed, err = strconv.ParseUint(r.URL.Query().Get("after"), 10, 64)
if err != nil {
apierrors.ResponseError(w, r, errorsutil.NewError(ledger.ErrValidation,
errors.New("invalid 'after' query param")))
return
}
}

var startTimeParsed, endTimeParsed core.Time
if r.URL.Query().Get(QueryKeyStartTime) != "" {
Expand All @@ -109,7 +98,6 @@ func GetLogs(w http.ResponseWriter, r *http.Request) {
}

logsQuery = logsQuery.
WithAfterID(afterIDParsed).
WithStartTimeFilter(startTimeParsed).
WithEndTimeFilter(endTimeParsed).
WithPageSize(pageSize)
Expand Down
15 changes: 0 additions & 15 deletions pkg/api/controllers/ledger_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,6 @@ func TestGetLogs(t *testing.T) {
name: "nominal",
expectQuery: storage.NewLogsQuery(),
},
{
name: "using after",
queryParams: url.Values{
"after": []string{"10"},
},
expectQuery: storage.NewLogsQuery().WithAfterID(10),
},
{
name: "using invalid after",
queryParams: url.Values{
"after": []string{"xxx"},
},
expectStatusCode: http.StatusBadRequest,
expectedErrorCode: apierrors.ErrValidation,
},
{
name: "using start time",
queryParams: url.Values{
Expand Down
27 changes: 27 additions & 0 deletions pkg/core/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/formancehq/stack/libs/go-libs/collectionutils"
"github.com/formancehq/stack/libs/go-libs/metadata"
"github.com/pkg/errors"
)

type LogType int16
Expand All @@ -35,6 +36,32 @@ func (l LogType) String() string {
return ""
}

// Needed in order to keep the compatibility with the openapi response for
// ListLogs.
func (lt LogType) MarshalJSON() ([]byte, error) {
return json.Marshal(lt.String())
}

func (lt *LogType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}

switch s {
case "SET_METADATA":
*lt = SetMetadataLogType
case "NEW_TRANSACTION":
*lt = NewTransactionLogType
case "REVERTED_TRANSACTION":
*lt = RevertedTransactionLogType
default:
return errors.New("invalid log type")
}

return nil
}

type hashable interface {
hashString(buf *buffer)
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/storage/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
type LogsQueryFilters struct {
EndTime core.Time `json:"endTime"`
StartTime core.Time `json:"startTime"`
AfterID uint64 `json:"afterID"`
}

type LogsQuery ColumnPaginatedQuery[LogsQueryFilters]
Expand Down Expand Up @@ -49,8 +48,3 @@ func (l LogsQuery) WithEndTimeFilter(end core.Time) LogsQuery {

return l
}

func (q LogsQuery) WithAfterID(id uint64) LogsQuery {
q.Filters.AfterID = id
return q
}
4 changes: 2 additions & 2 deletions pkg/storage/sqlstorage/ledger/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func (s *Store) buildAccountsQuery(p storage.AccountsQuery) *bun.SelectQuery {
}

if p.Filters.Balance != "" {
sb.Join("JOIN " + s.schema.Table(volumesTableName)).
sb.Join("LEFT JOIN " + s.schema.Table(volumesTableName)).
JoinOn("accounts.address = volumes.account")
balanceOperation := "volumes.input - volumes.output"
balanceOperation := "coalesce(volumes.input - volumes.output, 0)"

balanceValue, err := strconv.ParseInt(p.Filters.Balance, 10, 0)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions pkg/storage/sqlstorage/ledger/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,6 @@ func (s *Store) buildLogsQuery(q storage.LogsQueryFilters) *bun.SelectQuery {
if !q.EndTime.IsZero() {
sb.Where("date < ?", q.EndTime.UTC())
}
if q.AfterID > 0 {
sb.Where("id < ?", q.AfterID)
}

return sb
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/storage/sqlstorage/ledger/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,6 @@ func TestGetLogs(t *testing.T) {
require.Equal(t, 1, cursor.PageSize)
require.Equal(t, uint64(2), cursor.Data[0].ID)

cursor, err = store.GetLogs(context.Background(), storage.NewLogsQuery().WithPageSize(1).WithAfterID(cursor.Data[0].ID))
require.NoError(t, err)
// Should get only the second log.
require.Equal(t, 1, cursor.PageSize)
require.Equal(t, uint64(1), cursor.Data[0].ID)

cursor, err = store.GetLogs(context.Background(), storage.NewLogsQuery().
WithStartTimeFilter(now.Add(-2*time.Hour)).
WithEndTimeFilter(now.Add(-1*time.Hour)).
Expand Down

0 comments on commit 8cbe59e

Please sign in to comment.