73 lines
2.4 KiB
Go
73 lines
2.4 KiB
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"primaya-api/cpone-middleware/internal/repository"
|
|
)
|
|
|
|
type fakePeriodeTarifRepository struct {
|
|
items []repository.PeriodeTarif
|
|
err error
|
|
}
|
|
|
|
func (f *fakePeriodeTarifRepository) ListPeriodeTarif(_ context.Context) ([]repository.PeriodeTarif, error) {
|
|
return f.items, f.err
|
|
}
|
|
|
|
func (f *fakePeriodeTarifRepository) ListPeriodeTarifLayanan(_ context.Context, _ repository.PeriodeTarifLayananListParams) (repository.PeriodeTarifLayananPage, error) {
|
|
return repository.PeriodeTarifLayananPage{}, f.err
|
|
}
|
|
|
|
func TestListPeriodeTarifReturnsAvailablePeriods(t *testing.T) {
|
|
keterangan := "Tarif Dasar 2026"
|
|
repo := &fakePeriodeTarifRepository{items: []repository.PeriodeTarif{{
|
|
PeriodeTarifID: 42, Keterangan: &keterangan,
|
|
TanggalBerlaku: "2026-01-01", TanggalBerakhir: "2026-12-31",
|
|
}}}
|
|
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodGet, "/api/cpone/periode-tarif", nil)
|
|
Handler{periodeTarifRepo: repo}.ListPeriodeTarif(recorder, request)
|
|
|
|
if recorder.Code != http.StatusOK {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
for _, expected := range []string{
|
|
`"success":true`, `"message":"ok"`, `"PeriodeTarifID":42`,
|
|
`"Keterangan":"Tarif Dasar 2026"`, `"TanggalBerlaku":"2026-01-01"`,
|
|
`"TanggalBerakhir":"2026-12-31"`,
|
|
} {
|
|
if !strings.Contains(recorder.Body.String(), expected) {
|
|
t.Fatalf("response missing %s: %s", expected, recorder.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestListPeriodeTarifReturnsEmptyArray(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodGet, "/api/cpone/periode-tarif", nil)
|
|
Handler{periodeTarifRepo: &fakePeriodeTarifRepository{items: []repository.PeriodeTarif{}}}.
|
|
ListPeriodeTarif(recorder, request)
|
|
|
|
if recorder.Code != http.StatusOK || !strings.Contains(recorder.Body.String(), `"data":[]`) {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestListPeriodeTarifInternalError(t *testing.T) {
|
|
recorder := httptest.NewRecorder()
|
|
request := httptest.NewRequest(http.MethodGet, "/api/cpone/periode-tarif", nil)
|
|
Handler{periodeTarifRepo: &fakePeriodeTarifRepository{err: errors.New("query failed")}}.
|
|
ListPeriodeTarif(recorder, request)
|
|
|
|
if recorder.Code != http.StatusInternalServerError {
|
|
t.Fatalf("status=%d body=%s", recorder.Code, recorder.Body.String())
|
|
}
|
|
}
|