diff --git a/tests/integration/suite/ledger-aggregated-balances.go b/tests/integration/suite/ledger-aggregated-balances.go deleted file mode 100644 index 5d654228ef..0000000000 --- a/tests/integration/suite/ledger-aggregated-balances.go +++ /dev/null @@ -1,169 +0,0 @@ -package suite - -import ( - "github.com/formancehq/go-libs/pointer" - "math/big" - "net/http" - "time" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/formancehq/stack/tests/integration/internal/modules" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - now := time.Now().UTC().Round(time.Second) - When("creating two transactions on a ledger with custom metadata", func() { - var firstTransactionsInsertedAt time.Time - BeforeEach(func() { - response, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(response.StatusCode).To(Equal(http.StatusNoContent)) - - _, err = Client().Ledger.V2.CreateBulk(TestContext(), operations.V2CreateBulkRequest{ - RequestBody: []shared.V2BulkElement{ - shared.CreateV2BulkElementCreateTransaction(shared.V2BulkElementCreateTransaction{ - Data: &shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{{ - Amount: big.NewInt(100), - Asset: "USD/2", - Destination: "bank1", - Source: "world", - }}, - Timestamp: pointer.For(now.Add(-time.Minute)), - }, - }), - shared.CreateV2BulkElementCreateTransaction(shared.V2BulkElementCreateTransaction{ - Data: &shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{{ - Amount: big.NewInt(100), - Asset: "USD/2", - Destination: "bank2", - Source: "world", - }}, - Timestamp: pointer.For(now.Add(-2 * time.Minute)), - }, - }), - shared.CreateV2BulkElementCreateTransaction(shared.V2BulkElementCreateTransaction{ - Data: &shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{{ - Amount: big.NewInt(100), - Asset: "USD/2", - Destination: "bank1", - Source: "world", - }}, - Timestamp: pointer.For(now), - }, - }), - shared.CreateV2BulkElementAddMetadata(shared.V2BulkElementAddMetadata{ - Data: &shared.V2BulkElementAddMetadataData{ - Metadata: map[string]string{ - "category": "premium", - }, - TargetID: shared.CreateV2TargetIDStr("bank2"), - TargetType: shared.V2TargetTypeAccount, - }, - }), - shared.CreateV2BulkElementAddMetadata(shared.V2BulkElementAddMetadata{ - Data: &shared.V2BulkElementAddMetadataData{ - Metadata: map[string]string{ - "category": "premium", - }, - TargetID: shared.CreateV2TargetIDStr("bank1"), - TargetType: shared.V2TargetTypeAccount, - }, - }), - }, - Ledger: "default", - }) - Expect(err).To(Succeed()) - - firstTransactionsInsertedAt = time.Now() - <-time.After(time.Second) - - _, err = Client().Ledger.V2.CreateBulk(TestContext(), operations.V2CreateBulkRequest{ - RequestBody: []shared.V2BulkElement{ - shared.CreateV2BulkElementCreateTransaction(shared.V2BulkElementCreateTransaction{ - Data: &shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{{ - Amount: big.NewInt(100), - Asset: "USD/2", - Destination: "bank1", - Source: "world", - }}, - Timestamp: pointer.For(now), - }, - }), - }, - Ledger: "default", - }) - Expect(err).To(Succeed()) - }) - It("should be ok when aggregating using the metadata", func() { - response, err := Client().Ledger.V2.GetBalancesAggregated( - TestContext(), - operations.V2GetBalancesAggregatedRequest{ - RequestBody: map[string]any{ - "$match": map[string]any{ - "metadata[category]": "premium", - }, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - Expect(response.V2AggregateBalancesResponse.Data).To(HaveLen(1)) - Expect(response.V2AggregateBalancesResponse.Data["USD/2"]).To(Equal(big.NewInt(400))) - }) - It("should be ok when aggregating using pit on effective date", func() { - response, err := Client().Ledger.V2.GetBalancesAggregated( - TestContext(), - operations.V2GetBalancesAggregatedRequest{ - Ledger: "default", - Pit: pointer.For(now.Add(-time.Minute)), - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "address": "bank1", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - Expect(response.V2AggregateBalancesResponse.Data).To(HaveLen(1)) - Expect(response.V2AggregateBalancesResponse.Data["USD/2"]).To(Equal(big.NewInt(100))) - }) - It("should be ok when aggregating using pit on insertion date", func() { - response, err := Client().Ledger.V2.GetBalancesAggregated( - TestContext(), - operations.V2GetBalancesAggregatedRequest{ - Ledger: "default", - Pit: pointer.For(firstTransactionsInsertedAt), - UseInsertionDate: pointer.For(true), - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "address": "bank1", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - Expect(response.V2AggregateBalancesResponse.Data).To(HaveLen(1)) - Expect(response.V2AggregateBalancesResponse.Data["USD/2"]).To(Equal(big.NewInt(200))) - }) - }) -}) diff --git a/tests/integration/suite/ledger-create-bulk.go b/tests/integration/suite/ledger-create-bulk.go deleted file mode 100644 index 6537f7d301..0000000000 --- a/tests/integration/suite/ledger-create-bulk.go +++ /dev/null @@ -1,136 +0,0 @@ -package suite - -import ( - "math/big" - "net/http" - "time" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - "github.com/formancehq/go-libs/metadata" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/formancehq/stack/tests/integration/internal/modules" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(response.StatusCode).To(Equal(http.StatusNoContent)) - }) - When("creating a bulk on a ledger", func() { - var ( - now = time.Now().Round(time.Microsecond).UTC() - ) - BeforeEach(func() { - _, err := Client().Ledger.V2.CreateBulk(TestContext(), operations.V2CreateBulkRequest{ - RequestBody: []shared.V2BulkElement{ - shared.CreateV2BulkElementCreateTransaction(shared.V2BulkElementCreateTransaction{ - Data: &shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{{ - Amount: big.NewInt(100), - Asset: "USD/2", - Destination: "bank", - Source: "world", - }}, - Timestamp: &now, - }, - }), - shared.CreateV2BulkElementAddMetadata(shared.V2BulkElementAddMetadata{ - Data: &shared.V2BulkElementAddMetadataData{ - Metadata: metadata.Metadata{ - "foo": "bar", - "role": "admin", - }, - TargetID: shared.CreateV2TargetIDBigint(big.NewInt(0)), - TargetType: shared.V2TargetTypeTransaction, - }, - }), - shared.CreateV2BulkElementDeleteMetadata(shared.V2BulkElementDeleteMetadata{ - Data: &shared.V2BulkElementDeleteMetadataData{ - Key: "foo", - TargetID: shared.CreateV2TargetIDBigint(big.NewInt(0)), - TargetType: shared.V2TargetTypeTransaction, - }, - }), - shared.CreateV2BulkElementRevertTransaction(shared.V2BulkElementRevertTransaction{ - Data: &shared.V2BulkElementRevertTransactionData{ - ID: big.NewInt(0), - }, - }), - }, - Ledger: "default", - }) - Expect(err).To(Succeed()) - }) - It("should be ok", func() { - tx, err := Client().Ledger.V2.GetTransaction(TestContext(), operations.V2GetTransactionRequest{ - ID: big.NewInt(0), - Ledger: "default", - }) - Expect(err).To(Succeed()) - Expect(tx.V2GetTransactionResponse.Data).To(Equal(shared.V2ExpandedTransaction{ - ID: big.NewInt(0), - Metadata: metadata.Metadata{ - "role": "admin", - }, - Postings: []shared.V2Posting{{ - Amount: big.NewInt(100), - Asset: "USD/2", - Destination: "bank", - Source: "world", - }}, - Reverted: true, - Timestamp: now, - })) - }) - }) - When("creating a bulk with an error on a ledger", func() { - var ( - now = time.Now().Round(time.Microsecond).UTC() - err error - bulkResponse *operations.V2CreateBulkResponse - ) - BeforeEach(func() { - bulkResponse, err = Client().Ledger.V2.CreateBulk(TestContext(), operations.V2CreateBulkRequest{ - RequestBody: []shared.V2BulkElement{ - shared.CreateV2BulkElementCreateTransaction(shared.V2BulkElementCreateTransaction{ - Data: &shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{{ - Amount: big.NewInt(100), - Asset: "USD/2", - Destination: "bank", - Source: "world", - }}, - Timestamp: &now, - }, - }), - shared.CreateV2BulkElementCreateTransaction(shared.V2BulkElementCreateTransaction{ - Data: &shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{{ - Amount: big.NewInt(200), // Insufficient fund - Asset: "USD/2", - Destination: "user", - Source: "bank", - }}, - Timestamp: &now, - }, - }), - }, - Ledger: "default", - }) - Expect(err).To(Succeed()) - }) - It("should respond with an error", func() { - Expect(bulkResponse.V2BulkResponse.Data[1].Type).To(Equal(shared.V2BulkElementResultType("ERROR"))) - Expect(bulkResponse.V2BulkResponse.Data[1].V2BulkElementResultErrorSchemas.ErrorCode).To(Equal("INSUFFICIENT_FUND")) - }) - }) -}) diff --git a/tests/integration/suite/ledger-create-transaction.go b/tests/integration/suite/ledger-create-transaction.go deleted file mode 100644 index ea84f20ba0..0000000000 --- a/tests/integration/suite/ledger-create-transaction.go +++ /dev/null @@ -1,646 +0,0 @@ -package suite - -import ( - "math/big" - "net/http" - "time" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/sdkerrors" - "github.com/formancehq/stack/tests/integration/internal/modules" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - "github.com/formancehq/go-libs/metadata" - "github.com/formancehq/go-libs/pointer" - ledgerevents "github.com/formancehq/ledger/pkg/events" - "github.com/formancehq/stack/libs/events" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/nats-io/nats.go" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = WithModules([]*Module{modules.Search, modules.Ledger}, func() { - BeforeEach(func() { - createLedgerResponse, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - - createLedgerResponse, err = Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "test", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - }) - When("creating a transaction on a ledger", func() { - var ( - msgs chan *nats.Msg - cancelSubscription func() - timestamp = time.Now().Round(time.Second).UTC() - rsp *shared.V2CreateTransactionResponse - ) - BeforeEach(func() { - - // Subscribe to nats subject - cancelSubscription, msgs = SubscribeLedger() - - // Create a transaction - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "alice", - }, - }, - Timestamp: ×tamp, - Reference: pointer.For("foo"), - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2CreateTransactionResponse - }) - AfterEach(func() { - cancelSubscription() - }) - It("should be available on api", func() { - response, err := Client().Ledger.V2.GetTransaction( - TestContext(), - operations.V2GetTransactionRequest{ - Ledger: "default", - ID: rsp.Data.ID, - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - transactionResponse := response.V2GetTransactionResponse - Expect(transactionResponse.Data).To(Equal(shared.V2ExpandedTransaction{ - Timestamp: rsp.Data.Timestamp, - Postings: rsp.Data.Postings, - Reference: rsp.Data.Reference, - Metadata: rsp.Data.Metadata, - ID: rsp.Data.ID, - PreCommitVolumes: map[string]map[string]shared.V2Volume{ - "world": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(0), - Balance: big.NewInt(0), - }, - }, - "alice": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(0), - Balance: big.NewInt(0), - }, - }, - }, - PostCommitVolumes: map[string]map[string]shared.V2Volume{ - "world": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(100), - Balance: big.NewInt(-100), - }, - }, - "alice": { - "USD": { - Input: big.NewInt(100), - Output: big.NewInt(0), - Balance: big.NewInt(100), - }, - }, - }, - })) - - accResponse, err := Client().Ledger.V2.GetAccount( - TestContext(), - operations.V2GetAccountRequest{ - Address: "alice", - Ledger: "default", - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(accResponse.StatusCode).To(Equal(200)) - - accountResponse := accResponse.V2AccountResponse - Expect(accountResponse.Data).Should(Equal(shared.V2Account{ - Address: "alice", - Metadata: metadata.Metadata{}, - Volumes: map[string]shared.V2Volume{ - "USD": { - Input: big.NewInt(100), - Output: big.NewInt(0), - Balance: big.NewInt(100), - }, - }, - })) - }) - Then("trying to commit a new transaction with the same reference", func() { - var ( - err error - ) - BeforeEach(func() { - // Create a transaction - _, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "alice", - }, - }, - Timestamp: ×tamp, - Reference: pointer.For("foo"), - }, - Ledger: "default", - }, - ) - Expect(err).To(HaveOccurred()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumConflict)) - }) - It("Should fail with "+string(shared.V2ErrorsEnumConflict)+" error code", func() {}) - }) - It("should trigger a new event", func() { - // Wait for created transaction event - msg := WaitOnChanWithTimeout(msgs, 5*time.Second) - Expect(events.Check(msg.Data, "ledger", ledgerevents.EventTypeCommittedTransactions)).Should(Succeed()) - }) - It("should pop a transaction, two accounts and two assets entries on search service", func() { - expectedTx := map[string]any{ - "metadata": map[string]any{}, - "reference": "foo", - "postings": []any{ - map[string]any{ - "source": "world", - "asset": "USD", - "amount": float64(100), - "destination": "alice", - }, - }, - "txid": float64(0), - "timestamp": timestamp.Format(time.RFC3339), - "ledger": "default", - } - Eventually(func(g Gomega) bool { - response, err := Client().Search.V1.Search( - TestContext(), - shared.Query{ - Target: ptr("TRANSACTION"), - }, - ) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(response.StatusCode).To(Equal(200)) - - res := response.Response - g.Expect(res.Cursor.Data).To(HaveLen(1)) - g.Expect(res.Cursor.Data[0]).To(Equal(expectedTx)) - - return true - }).Should(BeTrue()) - - Eventually(func(g Gomega) []map[string]any { - response, err := Client().Search.V1.Search( - TestContext(), - shared.Query{ - Target: ptr("TRANSACTION"), - Terms: []string{"alice"}, - }, - ) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(response.StatusCode).To(Equal(200)) - - res := response.Response - g.Expect(res.Cursor.Data[0]).To(Equal(expectedTx)) - return res.Cursor.Data - }).Should(HaveLen(1)) - - Eventually(func(g Gomega) bool { - response, err := Client().Search.V1.Search( - TestContext(), - shared.Query{ - Target: ptr("ACCOUNT"), - }, - ) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(response.StatusCode).To(Equal(200)) - - res := response.Response - g.Expect(res.Cursor.Data).To(HaveLen(2)) - g.Expect(res.Cursor.Data).To(ContainElements( - map[string]any{ - "address": "world", - "ledger": "default", - "metadata": map[string]any{}, - }, - map[string]any{ - "address": "alice", - "ledger": "default", - "metadata": map[string]any{}, - }, - )) - return true - }).Should(BeTrue()) - }) - }) - - When("creating a transaction on a ledger with insufficient funds", func() { - It("should fail", func() { - _, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "bob", - Destination: "alice", - }, - }, - }, - Ledger: "default", - }, - ) - Expect(err).To(HaveOccurred()) - - Expect(err).Should(Equal(&sdkerrors.V2ErrorResponse{ - ErrorCode: shared.V2ErrorsEnumInsufficientFund, - ErrorMessage: "running numscript: script execution failed: account(s) @bob had/have insufficient funds", - })) - }) - }) - - When("creating a transaction on a ledger with an idempotency key and a specific ledger", func() { - var ( - err error - response *operations.V2CreateTransactionResponse - timestamp = time.Now().Add(-1 * time.Minute).Round(time.Second).UTC() - timestamp2 = time.Now().Round(time.Second).UTC() - ) - BeforeEach(func() { - // Create a transaction - response, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - IdempotencyKey: pointer.For("foo"), - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "alice", - }, - }, - Timestamp: ×tamp, - Reference: pointer.For("foo"), - }, - Ledger: "default", - }, - ) - }) - It("should be ok", func() { - Expect(err).To(Succeed()) - Expect(response.V2CreateTransactionResponse.Data.ID).To(Equal(big.NewInt(0))) - }) - Then("creating a ledger transaction with same ik and different ledger", func() { - BeforeEach(func() { - response, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - IdempotencyKey: pointer.For("foo"), - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "alice", - }, - }, - Timestamp: ×tamp2, - Reference: pointer.For("foo2"), - }, - Ledger: "test", - }, - ) - }) - It("should not have an error", func() { - Expect(err).To(Succeed()) - Expect(response.V2CreateTransactionResponse.Data.ID).To(Equal(big.NewInt(0))) - }) - }) - }) - - When("creating a transaction on a ledger with an idempotency key", func() { - var ( - err error - response *operations.V2CreateTransactionResponse - ) - createTransaction := func() { - response, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - IdempotencyKey: ptr("testing"), - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "alice", - }, - }, - }, - Ledger: "default", - }, - ) - } - BeforeEach(createTransaction) - It("should be ok", func() { - Expect(err).To(Succeed()) - Expect(response.V2CreateTransactionResponse.Data.ID).To(Equal(big.NewInt(0))) - }) - Then("replaying with the same IK", func() { - BeforeEach(createTransaction) - It("should respond with the same tx id", func() { - Expect(err).To(Succeed()) - Expect(response.V2CreateTransactionResponse.Data.ID).To(Equal(big.NewInt(0))) - }) - }) - }) - // TODO(gfyrag): test negative amount with a variable - When("creating a transaction on a ledger with a negative amount in the script", func() { - var ( - err error - ) - BeforeEach(func() { - _, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - IdempotencyKey: ptr("testing"), - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Script: &shared.V2PostTransactionScript{ - Plain: `send [COIN -100] ( - source = @world - destination = @bob - )`, - Vars: map[string]interface{}{}, - }, - }, - Ledger: "default", - }, - ) - }) - It("should fail with "+string(shared.V2ErrorsEnumCompilationFailed)+" code", func() { - Expect(err).NotTo(Succeed()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumCompilationFailed)) - Expect(err.(*sdkerrors.V2ErrorResponse).Details).To(Equal(pointer.For("https://play.numscript.org/?payload=eyJlcnJvciI6Ilx1MDAxYlszMW0tLVx1MDAzZVx1MDAxYlswbSBlcnJvcjoxOjE1XHJcbiAgXHUwMDFiWzM0bXxcdTAwMWJbMG1cclxuXHUwMDFiWzMxbTEgfCBcdTAwMWJbMG1cdTAwMWJbOTBtc2VuZCBbQ09JTiAtMTAwXHUwMDFiWzBtXVx1MDAxYls5MG0gKFxyXG5cdTAwMWJbMG0gIFx1MDAxYlszNG18XHUwMDFiWzBtICAgICAgICAgICAgICAgIFx1MDAxYlszMW1eXHUwMDFiWzBtIG5vIHZpYWJsZSBhbHRlcm5hdGl2ZSBhdCBpbnB1dCAnW0NPSU4tMTAwXSdcclxuIn0="))) - }) - }) - When("creating a transaction on a ledger with a negative amount in the script", func() { - var ( - err error - ) - BeforeEach(func() { - _, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - IdempotencyKey: ptr("testing"), - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Script: &shared.V2PostTransactionScript{ - Plain: `vars { - monetary $amount - } - send $amount ( - source = @world - destination = @bob - )`, - Vars: map[string]interface{}{ - "amount": "USD -100", - }, - }, - }, - Ledger: "default", - }, - ) - }) - It("should fail with "+string(shared.V2ErrorsEnumCompilationFailed)+" code", func() { - Expect(err).NotTo(Succeed()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumCompilationFailed)) - Expect(err.(*sdkerrors.V2ErrorResponse).Details).To(Equal(pointer.For("https://play.numscript.org/?payload=eyJlcnJvciI6ImludmFsaWQgSlNPTiB2YWx1ZSBmb3IgdmFyaWFibGUgJGFtb3VudCBvZiB0eXBlIG1vbmV0YXJ5OiB2YWx1ZSBbVVNEIC0xMDBdOiBuZWdhdGl2ZSBhbW91bnQifQ=="))) - }) - }) - When("creating a transaction on the ledger v1 with old variable format", func() { - var ( - err error - response *operations.CreateTransactionResponse - ) - BeforeEach(func() { - v, _ := big.NewInt(0).SetString("1320000000000000000000000000000000000000000000000001", 10) - response, err = Client().Ledger.V1.CreateTransaction( - TestContext(), - operations.CreateTransactionRequest{ - PostTransaction: shared.PostTransaction{ - Metadata: map[string]any{}, - Script: &shared.PostTransactionScript{ - Plain: `vars { - monetary $amount - } - send $amount ( - source = @world - destination = @bob - )`, - Vars: map[string]interface{}{ - "amount": map[string]any{ - "asset": "EUR/12", - "amount": v, - }, - }, - }, - }, - Ledger: "default", - }, - ) - }) - It("should be ok", func() { - Expect(err).To(Succeed()) - Expect(response.TransactionsResponse.Data[0].Txid).To(Equal(big.NewInt(0))) - }) - }) - When("creating a transaction on a ledger with error on script", func() { - var ( - err error - ) - BeforeEach(func() { - _, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - IdempotencyKey: ptr("testing"), - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Script: &shared.V2PostTransactionScript{ - Plain: `XXX`, - Vars: map[string]interface{}{}, - }, - }, - Ledger: "default", - }, - ) - }) - It("should fail with "+string(shared.V2ErrorsEnumCompilationFailed)+" code", func() { - Expect(err).NotTo(Succeed()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumCompilationFailed)) - Expect(err.(*sdkerrors.V2ErrorResponse).Details).To(Equal(pointer.For("https://play.numscript.org/?payload=eyJlcnJvciI6Ilx1MDAxYlszMW0tLVx1MDAzZVx1MDAxYlswbSBlcnJvcjoxOjBcclxuICBcdTAwMWJbMzRtfFx1MDAxYlswbVxyXG5cdTAwMWJbMzFtMSB8IFx1MDAxYlswbVx1MDAxYls5MG1cdTAwMWJbMG1YWFhcdTAwMWJbOTBtXHJcblx1MDAxYlswbSAgXHUwMDFiWzM0bXxcdTAwMWJbMG0gXHUwMDFiWzMxbV5eXHUwMDFiWzBtIG1pc21hdGNoZWQgaW5wdXQgJ1hYWCcgZXhwZWN0aW5nIHtORVdMSU5FLCAndmFycycsICdzZXRfdHhfbWV0YScsICdzZXRfYWNjb3VudF9tZXRhJywgJ3ByaW50JywgJ2ZhaWwnLCAnc2VuZCcsICdzYXZlJ31cclxuIn0="))) - }) - }) - When("creating a transaction with no postings", func() { - var ( - err error - ) - BeforeEach(func() { - _, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - IdempotencyKey: ptr("testing"), - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Script: &shared.V2PostTransactionScript{ - Plain: `vars { - monetary $amount - } - set_tx_meta("foo", "bar") - `, - Vars: map[string]interface{}{ - "amount": "USD 100", - }, - }, - }, - Ledger: "default", - }, - ) - }) - It("should fail with "+string(shared.V2ErrorsEnumNoPostings)+" code", func() { - Expect(err).NotTo(Succeed()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumNoPostings)) - }) - }) - When("creating a transaction with metadata override", func() { - var ( - err error - ) - BeforeEach(func() { - _, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - IdempotencyKey: ptr("testing"), - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{ - "foo": "baz", - }, - Script: &shared.V2PostTransactionScript{ - Plain: `send [COIN 100] ( - source = @world - destination = @bob - ) - set_tx_meta("foo", "bar")`, - Vars: map[string]interface{}{}, - }, - }, - Ledger: "default", - }, - ) - }) - It("should fail with "+string(shared.V2ErrorsEnumMetadataOverride)+" code", func() { - Expect(err).NotTo(Succeed()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumMetadataOverride)) - }) - }) - When("creating a tx with dry run mode", func() { - var ( - err error - ret *operations.V2CreateTransactionResponse - ) - BeforeEach(func() { - ret, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - IdempotencyKey: ptr("testing"), - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Script: &shared.V2PostTransactionScript{ - Plain: `send [COIN 100] ( - source = @world - destination = @bob - )`, - Vars: map[string]interface{}{}, - }, - }, - DryRun: pointer.For(true), - Ledger: "default", - }, - ) - }) - It("should be ok", func() { - Expect(err).To(BeNil()) - }) - Then("creating a tx without dry run", func() { - var ( - txResponse *operations.V2CreateTransactionResponse - ) - BeforeEach(func() { - txResponse, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - IdempotencyKey: ptr("testing"), - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Script: &shared.V2PostTransactionScript{ - Plain: `send [COIN 100] ( - source = @world - destination = @bob - )`, - Vars: map[string]interface{}{}, - }, - }, - Ledger: "default", - }, - ) - }) - It("Should return the same tx id as with dry run", func() { - Expect(txResponse.V2CreateTransactionResponse.Data.ID.Uint64()).To(Equal(ret.V2CreateTransactionResponse.Data.ID.Uint64())) - }) - }) - }) -}) - -type GenericOpenAPIError interface { - Model() any -} diff --git a/tests/integration/suite/ledger-create.go b/tests/integration/suite/ledger-create.go deleted file mode 100644 index 733724a6c2..0000000000 --- a/tests/integration/suite/ledger-create.go +++ /dev/null @@ -1,67 +0,0 @@ -package suite - -import ( - "net/http" - "strings" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/sdkerrors" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - "github.com/formancehq/go-libs/pointer" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/formancehq/stack/tests/integration/internal/modules" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - When("creating a bulk on a ledger", func() { - var m map[string]string - JustBeforeEach(func() { - response, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - V2CreateLedgerRequest: &shared.V2CreateLedgerRequest{ - Metadata: m, - }, - }) - Expect(err).To(BeNil()) - Expect(response.StatusCode).To(Equal(http.StatusNoContent)) - }) - It("Should be ok", func() {}) - Then("trying to create another ledger with the same name", func() { - JustBeforeEach(func() { - _, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).NotTo(BeNil()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumValidation)) - }) - It("should fail", func() {}) - }) - Context("With metadata", func() { - BeforeEach(func() { - m = map[string]string{ - "foo": "bar", - } - }) - It("Should be ok", func() { - ledger, err := Client().Ledger.V2.GetLedger(TestContext(), operations.V2GetLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(ledger.V2GetLedgerResponse.Data.Metadata).To(Equal(m)) - }) - }) - }) - When("bucket naming convention depends on the database 63 bytes length (pg constraint)", func() { - It("should fail with > 63 characters in ledger or bucket name", func() { - _, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - V2CreateLedgerRequest: &shared.V2CreateLedgerRequest{ - Bucket: pointer.For(strings.Repeat("a", 64)), - }, - Ledger: "default", - }) - Expect(err).To(HaveOccurred()) - }) - }) -}) diff --git a/tests/integration/suite/ledger-delete-metadata.go b/tests/integration/suite/ledger-delete-metadata.go deleted file mode 100644 index c36e9b0cbb..0000000000 --- a/tests/integration/suite/ledger-delete-metadata.go +++ /dev/null @@ -1,44 +0,0 @@ -package suite - -import ( - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/formancehq/stack/tests/integration/internal/modules" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "net/http" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - When("creating a ledger with some metadata", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - V2CreateLedgerRequest: &shared.V2CreateLedgerRequest{ - Metadata: map[string]string{ - "foo": "bar", - }, - }, - }) - Expect(err).To(BeNil()) - Expect(response.StatusCode).To(Equal(http.StatusNoContent)) - }) - Then("deleting metadata", func() { - BeforeEach(func() { - _, err := Client().Ledger.V2.DeleteLedgerMetadata(TestContext(), operations.V2DeleteLedgerMetadataRequest{ - Ledger: "default", - Key: "foo", - }) - Expect(err).To(BeNil()) - }) - It("should be ok", func() { - ledger, err := Client().Ledger.V2.GetLedger(TestContext(), operations.V2GetLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(ledger.V2GetLedgerResponse.Data.Metadata).To(BeEmpty()) - }) - }) - }) -}) diff --git a/tests/integration/suite/ledger-import-export.go b/tests/integration/suite/ledger-import-export.go deleted file mode 100644 index ceff94b92a..0000000000 --- a/tests/integration/suite/ledger-import-export.go +++ /dev/null @@ -1,101 +0,0 @@ -package suite - -import ( - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - "github.com/formancehq/go-libs/pointer" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/formancehq/stack/tests/integration/internal/modules" - "github.com/google/uuid" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "io" - "math/big" - "net/http" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - const logsToWrite = 5 - When("creating a ledger with a bunch of transactions", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(response.StatusCode).To(Equal(http.StatusNoContent)) - - for i := 0; i < logsToWrite; i++ { - _, err := Client().Ledger.V2.CreateTransaction(TestContext(), operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Postings: []shared.V2Posting{{ - Amount: big.NewInt(100), - Asset: "USD/2", - Destination: "bank", - Source: "world", - }}, - Metadata: map[string]string{}, - }, - Ledger: "default", - }) - Expect(err).To(BeNil()) - } - }) - Then("exporting the ledger", func() { - var ( - data []byte - ) - BeforeEach(func() { - ret, err := Client().Ledger.V2.ExportLogs(TestContext(), operations.V2ExportLogsRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - data, err = io.ReadAll(ret.RawResponse.Body) - Expect(err).To(BeNil()) - }) - It("Should be ok", func() { - Expect(len(data)).NotTo(Equal(0)) - }) - Then("Import the ledger under a new name", func() { - var ( - newLedger string - ) - BeforeEach(func() { - newLedger = uuid.NewString() - _, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: newLedger, - V2CreateLedgerRequest: &shared.V2CreateLedgerRequest{}, - }) - Expect(err).To(BeNil()) - - _, err = Client().Ledger.V2.ImportLogs(TestContext(), operations.V2ImportLogsRequest{ - RequestBody: pointer.For(string(data)), - Ledger: newLedger, - }) - Expect(err).To(BeNil()) - }) - It("Should be ok", func() { - logs, err := Client().Ledger.V2.ListLogs(TestContext(), operations.V2ListLogsRequest{ - Ledger: newLedger, - PageSize: pointer.For(int64(200)), - }) - Expect(err).To(BeNil()) - Expect(logs.V2LogsCursorResponse.Cursor.Data).To(HaveLen(logsToWrite)) - }) - Then("retrying to import the same export", func() { - var ( - err error - ) - BeforeEach(func() { - _, err = Client().Ledger.V2.ImportLogs(TestContext(), operations.V2ImportLogsRequest{ - RequestBody: pointer.For(string(data)), - Ledger: newLedger, - }) - }) - It("Should trigger an error with IMPORT error code", func() { - Expect(err).NotTo(BeNil()) - }) - }) - }) - }) - }) -}) diff --git a/tests/integration/suite/ledger-list-count-accounts.go b/tests/integration/suite/ledger-list-count-accounts.go deleted file mode 100644 index 21bb7b4909..0000000000 --- a/tests/integration/suite/ledger-list-count-accounts.go +++ /dev/null @@ -1,425 +0,0 @@ -package suite - -import ( - "fmt" - "math/big" - "net/http" - "sort" - "time" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/sdkerrors" - "github.com/formancehq/go-libs/pointer" - "github.com/formancehq/stack/tests/integration/internal/modules" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - "github.com/formancehq/go-libs/metadata" - . "github.com/formancehq/stack/tests/integration/internal" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - BeforeEach(func() { - createLedgerResponse, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - }) - When("counting and listing accounts", func() { - var ( - metadata1 = map[string]string{ - "clientType": "gold", - } - - metadata2 = map[string]string{ - "clientType": "silver", - } - - timestamp = time.Now().Round(time.Second).UTC() - bigInt, _ = big.NewInt(0).SetString("999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", 10) - ) - BeforeEach(func() { - // Subscribe to nats subject - response, err := Client().Ledger.V2.AddMetadataToAccount( - TestContext(), - operations.V2AddMetadataToAccountRequest{ - RequestBody: metadata1, - Address: "foo:foo", - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - - response, err = Client().Ledger.V2.AddMetadataToAccount( - TestContext(), - operations.V2AddMetadataToAccountRequest{ - RequestBody: metadata2, - Address: "foo:bar", - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - - createTransactionResponse, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: bigInt, - Asset: "USD", - Source: "world", - Destination: "foo:foo", - }, - }, - Timestamp: ×tamp, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(createTransactionResponse.StatusCode).To(Equal(200)) - }) - It("should return a "+string(shared.V2ErrorsEnumValidation)+" on invalid filter", func() { - _, err := Client().Ledger.V2.ListAccounts( - TestContext(), - operations.V2ListAccountsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "invalid-key": 0, - }, - }, - }, - ) - Expect(err).To(HaveOccurred()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumValidation)) - }) - It("should be countable on api", func() { - response, err := Client().Ledger.V2.CountAccounts( - TestContext(), - operations.V2CountAccountsRequest{ - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("3")) - }) - It("should be listed on api", func() { - response, err := Client().Ledger.V2.ListAccounts( - TestContext(), - operations.V2ListAccountsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - accountsCursorResponse := response.V2AccountsCursorResponse - Expect(accountsCursorResponse.Cursor.Data).To(HaveLen(3)) - Expect(accountsCursorResponse.Cursor.Data[0]).To(Equal(shared.V2Account{ - Address: "foo:bar", - Metadata: metadata2, - })) - Expect(accountsCursorResponse.Cursor.Data[1]).To(Equal(shared.V2Account{ - Address: "foo:foo", - Metadata: metadata1, - Volumes: map[string]shared.V2Volume{ - "USD": { - Input: bigInt, - Output: big.NewInt(0), - Balance: bigInt, - }, - }, - })) - Expect(accountsCursorResponse.Cursor.Data[2]).To(Equal(shared.V2Account{ - Address: "world", - Metadata: metadata.Metadata{}, - Volumes: map[string]shared.V2Volume{ - "USD": { - Output: bigInt, - Input: big.NewInt(0), - Balance: big.NewInt(0).Neg(bigInt), - }, - }, - })) - }) - It("should be listed on api using address filters", func() { - response, err := Client().Ledger.V2.ListAccounts( - TestContext(), - operations.V2ListAccountsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "address": "foo:", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - accountsCursorResponse := response.V2AccountsCursorResponse - Expect(accountsCursorResponse.Cursor.Data).To(HaveLen(2)) - Expect(accountsCursorResponse.Cursor.Data[0]).To(Equal(shared.V2Account{ - Address: "foo:bar", - Metadata: metadata2, - })) - Expect(accountsCursorResponse.Cursor.Data[1]).To(Equal(shared.V2Account{ - Address: "foo:foo", - Metadata: metadata1, - })) - - response, err = Client().Ledger.V2.ListAccounts( - TestContext(), - operations.V2ListAccountsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "address": ":foo", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - accountsCursorResponse = response.V2AccountsCursorResponse - Expect(accountsCursorResponse.Cursor.Data).To(HaveLen(1)) - Expect(accountsCursorResponse.Cursor.Data[0]).To(Equal(shared.V2Account{ - Address: "foo:foo", - Metadata: metadata1, - })) - }) - It("should be listed on api using metadata filters", func() { - response, err := Client().Ledger.V2.ListAccounts( - TestContext(), - operations.V2ListAccountsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "metadata[clientType]": "gold", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - accountsCursorResponse := response.V2AccountsCursorResponse - Expect(accountsCursorResponse.Cursor.Data).To(HaveLen(1)) - Expect(accountsCursorResponse.Cursor.Data[0]).To(Equal(shared.V2Account{ - Address: "foo:foo", - Metadata: metadata1, - })) - }) - It("should be listable on api using $not filter", func() { - response, err := Client().Ledger.V2.ListAccounts( - TestContext(), - operations.V2ListAccountsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$not": map[string]any{ - "$match": map[string]any{ - "address": "world", - }, - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - accountsCursorResponse := response.V2AccountsCursorResponse - Expect(accountsCursorResponse.Cursor.Data).To(HaveLen(2)) - }) - }) - - When("counting and listing accounts empty", func() { - It("should be countable on api even if empty", func() { - response, err := Client().Ledger.V2.CountAccounts( - TestContext(), - operations.V2CountAccountsRequest{ - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("0")) - }) - It("should be listed on api even if empty", func() { - response, err := Client().Ledger.V2.ListAccounts( - TestContext(), - operations.V2ListAccountsRequest{ - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - Expect(response.V2AccountsCursorResponse.Cursor.Data).To(HaveLen(0)) - }) - }) - - const ( - pageSize = int64(10) - accountCounts = 2 * pageSize - ) - When("creating accounts", func() { - var ( - accounts []shared.V2Account - ) - BeforeEach(func() { - for i := 0; i < int(accountCounts); i++ { - m := map[string]string{ - "id": fmt.Sprintf("%d", i), - } - - response, err := Client().Ledger.V2.AddMetadataToAccount( - TestContext(), - operations.V2AddMetadataToAccountRequest{ - RequestBody: m, - Address: fmt.Sprintf("foo:%d", i), - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - - accounts = append(accounts, shared.V2Account{ - Address: fmt.Sprintf("foo:%d", i), - Metadata: m, - }) - - sort.Slice(accounts, func(i, j int) bool { - return accounts[i].Address < accounts[j].Address - }) - } - }) - AfterEach(func() { - accounts = nil - }) - Then(fmt.Sprintf("listing accounts using page size of %d", pageSize), func() { - var ( - rsp *shared.V2AccountsCursorResponse - ) - BeforeEach(func() { - response, err := Client().Ledger.V2.ListAccounts( - TestContext(), - operations.V2ListAccountsRequest{ - Ledger: "default", - PageSize: ptr(pageSize), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2AccountsCursorResponse - Expect(rsp.Cursor.HasMore).To(BeTrue()) - Expect(rsp.Cursor.Previous).To(BeNil()) - Expect(rsp.Cursor.Next).NotTo(BeNil()) - }) - It("should return the first page", func() { - Expect(rsp.Cursor.PageSize).To(Equal(pageSize)) - Expect(rsp.Cursor.Data).To(Equal(accounts[:pageSize])) - }) - Then("following next cursor", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.ListAccounts( - TestContext(), - operations.V2ListAccountsRequest{ - Cursor: rsp.Cursor.Next, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2AccountsCursorResponse - }) - It("should return next page", func() { - Expect(rsp.Cursor.PageSize).To(Equal(pageSize)) - Expect(rsp.Cursor.Data).To(Equal(accounts[pageSize : 2*pageSize])) - Expect(rsp.Cursor.Next).To(BeNil()) - }) - Then("following previous cursor", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.ListAccounts( - TestContext(), - operations.V2ListAccountsRequest{ - Ledger: "default", - Cursor: rsp.Cursor.Previous, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2AccountsCursorResponse - }) - It("should return first page", func() { - Expect(rsp.Cursor.PageSize).To(Equal(pageSize)) - Expect(rsp.Cursor.Data).To(Equal(accounts[:pageSize])) - Expect(rsp.Cursor.Previous).To(BeNil()) - }) - }) - }) - }) - }) - - When("Inserting one transaction in past and one in the future", func() { - now := time.Now() - BeforeEach(func() { - _, err := Client().Ledger.V2.CreateTransaction(TestContext(), operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Postings: []shared.V2Posting{{ - Amount: big.NewInt(100), - Asset: "USD", - Destination: "foo", - Source: "world", - }}, - Timestamp: pointer.For(now.Add(-12 * time.Hour)), - Metadata: map[string]string{}, - }, - Ledger: "default", - }) - Expect(err).To(Succeed()) - - _, err = Client().Ledger.V2.CreateTransaction(TestContext(), operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Postings: []shared.V2Posting{{ - Amount: big.NewInt(100), - Asset: "USD", - Destination: "foo", - Source: "world", - }}, - Timestamp: pointer.For(now.Add(12 * time.Hour)), - Metadata: map[string]string{}, - }, - Ledger: "default", - }) - Expect(err).To(Succeed()) - }) - When("getting account in the present", func() { - It("should ignore future transaction on effective volumes", func() { - accountResponse, err := Client().Ledger.V2.GetAccount(TestContext(), operations.V2GetAccountRequest{ - Address: "foo", - Expand: pointer.For("effectiveVolumes"), - Ledger: "default", - Pit: pointer.For(time.Now().Add(time.Minute)), - }) - Expect(err).To(Succeed()) - Expect(accountResponse.V2AccountResponse.Data.EffectiveVolumes["USD"].Balance).To(Equal(big.NewInt(100))) - }) - }) - }) -}) diff --git a/tests/integration/suite/ledger-list-logs.go b/tests/integration/suite/ledger-list-logs.go deleted file mode 100644 index 3480346cb9..0000000000 --- a/tests/integration/suite/ledger-list-logs.go +++ /dev/null @@ -1,329 +0,0 @@ -package suite - -import ( - "fmt" - "github.com/formancehq/stack/tests/integration/internal/modules" - "math/big" - "net/http" - "sort" - "time" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - . "github.com/formancehq/stack/tests/integration/internal" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - BeforeEach(func() { - createLedgerResponse, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - - _, err = Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "another", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - }) - When("listing logs", func() { - var ( - timestamp1 = time.Date(2023, 4, 11, 10, 0, 0, 0, time.UTC) - timestamp2 = time.Date(2023, 4, 12, 10, 0, 0, 0, time.UTC) - - m1 = map[string]string{ - "clientType": "silver", - } - m2 = map[string]string{ - "clientType": "gold", - } - ) - BeforeEach(func() { - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "foo:foo", - }, - }, - Timestamp: ×tamp1, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - _, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "foo:foo", - }, - }, - Timestamp: ×tamp1, - }, - Ledger: "another", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - response, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: m1, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(200), - Asset: "USD", - Source: "world", - Destination: "foo:bar", - }, - }, - Timestamp: ×tamp2, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - addMetadataResponse, err := Client().Ledger.V2.AddMetadataToAccount( - TestContext(), - operations.V2AddMetadataToAccountRequest{ - RequestBody: m2, - Address: "foo:baz", - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(addMetadataResponse.StatusCode).To(Equal(204)) - }) - It("should be listed on api with ListLogs", func() { - response, err := Client().Ledger.V2.ListLogs( - TestContext(), - operations.V2ListLogsRequest{ - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - logsCursorResponse := response.V2LogsCursorResponse - Expect(logsCursorResponse.Cursor.Data).To(HaveLen(3)) - - // Cannot check the date and the hash since they are changing at - // every run - Expect(logsCursorResponse.Cursor.Data[0].ID).To(Equal(big.NewInt(2))) - Expect(logsCursorResponse.Cursor.Data[0].Type).To(Equal(shared.V2LogTypeSetMetadata)) - Expect(logsCursorResponse.Cursor.Data[0].Data).To(Equal(map[string]any{ - "targetType": "ACCOUNT", - "metadata": map[string]any{ - "clientType": "gold", - }, - "targetId": "foo:baz", - })) - - Expect(logsCursorResponse.Cursor.Data[1].ID).To(Equal(big.NewInt((1)))) - Expect(logsCursorResponse.Cursor.Data[1].Type).To(Equal(shared.V2LogTypeNewTransaction)) - // Cannot check date and txid inside Data since they are changing at - // every run - Expect(logsCursorResponse.Cursor.Data[1].Data["accountMetadata"]).To(Equal(map[string]any{})) - Expect(logsCursorResponse.Cursor.Data[1].Data["transaction"]).To(BeAssignableToTypeOf(map[string]any{})) - transaction := logsCursorResponse.Cursor.Data[1].Data["transaction"].(map[string]any) - Expect(transaction["metadata"]).To(Equal(map[string]any{ - "clientType": "silver", - })) - Expect(transaction["timestamp"]).To(Equal("2023-04-12T10:00:00Z")) - Expect(transaction["postings"]).To(Equal([]any{ - map[string]any{ - "amount": float64(200), - "asset": "USD", - "source": "world", - "destination": "foo:bar", - }, - })) - - Expect(logsCursorResponse.Cursor.Data[2].ID).To(Equal(big.NewInt((0)))) - Expect(logsCursorResponse.Cursor.Data[2].Type).To(Equal(shared.V2LogTypeNewTransaction)) - Expect(logsCursorResponse.Cursor.Data[2].Data["accountMetadata"]).To(Equal(map[string]any{})) - Expect(logsCursorResponse.Cursor.Data[2].Data["transaction"]).To(BeAssignableToTypeOf(map[string]any{})) - transaction = logsCursorResponse.Cursor.Data[2].Data["transaction"].(map[string]any) - Expect(transaction["metadata"]).To(Equal(map[string]any{})) - Expect(transaction["timestamp"]).To(Equal("2023-04-11T10:00:00Z")) - Expect(transaction["postings"]).To(Equal([]any{ - map[string]any{ - "amount": float64(100), - "asset": "USD", - "source": "world", - "destination": "foo:foo", - }, - })) - }) - }) - - type expectedLog struct { - id *big.Int - typ shared.V2LogType - postings []any - } - - var ( - compareLogs = func(log shared.V2Log, expected expectedLog) { - Expect(log.ID).To(Equal(expected.id)) - Expect(log.Type).To(Equal(expected.typ)) - Expect(log.Data["accountMetadata"]).To(Equal(map[string]any{})) - Expect(log.Data["transaction"]).To(BeAssignableToTypeOf(map[string]any{})) - transaction := log.Data["transaction"].(map[string]any) - Expect(transaction["metadata"]).To(Equal(map[string]any{})) - Expect(transaction["postings"]).To(Equal(expected.postings)) - } - ) - - const ( - pageSize = int64(10) - accountCounts = 2 * pageSize - ) - When("creating logs with transactions", func() { - var ( - expectedLogs []expectedLog - ) - BeforeEach(func() { - for i := int64(0); i < accountCounts; i++ { - now := time.Now().Round(time.Millisecond).UTC() - - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: fmt.Sprintf("foo:%d", i), - }, - }, - Timestamp: &now, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - expectedLogs = append(expectedLogs, expectedLog{ - id: big.NewInt(i), - typ: shared.V2LogTypeNewTransaction, - postings: []any{ - map[string]any{ - "amount": float64(100), - "asset": "USD", - "source": "world", - "destination": fmt.Sprintf("foo:%d", i), - }, - }, - }) - } - - sort.Slice(expectedLogs, func(i, j int) bool { - return expectedLogs[i].id.Cmp(expectedLogs[j].id) > 0 - }) - }) - AfterEach(func() { - expectedLogs = nil - }) - Then(fmt.Sprintf("listing accounts using page size of %d", pageSize), func() { - var ( - rsp *shared.V2LogsCursorResponse - ) - BeforeEach(func() { - response, err := Client().Ledger.V2.ListLogs( - TestContext(), - operations.V2ListLogsRequest{ - Ledger: "default", - PageSize: ptr(pageSize), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2LogsCursorResponse - Expect(rsp.Cursor.HasMore).To(BeTrue()) - Expect(rsp.Cursor.Previous).To(BeNil()) - Expect(rsp.Cursor.Next).NotTo(BeNil()) - }) - It("should return the first page", func() { - Expect(rsp.Cursor.PageSize).To(Equal(pageSize)) - Expect(len(rsp.Cursor.Data)).To(Equal(len(expectedLogs[:pageSize]))) - for i := range rsp.Cursor.Data { - compareLogs(rsp.Cursor.Data[i], expectedLogs[i]) - } - }) - Then("following next cursor", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.ListLogs( - TestContext(), - operations.V2ListLogsRequest{ - Cursor: rsp.Cursor.Next, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2LogsCursorResponse - }) - It("should return next page", func() { - Expect(rsp.Cursor.PageSize).To(Equal(pageSize)) - Expect(len(rsp.Cursor.Data)).To(Equal(len(expectedLogs[pageSize : 2*pageSize]))) - for i := range rsp.Cursor.Data { - compareLogs(rsp.Cursor.Data[i], expectedLogs[int64(i)+pageSize]) - } - Expect(rsp.Cursor.Next).To(BeNil()) - }) - Then("following previous cursor", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.ListLogs( - TestContext(), - operations.V2ListLogsRequest{ - Cursor: rsp.Cursor.Previous, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2LogsCursorResponse - }) - It("should return first page", func() { - Expect(rsp.Cursor.PageSize).To(Equal(pageSize)) - Expect(len(rsp.Cursor.Data)).To(Equal(len(expectedLogs[:pageSize]))) - for i := range rsp.Cursor.Data { - compareLogs(rsp.Cursor.Data[i], expectedLogs[i]) - } - Expect(rsp.Cursor.Previous).To(BeNil()) - }) - }) - }) - }) - }) -}) diff --git a/tests/integration/suite/ledger-list-transactions.go b/tests/integration/suite/ledger-list-transactions.go deleted file mode 100644 index cd61043fdd..0000000000 --- a/tests/integration/suite/ledger-list-transactions.go +++ /dev/null @@ -1,1185 +0,0 @@ -package suite - -import ( - "fmt" - "github.com/formancehq/go-libs/bun/bunpaginate" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "math/big" - "net/http" - "sort" - "time" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/sdkerrors" - "github.com/formancehq/stack/tests/integration/internal/modules" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - "github.com/formancehq/go-libs/metadata" - "github.com/formancehq/go-libs/pointer" - . "github.com/formancehq/stack/tests/integration/internal" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - When("trying to list transactions of a non existent ledger", func() { - BeforeEach(func() { - _, err := Client().Ledger.V2.ListTransactions(TestContext(), operations.V2ListTransactionsRequest{ - Ledger: "default", - }) - Expect(err).NotTo(BeNil()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumLedgerNotFound)) - }) - It("Should fail with a 404", func() { - }) - }) -}) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - JustBeforeEach(func() { - createLedgerResponse, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - }) - const ( - pageSize = int64(10) - txCount = 2 * pageSize - ) - When(fmt.Sprintf("creating %d transactions", txCount), func() { - var ( - timestamp = time.Now().Round(time.Second).UTC() - transactions []shared.V2ExpandedTransaction - ) - JustBeforeEach(func() { - for i := 0; i < int(txCount); i++ { - offset := time.Duration(int(txCount)-i) * time.Minute - // 1 transaction of 2 is backdated to test pagination using effective date - if offset%2 == 0 { - offset += 1 - } else { - offset -= 1 - } - txTimestamp := timestamp.Add(-offset) - - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: fmt.Sprintf("account:%d", i), - }, - }, - Timestamp: pointer.For(txTimestamp), - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - ret := response.V2CreateTransactionResponse - transactions = append([]shared.V2ExpandedTransaction{ - { - Timestamp: ret.Data.Timestamp, - Postings: ret.Data.Postings, - Reference: ret.Data.Reference, - Metadata: ret.Data.Metadata, - ID: ret.Data.ID, - PreCommitVolumes: map[string]map[string]shared.V2Volume{ - "world": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(int64(i * 100)), - Balance: big.NewInt(int64(-i * 100)), - }, - }, - fmt.Sprintf("account:%d", i): { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(0), - Balance: big.NewInt(0), - }, - }, - }, - PostCommitVolumes: map[string]map[string]shared.V2Volume{ - "world": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(int64((i + 1) * 100)), - Balance: big.NewInt(int64(-(i + 1) * 100)), - }, - }, - fmt.Sprintf("account:%d", i): { - "USD": { - Input: big.NewInt(100), - Output: big.NewInt(0), - Balance: big.NewInt(int64(100)), - }, - }, - }, - }, - }, transactions...) - } - }) - AfterEach(func() { - transactions = nil - }) - Then(fmt.Sprintf("listing transactions using page size of %d", pageSize), func() { - var ( - rsp *shared.V2TransactionsCursorResponse - req operations.V2ListTransactionsRequest - ) - BeforeEach(func() { - req = operations.V2ListTransactionsRequest{ - Ledger: "default", - PageSize: ptr(pageSize), - Expand: pointer.For("volumes"), - RequestBody: map[string]any{ - "$and": []map[string]any{ - { - "$match": map[string]any{ - "source": "world", - }, - }, - { - "$not": map[string]any{ - "$exists": map[string]any{ - "metadata": "foo", - }, - }, - }, - }, - }, - } - }) - JustBeforeEach(func() { - response, err := Client().Ledger.V2.ListTransactions(TestContext(), req) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2TransactionsCursorResponse - Expect(rsp.Cursor.HasMore).To(BeTrue()) - Expect(rsp.Cursor.Previous).To(BeNil()) - Expect(rsp.Cursor.Next).NotTo(BeNil()) - }) - Context("with effective ordering", func() { - BeforeEach(func() { - req.Order = pointer.For(operations.OrderEffective) - }) - It("Should be ok, and returns transactions ordered by effective timestamp", func() { - Expect(rsp.Cursor.PageSize).To(Equal(pageSize)) - sorted := transactions[:pageSize] - sort.SliceStable(sorted, func(i, j int) bool { - return sorted[i].Timestamp.After(sorted[j].Timestamp) - }) - Expect(rsp.Cursor.Data).To(Equal(sorted)) - }) - }) - It("Should be ok", func() { - Expect(rsp.Cursor.PageSize).To(Equal(pageSize)) - Expect(rsp.Cursor.Data).To(Equal(transactions[:pageSize])) - }) - Then("following next cursor", func() { - JustBeforeEach(func() { - - // Create a new transaction to ensure cursor is stable - _, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "account:0", - }, - }, - Timestamp: pointer.For(time.Now()), - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - - response, err := Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Cursor: rsp.Cursor.Next, - Ledger: "default", - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2TransactionsCursorResponse - }) - It("should return next page", func() { - Expect(rsp.Cursor.PageSize).To(Equal(pageSize)) - Expect(rsp.Cursor.Data).To(Equal(transactions[pageSize : 2*pageSize])) - Expect(rsp.Cursor.Next).To(BeNil()) - }) - Then("following previous cursor", func() { - JustBeforeEach(func() { - response, err := Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Cursor: rsp.Cursor.Previous, - Ledger: "default", - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2TransactionsCursorResponse - }) - It("should return first page", func() { - Expect(rsp.Cursor.PageSize).To(Equal(pageSize)) - Expect(rsp.Cursor.Data).To(Equal(transactions[:pageSize])) - Expect(rsp.Cursor.Previous).To(BeNil()) - }) - }) - }) - }) - - Then("listing transactions using filter on a single match", func() { - var ( - err error - response *operations.V2ListTransactionsResponse - now = time.Now().Round(time.Second).UTC() - ) - JustBeforeEach(func() { - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "source": "world", - }, - }, - Ledger: "default", - PageSize: ptr(pageSize), - Pit: &now, - }, - ) - Expect(err).To(BeNil()) - }) - It("Should be ok", func() { - Expect(response.V2TransactionsCursorResponse.Cursor.Next).NotTo(BeNil()) - cursor := &bunpaginate.ColumnPaginatedQuery[map[string]any]{} - Expect(bunpaginate.UnmarshalCursor(*response.V2TransactionsCursorResponse.Cursor.Next, cursor)).To(BeNil()) - Expect(cursor.Options).To(Equal(map[string]any{ - "qb": map[string]any{ - "$match": map[string]any{ - "source": "world", - }, - }, - "pageSize": float64(10), - "options": map[string]any{ - "pit": now.Format(time.RFC3339), - "oot": nil, - "volumes": false, - "effectiveVolumes": false, - }, - })) - }) - }) - Then("listing transactions using filter on a single match", func() { - var ( - err error - response *operations.V2ListTransactionsResponse - now = time.Now().Round(time.Second).UTC() - ) - JustBeforeEach(func() { - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - RequestBody: map[string]interface{}{ - "$and": []map[string]any{ - { - "$match": map[string]any{ - "source": "world", - }, - }, - { - "$match": map[string]any{ - "destination": "account:", - }, - }, - }, - }, - Ledger: "default", - PageSize: ptr(pageSize), - Pit: &now, - }, - ) - Expect(err).To(BeNil()) - }) - It("Should be ok", func() { - Expect(response.V2TransactionsCursorResponse.Cursor.Next).NotTo(BeNil()) - cursor := &bunpaginate.ColumnPaginatedQuery[map[string]any]{} - Expect(bunpaginate.UnmarshalCursor(*response.V2TransactionsCursorResponse.Cursor.Next, cursor)).To(BeNil()) - Expect(cursor.Options).To(Equal(map[string]any{ - "qb": map[string]any{ - "$and": []any{ - map[string]any{ - "$match": map[string]any{ - "source": "world", - }, - }, - map[string]any{ - "$match": map[string]any{ - "destination": "account:", - }, - }, - }, - }, - "pageSize": float64(10), - "options": map[string]any{ - "pit": now.Format(time.RFC3339), - "oot": nil, - "volumes": false, - "effectiveVolumes": false, - }, - })) - }) - }) - Then("listing transactions using invalid filter", func() { - var ( - err error - ) - JustBeforeEach(func() { - _, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "invalid-key": 0, - }, - }, - Ledger: "default", - PageSize: ptr(pageSize), - }, - ) - Expect(err).To(HaveOccurred()) - }) - It("Should fail with "+string(shared.V2ErrorsEnumValidation)+" error code", func() { - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumValidation)) - }) - }) - }) - var ( - timestamp1 = time.Date(2023, 4, 10, 10, 0, 0, 0, time.UTC) - timestamp2 = time.Date(2023, 4, 11, 10, 0, 0, 0, time.UTC) - timestamp3 = time.Date(2023, 4, 12, 10, 0, 0, 0, time.UTC) - - m1 = metadata.Metadata{ - "foo": "bar", - } - ) - - var ( - t1 shared.V2ExpandedTransaction - t2 shared.V2ExpandedTransaction - t3 shared.V2ExpandedTransaction - ) - When("creating transactions", func() { - JustBeforeEach(func() { - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: m1, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "foo:foo", - }, - }, - Timestamp: ×tamp1, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - ret := response.V2CreateTransactionResponse - t1 = shared.V2ExpandedTransaction{ - Timestamp: ret.Data.Timestamp, - Postings: ret.Data.Postings, - Reference: ret.Data.Reference, - Metadata: ret.Data.Metadata, - ID: ret.Data.ID, - PreCommitVolumes: map[string]map[string]shared.V2Volume{ - "world": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(0), - Balance: big.NewInt(int64(0)), - }, - }, - "foo:foo": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(0), - Balance: big.NewInt(int64(0)), - }, - }, - }, - PostCommitVolumes: map[string]map[string]shared.V2Volume{ - "world": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(100), - Balance: big.NewInt(int64(-100)), - }, - }, - "foo:foo": { - "USD": { - Input: big.NewInt(100), - Output: big.NewInt(0), - Balance: big.NewInt(int64(100)), - }, - }, - }, - } - - response, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: m1, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "foo:bar", - }, - }, - Timestamp: ×tamp2, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - ret = response.V2CreateTransactionResponse - t2 = shared.V2ExpandedTransaction{ - Timestamp: ret.Data.Timestamp, - Postings: ret.Data.Postings, - Reference: ret.Data.Reference, - Metadata: ret.Data.Metadata, - ID: ret.Data.ID, - PreCommitVolumes: map[string]map[string]shared.V2Volume{ - "world": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(100), - Balance: big.NewInt(int64(-100)), - }, - }, - "foo:bar": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(0), - Balance: big.NewInt(int64(0)), - }, - }, - }, - PostCommitVolumes: map[string]map[string]shared.V2Volume{ - "world": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(200), - Balance: big.NewInt(int64(-200)), - }, - }, - "foo:bar": { - "USD": { - Input: big.NewInt(100), - Output: big.NewInt(0), - Balance: big.NewInt(int64(100)), - }, - }, - }, - } - - response, err = Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "foo:baz", - }, - }, - Timestamp: ×tamp3, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - ret = response.V2CreateTransactionResponse - t3 = shared.V2ExpandedTransaction{ - Timestamp: ret.Data.Timestamp, - Postings: ret.Data.Postings, - Reference: ret.Data.Reference, - Metadata: ret.Data.Metadata, - ID: ret.Data.ID, - PreCommitVolumes: map[string]map[string]shared.V2Volume{ - "world": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(200), - Balance: big.NewInt(int64(-200)), - }, - }, - "foo:baz": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(0), - Balance: big.NewInt(int64(0)), - }, - }, - }, - PostCommitVolumes: map[string]map[string]shared.V2Volume{ - "world": { - "USD": { - Input: big.NewInt(0), - Output: big.NewInt(300), - Balance: big.NewInt(int64(-300)), - }, - }, - "foo:baz": { - "USD": { - Input: big.NewInt(100), - Output: big.NewInt(0), - Balance: big.NewInt(int64(100)), - }, - }, - }, - } - }) - It("should be countable on api", func() { - response, err := Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("3")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "account": "foo:", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("3")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "account": "not_existing", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("0")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "destination": ":baz", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("1")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "destination": "not_existing", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("0")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "source": "foo:", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("0")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "source": "world", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("3")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "metadata[foo]": "bar", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("2")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "metadata[foo]": "not_existing", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("0")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$gte": map[string]any{ - "timestamp": timestamp2.Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("2")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$gte": map[string]any{ - "timestamp": timestamp3.Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("1")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$gte": map[string]any{ - "timestamp": time.Now().UTC().Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("0")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$lt": map[string]any{ - "timestamp": timestamp3.Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("2")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$lt": map[string]any{ - "timestamp": timestamp2.Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("1")) - - response, err = Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$lt": map[string]any{ - "timestamp": time.Date(2023, 4, 9, 10, 0, 0, 0, time.UTC).Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("0")) - }) - It("should be listed on api", func() { - response, err := Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse := response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).To(HaveLen(3)) - Expect(transactionCursorResponse.Cursor.Data[0]).Should(Equal(t3)) - Expect(transactionCursorResponse.Cursor.Data[1]).Should(Equal(t2)) - Expect(transactionCursorResponse.Cursor.Data[2]).Should(Equal(t1)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "account": "foo:", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).To(HaveLen(3)) - Expect(transactionCursorResponse.Cursor.Data[0]).Should(Equal(t3)) - Expect(transactionCursorResponse.Cursor.Data[1]).Should(Equal(t2)) - Expect(transactionCursorResponse.Cursor.Data[2]).Should(Equal(t1)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "account": "not_existing", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).To(HaveLen(0)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "destination": "foo:", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).To(HaveLen(3)) - Expect(transactionCursorResponse.Cursor.Data[0]).Should(Equal(t3)) - Expect(transactionCursorResponse.Cursor.Data[1]).Should(Equal(t2)) - Expect(transactionCursorResponse.Cursor.Data[2]).Should(Equal(t1)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "destination": "not_existing", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).To(HaveLen(0)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "source": "foo:", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).To(HaveLen(0)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "source": "world", - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).To(HaveLen(3)) - Expect(transactionCursorResponse.Cursor.Data[0]).Should(Equal(t3)) - Expect(transactionCursorResponse.Cursor.Data[1]).Should(Equal(t2)) - Expect(transactionCursorResponse.Cursor.Data[2]).Should(Equal(t1)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "metadata[foo]": "bar", - }, - }, - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).Should(HaveLen(2)) - Expect(transactionCursorResponse.Cursor.Data[0]).Should(Equal(t2)) - Expect(transactionCursorResponse.Cursor.Data[1]).Should(Equal(t1)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "metadata[foo]": "not_existing", - }, - }, - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).Should(HaveLen(0)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$gte": map[string]any{ - "timestamp": timestamp2.Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).Should(HaveLen(2)) - Expect(transactionCursorResponse.Cursor.Data[0]).Should(Equal(t3)) - Expect(transactionCursorResponse.Cursor.Data[1]).Should(Equal(t2)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$gte": map[string]any{ - "timestamp": timestamp3.Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).Should(HaveLen(1)) - Expect(transactionCursorResponse.Cursor.Data[0]).Should(Equal(t3)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$gte": map[string]any{ - "timestamp": time.Now().UTC().Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).Should(HaveLen(0)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$lt": map[string]any{ - "timestamp": timestamp3.Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).Should(HaveLen(2)) - Expect(transactionCursorResponse.Cursor.Data[0]).Should(Equal(t2)) - Expect(transactionCursorResponse.Cursor.Data[1]).Should(Equal(t1)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$lt": map[string]any{ - "timestamp": timestamp2.Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).Should(HaveLen(1)) - Expect(transactionCursorResponse.Cursor.Data[0]).Should(Equal(t1)) - - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$lt": map[string]any{ - "timestamp": time.Date(2023, 4, 9, 10, 0, 0, 0, time.UTC).Format(time.RFC3339), - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).Should(HaveLen(0)) - - By("using $not operator on account 'world'", func() { - response, err = Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - Expand: pointer.For("volumes"), - RequestBody: map[string]interface{}{ - "$not": map[string]any{ - "$match": map[string]any{ - "account": "foo:bar", - }, - }, - }, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - transactionCursorResponse = response.V2TransactionsCursorResponse - Expect(transactionCursorResponse.Cursor.Data).Should(HaveLen(2)) - }) - }) - It("should be gettable on api", func() { - response, err := Client().Ledger.V2.GetTransaction( - TestContext(), - operations.V2GetTransactionRequest{ - Ledger: "default", - ID: t1.ID, - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - Expect(response.V2GetTransactionResponse.Data).Should(Equal(t1)) - - response, err = Client().Ledger.V2.GetTransaction( - TestContext(), - operations.V2GetTransactionRequest{ - Ledger: "default", - ID: t2.ID, - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - Expect(response.V2GetTransactionResponse.Data).Should(Equal(t2)) - - response, err = Client().Ledger.V2.GetTransaction( - TestContext(), - operations.V2GetTransactionRequest{ - Ledger: "default", - ID: t3.ID, - Expand: pointer.For("volumes"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - Expect(response.V2GetTransactionResponse.Data).Should(Equal(t3)) - - response, err = Client().Ledger.V2.GetTransaction( - TestContext(), - operations.V2GetTransactionRequest{ - Ledger: "default", - ID: big.NewInt(666), - }, - ) - Expect(err).To(HaveOccurred()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumNotFound)) - }) - }) - - When("counting and listing transactions empty", func() { - It("should be countable on api even if empty", func() { - response, err := Client().Ledger.V2.CountTransactions( - TestContext(), - operations.V2CountTransactionsRequest{ - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - Expect(response.Headers["Count"]).Should(HaveLen(1)) - Expect(response.Headers["Count"][0]).Should(Equal("0")) - }) - It("should be listed on api even if empty", func() { - response, err := Client().Ledger.V2.ListTransactions( - TestContext(), - operations.V2ListTransactionsRequest{ - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - Expect(response.V2TransactionsCursorResponse.Cursor.Data).To(HaveLen(0)) - }) - }) -}) diff --git a/tests/integration/suite/ledger-list.go b/tests/integration/suite/ledger-list.go deleted file mode 100644 index 94281860dd..0000000000 --- a/tests/integration/suite/ledger-list.go +++ /dev/null @@ -1,32 +0,0 @@ -package suite - -import ( - "fmt" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/formancehq/stack/tests/integration/internal/modules" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "net/http" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - const count = 3 - When(fmt.Sprintf("creating %d ledger", count), func() { - BeforeEach(func() { - for i := 0; i < count; i++ { - createLedgerResponse, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: fmt.Sprintf("ledger%d", i), - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - } - }) - It("should be listable on api", func() { - response, err := Client().Ledger.V2.ListLedgers(TestContext(), operations.V2ListLedgersRequest{}) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(http.StatusOK)) - Expect(response.V2LedgerListResponse.Cursor.Data).To(HaveLen(count)) - }) - }) -}) diff --git a/tests/integration/suite/ledger-revert-transaction.go b/tests/integration/suite/ledger-revert-transaction.go deleted file mode 100644 index c5994aebb0..0000000000 --- a/tests/integration/suite/ledger-revert-transaction.go +++ /dev/null @@ -1,199 +0,0 @@ -package suite - -import ( - "math/big" - "net/http" - "time" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/sdkerrors" - "github.com/formancehq/go-libs/pointer" - "github.com/formancehq/stack/tests/integration/internal/modules" - "github.com/nats-io/nats.go" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - ledgerevents "github.com/formancehq/ledger/pkg/events" - "github.com/formancehq/stack/libs/events" - . "github.com/formancehq/stack/tests/integration/internal" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - BeforeEach(func() { - createLedgerResponse, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - }) - When("creating a transaction on a ledger", func() { - var ( - msgs chan *nats.Msg - cancelSubscription func() - timestamp = time.Now().Round(time.Second).UTC() - createTransactionResponse *shared.V2CreateTransactionResponse - ) - BeforeEach(func() { - // Subscribe to nats subject - cancelSubscription, msgs = SubscribeLedger() - - // Create a transaction - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "alice", - }, - }, - Timestamp: ×tamp, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - createTransactionResponse = response.V2CreateTransactionResponse - - // Wait for created transaction event to drain events - WaitOnChanWithTimeout(msgs, 5*time.Second) - }) - AfterEach(func() { - cancelSubscription() - }) - Then("transferring funds from destination to another account", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "alice", - Destination: "foo", - }, - }, - Timestamp: ×tamp, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - }) - Then("trying to revert the original transaction", func() { - var ( - force bool - err error - response *operations.V2RevertTransactionResponse - ) - revertTx := func() { - response, err = Client().Ledger.V2.RevertTransaction( - TestContext(), - operations.V2RevertTransactionRequest{ - Force: pointer.For(force), - ID: big.NewInt(0), - Ledger: "default", - }, - ) - } - JustBeforeEach(revertTx) - It("Should fail", func() { - Expect(err).To(HaveOccurred()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumInsufficientFund)) - }) - Context("With forcing", func() { - BeforeEach(func() { - force = true - }) - It("Should be ok", func() { - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(201)) - }) - }) - }) - }) - Then("reverting it", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.RevertTransaction( - TestContext(), - operations.V2RevertTransactionRequest{ - Ledger: "default", - ID: createTransactionResponse.Data.ID, - }, - ) - Expect(err).To(Succeed()) - Expect(response.StatusCode).To(Equal(201)) - }) - It("should trigger a new event", func() { - // Wait for created transaction event - msg := WaitOnChanWithTimeout(msgs, 5*time.Second) - Expect(events.Check(msg.Data, "ledger", ledgerevents.EventTypeRevertedTransaction)).Should(Succeed()) - }) - It("should revert the original transaction", func() { - response, err := Client().Ledger.V2.GetTransaction( - TestContext(), - operations.V2GetTransactionRequest{ - Ledger: "default", - ID: createTransactionResponse.Data.ID, - }, - ) - Expect(err).NotTo(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - Expect(response.V2GetTransactionResponse.Data.Reverted).To(BeTrue()) - }) - Then("trying to revert again", func() { - It("should be rejected", func() { - _, err := Client().Ledger.V2.RevertTransaction( - TestContext(), - operations.V2RevertTransactionRequest{ - Ledger: "default", - ID: createTransactionResponse.Data.ID, - }, - ) - Expect(err).NotTo(BeNil()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumAlreadyRevert)) - }) - }) - }) - Then("reverting it at effective date", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.RevertTransaction( - TestContext(), - operations.V2RevertTransactionRequest{ - Ledger: "default", - ID: createTransactionResponse.Data.ID, - AtEffectiveDate: pointer.For(true), - }, - ) - Expect(err).To(Succeed()) - Expect(response.StatusCode).To(Equal(201)) - }) - It("should revert the original transaction at date of the original tx", func() { - response, err := Client().Ledger.V2.GetTransaction( - TestContext(), - operations.V2GetTransactionRequest{ - Ledger: "default", - ID: createTransactionResponse.Data.ID, - }, - ) - Expect(err).NotTo(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - Expect(response.V2GetTransactionResponse.Data.Reverted).To(BeTrue()) - Expect(response.V2GetTransactionResponse.Data.Timestamp).To(Equal(createTransactionResponse.Data.Timestamp)) - }) - }) - }) -}) diff --git a/tests/integration/suite/ledger-search-ingestion-v1.go b/tests/integration/suite/ledger-search-ingestion-v1.go deleted file mode 100644 index b5f3512217..0000000000 --- a/tests/integration/suite/ledger-search-ingestion-v1.go +++ /dev/null @@ -1,152 +0,0 @@ -package suite - -import ( - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - "github.com/formancehq/go-libs/publish" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/formancehq/stack/tests/integration/internal/modules" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "math/big" - "time" -) - -var _ = WithModules([]*Module{modules.Search}, func() { - When("sending a v1 created transaction event in the event bus", func() { - now := time.Now() - BeforeEach(func() { - CreateTopic("ledger") - - PublishLedger(publish.EventMessage{ - Date: time.Now(), - App: "ledger", - Version: "v1", - Type: "COMMITTED_TRANSACTIONS", - Payload: map[string]any{ - "ledger": "foo", - "transactions": []map[string]any{ - { - "preCommitVolumes": map[string]map[string]*big.Int{}, - "postCommitVolumes": map[string]map[string]*big.Int{}, - "txid": big.NewInt(0), - "postings": []any{ - map[string]any{ - "source": "world", - "destination": "bank", - "asset": "USD/2", - "amount": 1000, - }, - }, - "reference": "foo", - "timestamp": now.Format(time.RFC3339), - "ledger": "foo", - }, - }, - }, - }) - }) - It("should be ingested properly", func() { - Eventually(func(g Gomega) bool { - response, err := Client().Search.V1.Search( - TestContext(), - shared.Query{ - Target: ptr("TRANSACTION"), - }, - ) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(response.StatusCode).To(Equal(200)) - - res := response.Response - g.Expect(res.Cursor.Data).To(HaveLen(1)) - g.Expect(res.Cursor.Data[0]).To(Equal(map[string]any{ - "metadata": map[string]any{}, - "reference": "foo", - "postings": []any{ - map[string]any{ - "source": "world", - "destination": "bank", - "asset": "USD/2", - "amount": float64(1000), - }, - }, - "txid": float64(0), - "timestamp": now.Format(time.RFC3339), - "ledger": "foo", - })) - - return true - }).Should(BeTrue()) - Eventually(func(g Gomega) bool { - response, err := Client().Search.V1.Search( - TestContext(), - shared.Query{ - Target: ptr("ACCOUNT"), - }, - ) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(response.StatusCode).To(Equal(200)) - - res := response.Response - g.Expect(res.Cursor.Data).To(HaveLen(2)) - g.Expect(res.Cursor.Data).To(ContainElements( - map[string]any{ - "address": "world", - "ledger": "foo", - "metadata": map[string]any{}, - }, - map[string]any{ - "address": "bank", - "ledger": "foo", - "metadata": map[string]any{}, - }, - )) - return true - }).Should(BeTrue()) - }) - Then("adding metadata on a specific account", func() { - BeforeEach(func() { - PublishLedger(publish.EventMessage{ - Date: time.Now(), - App: "ledger", - Version: "v1", - Type: "SAVED_METADATA", - Payload: map[string]any{ - "targetId": "bank", - "targetType": "ACCOUNT", - "metadata": map[string]any{ - "custom": map[string]any{ - "foo": "bar", - }, - }, - "ledger": "foo", - }, - }) - }) - It("should be ok", func() { - Eventually(func(g Gomega) bool { - response, err := Client().Search.V1.Search( - TestContext(), - shared.Query{ - Target: ptr("ACCOUNT"), - }, - ) - g.Expect(err).ToNot(HaveOccurred()) - g.Expect(response.StatusCode).To(Equal(200)) - - res := response.Response - g.Expect(res.Cursor.Data).To(HaveLen(2)) - g.Expect(res.Cursor.Data).To(ContainElements( - map[string]any{ - "address": "bank", - "ledger": "foo", - "metadata": map[string]any{ - "custom": `{"foo":"bar"}`, - }, - }, - )) - return true - }).Should(BeTrue()) - }) - }) - }) -}) diff --git a/tests/integration/suite/ledger-set-metadata-on-account.go b/tests/integration/suite/ledger-set-metadata-on-account.go deleted file mode 100644 index 6050c8f5a1..0000000000 --- a/tests/integration/suite/ledger-set-metadata-on-account.go +++ /dev/null @@ -1,192 +0,0 @@ -package suite - -import ( - "github.com/formancehq/stack/tests/integration/internal/modules" - "math/big" - "net/http" - "reflect" - "time" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - ledgerevents "github.com/formancehq/ledger/pkg/events" - "github.com/formancehq/stack/libs/events" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/nats-io/nats.go" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = WithModules([]*Module{modules.Search, modules.Ledger}, func() { - BeforeEach(func() { - createLedgerResponse, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - }) - When("creating a new transaction", func() { - BeforeEach(func() { - // Create a transaction - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "alice", - }, - }, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - Eventually(func() bool { - response, err := Client().Search.V1.Search( - TestContext(), - shared.Query{ - Target: ptr("ACCOUNT"), - Terms: []string{"address=alice"}, - }, - ) - if err != nil { - return false - } - if response.StatusCode != 200 { - return false - } - if len(response.Response.Cursor.Data) != 1 { - return false - } - return reflect.DeepEqual(response.Response.Cursor.Data[0], map[string]any{ - "ledger": "default", - "address": "alice", - "metadata": map[string]any{}, - }) - }).Should(BeTrue()) - }) - Then("setting a metadata on the destination account", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.AddMetadataToAccount( - TestContext(), - operations.V2AddMetadataToAccountRequest{ - RequestBody: map[string]string{ - "foo": "bar", - }, - Address: "alice", - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - }) - It("should be ok", func() { - Eventually(func() bool { - response, err := Client().Search.V1.Search( - TestContext(), - shared.Query{ - Target: ptr("ACCOUNT"), - Terms: []string{"address=alice"}, - }, - ) - if err != nil { - return false - } - if response.StatusCode != 200 { - return false - } - if len(response.Response.Cursor.Data) != 1 { - return false - } - return reflect.DeepEqual(response.Response.Cursor.Data[0], map[string]any{ - "ledger": "default", - "metadata": map[string]any{ - "foo": "bar", - }, - "address": "alice", - }) - }).Should(BeTrue()) - }) - }) - }) - When("setting metadata on a unknown account", func() { - var ( - msgs chan *nats.Msg - cancelSubscription func() - metadata = map[string]string{ - "clientType": "gold", - } - ) - BeforeEach(func() { - // Subscribe to nats subject - cancelSubscription, msgs = SubscribeLedger() - - response, err := Client().Ledger.V2.AddMetadataToAccount( - TestContext(), - operations.V2AddMetadataToAccountRequest{ - RequestBody: metadata, - Address: "foo", - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(204)) - }) - AfterEach(func() { - cancelSubscription() - }) - It("should be available on api", func() { - response, err := Client().Ledger.V2.GetAccount( - TestContext(), - operations.V2GetAccountRequest{ - Address: "foo", - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - Expect(response.V2AccountResponse.Data).Should(Equal(shared.V2Account{ - Address: "foo", - Metadata: metadata, - })) - }) - It("should trigger a new event", func() { - msg := WaitOnChanWithTimeout(msgs, 5*time.Second) - Expect(events.Check(msg.Data, "ledger", ledgerevents.EventTypeSavedMetadata)).Should(Succeed()) - }) - It("should pop an account with the correct metadata on search service", func() { - Eventually(func() bool { - response, err := Client().Search.V1.Search( - TestContext(), - shared.Query{ - Target: ptr("ACCOUNT"), - }, - ) - if err != nil { - return false - } - if response.StatusCode != 200 { - return false - } - if len(response.Response.Cursor.Data) != 1 { - return false - } - return reflect.DeepEqual(response.Response.Cursor.Data[0], map[string]any{ - "ledger": "default", - "metadata": map[string]any{ - "clientType": "gold", - }, - "address": "foo", - }) - }).Should(BeTrue()) - }) - }) -}) diff --git a/tests/integration/suite/ledger-set-metadata-on-transaction.go b/tests/integration/suite/ledger-set-metadata-on-transaction.go deleted file mode 100644 index f9f9def7d9..0000000000 --- a/tests/integration/suite/ledger-set-metadata-on-transaction.go +++ /dev/null @@ -1,115 +0,0 @@ -package suite - -import ( - "math/big" - "net/http" - "time" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/sdkerrors" - "github.com/formancehq/stack/tests/integration/internal/modules" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - . "github.com/formancehq/stack/tests/integration/internal" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - BeforeEach(func() { - createLedgerResponse, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - }) - When("creating a transaction on a ledger", func() { - var ( - timestamp = time.Now().Round(time.Second).UTC() - rsp *shared.V2CreateTransactionResponse - ) - BeforeEach(func() { - // Create a transaction - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "alice", - }, - }, - Timestamp: ×tamp, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - rsp = response.V2CreateTransactionResponse - - // Check existence on api - getResponse, err := Client().Ledger.V2.GetTransaction( - TestContext(), - operations.V2GetTransactionRequest{ - Ledger: "default", - ID: rsp.Data.ID, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(getResponse.StatusCode).To(Equal(200)) - }) - It("should fail if the transaction does not exist", func() { - metadata := map[string]string{ - "foo": "bar", - } - - _, err := Client().Ledger.V2.AddMetadataOnTransaction( - TestContext(), - operations.V2AddMetadataOnTransactionRequest{ - RequestBody: metadata, - Ledger: "default", - ID: big.NewInt(666), - }, - ) - Expect(err).To(HaveOccurred()) - Expect(err.(*sdkerrors.V2ErrorResponse).ErrorCode).To(Equal(shared.V2ErrorsEnumNotFound)) - }) - Then("adding a metadata", func() { - metadata := map[string]string{ - "foo": "bar", - } - BeforeEach(func() { - response, err := Client().Ledger.V2.AddMetadataOnTransaction( - TestContext(), - operations.V2AddMetadataOnTransactionRequest{ - RequestBody: metadata, - Ledger: "default", - ID: rsp.Data.ID, - }, - ) - Expect(err).To(Succeed()) - Expect(response.StatusCode).To(Equal(204)) - }) - It("should be available on api", func() { - // Check existence on api - response, err := Client().Ledger.V2.GetTransaction( - TestContext(), - operations.V2GetTransactionRequest{ - Ledger: "default", - ID: rsp.Data.ID, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - - Expect(response.V2GetTransactionResponse.Data.Metadata).Should(Equal(metadata)) - }) - }) - }) -}) diff --git a/tests/integration/suite/ledger-update-metadata.go b/tests/integration/suite/ledger-update-metadata.go deleted file mode 100644 index 0f1e364269..0000000000 --- a/tests/integration/suite/ledger-update-metadata.go +++ /dev/null @@ -1,41 +0,0 @@ -package suite - -import ( - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/formancehq/stack/tests/integration/internal/modules" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "net/http" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - When("creating a ledger", func() { - BeforeEach(func() { - response, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(response.StatusCode).To(Equal(http.StatusNoContent)) - }) - Then("updating metadata", func() { - m := map[string]string{ - "foo": "bar", - } - BeforeEach(func() { - _, err := Client().Ledger.V2.UpdateLedgerMetadata(TestContext(), operations.V2UpdateLedgerMetadataRequest{ - Ledger: "default", - RequestBody: m, - }) - Expect(err).To(BeNil()) - }) - It("should be ok", func() { - ledger, err := Client().Ledger.V2.GetLedger(TestContext(), operations.V2GetLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(ledger.V2GetLedgerResponse.Data.Metadata).To(Equal(m)) - }) - }) - }) -}) diff --git a/tests/integration/suite/ledger-use-alternate-bucket.go b/tests/integration/suite/ledger-use-alternate-bucket.go deleted file mode 100644 index 6c53cab5a8..0000000000 --- a/tests/integration/suite/ledger-use-alternate-bucket.go +++ /dev/null @@ -1,112 +0,0 @@ -package suite - -import ( - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - "github.com/formancehq/go-libs/pointer" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/formancehq/stack/tests/integration/internal/modules" - "github.com/google/uuid" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" - "math/big" - "net/http" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - When("Creating a ledger on an alternate bucket", func() { - var ( - ledger1 string - ) - BeforeEach(func() { - ledger1 = uuid.NewString() - response, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - V2CreateLedgerRequest: &shared.V2CreateLedgerRequest{ - Bucket: pointer.For("bucket0"), - }, - Ledger: ledger1, - }) - Expect(err).To(BeNil()) - Expect(response.StatusCode).To(Equal(http.StatusNoContent)) - }) - Then("Creating a tx on this ledger", func() { - BeforeEach(func() { - // Create a transaction - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "alice", - }, - }, - }, - Ledger: ledger1, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - }) - Then("creating another ledger on the same bucket", func() { - var ( - ledger2 string - ) - BeforeEach(func() { - ledger2 = uuid.NewString() - response, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - V2CreateLedgerRequest: &shared.V2CreateLedgerRequest{ - Bucket: pointer.For("bucket0"), - }, - Ledger: ledger2, - }) - Expect(err).To(BeNil()) - Expect(response.StatusCode).To(Equal(http.StatusNoContent)) - }) - Then("Creating another tx on this new ledger", func() { - BeforeEach(func() { - // Create a transaction - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: "alice", - }, - }, - }, - Ledger: ledger2, - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - }) - It("should have one tx on both ledger", func() { - response, err := Client().Ledger.V2.ListTransactions(TestContext(), operations.V2ListTransactionsRequest{ - Ledger: ledger1, - }) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - Expect(response.V2TransactionsCursorResponse.Cursor.Data).To(HaveLen(1)) - - response, err = Client().Ledger.V2.ListTransactions(TestContext(), operations.V2ListTransactionsRequest{ - Ledger: ledger2, - }) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - Expect(response.V2TransactionsCursorResponse.Cursor.Data).To(HaveLen(1)) - }) - }) - }) - }) - }) -}) diff --git a/tests/integration/suite/ledger-v1-get-balances.go b/tests/integration/suite/ledger-v1-get-balances.go deleted file mode 100644 index dc929fc13c..0000000000 --- a/tests/integration/suite/ledger-v1-get-balances.go +++ /dev/null @@ -1,84 +0,0 @@ -package suite - -import ( - "fmt" - "github.com/formancehq/go-libs/pointer" - "math/big" - "net/http" - "time" - - "github.com/formancehq/stack/tests/integration/internal/modules" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - . "github.com/formancehq/stack/tests/integration/internal" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -var _ = WithModules([]*Module{modules.Ledger}, func() { - BeforeEach(func() { - createLedgerResponse, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - }) - const ( - pageSize = int64(10) - txCount = 2 * pageSize - ) - When(fmt.Sprintf("creating %d transactions", txCount), func() { - var ( - timestamp = time.Now().Round(time.Second).UTC() - ) - BeforeEach(func() { - for i := 0; i < int(txCount); i++ { - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(100), - Asset: "USD", - Source: "world", - Destination: fmt.Sprintf("account:%d", i), - }, - }, - Timestamp: ×tamp, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - } - }) - Then("Listing balances using v1 endpoint", func() { - var ( - rsp *operations.GetBalancesResponse - err error - ) - BeforeEach(func() { - rsp, err = Client().Ledger.V1.GetBalances( - TestContext(), - operations.GetBalancesRequest{ - Ledger: "default", - Address: pointer.For("world"), - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(rsp.StatusCode).To(Equal(200)) - }) - It("Should be return non empty balances", func() { - Expect(rsp.BalancesCursorResponse.Cursor.Data).To(HaveLen(1)) - balances := rsp.BalancesCursorResponse.Cursor.Data[0] - Expect(balances).To(HaveKey("world")) - Expect(balances["world"]).To(HaveKey("USD")) - Expect(balances["world"]["USD"]).To(Equal(int64(-2000))) - }) - }) - }) -}) diff --git a/tests/integration/suite/ledger-volumes-list.go b/tests/integration/suite/ledger-volumes-list.go deleted file mode 100644 index 9c42527267..0000000000 --- a/tests/integration/suite/ledger-volumes-list.go +++ /dev/null @@ -1,303 +0,0 @@ -package suite - -import ( - "fmt" - "math/big" - "net/http" - "time" - - "github.com/formancehq/formance-sdk-go/v3/pkg/models/operations" - "github.com/formancehq/formance-sdk-go/v3/pkg/models/shared" - . "github.com/formancehq/stack/tests/integration/internal" - "github.com/formancehq/stack/tests/integration/internal/modules" - . "github.com/onsi/ginkgo/v2" - . "github.com/onsi/gomega" -) - -type Transaction struct { - Amount int64 - Asset string - Source string - Destination string - EffectiveDate time.Time -} - -var now = time.Now() - -var transactions = []Transaction{ - Transaction{Amount: 100, Asset: "USD", Source: "world", Destination: "account:user1", EffectiveDate: now.Add(-4 * time.Hour)}, //user1:100, world:-100 - Transaction{Amount: 125, Asset: "USD", Source: "world", Destination: "account:user2", EffectiveDate: now.Add(-3 * time.Hour)}, //user1:100, user2:125, world:-225 - Transaction{Amount: 75, Asset: "USD", Source: "account:user1", Destination: "account:user2", EffectiveDate: now.Add(-2 * time.Hour)}, //user1:25, user2:200, world:-200 - Transaction{Amount: 175, Asset: "USD", Source: "world", Destination: "account:user1", EffectiveDate: now.Add(-1 * time.Hour)}, //user1:200, user2:200, world:-400 - Transaction{Amount: 50, Asset: "USD", Source: "account:user2", Destination: "bank", EffectiveDate: now}, //user1:200, user2:150, world:-400, bank:50 - Transaction{Amount: 100, Asset: "USD", Source: "account:user2", Destination: "account:user1", EffectiveDate: now.Add(1 * time.Hour)}, //user1:300, user2:50, world:-400, bank:50 - Transaction{Amount: 150, Asset: "USD", Source: "account:user1", Destination: "bank", EffectiveDate: now.Add(2 * time.Hour)}, //user1:150, user2:50, world:-400, bank:200 -} - -var _ = WithModules([]*Module{modules.Ledger}, func() { - - BeforeEach(func() { - createLedgerResponse, err := Client().Ledger.V2.CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ - Ledger: "default", - }) - Expect(err).To(BeNil()) - Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) - - for _, transaction := range transactions { - response, err := Client().Ledger.V2.CreateTransaction( - TestContext(), - operations.V2CreateTransactionRequest{ - V2PostTransaction: shared.V2PostTransaction{ - Metadata: map[string]string{}, - Postings: []shared.V2Posting{ - { - Amount: big.NewInt(transaction.Amount), - Asset: transaction.Asset, - Source: transaction.Source, - Destination: transaction.Destination, - }, - }, - Timestamp: &transaction.EffectiveDate, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - } - }) - - When(fmt.Sprint("Get current Volumes and Balances From origin of time till now (insertion-date)"), func() { - - It("should be ok", func() { - - inserDate := true - - response, err := Client().Ledger.V2.GetVolumesWithBalances( - TestContext(), - operations.V2GetVolumesWithBalancesRequest{ - InsertionDate: &inserDate, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - ret := response.V2VolumesWithBalanceCursorResponse - Expect(len(ret.Cursor.Data)).To(Equal(4)) - for _, volume := range ret.Cursor.Data { - if volume.Account == "account:user1" { - Expect(volume.Balance).To(Equal(big.NewInt(150))) - } - if volume.Account == "account:user2" { - Expect(volume.Balance).To(Equal(big.NewInt(50))) - } - - if volume.Account == "bank" { - Expect(volume.Balance).To(Equal(big.NewInt(200))) - } - - if volume.Account == "world" { - Expect(volume.Balance).To(Equal(big.NewInt(-400))) - } - - } - - }) - - }) - - When(fmt.Sprint("Get Volumes and Balances From oot til oot+2 hours (effectiveDate) "), func() { - - It("should be ok", func() { - - startTime := now.Add(-4 * time.Hour) - stopTime := now.Add(-2 * time.Hour) - - response, err := Client().Ledger.V2.GetVolumesWithBalances( - TestContext(), - operations.V2GetVolumesWithBalancesRequest{ - StartTime: &startTime, - EndTime: &stopTime, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - ret := response.V2VolumesWithBalanceCursorResponse - Expect(len(ret.Cursor.Data)).To(Equal(3)) - for _, volume := range ret.Cursor.Data { - //fmt.Println(fmt.Sprintf("%s | %s | %d | %d | %d", volume.Account, volume.Asset, volume.Input, volume.Output, volume.Balance)) - - if volume.Account == "account:user1" { - Expect(volume.Balance).To(Equal(big.NewInt(25))) - } - if volume.Account == "account:user2" { - Expect(volume.Balance).To(Equal(big.NewInt(200))) - } - - if volume.Account == "world" { - Expect(volume.Balance).To(Equal(big.NewInt(-225))) - } - - } - - }) - - }) - - When(fmt.Sprint("Get Volumes and Balances Filter by address account"), func() { - - It("should be ok", func() { - inserDate := true - response, err := Client().Ledger.V2.GetVolumesWithBalances( - TestContext(), - operations.V2GetVolumesWithBalancesRequest{ - InsertionDate: &inserDate, - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "account": "account:", - }, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - ret := response.V2VolumesWithBalanceCursorResponse - Expect(len(ret.Cursor.Data)).To(Equal(2)) - for _, volume := range ret.Cursor.Data { - //fmt.Println(fmt.Sprintf("%s | %s | %d | %d | %d", volume.Account, volume.Asset, volume.Input, volume.Output, volume.Balance)) - - if volume.Account == "account:user1" { - Expect(volume.Balance).To(Equal(big.NewInt(150))) - } - if volume.Account == "account:user2" { - Expect(volume.Balance).To(Equal(big.NewInt(50))) - } - - } - - }) - - }) - - When(fmt.Sprint("Get Volumes and Balances Filter by address account a,d and end-time now effective"), func() { - - It("should be ok", func() { - - response, err := Client().Ledger.V2.GetVolumesWithBalances( - TestContext(), - operations.V2GetVolumesWithBalancesRequest{ - - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "account": "account:", - }, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - ret := response.V2VolumesWithBalanceCursorResponse - Expect(len(ret.Cursor.Data)).To(Equal(2)) - for _, volume := range ret.Cursor.Data { - //fmt.Println(fmt.Sprintf("%s | %s | %d | %d | %d", volume.Account, volume.Asset, volume.Input, volume.Output, volume.Balance)) - if volume.Account == "account:user1" { - Expect(volume.Balance).To(Equal(big.NewInt(200))) - } - if volume.Account == "account:user2" { - Expect(volume.Balance).To(Equal(big.NewInt(150))) - } - - } - - }) - - }) - - When(fmt.Sprint("Get Volumes and Balances Filter by address account which doesn't exist"), func() { - - It("should be ok", func() { - - response, err := Client().Ledger.V2.GetVolumesWithBalances( - TestContext(), - operations.V2GetVolumesWithBalancesRequest{ - - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "account": "foo:", - }, - }, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - ret := response.V2VolumesWithBalanceCursorResponse - Expect(len(ret.Cursor.Data)).To(Equal(0)) - - }) - - }) - - When(fmt.Sprint("Get Volumes and Balances Filter With futures dates empty"), func() { - - It("should be ok", func() { - - startDate := time.Now().Add(8 * time.Hour) - endDate := time.Now().Add(12 * time.Hour) - - response, err := Client().Ledger.V2.GetVolumesWithBalances( - TestContext(), - operations.V2GetVolumesWithBalancesRequest{ - StartTime: &startDate, - EndTime: &endDate, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - ret := response.V2VolumesWithBalanceCursorResponse - Expect(len(ret.Cursor.Data)).To(Equal(0)) - - }) - - }) - - When(fmt.Sprint("Get Volumes and Balances Filter by address account aggregation by level 1"), func() { - - It("should be ok", func() { - inserDate := true - groupBylvl := int64(1) - response, err := Client().Ledger.V2.GetVolumesWithBalances( - TestContext(), - operations.V2GetVolumesWithBalancesRequest{ - InsertionDate: &inserDate, - RequestBody: map[string]interface{}{ - "$match": map[string]any{ - "account": "account:", - }, - }, - GroupBy: &groupBylvl, - Ledger: "default", - }, - ) - Expect(err).ToNot(HaveOccurred()) - Expect(response.StatusCode).To(Equal(200)) - ret := response.V2VolumesWithBalanceCursorResponse - Expect(len(ret.Cursor.Data)).To(Equal(1)) - for _, volume := range ret.Cursor.Data { - //fmt.Println(fmt.Sprintf("%s | %s | %d | %d | %d", volume.Account, volume.Asset, volume.Input, volume.Output, volume.Balance)) - - if volume.Account == "account" { - Expect(volume.Balance).To(Equal(big.NewInt(200))) - } - - } - - }) - - }) - -})