Skip to content

Commit

Permalink
feat: move code
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent 1f1034a commit 0c39cbe
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 54 deletions.
63 changes: 63 additions & 0 deletions pkg/testserver/matchers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package testserver

import (
"context"
"fmt"
"github.com/formancehq/go-libs/pointer"
"github.com/formancehq/stack/ledger/client/models/operations"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
"math/big"
)

type HaveCoherentStateMatcher struct{}

func (h HaveCoherentStateMatcher) Match(actual interface{}) (success bool, err error) {
srv, ok := actual.(*Server)
if !ok {
return false, fmt.Errorf("expect type %T", new(Server))
}
ctx := context.Background()

ledgers, err := ListLedgers(ctx, srv, operations.V2ListLedgersRequest{
PageSize: pointer.For(int64(100)),
})
if err != nil {
return false, err
}

for _, ledger := range ledgers.Data {
aggregatedBalances, err := GetAggregatedBalances(ctx, srv, operations.V2GetBalancesAggregatedRequest{
Ledger: ledger.Name,
UseInsertionDate: pointer.For(true),
})
Expect(err).To(BeNil())
if len(aggregatedBalances) == 0 { // it's random, a ledger could not have been targeted
// just in case, check if the ledger has transactions
txs, err := ListTransactions(ctx, srv, operations.V2ListTransactionsRequest{
Ledger: ledger.Name,
})
Expect(err).To(BeNil())
Expect(txs.Data).To(HaveLen(0))
} else {
Expect(aggregatedBalances).To(HaveLen(1))
Expect(aggregatedBalances["USD"]).To(Equal(big.NewInt(0)))
}
}

return true, nil
}

func (h HaveCoherentStateMatcher) FailureMessage(_ interface{}) (message string) {
return "server should has coherent state"
}

func (h HaveCoherentStateMatcher) NegatedFailureMessage(_ interface{}) (message string) {
return "server should not has coherent state but has"
}

var _ types.GomegaMatcher = (*HaveCoherentStateMatcher)(nil)

func HaveCoherentState() *HaveCoherentStateMatcher {
return &HaveCoherentStateMatcher{}
}
54 changes: 0 additions & 54 deletions test/e2e/stress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package test_suite

import (
"context"
"fmt"
"math/big"
"math/rand"
Expand All @@ -20,7 +19,6 @@ import (
"github.com/formancehq/stack/ledger/client/models/operations"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
)

var _ = Context("Ledger stress tests", func() {
Expand Down Expand Up @@ -147,55 +145,3 @@ var _ = Context("Ledger stress tests", func() {
})
})
})

type HaveCoherentStateMatcher struct{}

func (h HaveCoherentStateMatcher) Match(actual interface{}) (success bool, err error) {
srv, ok := actual.(*Server)
if !ok {
return false, fmt.Errorf("expect type %T", new(Server))
}
ctx := context.Background()

ledgers, err := ListLedgers(ctx, srv, operations.V2ListLedgersRequest{
PageSize: pointer.For(int64(100)),
})
if err != nil {
return false, err
}

for _, ledger := range ledgers.Data {
aggregatedBalances, err := GetAggregatedBalances(ctx, srv, operations.V2GetBalancesAggregatedRequest{
Ledger: ledger.Name,
UseInsertionDate: pointer.For(true),
})
Expect(err).To(BeNil())
if len(aggregatedBalances) == 0 { // it's random, a ledger could not have been targeted
// just in case, check if the ledger has transactions
txs, err := ListTransactions(ctx, srv, operations.V2ListTransactionsRequest{
Ledger: ledger.Name,
})
Expect(err).To(BeNil())
Expect(txs.Data).To(HaveLen(0))
} else {
Expect(aggregatedBalances).To(HaveLen(1))
Expect(aggregatedBalances["USD"]).To(Equal(big.NewInt(0)))
}
}

return true, nil
}

func (h HaveCoherentStateMatcher) FailureMessage(_ interface{}) (message string) {
return "server should has coherent state"
}

func (h HaveCoherentStateMatcher) NegatedFailureMessage(_ interface{}) (message string) {
return "server should not has coherent state but has"
}

var _ types.GomegaMatcher = (*HaveCoherentStateMatcher)(nil)

func HaveCoherentState() *HaveCoherentStateMatcher {
return &HaveCoherentStateMatcher{}
}

0 comments on commit 0c39cbe

Please sign in to comment.