feat: improve layanan search and pagination

This commit is contained in:
2026-09-02 09:28:54 +07:00
parent 181d53af45
commit 10fa7a6e38
3 changed files with 63 additions and 23 deletions

View File

@@ -28,29 +28,29 @@ func (f *fakeLayananRepository) MappingHarga(context.Context, repository.Mapping
func TestListLayananResponseParity(t *testing.T) {
group, department := "MCU", "MEDICAL CHECK UP"
layananID, name, price := "6413", "DARAH LENGKAP", "125000.00"
layananID, code, name := "6413", "DL", "DARAH LENGKAP"
repo := &fakeLayananRepository{page: repository.LayananPage{
Total: 16,
Items: []repository.LayananDepartemen{{
GroupLayananID: &group, Departemen: &department, LayananDepartemenID: 987,
LayananID: &layananID, NamaLayanan: &name, Harga: &price,
LayananID: &layananID, LayananCode: &code, NamaLayanan: &name,
}},
}}
handler := Handler{layananRepo: repo}
request := httptest.NewRequest(http.MethodGet, "http://localhost/api/cpone/layanan?page=1&kode_rs=AWALBROS_DEV", nil)
request := httptest.NewRequest(http.MethodGet, "http://localhost/api/cpone/layanan?page=1&search=tes&departemen_id=MCU&kode_rs=AWALBROS_DEV", nil)
recorder := httptest.NewRecorder()
handler.ListLayanan(recorder, request)
if recorder.Code != http.StatusOK {
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
}
if repo.params.Page != 1 || repo.params.PerPage != 15 {
if repo.params.Page != 1 || repo.params.PerPage != defaultLayananPerPage || repo.params.Search != "tes" || repo.params.DepartemenID != "MCU" {
t.Fatalf("params = %+v", repo.params)
}
for _, expected := range []string{
`"GroupLayananID":"MCU"`, `"Departemen":"MEDICAL CHECK UP"`,
`"LayananDepartemenID":987`, `"LayananID":"6413"`,
`"NamaLayanan":"DARAH LENGKAP"`, `"Harga":"125000.00"`,
`"NamaLayanan":"DARAH LENGKAP"`, `"LayananCode":"DL"`,
`"last_page":2`, `"per_page":15`,
`"next_page_url":"http://localhost/api/cpone/layanan?kode_rs=AWALBROS_DEV\u0026page=2"`,
} {
@@ -66,12 +66,27 @@ func TestListLayananUsesLaravelPageFallback(t *testing.T) {
for _, target := range []string{"/api/cpone/layanan", "/api/cpone/layanan?page=0", "/api/cpone/layanan?page=invalid"} {
recorder := httptest.NewRecorder()
handler.ListLayanan(recorder, httptest.NewRequest(http.MethodGet, target, nil))
if recorder.Code != http.StatusOK || repo.params.Page != 1 || repo.params.PerPage != layananPerPage {
if recorder.Code != http.StatusOK || repo.params.Page != 1 || repo.params.PerPage != defaultLayananPerPage {
t.Fatalf("target=%s status=%d params=%+v", target, recorder.Code, repo.params)
}
}
}
func TestListLayananAcceptsBoundedPerPage(t *testing.T) {
repo := &fakeLayananRepository{page: repository.LayananPage{Items: []repository.LayananDepartemen{}}}
handler := Handler{layananRepo: repo}
for target, expected := range map[string]int{
"/api/cpone/layanan?per_page=250": 250,
"/api/cpone/layanan?per_page=5000": maxLayananPerPage,
} {
recorder := httptest.NewRecorder()
handler.ListLayanan(recorder, httptest.NewRequest(http.MethodGet, target, nil))
if recorder.Code != http.StatusOK || repo.params.PerPage != expected {
t.Fatalf("target=%s status=%d per_page=%d", target, recorder.Code, repo.params.PerPage)
}
}
}
func TestListLayananInternalError(t *testing.T) {
handler := Handler{layananRepo: &fakeLayananRepository{err: errors.New("query failed")}}
recorder := httptest.NewRecorder()