84 lines
2.7 KiB
Go
84 lines
2.7 KiB
Go
package booking
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestCleanFieldStripsNewlinesAndTrims(t *testing.T) {
|
|
got := cleanField(" Иван\nТелефон: 000\r ")
|
|
if got != "Иван Телефон: 000" {
|
|
t.Errorf("cleanField = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestValidateAcceptsWellFormedInput(t *testing.T) {
|
|
b := createInput{
|
|
Name: "Иван Иванов", Phone: "+79991234567", DeviceType: "ноутбук",
|
|
ProblemDescription: "не включается", PreferredAt: time.Now().Add(24 * time.Hour).Format(time.RFC3339),
|
|
}
|
|
if msg := b.validate(); msg != "" {
|
|
t.Errorf("expected valid input to pass, got error: %q", msg)
|
|
}
|
|
}
|
|
|
|
func TestValidateRejectsMissingName(t *testing.T) {
|
|
b := createInput{Phone: "+79991234567", DeviceType: "ноутбук", PreferredAt: time.Now().Add(time.Hour).Format(time.RFC3339)}
|
|
if msg := b.validate(); msg == "" {
|
|
t.Error("expected error for missing name")
|
|
}
|
|
}
|
|
|
|
func TestValidateRejectsMissingPhone(t *testing.T) {
|
|
b := createInput{Name: "Иван", DeviceType: "ноутбук", PreferredAt: time.Now().Add(time.Hour).Format(time.RFC3339)}
|
|
if msg := b.validate(); msg == "" {
|
|
t.Error("expected error for missing phone")
|
|
}
|
|
}
|
|
|
|
func TestValidateRejectsMissingDeviceType(t *testing.T) {
|
|
b := createInput{Name: "Иван", Phone: "+79991234567", PreferredAt: time.Now().Add(time.Hour).Format(time.RFC3339)}
|
|
if msg := b.validate(); msg == "" {
|
|
t.Error("expected error for missing device_type")
|
|
}
|
|
}
|
|
|
|
func TestValidateRejectsMalformedPreferredAt(t *testing.T) {
|
|
b := createInput{Name: "Иван", Phone: "+79991234567", DeviceType: "ноутбук", PreferredAt: "not-a-date"}
|
|
if msg := b.validate(); msg == "" {
|
|
t.Error("expected error for malformed preferred_at")
|
|
}
|
|
}
|
|
|
|
func TestValidateRejectsPastPreferredAt(t *testing.T) {
|
|
b := createInput{
|
|
Name: "Иван", Phone: "+79991234567", DeviceType: "ноутбук",
|
|
PreferredAt: time.Now().Add(-48 * time.Hour).Format(time.RFC3339),
|
|
}
|
|
if msg := b.validate(); msg == "" {
|
|
t.Error("expected error for a preferred_at in the past")
|
|
}
|
|
}
|
|
|
|
func TestValidateRejectsOverlongName(t *testing.T) {
|
|
b := createInput{
|
|
Name: strings.Repeat("a", maxShortFieldLen+1), Phone: "+79991234567", DeviceType: "ноутбук",
|
|
PreferredAt: time.Now().Add(time.Hour).Format(time.RFC3339),
|
|
}
|
|
if msg := b.validate(); msg == "" {
|
|
t.Error("expected error for overlong name")
|
|
}
|
|
}
|
|
|
|
func TestValidateRejectsOverlongProblemDescription(t *testing.T) {
|
|
b := createInput{
|
|
Name: "Иван", Phone: "+79991234567", DeviceType: "ноутбук",
|
|
ProblemDescription: strings.Repeat("a", maxLongFieldLen+1),
|
|
PreferredAt: time.Now().Add(time.Hour).Format(time.RFC3339),
|
|
}
|
|
if msg := b.validate(); msg == "" {
|
|
t.Error("expected error for overlong problem_description")
|
|
}
|
|
}
|