75 lines
2.6 KiB
Go
75 lines
2.6 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"primaya-api/cpone-middleware/internal/repository"
|
|
)
|
|
|
|
type fakeLaporanMCURepository struct {
|
|
rows []repository.LaporanMCURow
|
|
err error
|
|
params repository.LaporanMCUParams
|
|
}
|
|
|
|
func (f *fakeLaporanMCURepository) ListLaporanMCU(_ context.Context, params repository.LaporanMCUParams) ([]repository.LaporanMCURow, error) {
|
|
f.params = params
|
|
return f.rows, f.err
|
|
}
|
|
|
|
func TestListLaporanMCUResponseAndDateRange(t *testing.T) {
|
|
regID := "MCU260900001"
|
|
repo := &fakeLaporanMCURepository{rows: []repository.LaporanMCURow{{No: 1, RegID: ®ID}}}
|
|
handler := Handler{laporanMCURepo: repo}
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodGet, "/api/cpone/reports/mcu?tanggal=2026-09-01&tanggal2=2026-09-07", nil)
|
|
|
|
handler.ListLaporanMCU(recorder, request)
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
if repo.params.Tanggal != "2026-09-01" || repo.params.Tanggal2 != "2026-09-07" {
|
|
t.Fatalf("params=%+v", repo.params)
|
|
}
|
|
for _, expected := range []string{`"success":true`, `"message":"ok"`, `"No":1`, `"RegID":"MCU260900001"`} {
|
|
if !strings.Contains(recorder.Body.String(), expected) {
|
|
t.Fatalf("response missing %s: %s", expected, recorder.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestListLaporanMCUValidatesDateRange(t *testing.T) {
|
|
repo := &fakeLaporanMCURepository{}
|
|
handler := Handler{laporanMCURepo: repo}
|
|
for target := range map[string]struct{}{
|
|
"/api/cpone/reports/mcu": {},
|
|
"/api/cpone/reports/mcu?tanggal=01-09-2026&tanggal2=2026-09-07": {},
|
|
"/api/cpone/reports/mcu?tanggal=2026-09-08&tanggal2=2026-09-07": {},
|
|
"/api/cpone/reports/mcu?tanggal=2026-09-01&tanggal2=not-a-date": {},
|
|
} {
|
|
recorder := httptest.NewRecorder()
|
|
handler.ListLaporanMCU(recorder, httptest.NewRequest(http.MethodGet, target, nil))
|
|
if recorder.Code != http.StatusUnprocessableEntity {
|
|
t.Fatalf("target=%s status=%d body=%s", target, recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
if repo.params != (repository.LaporanMCUParams{}) {
|
|
t.Fatalf("repository should not be called for invalid dates: %+v", repo.params)
|
|
}
|
|
}
|
|
|
|
func TestListLaporanMCUInternalError(t *testing.T) {
|
|
handler := Handler{laporanMCURepo: &fakeLaporanMCURepository{err: errors.New("database unavailable")}}
|
|
recorder := httptest.NewRecorder()
|
|
handler.ListLaporanMCU(recorder, httptest.NewRequest(http.MethodGet, "/api/cpone/reports/mcu?tanggal=2026-09-01&tanggal2=2026-09-01", nil))
|
|
if recorder.Code != http.StatusInternalServerError {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|