Skip to content

Commit

Permalink
Addressing review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
urvisavla committed Sep 19, 2023
1 parent ae514ba commit 40d3587
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (q *Q) GetClaimableBalances(ctx context.Context, query ClaimableBalancesQue

var results []ClaimableBalance
if err := q.Select(ctx, &results, sql); err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("could not run select query"))
return nil, errors.Wrap(err, "could not run select query")
}

return results, nil
Expand Down
56 changes: 33 additions & 23 deletions services/horizon/internal/db2/history/claimable_balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,25 +249,25 @@ func insertClaimants(q *Q, tt *test.T, cBalance ClaimableBalance) error {
return claimantsInsertBuilder.Exec(tt.Ctx)
}

func validateClaimableBalanceQuery(t *test.T, q *Q, query ClaimableBalancesQuery, expectedLen int, expectedClaimants []string, expectedAsset string, expectedSponsor string) {
type claimableBalanceQueryResult struct {
Claimants []string
Asset string
Sponsor string
}

func validateClaimableBalanceQuery(t *test.T, q *Q, query ClaimableBalancesQuery, expectedQueryResult []claimableBalanceQueryResult) {
cbs, err := q.GetClaimableBalances(t.Ctx, query)
t.Assert.NoError(err)
t.Assert.Len(cbs, expectedLen)

if expectedLen > 0 {
t.Assert.Equal(expectedClaimants[0], cbs[0].Claimants[0].Destination)
}

if expectedLen > 1 {
t.Assert.Equal(expectedClaimants[1], cbs[0].Claimants[1].Destination)
}

if expectedAsset != "" {
t.Assert.Equal(expectedAsset, cbs[0].Asset.String())
}

if expectedSponsor != "" {
t.Assert.Equal(expectedSponsor, cbs[0].Sponsor.String)
for i, expected := range expectedQueryResult {
for j, claimant := range expected.Claimants {
t.Assert.Equal(claimant, cbs[i].Claimants[j].Destination)
}
if expected.Asset != "" {
t.Assert.Equal(expected.Asset, cbs[i].Asset.String())
}
if expected.Sponsor != "" {
t.Assert.Equal(expected.Sponsor, cbs[i].Sponsor.String)
}
}
}

Expand Down Expand Up @@ -360,7 +360,9 @@ func TestFindClaimableBalancesByDestinationWithLimit(t *testing.T) {
query := ClaimableBalancesQuery{
PageQuery: pageQuery,
}
validateClaimableBalanceQuery(tt, q, query, 1, []string{dest1, dest2}, "", "")
validateClaimableBalanceQuery(tt, q, query, []claimableBalanceQueryResult{
{Claimants: []string{dest1, dest2}},
})

// invalid claimant parameter
query = ClaimableBalancesQuery{
Expand All @@ -369,30 +371,36 @@ func TestFindClaimableBalancesByDestinationWithLimit(t *testing.T) {
Asset: &asset2,
Sponsor: xdr.MustAddressPtr(sponsor1),
}
validateClaimableBalanceQuery(tt, q, query, 0, []string{}, "", "")
validateClaimableBalanceQuery(tt, q, query, []claimableBalanceQueryResult{})

// claimant parameter, no filters
query = ClaimableBalancesQuery{
PageQuery: pageQuery,
Claimant: xdr.MustAddressPtr(dest1),
}
validateClaimableBalanceQuery(tt, q, query, 1, []string{dest1, dest2}, "", "")
validateClaimableBalanceQuery(tt, q, query, []claimableBalanceQueryResult{
{Claimants: []string{dest1, dest2}},
})

// claimant parameter, asset filter
query = ClaimableBalancesQuery{
PageQuery: pageQuery,
Claimant: xdr.MustAddressPtr(dest2),
Asset: &asset1,
}
validateClaimableBalanceQuery(tt, q, query, 1, []string{dest1, dest2}, asset1.String(), "")
validateClaimableBalanceQuery(tt, q, query, []claimableBalanceQueryResult{
{Claimants: []string{dest1, dest2}, Asset: asset1.String()},
})

// claimant parameter, sponsor filter
query = ClaimableBalancesQuery{
PageQuery: pageQuery,
Claimant: xdr.MustAddressPtr(dest2),
Sponsor: xdr.MustAddressPtr(sponsor1),
}
validateClaimableBalanceQuery(tt, q, query, 1, []string{dest1, dest2}, "", sponsor1)
validateClaimableBalanceQuery(tt, q, query, []claimableBalanceQueryResult{
{Claimants: []string{dest1, dest2}, Sponsor: sponsor1},
})

//claimant parameter, asset filter, sponsor filter
query = ClaimableBalancesQuery{
Expand All @@ -401,7 +409,9 @@ func TestFindClaimableBalancesByDestinationWithLimit(t *testing.T) {
Asset: &asset2,
Sponsor: xdr.MustAddressPtr(sponsor2),
}
validateClaimableBalanceQuery(tt, q, query, 1, []string{dest2}, asset2.String(), sponsor2)
validateClaimableBalanceQuery(tt, q, query, []claimableBalanceQueryResult{
{Claimants: []string{dest2}, Asset: asset2.String(), Sponsor: sponsor2},
})
}

func TestUpdateClaimableBalance(t *testing.T) {
Expand Down

0 comments on commit 40d3587

Please sign in to comment.