Files
apskel-pos-backend/internal/models/loyalty_test.go

41 lines
1.6 KiB
Go

package models
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestLoyaltyCashbackPercent(t *testing.T) {
assert.Equal(t, 1.0, LoyaltyCashbackPercent(1, 1, 100))
assert.Equal(t, 0.1, LoyaltyCashbackPercent(1, 1, 1000))
assert.Equal(t, 0.33, LoyaltyCashbackPercent(1, 1, 300), "rounded to two decimals")
assert.Equal(t, 0.67, LoyaltyCashbackPercent(2, 1, 300), "rounded, not cut")
assert.Equal(t, 0.0, LoyaltyCashbackPercent(1, 1, 0))
}
func TestLoyaltyEarnSettings_CashbackPercent(t *testing.T) {
s := LoyaltyEarnSettings{EarnMode: "PER_AMOUNT", EarnPerAmount: 100, EarnValue: 1, EarnPercent: 2.5}
assert.Equal(t, 1.0, s.CashbackPercent(1))
s.EarnMode = "PERCENTAGE"
assert.Equal(t, 2.5, s.CashbackPercent(1))
assert.Equal(t, 250.0, s.CashbackPercent(100), "each point earned is worth point_value rupiah")
}
func TestNewLoyaltySettingsImpact(t *testing.T) {
before := OrganizationLoyaltySettings{PointValue: 1, Exchange: LoyaltyExchangeSettings{CoinAmount: 1, PointAmount: 1}}
after := OrganizationLoyaltySettings{PointValue: 100, Exchange: LoyaltyExchangeSettings{CoinAmount: 10, PointAmount: 1}}
got := NewLoyaltySettingsImpact(50000, 105, before, after)
assert.Equal(t, LoyaltySettingsImpact{
OutstandingPoints: 50000, OutstandingCoins: 105,
PointValueBefore: 1, PointValueAfter: 100,
PointRupiahBefore: 50000, PointRupiahAfter: 5000000,
CoinsAsPointsBefore: 105, CoinsAsPointsAfter: 10, // floor(105 / 10)
CoinRupiahBefore: 105, CoinRupiahAfter: 1000,
}, got)
same := NewLoyaltySettingsImpact(10, 10, before, before)
assert.Equal(t, same.PointRupiahBefore, same.PointRupiahAfter)
}