Skip to content

Commit

Permalink
feat: improve generator package by allowing to pass any write action …
Browse files Browse the repository at this point in the history
…on the ledger
  • Loading branch information
gfyrag committed Nov 13, 2024
1 parent 61b2c63 commit 391d894
Show file tree
Hide file tree
Showing 66 changed files with 1,424 additions and 478 deletions.
16 changes: 13 additions & 3 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ Accept: application/json
"data": [
{
"responseType": "string",
"logID": 0,
"data": {
"insertedAt": "2019-08-24T14:15:22Z",
"timestamp": "2019-08-24T14:15:22Z",
Expand Down Expand Up @@ -3655,6 +3656,7 @@ and
"data": [
{
"responseType": "string",
"logID": 0,
"data": {
"insertedAt": "2019-08-24T14:15:22Z",
"timestamp": "2019-08-24T14:15:22Z",
Expand Down Expand Up @@ -3760,6 +3762,7 @@ and
```json
{
"responseType": "string",
"logID": 0,
"data": {
"insertedAt": "2019-08-24T14:15:22Z",
"timestamp": "2019-08-24T14:15:22Z",
Expand Down Expand Up @@ -3888,7 +3891,8 @@ xor

```json
{
"responseType": "string"
"responseType": "string",
"logID": 0
}

```
Expand All @@ -3898,6 +3902,7 @@ xor
|Name|Type|Required|Restrictions|Description|
|---|---|---|---|---|
|responseType|string|true|none|none|
|logID|integer|true|none|none|

<h2 id="tocS_V2BulkElementResultCreateTransaction">V2BulkElementResultCreateTransaction</h2>
<!-- backwards compatibility -->
Expand All @@ -3909,6 +3914,7 @@ xor
```json
{
"responseType": "string",
"logID": 0,
"data": {
"insertedAt": "2019-08-24T14:15:22Z",
"timestamp": "2019-08-24T14:15:22Z",
Expand Down Expand Up @@ -4020,7 +4026,8 @@ and

```json
{
"responseType": "string"
"responseType": "string",
"logID": 0
}

```
Expand All @@ -4039,6 +4046,7 @@ and
```json
{
"responseType": "string",
"logID": 0,
"data": {
"insertedAt": "2019-08-24T14:15:22Z",
"timestamp": "2019-08-24T14:15:22Z",
Expand Down Expand Up @@ -4150,7 +4158,8 @@ and

```json
{
"responseType": "string"
"responseType": "string",
"logID": 0
}

```
Expand All @@ -4169,6 +4178,7 @@ and
```json
{
"responseType": "string",
"logID": 0,
"errorCode": "string",
"errorDescription": "string",
"errorDetails": "string"
Expand Down
46 changes: 26 additions & 20 deletions internal/api/common/mocks_ledger_controller_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/api/v1/controllers_accounts_add_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func addAccountMetadata(w http.ResponseWriter, r *http.Request) {
return
}

err = l.SaveAccountMetadata(r.Context(), getCommandParameters(r, ledger.SaveAccountMetadata{
_, err = l.SaveAccountMetadata(r.Context(), getCommandParameters(r, ledger.SaveAccountMetadata{
Address: address,
Metadata: m,
}))
Expand Down
3 changes: 2 additions & 1 deletion internal/api/v1/controllers_accounts_add_metadata_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
ledger "github.com/formancehq/ledger/internal"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestAccountsAddMetadata(t *testing.T) {
Metadata: testCase.body.(metadata.Metadata),
},
}).
Return(nil)
Return(&ledger.Log{}, nil)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", os.Getenv("DEBUG") == "true")
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v1/controllers_accounts_delete_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func deleteAccountMetadata(w http.ResponseWriter, r *http.Request) {
return
}

if err := common.LedgerFromContext(r.Context()).
if _, err := common.LedgerFromContext(r.Context()).
DeleteAccountMetadata(
r.Context(),
getCommandParameters(r, ledger.DeleteAccountMetadata{
Expand Down
3 changes: 2 additions & 1 deletion internal/api/v1/controllers_accounts_delete_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"encoding/json"
ledger "github.com/formancehq/ledger/internal"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -70,7 +71,7 @@ func TestAccountsDeleteMetadata(t *testing.T) {
},
},
).
Return(tc.returnErr)
Return(&ledger.Log{}, tc.returnErr)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", os.Getenv("DEBUG") == "true")
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v1/controllers_transactions_add_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func addTransactionMetadata(w http.ResponseWriter, r *http.Request) {
return
}

if err := l.SaveTransactionMetadata(r.Context(), getCommandParameters(r, ledgercontroller.SaveTransactionMetadata{
if _, err := l.SaveTransactionMetadata(r.Context(), getCommandParameters(r, ledgercontroller.SaveTransactionMetadata{
TransactionID: int(txID),
Metadata: m,
})); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
ledger "github.com/formancehq/ledger/internal"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -58,7 +59,7 @@ func TestTransactionsAddMetadata(t *testing.T) {
Metadata: testCase.body.(metadata.Metadata),
},
}).
Return(nil)
Return(&ledger.Log{}, nil)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", os.Getenv("DEBUG") == "true")
Expand Down
4 changes: 2 additions & 2 deletions internal/api/v1/controllers_transactions_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
Metadata: payload.Metadata,
}

res, err := l.CreateTransaction(r.Context(), getCommandParameters(r, common.TxToScriptData(txData, false)))
_, res, err := l.CreateTransaction(r.Context(), getCommandParameters(r, common.TxToScriptData(txData, false)))
if err != nil {
switch {
case errors.Is(err, &ledgercontroller.ErrInsufficientFunds{}):
Expand Down Expand Up @@ -119,7 +119,7 @@ func createTransaction(w http.ResponseWriter, r *http.Request) {
Metadata: payload.Metadata,
}

res, err := l.CreateTransaction(r.Context(), getCommandParameters(r, runScript))
_, res, err := l.CreateTransaction(r.Context(), getCommandParameters(r, runScript))
if err != nil {
switch {
case errors.Is(err, &ledgercontroller.ErrInsufficientFunds{}):
Expand Down
25 changes: 12 additions & 13 deletions internal/api/v1/controllers_transactions_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,47 +211,46 @@ func TestTransactionsCreate(t *testing.T) {
},
}

for _, testCase := range testCases {
tc := testCase
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
if testCase.expectedStatusCode == 0 {
testCase.expectedStatusCode = http.StatusOK
if tc.expectedStatusCode == 0 {
tc.expectedStatusCode = http.StatusOK
}

expectedTx := ledger.NewTransaction().WithPostings(
ledger.NewPosting("world", "bank", "USD", big.NewInt(100)),
)

systemController, ledgerController := newTestingSystemController(t, true)
if testCase.expectedStatusCode < 300 && testCase.expectedStatusCode >= 200 {
testCase.expectedRunScript.Timestamp = time.Time{}
if tc.expectedStatusCode < 300 && tc.expectedStatusCode >= 200 {
tc.expectedRunScript.Timestamp = time.Time{}
ledgerController.EXPECT().
CreateTransaction(gomock.Any(), ledgercontroller.Parameters[ledgercontroller.RunScript]{
DryRun: tc.expectedPreview,
Input: testCase.expectedRunScript,
Input: tc.expectedRunScript,
}).
Return(&ledger.CreatedTransaction{
Return(&ledger.Log{}, &ledger.CreatedTransaction{
Transaction: expectedTx,
}, nil)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", os.Getenv("DEBUG") == "true")

req := httptest.NewRequest(http.MethodPost, "/xxx/transactions", api.Buffer(t, testCase.payload))
req := httptest.NewRequest(http.MethodPost, "/xxx/transactions", api.Buffer(t, tc.payload))
rec := httptest.NewRecorder()
req.URL.RawQuery = testCase.queryParams.Encode()
req.URL.RawQuery = tc.queryParams.Encode()

router.ServeHTTP(rec, req)

require.Equal(t, testCase.expectedStatusCode, rec.Code)
if testCase.expectedStatusCode < 300 && testCase.expectedStatusCode >= 200 {
require.Equal(t, tc.expectedStatusCode, rec.Code)
if tc.expectedStatusCode < 300 && tc.expectedStatusCode >= 200 {
tx, ok := api.DecodeSingleResponse[[]ledger.Transaction](t, rec.Body)
require.True(t, ok)
require.Equal(t, expectedTx, tx[0])
} else {
err := api.ErrorResponse{}
api.Decode(t, rec.Body, &err)
require.EqualValues(t, testCase.expectedErrorCode, err.ErrorCode)
require.EqualValues(t, tc.expectedErrorCode, err.ErrorCode)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func deleteTransactionMetadata(w http.ResponseWriter, r *http.Request) {

metadataKey := chi.URLParam(r, "key")

if err := l.DeleteTransactionMetadata(r.Context(), getCommandParameters(r, ledgercontroller.DeleteTransactionMetadata{
if _, err := l.DeleteTransactionMetadata(r.Context(), getCommandParameters(r, ledgercontroller.DeleteTransactionMetadata{
TransactionID: int(transactionID),
Key: metadataKey,
})); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package v1

import (
"encoding/json"
ledger "github.com/formancehq/ledger/internal"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestTransactionsDeleteMetadata(t *testing.T) {
Key: "foo",
},
}).
Return(tc.returnErr)
Return(&ledger.Log{}, tc.returnErr)
}

router := NewRouter(systemController, auth.NewNoAuth(), "develop", os.Getenv("DEBUG") == "true")
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v1/controllers_transactions_revert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func revertTransaction(w http.ResponseWriter, r *http.Request) {
return
}

ret, err := l.RevertTransaction(
_, ret, err := l.RevertTransaction(
r.Context(),
getCommandParameters(r, ledgercontroller.RevertTransaction{
Force: api.QueryParamBool(r, "disableChecks"),
Expand Down
2 changes: 1 addition & 1 deletion internal/api/v1/controllers_transactions_revert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestTransactionsRevert(t *testing.T) {
Force: tc.expectForce,
},
}).
Return(pointer.For(ledger.RevertedTransaction{
Return(&ledger.Log{}, pointer.For(ledger.RevertedTransaction{
RevertTransaction: tc.returnTx,
}), tc.returnErr)

Expand Down
Loading

0 comments on commit 391d894

Please sign in to comment.