Files
mats aa8b2bdedc
Build and Publish / BuildAndDeployAmd64 (push) Failing after 5s
Build and Publish / BuildAndDeployArm64 (push) Failing after 6s
all the refactor
2026-06-28 17:51:52 +02:00

96 lines
2.2 KiB
Go

package cart
import (
"testing"
)
func TestNordicTaxProvider_Name(t *testing.T) {
p := NewNordicTaxProvider("SE")
if got := p.Name(); got != "nordic" {
t.Errorf("Name() = %q, want %q", got, "nordic")
}
}
func TestNordicTaxProvider_Compute(t *testing.T) {
// taxRate is basis points (2500 = 25%).
tests := []struct {
total int64
taxRate int
want int64
desc string
}{
{0, 2500, 0, "zero total"},
{1250, 2500, 250, "25 % VAT on 1250"},
{1000, 2000, 167, "20 % VAT on 1000"},
{1200, 2500, 240, "25 % VAT on 1200"},
{25000, 2500, 5000, "25 % VAT on 25000"},
{100, 1000, 10, "10 % VAT on 100"},
{100, 10000, 50, "100 % VAT on 100"},
{100, 0, 0, "zero-rate"},
{100, -1, 0, "negative rate -> zero"},
}
for _, tt := range tests {
p := NewNordicTaxProvider("SE")
got := p.Compute(tt.total, tt.taxRate)
if got != tt.want {
t.Errorf("Compute(%d, %d) [%s] = %d; want %d", tt.total, tt.taxRate, tt.desc, got, tt.want)
}
}
}
func TestNordicTaxProvider_DefaultTaxRate(t *testing.T) {
tests := []struct {
country string
want int
desc string
}{
{"SE", 2500, "Sweden"},
{"NO", 2500, "Norway"},
{"DK", 2500, "Denmark"},
{"FI", 2550, "Finland (25.5%)"},
{"", 2500, "empty -> default SE"},
{"DE", 2500, "unknown -> fallback SE"},
}
for _, tt := range tests {
p := NewNordicTaxProvider("SE")
got := p.DefaultTaxRate(tt.country)
if got != tt.want {
t.Errorf("DefaultTaxRate(%q) [%s] = %d; want %d", tt.country, tt.desc, got, tt.want)
}
}
}
func TestStaticTaxProvider_Compute(t *testing.T) {
p := NewStaticTaxProvider()
tests := []struct {
total int64
taxRate int
want int64
}{
{1250, 2500, 250},
{25000, 2500, 5000},
{1000, 2000, 167},
{0, 2500, 0},
}
for _, tt := range tests {
got := p.Compute(tt.total, tt.taxRate)
if got != tt.want {
t.Errorf("Compute(%d, %d) = %d; want %d", tt.total, tt.taxRate, got, tt.want)
}
}
}
func TestStaticTaxProvider_DefaultTaxRate(t *testing.T) {
p := NewStaticTaxProvider()
if got := p.DefaultTaxRate("SE"); got != 0 {
t.Errorf("StaticTaxProvider.DefaultTaxRate(\"SE\") = %d; want 0", got)
}
}
func TestTaxProviderImplementsInterface(t *testing.T) {
var p TaxProvider = NewNordicTaxProvider("SE")
_ = p
p2 := NewStaticTaxProvider()
_ = p2
}