Skip to content

Commit

Permalink
loans: Update mocks and add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cdamian committed Apr 10, 2024
1 parent 613ca54 commit 0209b88
Show file tree
Hide file tree
Showing 6 changed files with 452 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# build workflow builds docker images, pushes images to docker hub and updates swagger API
on:
push:
branches: [main, invest-endpoint-loan-retrieval-fix]
branches: [main]
name: Build
jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/ChainSafe/go-schnorrkel v1.0.0
github.com/Masterminds/semver v1.5.0
github.com/centrifuge/centrifuge-protobufs v1.0.0
github.com/centrifuge/chain-custom-types v1.0.8
github.com/centrifuge/chain-custom-types v1.0.9
github.com/centrifuge/go-substrate-rpc-client/v4 v4.1.0
github.com/centrifuge/gocelery/v2 v2.0.0-20221101190423-3b07af1b49a6
github.com/centrifuge/precise-proofs v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QH
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/centrifuge/centrifuge-protobufs v1.0.0 h1:ZPg0XpkTrGrjQu8scXjMGs7jjqsWPiXmOXdV/bz30ng=
github.com/centrifuge/centrifuge-protobufs v1.0.0/go.mod h1:VL6mcnK6vTRiFljHP39J0WBI3Uu5BHQjhdFkCxY9/9I=
github.com/centrifuge/chain-custom-types v1.0.8 h1:JcXQNzjzs1y/xEBK23XlRJeD1OxLZByfnwstQkORuIg=
github.com/centrifuge/chain-custom-types v1.0.8/go.mod h1:kSUJ3O83vaLutJIiaEfqwn3lfTaisn/G/baS8WrycTg=
github.com/centrifuge/chain-custom-types v1.0.9 h1:utkYu/8Tgze6xktHMZ9wgcDHXUsM2yWuwSHL2YqfZ+8=
github.com/centrifuge/chain-custom-types v1.0.9/go.mod h1:kSUJ3O83vaLutJIiaEfqwn3lfTaisn/G/baS8WrycTg=
github.com/centrifuge/go-merkle v0.0.0-20190727075423-0ac78bbbc01b h1:TPvvMcGAc3TVBVgQ4XYYEWTXxYls8YuylZ8JzrVxPzc=
github.com/centrifuge/go-merkle v0.0.0-20190727075423-0ac78bbbc01b/go.mod h1:0voJY6Qzxvr2S0LeDSFQiCnJzGq5gORg2SwCmn8602I=
github.com/centrifuge/go-substrate-rpc-client/v4 v4.1.0 h1:GEvub7kU5YFAcn5A2uOo4AZSM1/cWZCOvfu7E3gQmK8=
Expand Down
38 changes: 6 additions & 32 deletions pallets/loans/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,15 @@ type CreatedLoanStorageEntry struct {

type ActiveLoanStorageEntry struct {
LoanID types.U64
ActiveLoan ActiveLoan
}

type ActiveLoan struct {
Schedule loans.RepaymentSchedule
Collateral loans.Asset
Restrictions loans.LoanRestrictions
Borrower types.AccountID
WriteOffPercentage types.U128
OriginationDate types.U64
Pricing loans.Pricing
TotalBorrowed types.U128
TotalRepaid RepaidAmount
RepaymentsOnScheduleUntil types.U64
}

type RepaidAmount struct {
Principal types.U128
Interest types.U128
Unscheduled types.U128
}

type ClosedLoan struct {
ClosedAt types.U32
Info loans.LoanInfo
TotalBorrowed types.U128
TotalRepaid RepaidAmount
ActiveLoan loans.ActiveLoan
}

//go:generate mockery --name API --structname APIMock --filename api_mock.go --inpackage

type API interface {
GetCreatedLoan(poolID types.U64, loanID types.U64) (*CreatedLoanStorageEntry, error)
GetActiveLoan(poolID types.U64, loanID types.U64) (*ActiveLoan, error)
GetClosedLoan(poolID types.U64, loanID types.U64) (*ClosedLoan, error)
GetActiveLoan(poolID types.U64, loanID types.U64) (*loans.ActiveLoan, error)
GetClosedLoan(poolID types.U64, loanID types.U64) (*loans.ClosedLoan, error)
}

type api struct {
Expand Down Expand Up @@ -152,7 +126,7 @@ func (a *api) GetCreatedLoan(poolID types.U64, loanID types.U64) (*CreatedLoanSt
return &createdLoan, nil
}

func (a *api) GetActiveLoan(poolID types.U64, loanID types.U64) (*ActiveLoan, error) {
func (a *api) GetActiveLoan(poolID types.U64, loanID types.U64) (*loans.ActiveLoan, error) {
err := validation.Validate(
validation.NewValidator(poolID, validation.U64ValidationFn),
)
Expand Down Expand Up @@ -219,7 +193,7 @@ func (a *api) GetActiveLoan(poolID types.U64, loanID types.U64) (*ActiveLoan, er
return nil, ErrActiveLoanNotFound
}

func (a *api) GetClosedLoan(poolID types.U64, loanID types.U64) (*ClosedLoan, error) {
func (a *api) GetClosedLoan(poolID types.U64, loanID types.U64) (*loans.ClosedLoan, error) {
err := validation.Validate(
validation.NewValidator(poolID, validation.U64ValidationFn),
)
Expand Down Expand Up @@ -268,7 +242,7 @@ func (a *api) GetClosedLoan(poolID types.U64, loanID types.U64) (*ClosedLoan, er
return nil, errors.ErrStorageKeyCreation
}

var closedLoan ClosedLoan
var closedLoan loans.ClosedLoan

ok, err := a.centAPI.GetStorageLatest(storageKey, &closedLoan)

Expand Down
47 changes: 47 additions & 0 deletions pallets/loans/api_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0209b88

Please sign in to comment.