Files
bwilliams 746fa0a9c1 Added a "Published in the last X days" filter, applied by the backend
GET /api/opportunities takes released_from, an RFC 3339 time, and lists
only the opportunities whose latest release is at or after it. It works
with paging and with changed_after, which keeps the same batch either
way; an invalid value is 400.

The frontend's filter defaults to 7 days, from midnight UK time, and
sends it with every load and catch-up, so a year of opportunities isn't
downloaded to show a week's. Changing the days loads the list again;
Clear filters goes back to 7.
2026-10-19 10:05:45 +01:00

125 lines
2.8 KiB
Go

package store
import (
"context"
"errors"
"testing"
"time"
"git.avsdev.uk/Go/job-search/internal/types"
)
// A batch recorded after the clock went back is still created after the
// newest batch, so asking for the batches after that one finds it.
func TestChangeBatchAfterClockWentBack(t *testing.T) {
db := openTestDB(t)
s := &Store{db: db}
ctx := context.Background()
_, err := s.SaveOpportunity(
ctx,
&types.Opportunity{
UUID: "ocds-1",
LatestReleaseDate: time.Now(),
},
)
if err != nil {
t.Fatal(err)
}
future := time.Now().UTC().Add(time.Hour)
db.MustExec(insertChangeBatch, "future", future)
err = s.SetMarks(ctx, types.OpportunityMarks{
UUID: "ocds-1",
Won: true,
})
if err != nil {
t.Fatal(err)
}
var latest changeBatch
if err := db.Get(&latest, selectLatestChangeBatch); err != nil {
t.Fatal(err)
}
if !latest.CreatedAt.After(future) {
t.Errorf("batch created at %v, not after %v", latest.CreatedAt, future)
}
got, hash, err := s.ListChangedOpportunities(ctx, "future", time.Time{})
if err != nil {
t.Fatal(err)
}
if len(got) != 1 || got[0].UUID != "ocds-1" || hash != latest.Hash {
t.Errorf("changed after future: %v, %q", got, hash)
}
}
// Recording a batch deletes the batches older than the retention, with their
// records of opportunities; with no retention, none are deleted.
func TestChangeBatchRetention(t *testing.T) {
for _, retention := range []time.Duration{time.Hour, 0} {
db := openTestDB(t)
s := &Store{
db: db,
changeBatchRetention: retention,
}
ctx := context.Background()
_, err := s.SaveOpportunity(
ctx,
&types.Opportunity{
UUID: "ocds-1",
LatestReleaseDate: time.Now(),
},
)
if err != nil {
t.Fatal(err)
}
// Backdate every batch so far by two hours.
db.MustExec(
`UPDATE opportunity_change_batches SET created_at = ?`,
time.Now().UTC().Add(-2*time.Hour),
)
old, err := s.LatestChangeBatch(ctx)
if err != nil {
t.Fatal(err)
}
err = s.SetMarks(ctx, types.OpportunityMarks{
UUID: "ocds-1",
Skipped: true,
})
if err != nil {
t.Fatal(err)
}
var batches, records int
db.Get(&batches, `SELECT COUNT(*) FROM opportunity_change_batches`)
db.Get(
&records,
`SELECT COUNT(*) FROM opportunity_change_batch_opportunities`,
)
_, _, err = s.ListChangedOpportunities(ctx, old, time.Time{})
if retention > 0 {
if batches != 1 || records != 1 ||
!errors.Is(err, ErrUnknownBatch) {
t.Errorf(
"retention %v: %d batches, %d records, err %v; "+
"want the old batch gone",
retention, batches, records, err,
)
}
continue
}
if batches != 2 || records != 2 || err != nil {
t.Errorf(
"no retention: %d batches, %d records, err %v; want both kept",
batches, records, err,
)
}
}
}