Skip to content

Commit

Permalink
feat: test save metadata event for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent c839ff8 commit 7f7f3cd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
6 changes: 4 additions & 2 deletions pkg/testserver/matchers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
}
}
45 changes: 32 additions & 13 deletions test/e2e/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 7f7f3cd

Please sign in to comment.