74 lines
2.4 KiB
Go
74 lines
2.4 KiB
Go
package insight
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
type schedulingRepository struct {
|
|
datasetCalls int
|
|
catalogCalls int
|
|
created []CreateRequest
|
|
}
|
|
|
|
func (r *schedulingRepository) DatasetMetadata(context.Context) (string, time.Time, error) {
|
|
r.datasetCalls++
|
|
return "watermark", time.Date(2026, 6, 30, 0, 0, 0, 0, time.UTC), nil
|
|
}
|
|
func (r *schedulingRepository) ActiveCatalogVersion(context.Context) (string, error) {
|
|
r.catalogCalls++
|
|
return "catalog-v1", nil
|
|
}
|
|
func (r *schedulingRepository) MerchantScopes(context.Context) ([]MerchantScope, error) {
|
|
return []MerchantScope{{MerchantID: 1}, {MerchantID: 1, TerminalID: "terminal-1"}}, nil
|
|
}
|
|
func (r *schedulingRepository) Create(_ context.Context, request CreateRequest) (Job, bool, error) {
|
|
r.created = append(r.created, request)
|
|
return Job{}, false, nil
|
|
}
|
|
func (*schedulingRepository) Latest(context.Context, int64, string, Module) (Snapshot, error) {
|
|
panic("unexpected Latest call")
|
|
}
|
|
func (*schedulingRepository) GetJob(context.Context, int64, string) (Job, error) {
|
|
panic("unexpected GetJob call")
|
|
}
|
|
func (*schedulingRepository) EventsAfter(context.Context, int64, string, int64) ([]Event, error) {
|
|
panic("unexpected EventsAfter call")
|
|
}
|
|
func (*schedulingRepository) Claim(context.Context) (claimedJob, error) {
|
|
panic("unexpected Claim call")
|
|
}
|
|
func (*schedulingRepository) Complete(context.Context, claimedJob, Analysis, string, string) error {
|
|
panic("unexpected Complete call")
|
|
}
|
|
|
|
func TestService_EnqueueAllLoadsBatchMetadataOnce(t *testing.T) {
|
|
repository := &schedulingRepository{}
|
|
service := NewService(repository, nil)
|
|
service.now = func() time.Time { return time.Date(2026, 8, 21, 12, 0, 0, 0, time.UTC) }
|
|
|
|
created, err := service.EnqueueAll(t.Context(), "NIGHTLY")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if created != 12 {
|
|
t.Fatalf("created %d jobs, want 12", created)
|
|
}
|
|
if repository.datasetCalls != 1 {
|
|
t.Fatalf("dataset metadata loaded %d times, want once", repository.datasetCalls)
|
|
}
|
|
if repository.catalogCalls != 1 {
|
|
t.Fatalf("catalog metadata loaded %d times, want once", repository.catalogCalls)
|
|
}
|
|
if len(repository.created) != 12 {
|
|
t.Fatalf("repository received %d create requests, want 12", len(repository.created))
|
|
}
|
|
for _, request := range repository.created {
|
|
if request.CatalogVersion != "catalog-v1" || request.Fingerprint == "" || !json.Valid(request.Evidence) {
|
|
t.Fatalf("invalid batched request: %#v", request)
|
|
}
|
|
}
|
|
}
|