Files
aura-crm/production/backend/internal/delivery/validate_test.go
T

89 lines
2.4 KiB
Go

package delivery
import (
"strings"
"testing"
)
func validInput() createInput {
return createInput{OrderID: "ord1", Address: "ул. Ленина, 1"}
}
func TestCreateInputValidate(t *testing.T) {
longAddress := strings.Repeat("a", maxAddressLen+1)
longNote := strings.Repeat("a", maxLongFieldLen+1)
tests := []struct {
name string
mutate func(b *createInput)
wantErr bool
}{
{"valid, no courier yet", func(b *createInput) {}, false},
{"valid with courier", func(b *createInput) { b.CourierStaffID = "staff1"; b.CourierStaffName = "Иван" }, false},
{"missing order_id", func(b *createInput) { b.OrderID = "" }, true},
{"missing address", func(b *createInput) { b.Address = "" }, true},
{"address too long", func(b *createInput) { b.Address = longAddress }, true},
{"note too long", func(b *createInput) { b.Note = longNote }, true},
{"courier id without name", func(b *createInput) { b.CourierStaffID = "staff1" }, true},
{"courier name without id", func(b *createInput) { b.CourierStaffName = "Иван" }, 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)
}
})
}
}
func TestRequiresSignature(t *testing.T) {
tests := []struct {
to string
want bool
}{
{"delivered", true},
{"in_transit", false},
{"pending", false},
{"failed", false},
{"cancelled", false},
}
for _, tt := range tests {
t.Run(tt.to, func(t *testing.T) {
if got := requiresSignature(tt.to); got != tt.want {
t.Errorf("requiresSignature(%q) = %v, want %v", tt.to, got, tt.want)
}
})
}
}
func TestCanTransition(t *testing.T) {
tests := []struct {
from, to string
want bool
}{
{"pending", "in_transit", true},
{"pending", "cancelled", true},
{"pending", "delivered", false},
{"in_transit", "delivered", true},
{"in_transit", "failed", true},
{"in_transit", "cancelled", true},
{"in_transit", "pending", false},
{"failed", "pending", true},
{"failed", "cancelled", true},
{"failed", "in_transit", false},
{"delivered", "pending", false},
{"delivered", "cancelled", false},
{"cancelled", "pending", false},
}
for _, tt := range tests {
t.Run(tt.from+"->"+tt.to, func(t *testing.T) {
if got := canTransition(tt.from, tt.to); got != tt.want {
t.Errorf("canTransition(%q, %q) = %v, want %v", tt.from, tt.to, got, tt.want)
}
})
}
}