From c93415f0687a0f819df40fcd04af25ff9092bb86 Mon Sep 17 00:00:00 2001 From: Geoffrey Ragot Date: Fri, 27 Sep 2024 16:58:28 +0200 Subject: [PATCH] feat: test save metadata event for transactions --- pkg/testserver/matchers.go | 6 +++-- test/e2e/integration_test.go | 45 +++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/pkg/testserver/matchers.go b/pkg/testserver/matchers.go index e1766ed2c..3c2b16140 100644 --- a/pkg/testserver/matchers.go +++ b/pkg/testserver/matchers.go @@ -88,7 +88,9 @@ func (e *EventMatcher) Match(actual any) (success bool, err error) { return false, fmt.Errorf("unable to unmarshal msg: %s", err) } - Expect(ev.Type).To(Equal(e.eventName)) + if ev.Type != e.eventName { + return false, nil + } rawSchema := jsonschema.Reflect(e.expected) data, err := json.Marshal(rawSchema) @@ -159,7 +161,7 @@ var _ types.GomegaMatcher = (*EventMatcher)(nil) func Event(eventName string, expected any) types.GomegaMatcher { return &EventMatcher{ - expected: expected, + expected: expected, eventName: eventName, } } diff --git a/test/e2e/integration_test.go b/test/e2e/integration_test.go index 4ddcb411c..c97255a86 100644 --- a/test/e2e/integration_test.go +++ b/test/e2e/integration_test.go @@ -9,7 +9,7 @@ import ( "github.com/formancehq/go-libs/metadata" "github.com/formancehq/go-libs/time" "github.com/formancehq/ledger/internal/bus" - "github.com/formancehq/ledger/pkg/events" + ledgerevents "github.com/formancehq/ledger/pkg/events" "github.com/nats-io/nats.go" "io" "math/big" @@ -41,6 +41,11 @@ var _ = Context("Ledger integration tests", func() { NatsURL: natsServer.GetValue().ClientURL(), } }) + var events chan *nats.Msg + BeforeEach(func() { + events = testServer.GetValue().Subscribe() + }) + When("starting the service", func() { It("should be ok", func() { info, err := testServer.GetValue().Client().Ledger.V2.GetInfo(ctx) @@ -134,18 +139,9 @@ var _ = Context("Ledger integration tests", func() { It("should be ok", func() { Expect(err).To(BeNil()) checkTx() - }) - It("should be ok", func() { - Expect(err).To(BeNil()) - checkTx() - }) - Context("when listening on event", func() { - var msgs chan *nats.Msg - BeforeEach(func() { - msgs = testServer.GetValue().Subscribe() - }) - It("should receive an event", func() { - Eventually(msgs).Should(Receive(Event(events.EventTypeCommittedTransactions, bus.CommittedTransactions{ + + By("it should also send an event", func() { + Eventually(events).Should(Receive(Event(ledgerevents.EventTypeCommittedTransactions, bus.CommittedTransactions{ Ledger: "foo", Transactions: []ledger.Transaction{{ TransactionData: ledger.TransactionData{ @@ -178,6 +174,29 @@ var _ = Context("Ledger integration tests", func() { }))) }) }) + Context("when adding a metadata on the newly created transaction", func() { + metadata := map[string]string{ + "status": "succeeded", + } + JustBeforeEach(func() { + err := AddMetadataToTransaction(ctx, testServer.GetValue(), operations.V2AddMetadataOnTransactionRequest{ + Ledger: createLedgerRequest.Ledger, + ID: tx.ID, + RequestBody: metadata, + }) + Expect(err).To(BeNil()) + }) + It("should be ok", func() { + By("it should send an event", func() { + Eventually(events).Should(Receive(Event(ledgerevents.EventTypeSavedMetadata, bus.SavedMetadata{ + Ledger: createLedgerRequest.Ledger, + TargetType: ledger.MetaTargetTypeTransaction, + TargetID: tx.ID.String(), + Metadata: metadata, + }))) + }) + }) + }) It("should be listable on api", func() { txs, err := ListTransactions(ctx, testServer.GetValue(), operations.V2ListTransactionsRequest{ Ledger: createLedgerRequest.Ledger,