Skip to content

Commit

Permalink
swap: correction of swapout in CheckPremiumAmount
Browse files Browse the repository at this point in the history
Corrected where to get premium limit at swapout in CheckPremiumAmount.
Also, unit tests for CheckPremiumAmount have been added.
  • Loading branch information
YusukeShimizu committed Feb 1, 2024
1 parent 9c34c47 commit 75fb65c
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion swap/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func (v *CheckPremiumAmount) Execute(services *SwapServices, swap *SwapData) Eve
} else if swap.SwapOutAgreement != nil {
if swap.SwapOutAgreement.Premium > swap.SwapOutRequest.PremiumLimit {
return swap.HandleError(fmt.Errorf("premium amt too high: %d, limit : %d",
swap.SwapOutAgreement.Premium, swap.SwapInRequest.PremiumLimit))
swap.SwapOutAgreement.Premium, swap.SwapOutRequest.PremiumLimit))
}
return v.next.Execute(services, swap)
}
Expand Down
73 changes: 73 additions & 0 deletions swap/actions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package swap

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCheckPremiumAmount_Execute(t *testing.T) {
t.Parallel()
next := &NoOpAction{}
v := CheckPremiumAmount{
next: next,
}
tests := map[string]struct {
swap *SwapData
want EventType
}{
"swap in premium amount is within limit": {
swap: &SwapData{
SwapInAgreement: &SwapInAgreementMessage{
Premium: 100,
},
SwapInRequest: &SwapInRequestMessage{
PremiumLimit: 200,
},
},
want: NoOp,
},
"swap in premium amount exceeds limit": {
swap: &SwapData{
SwapInAgreement: &SwapInAgreementMessage{
Premium: 300,
},
SwapInRequest: &SwapInRequestMessage{
PremiumLimit: 200,
},
},
want: Event_ActionFailed,
},
"swap out premium amount is within limit": {
swap: &SwapData{
SwapOutAgreement: &SwapOutAgreementMessage{
Premium: 100,
},
SwapOutRequest: &SwapOutRequestMessage{
PremiumLimit: 200,
},
},
want: NoOp,
},
"swap out premium amount exceeds limit": {
swap: &SwapData{
SwapOutAgreement: &SwapOutAgreementMessage{
Premium: 300,
},
SwapOutRequest: &SwapOutRequestMessage{
PremiumLimit: 200,
},
},
want: Event_ActionFailed,
},
}

for name, tt := range tests {
tt := tt
t.Run(name, func(t *testing.T) {
t.Parallel()
got := v.Execute(nil, tt.swap)
assert.Equal(t, tt.want, got, "Event type should match")
})
}
}

0 comments on commit 75fb65c

Please sign in to comment.