From 75fb65c522bf96ee7fd24adac163fec9f172b38b Mon Sep 17 00:00:00 2001 From: bruwbird Date: Thu, 1 Feb 2024 10:10:28 +0900 Subject: [PATCH] swap: correction of swapout in CheckPremiumAmount Corrected where to get premium limit at swapout in CheckPremiumAmount. Also, unit tests for CheckPremiumAmount have been added. --- swap/actions.go | 2 +- swap/actions_test.go | 73 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 swap/actions_test.go diff --git a/swap/actions.go b/swap/actions.go index 5c5a6a62..214db7b9 100644 --- a/swap/actions.go +++ b/swap/actions.go @@ -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) } diff --git a/swap/actions_test.go b/swap/actions_test.go new file mode 100644 index 00000000..99212d47 --- /dev/null +++ b/swap/actions_test.go @@ -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") + }) + } +}