Add optimized doctor search endpoint

This commit is contained in:
2026-09-07 10:50:19 +07:00
parent 9afd5b792c
commit aae3c733ae
5 changed files with 329 additions and 0 deletions

View File

@@ -0,0 +1,72 @@
package http
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"primaya-api/cpone-middleware/internal/repository"
)
type fakeDokterRepository struct {
page repository.DokterPage
params repository.DokterListParams
err error
}
func (f *fakeDokterRepository) ListDokter(_ context.Context, params repository.DokterListParams) (repository.DokterPage, error) {
f.params = params
return f.page, f.err
}
func TestListDokterResponseParity(t *testing.T) {
name := "DR. ANDI"
repo := &fakeDokterRepository{page: repository.DokterPage{
Total: 16,
Items: []repository.Dokter{{DokterID: "D001", Nama: &name}},
}}
handler := Handler{dokterRepo: repo}
request := httptest.NewRequest(http.MethodGet, "http://localhost/api/cpone/doctors?search=andi&DokterID_in=D001,D002&page=1&per_page=15&kode_rs=AWALBROS_DEV", nil)
recorder := httptest.NewRecorder()
handler.ListDokter(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
if repo.params.Search != "andi" || repo.params.Page != 1 || repo.params.PerPage != 15 || strings.Join(repo.params.DoctorIDs, ",") != "D001,D002" {
t.Fatalf("params=%+v", repo.params)
}
for _, expected := range []string{
`"DokterID":"D001"`, `"Nama":"DR. ANDI"`,
`"last_page":2`, `"per_page":15`,
`"next_page_url":"http://localhost/api/cpone/doctors?kode_rs=AWALBROS_DEV\u0026page=2"`,
} {
if !strings.Contains(recorder.Body.String(), expected) {
t.Fatalf("response missing %s: %s", expected, recorder.Body.String())
}
}
for _, excluded := range []string{`"spesialisasi"`, `"subspesialisasi"`, `"departementIDs"`, `"profileDokter"`} {
if strings.Contains(recorder.Body.String(), excluded) {
t.Fatalf("response should not contain %s: %s", excluded, recorder.Body.String())
}
}
}
func TestListDokterDefaultsAndPerPageLimit(t *testing.T) {
repo := &fakeDokterRepository{page: repository.DokterPage{Items: []repository.Dokter{}}}
handler := Handler{dokterRepo: repo}
for target, expected := range map[string]int{
"/api/cpone/doctors": defaultDokterPerPage,
"/api/cpone/doctors?page=0": defaultDokterPerPage,
"/api/cpone/doctors?per_page=250": maxDokterPerPage,
} {
recorder := httptest.NewRecorder()
handler.ListDokter(recorder, httptest.NewRequest(http.MethodGet, target, nil))
if recorder.Code != http.StatusOK || repo.params.PerPage != expected || repo.params.Page != 1 {
t.Fatalf("target=%s status=%d params=%+v", target, recorder.Code, repo.params)
}
}
}