Return explicit errors from AlertAPI construction instead of panicking inside the library layer, and let main own process-level failure handling. Also delete the unused config.MustLoad helper and lock the supported config loading paths with tests.
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
|
|
"lijiaoqiao/supply-api/internal/audit/model"
|
|
"lijiaoqiao/supply-api/internal/audit/service"
|
|
)
|
|
|
|
type recordingAlertStore struct {
|
|
createCalls int
|
|
}
|
|
|
|
func (s *recordingAlertStore) Create(ctx context.Context, alert *model.Alert) error {
|
|
s.createCalls++
|
|
return nil
|
|
}
|
|
|
|
func (s *recordingAlertStore) GetByID(ctx context.Context, alertID string) (*model.Alert, error) {
|
|
return nil, service.ErrAlertNotFound
|
|
}
|
|
|
|
func (s *recordingAlertStore) Update(ctx context.Context, alert *model.Alert) error {
|
|
return nil
|
|
}
|
|
|
|
func (s *recordingAlertStore) Delete(ctx context.Context, alertID string) error {
|
|
return nil
|
|
}
|
|
|
|
func (s *recordingAlertStore) List(ctx context.Context, filter *model.AlertFilter) ([]*model.Alert, int64, error) {
|
|
return nil, 0, nil
|
|
}
|
|
|
|
func TestNewAlertAPI_UsesInjectedStore(t *testing.T) {
|
|
store := &recordingAlertStore{}
|
|
api, err := NewAlertAPI(service.NewAlertService(store))
|
|
if err != nil {
|
|
t.Fatalf("expected no error, got %v", err)
|
|
}
|
|
if api == nil {
|
|
t.Fatal("expected api")
|
|
}
|
|
}
|
|
|
|
func TestNewAlertAPI_ReturnsErrorWhenServiceMissing(t *testing.T) {
|
|
api, err := NewAlertAPI(nil)
|
|
if err == nil {
|
|
t.Fatal("expected error")
|
|
}
|
|
if api != nil {
|
|
t.Fatal("expected nil api")
|
|
}
|
|
}
|