dana-medical-site/internal/api/admin_specialties_test.go
2026-10-14 13:33:13 +00:00

53 lines
1.9 KiB
Go

package api
import (
"net/http/httptest"
"strings"
"testing"
)
func TestDecodeSpecialtyRules(t *testing.T) {
cases := []struct {
name, body string
withSlug bool
wantCode int
wantText string
}{
{"plural only fills name", `{"name_plural":"Кардиологи","slug":"kardiologi"}`, true, 200, ""},
{"no name", `{"name_plural":" ","slug":"x"}`, true, 422, "название"},
{"cyrillic slug", `{"name_plural":"Кардиологи","slug":"кардиологи"}`, true, 422, "латиница"},
{"slug ignored on update", `{"name_plural":"Кардиологи"}`, false, 200, ""},
}
for _, tc := range cases {
w := httptest.NewRecorder()
r := httptest.NewRequest("POST", "/", strings.NewReader(tc.body))
in, valid := decodeSpecialty(w, r, tc.withSlug)
code := 200
if !valid {
code = w.Code
}
if code != tc.wantCode || (tc.wantText != "" && !strings.Contains(w.Body.String(), tc.wantText)) {
t.Errorf("%s: got %d %s, want %d containing %q", tc.name, code, w.Body.String(), tc.wantCode, tc.wantText)
}
if valid && (in.Name == "" || in.Sort != 500) {
t.Errorf("%s: defaults not applied: %+v", tc.name, in)
}
}
}
func TestCreateDoctorValidation(t *testing.T) {
a := &API{}
cases := []struct{ body, wantText string }{
{`{"last_name":"Иванов","slug":"ivanov","specialty_id":2}`, "фамилию и имя"},
{`{"last_name":"Иванов","first_name":"Иван","slug":"ivanov"}`, "специальность"},
{`{"last_name":"Иванов","first_name":"Иван","slug":"Иванов","specialty_id":2}`, "латиница"},
}
for _, tc := range cases {
w := httptest.NewRecorder()
a.adminCreateDoctor(w, httptest.NewRequest("POST", "/", strings.NewReader(tc.body)), nil)
if w.Code != 422 || !strings.Contains(w.Body.String(), tc.wantText) {
t.Errorf("%s: got %d %s", tc.body, w.Code, w.Body.String())
}
}
}