Files
ilaandClaude Opus 4.8 dc065a9c29 fix(handler): 药品目录按分页契约修正 YPML00001(T-211)
- DrugQuery.PageNo 去 omitempty(pageNo=0 也上送)+ 加 PageSize
- handler Drugs 改 pageNo 必填(parseRequiredInt,缺则返回 400)+ 读 pageSize
- 补缺 pageNo 返回 400 的测试
- 依 docx;YPML00001 未真实联调,pageNo 是否真必填待厂家样本核对

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-09 22:22:54 +08:00

112 lines
3.5 KiB
Go

package handler
import (
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
func TestPublicGridAddressesReturnsRawResponse(t *testing.T) {
client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":[{"regionCode":"441625000000","regionName":"测试网格"}]}`)
h := NewPublicHandler(client)
req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/grid-addresses?parentCode=441625&pageNo=1&operateUser=712041", nil)
rec := httptest.NewRecorder()
h.GridAddresses(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d", rec.Code)
}
if *seenPath != "/osi/api/auto/wgdzcx/query" {
t.Fatalf("osi path = %q", *seenPath)
}
body, _ := io.ReadAll(rec.Body)
if !strings.Contains(string(body), `"regionCode"`) {
t.Fatalf("body missing grid address data: %s", body)
}
}
func TestPublicDoctorsReturnsRawResponse(t *testing.T) {
client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":[{"personId":"doc-001","personName":"测试医生"}]}`)
h := NewPublicHandler(client)
req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/doctors?manaUnitId=441625001&operateUser=712041", nil)
rec := httptest.NewRecorder()
h.Doctors(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d", rec.Code)
}
if *seenPath != "/osi/api/auto/zryscx/query" {
t.Fatalf("osi path = %q", *seenPath)
}
body, _ := io.ReadAll(rec.Body)
if !strings.Contains(string(body), `"personId"`) {
t.Fatalf("body missing doctor data: %s", body)
}
}
func TestPublicDrugsReturnsRawResponse(t *testing.T) {
client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":[{"ypmc":"测试药品","ypdw":"盒"}]}`)
h := NewPublicHandler(client)
req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/drugs?ypmc=测试&pageNo=1&pym=CS", nil)
rec := httptest.NewRecorder()
h.Drugs(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d", rec.Code)
}
if *seenPath != "/osi/api/auto/ypmlcx/query" {
t.Fatalf("osi path = %q", *seenPath)
}
body, _ := io.ReadAll(rec.Body)
if !strings.Contains(string(body), `"ypmc"`) {
t.Fatalf("body missing drug data: %s", body)
}
}
func TestPublicOrgsReturnsRawResponse(t *testing.T) {
client, seenPath := newTestClient(t, `{"code":"01","message":"操作成功","data":[{"organizCode":"441625001","organizName":"测试机构"}]}`)
h := NewPublicHandler(client)
req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/orgs?organizCode=441625001&parentId=441625", nil)
rec := httptest.NewRecorder()
h.Orgs(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("status = %d", rec.Code)
}
if *seenPath != "/osi/api/auto/cxjg/query" {
t.Fatalf("osi path = %q", *seenPath)
}
body, _ := io.ReadAll(rec.Body)
if !strings.Contains(string(body), `"organizCode"`) {
t.Fatalf("body missing org data: %s", body)
}
}
func TestPublicDrugsRequiresPageNo(t *testing.T) {
h := NewPublicHandler(nil) // 缺 pageNo 在触达 client 前返回 400(药品目录分页必填)
req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/drugs?ypmc=测试", nil)
rec := httptest.NewRecorder()
h.Drugs(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400 (pageNo required)", rec.Code)
}
}
func TestPublicDictionaryRejectsInvalidPageNo(t *testing.T) {
h := NewPublicHandler(nil)
req := httptest.NewRequest(http.MethodGet, "/api/dictionaries/drugs?pageNo=bad", nil)
rec := httptest.NewRecorder()
h.Drugs(rec, req)
if rec.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", rec.Code)
}
}