50 lines
1.9 KiB
Go
50 lines
1.9 KiB
Go
package cash
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func validTransferInput() transferInput {
|
|
return transferInput{FromRegisterID: "reg1", ToRegisterID: "reg2", Method: "cash", Amount: "500.00"}
|
|
}
|
|
|
|
func TestTransferInputValidate(t *testing.T) {
|
|
longAmount := strings.Repeat("1", maxAmountLen+1)
|
|
longNote := strings.Repeat("a", maxLongFieldLen+1)
|
|
|
|
tests := []struct {
|
|
name string
|
|
mutate func(b *transferInput)
|
|
wantErr bool
|
|
}{
|
|
{"valid transfer", func(b *transferInput) {}, false},
|
|
{"missing from_register_id", func(b *transferInput) { b.FromRegisterID = "" }, true},
|
|
{"missing to_register_id", func(b *transferInput) { b.ToRegisterID = "" }, true},
|
|
{"same register on both sides", func(b *transferInput) { b.ToRegisterID = b.FromRegisterID }, true},
|
|
{"missing method", func(b *transferInput) { b.Method = "" }, true},
|
|
{"invalid method", func(b *transferInput) { b.Method = "crypto" }, true},
|
|
{"missing amount", func(b *transferInput) { b.Amount = "" }, true},
|
|
{"amount too long", func(b *transferInput) { b.Amount = longAmount }, true},
|
|
{"amount zero", func(b *transferInput) { b.Amount = "0" }, true},
|
|
{"amount negative", func(b *transferInput) { b.Amount = "-5.00" }, true},
|
|
{"amount not a number", func(b *transferInput) { b.Amount = "free" }, true},
|
|
{"amount over range", func(b *transferInput) { b.Amount = "100000000.00" }, true},
|
|
{"amount at range boundary is ok", func(b *transferInput) { b.Amount = "99999999.99" }, false},
|
|
{"amount is Infinity", func(b *transferInput) { b.Amount = "Infinity" }, true},
|
|
{"amount is NaN", func(b *transferInput) { b.Amount = "NaN" }, true},
|
|
{"note too long", func(b *transferInput) { b.Note = longNote }, true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
b := validTransferInput()
|
|
tt.mutate(&b)
|
|
got := b.validate()
|
|
if (got != "") != tt.wantErr {
|
|
t.Errorf("validate() = %q, wantErr %v", got, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|