58 lines
2.7 KiB
Go
58 lines
2.7 KiB
Go
package cash
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func validInput() createInput {
|
|
return createInput{Type: "income", Method: "cash", Amount: "1500.00", OrderID: "ord1"}
|
|
}
|
|
|
|
func TestCreateInputValidate(t *testing.T) {
|
|
longNote := strings.Repeat("a", maxLongFieldLen+1)
|
|
longAmount := strings.Repeat("1", maxAmountLen+1)
|
|
|
|
tests := []struct {
|
|
name string
|
|
mutate func(b *createInput)
|
|
wantErr bool
|
|
}{
|
|
{"valid income tied to an order", func(b *createInput) {}, false},
|
|
{"valid expense with no order/batch", func(b *createInput) { b.Type = "expense"; b.OrderID = "" }, false},
|
|
{"valid payroll with note only", func(b *createInput) { b.Type = "payroll"; b.OrderID = ""; b.Note = "зарплата за март" }, false},
|
|
{"invalid type", func(b *createInput) { b.Type = "refund" }, true},
|
|
{"missing type", func(b *createInput) { b.Type = "" }, true},
|
|
{"invalid method", func(b *createInput) { b.Method = "crypto" }, true},
|
|
{"missing method", func(b *createInput) { b.Method = "" }, true},
|
|
{"missing amount", func(b *createInput) { b.Amount = "" }, true},
|
|
{"amount too long", func(b *createInput) { b.Amount = longAmount }, true},
|
|
{"amount zero", func(b *createInput) { b.Amount = "0" }, true},
|
|
{"amount negative is ok (correction entry)", func(b *createInput) { b.Amount = "-5.00" }, false},
|
|
{"amount not a number", func(b *createInput) { b.Amount = "free" }, true},
|
|
{"amount over range", func(b *createInput) { b.Amount = "100000000.00" }, true},
|
|
{"amount under negative range", func(b *createInput) { b.Amount = "-100000000.00" }, true},
|
|
{"amount at range boundary is ok", func(b *createInput) { b.Amount = "99999999.99" }, false},
|
|
{"amount is Infinity", func(b *createInput) { b.Amount = "Infinity" }, true},
|
|
{"amount is NaN", func(b *createInput) { b.Amount = "NaN" }, true},
|
|
{"both order_id and cartridge_batch_id set", func(b *createInput) { b.CartridgeBatchID = "batch1" }, true},
|
|
{"cartridge_batch_id alone is ok", func(b *createInput) { b.OrderID = ""; b.CartridgeBatchID = "batch1" }, false},
|
|
{"category_id on expense is ok", func(b *createInput) { b.Type = "expense"; b.OrderID = ""; b.CategoryID = "cat1" }, false},
|
|
{"category_id on income is rejected", func(b *createInput) { b.CategoryID = "cat1" }, true},
|
|
{"is_pending on income is ok", func(b *createInput) { b.IsPending = true }, false},
|
|
{"is_pending on expense is rejected", func(b *createInput) { b.Type = "expense"; b.OrderID = ""; b.IsPending = true }, true},
|
|
{"note too long", func(b *createInput) { b.Note = longNote }, true},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
b := validInput()
|
|
tt.mutate(&b)
|
|
got := b.validate()
|
|
if (got != "") != tt.wantErr {
|
|
t.Errorf("validate() = %q, wantErr %v", got, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|