Published an OpportunityMarksUpdated event when an opportunity's marks change over the API

This commit is contained in:
bwilliams committed 2026-10-16 13:19:58 +01:00
1 parent 70d62f280e
commit 848edc73e2
7 files changed
+203 -34

No files matched your search

+58 -4
View File
@@ -14,13 +14,15 @@ import (
"github.com/go-chi/chi/v5"
"git.avsdev.uk/Go/job-search/internal/events"
"git.avsdev.uk/Go/job-search/internal/store"
"git.avsdev.uk/Go/job-search/internal/types"
)
// opportunitiesServer serves the opportunity routes over a store holding
// count opportunities, ocds-1 the oldest, and returns its URL.
func opportunitiesServer(t *testing.T, count int) string {
// opportunitiesServer serves the API, given opts as well as its store, over
// a store holding count opportunities, ocds-1 the oldest, and returns its
// URL.
func opportunitiesServer(t *testing.T, count int, opts ...Option) string {
t.Helper()
opportunityStore, err := store.Open(
context.Background(),
@@ -57,7 +59,7 @@ func opportunitiesServer(t *testing.T, count int) string {
}
router := chi.NewRouter()
New(WithStore(opportunityStore)).Routes(router)
New(append([]Option{WithStore(opportunityStore)}, opts...)...).Routes(router)
server := httptest.NewServer(router)
t.Cleanup(server.Close)
return server.URL
@@ -302,3 +304,55 @@ func TestFindMarks(t *testing.T) {
t.Error("setMark accepted a name that isn't a mark")
}
}
// A change to a mark is published with all the marks; a change that changes
// nothing, or is refused, isn't.
func TestChangeMarkPublishes(t *testing.T) {
var bus events.Bus
var mu sync.Mutex
var published []types.OpportunityMarks
bus.Subscribe(func(event events.Event) {
if event.Type != events.OpportunityMarksUpdated {
t.Errorf("event type %q", event.Type)
}
mu.Lock()
defer mu.Unlock()
published = append(published, event.Payload.(types.OpportunityMarks))
})
url := opportunitiesServer(t, 1, WithEvents(&bus))
marks := url + "/opportunities/ocds-1/marks/"
request(t, "PUT", marks+"interested", nil) // published
request(t, "PUT", marks+"interested", nil) // already set: not published
request(t, "DELETE", marks+"skipped", nil) // not set: not published
request(t, "PUT", marks+"shiny", nil) // no such mark: refused
request(t, "PUT", marks+"won", nil) // published
request(t, "PUT", marks+"lost", nil) // won and lost: refused
request(t, "DELETE", marks+"interested", nil) // published
mu.Lock()
defer mu.Unlock()
want := []types.OpportunityMarks{
{
UUID: "ocds-1",
Interested: true,
},
{
UUID: "ocds-1",
Interested: true,
Won: true,
},
{
UUID: "ocds-1",
Won: true,
},
}
if len(published) != len(want) {
t.Fatalf("published %+v; want %+v", published, want)
}
for i := range want {
if published[i] != want[i] {
t.Errorf("event %d: %+v; want %+v", i, published[i], want[i])
}
}
}