Repository navigation
feat(api): updated_since change feed for GET /v1/bookings (#262) #281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dominik-myszkowski
wants to merge
2
commits into
Calnode:main
Choose a base branch
from
dominik-myszkowski:feat/bookings-updated-since
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| package booking_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "database/sql" | ||
| "regexp" | ||
| "sort" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/calnode/calnode/internal/booking" | ||
| ) | ||
|
|
||
| // wholeSecond is the only form updated_at may take after migration 00075. | ||
| var wholeSecond = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$`) | ||
|
|
||
| func createAt(t *testing.T, svc *booking.Service, etID, hostID string, h int) *booking.Booking { | ||
| t.Helper() | ||
| b, err := svc.Create(context.Background(), booking.CreateParams{ | ||
| EventTypeID: etID, | ||
| HostIDs: []string{hostID}, | ||
| StartAt: slot(h, 0), | ||
| EndAt: slot(h, 30), | ||
| Organizer: booking.Attendee{Name: "Alice", Email: "alice@example.com", IANATimezone: "UTC"}, | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("Create: %v", err) | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func rawUpdatedAt(t *testing.T, database *sql.DB, id string) string { | ||
| t.Helper() | ||
| var s string | ||
| if err := database.QueryRow(`SELECT updated_at FROM bookings WHERE id = ?`, id).Scan(&s); err != nil { | ||
| t.Fatalf("read updated_at: %v", err) | ||
| } | ||
| return s | ||
| } | ||
|
|
||
| // Every change to a booking must move updated_at, including the UPDATEs that never set | ||
| // it themselves, or a poller misses the change for good. | ||
| func TestUpdatedAt_everyChangeBumpsIt(t *testing.T) { | ||
| database := newTestDB(t) | ||
| svc := booking.New(database) | ||
| hostID := seedHost(t, database) | ||
| etID := seedEventType(t, database, hostID) | ||
|
|
||
| changes := map[string]string{ | ||
| // The worker releasing an unpaid hold: a cancellation that never set updated_at. | ||
| "hold released": `UPDATE bookings SET status = 'cancelled', cancellation_reason = 'payment not completed' WHERE id = ?`, | ||
| "payment": `UPDATE bookings SET payment_status = 'paid' WHERE id = ?`, | ||
| "meet link": `UPDATE bookings SET location_value = 'https://meet.google.com/abc' WHERE id = ?`, | ||
| "rsvp": `UPDATE booking_attendees SET rsvp_status = 'accepted' WHERE booking_id = ?`, | ||
| // A caller writing its own (stale) value doesn't get to keep it. | ||
| "explicit stale value": `UPDATE bookings SET updated_at = '2020-01-01T00:00:00Z' WHERE id = ?`, | ||
| } | ||
| ids := map[string]string{} | ||
| before := map[string]string{} | ||
| h := 9 | ||
| for name := range changes { | ||
| b := createAt(t, svc, etID, hostID, h) | ||
| h++ | ||
| ids[name] = b.ID | ||
| before[name] = rawUpdatedAt(t, database, b.ID) | ||
| if !wholeSecond.MatchString(before[name]) { | ||
| t.Fatalf("%s: updated_at on insert = %q; want whole-second RFC 3339", name, before[name]) | ||
| } | ||
| } | ||
| untouched := createAt(t, svc, etID, hostID, h).ID | ||
| untouchedBefore := rawUpdatedAt(t, database, untouched) | ||
|
|
||
| time.Sleep(1100 * time.Millisecond) // updated_at has whole-second resolution | ||
|
|
||
| for name, q := range changes { | ||
| if _, err := database.Exec(q, ids[name]); err != nil { | ||
| t.Fatalf("%s: %v", name, err) | ||
| } | ||
| after := rawUpdatedAt(t, database, ids[name]) | ||
| if !wholeSecond.MatchString(after) { | ||
| t.Errorf("%s: updated_at = %q; want whole-second RFC 3339", name, after) | ||
| } | ||
| if after <= before[name] { | ||
| t.Errorf("%s: updated_at = %q, was %q; want it moved forward", name, after, before[name]) | ||
| } | ||
| } | ||
| if got := rawUpdatedAt(t, database, untouched); got != untouchedBefore { | ||
| t.Errorf("untouched booking: updated_at = %q, was %q; want unchanged", got, untouchedBefore) | ||
| } | ||
| } | ||
|
|
||
| func TestList_updatedSince_includesCancelledInUpdateOrder(t *testing.T) { | ||
| database := newTestDB(t) | ||
| svc := booking.New(database) | ||
| hostID := seedHost(t, database) | ||
| etID := seedEventType(t, database, hostID) | ||
| ctx := context.Background() | ||
|
|
||
| b1 := createAt(t, svc, etID, hostID, 9) | ||
| b2 := createAt(t, svc, etID, hostID, 10) | ||
| if err := svc.Cancel(ctx, hostID, b2.ID, "changed plans"); err != nil { | ||
| t.Fatalf("Cancel: %v", err) | ||
| } | ||
|
|
||
| // The default listing hides the cancellation; the feed must not. | ||
| plain, err := svc.List(ctx, booking.ListFilter{}) | ||
| if err != nil { | ||
| t.Fatalf("List: %v", err) | ||
| } | ||
| if len(plain) != 1 { | ||
| t.Fatalf("plain list: %d bookings; want 1 (cancelled excluded)", len(plain)) | ||
| } | ||
|
|
||
| got, err := svc.List(ctx, booking.ListFilter{ | ||
| UpdatedSince: time.Now().Add(-time.Hour), | ||
| Now: time.Now().Add(2 * time.Second), // past the held-back second | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("List feed: %v", err) | ||
| } | ||
| if len(got) != 2 { | ||
| t.Fatalf("feed: %d bookings; want 2", len(got)) | ||
| } | ||
| seen := map[string]string{} | ||
| for i, b := range got { | ||
| seen[b.ID] = b.Status | ||
| if i > 0 { | ||
| prev := got[i-1] | ||
| if b.UpdatedAt.Before(prev.UpdatedAt) || (b.UpdatedAt.Equal(prev.UpdatedAt) && b.ID < prev.ID) { | ||
| t.Errorf("feed not ordered by (updated_at, id): %v then %v", prev.ID, b.ID) | ||
| } | ||
| } | ||
| } | ||
| if seen[b1.ID] != "confirmed" || seen[b2.ID] != "cancelled" { | ||
| t.Errorf("feed statuses = %v; want %s confirmed and %s cancelled", seen, b1.ID, b2.ID) | ||
| } | ||
|
|
||
| // An explicit status still narrows the feed. | ||
| onlyCancelled, err := svc.List(ctx, booking.ListFilter{ | ||
| UpdatedSince: time.Now().Add(-time.Hour), | ||
| Now: time.Now().Add(2 * time.Second), | ||
| Status: "cancelled", | ||
| }) | ||
| if err != nil { | ||
| t.Fatalf("List feed cancelled: %v", err) | ||
| } | ||
| if len(onlyCancelled) != 1 || onlyCancelled[0].ID != b2.ID { | ||
| t.Errorf("feed with status=cancelled = %d items; want just %s", len(onlyCancelled), b2.ID) | ||
| } | ||
| } | ||
|
|
||
| // Bookings created together share one updated_at. Paging one at a time with the | ||
| // (updated_at, id) cursor must visit each exactly once. | ||
| func TestList_updatedSince_cursorWalksTiesExactlyOnce(t *testing.T) { | ||
| database := newTestDB(t) | ||
| svc := booking.New(database) | ||
| hostID := seedHost(t, database) | ||
| etID := seedEventType(t, database, hostID) | ||
| ctx := context.Background() | ||
|
|
||
| var want []string | ||
| for h := 9; h < 14; h++ { | ||
| want = append(want, createAt(t, svc, etID, hostID, h).ID) | ||
| } | ||
| sort.Strings(want) | ||
|
|
||
| now := time.Now().Add(2 * time.Second) | ||
| f := booking.ListFilter{UpdatedSince: time.Now().Add(-time.Hour), Now: now, Limit: 1} | ||
| var walked []string | ||
| for range 10 { | ||
| page, err := svc.List(ctx, f) | ||
| if err != nil { | ||
| t.Fatalf("List: %v", err) | ||
| } | ||
| if len(page) == 0 { | ||
| break | ||
| } | ||
| last := page[0] | ||
| walked = append(walked, last.ID) | ||
| f.UpdatedSince, f.AfterID = last.UpdatedAt, last.ID | ||
| } | ||
| sort.Strings(walked) | ||
| if len(walked) != len(want) { | ||
| t.Fatalf("walked %d bookings %v; want %d %v", len(walked), walked, len(want), want) | ||
| } | ||
| for i := range want { | ||
| if walked[i] != want[i] { | ||
| t.Fatalf("walked %v; want %v", walked, want) | ||
| } | ||
| } | ||
|
|
||
| // Without after_id the bound is inclusive: the last item comes back, nothing is lost. | ||
| f.AfterID = "" | ||
| again, err := svc.List(ctx, f) | ||
| if err != nil { | ||
| t.Fatalf("List inclusive: %v", err) | ||
| } | ||
| if len(again) == 0 { | ||
| t.Error("inclusive updated_since returned nothing; want the boundary rows again") | ||
| } | ||
| } | ||
|
|
||
| // A change in the current second is held back until that second is over, so a later | ||
| // write in the same second can't sort in behind a cursor already handed out. | ||
| func TestList_updatedSince_holdsBackCurrentSecond(t *testing.T) { | ||
| database := newTestDB(t) | ||
| svc := booking.New(database) | ||
| hostID := seedHost(t, database) | ||
| etID := seedEventType(t, database, hostID) | ||
| ctx := context.Background() | ||
|
|
||
| b := createAt(t, svc, etID, hostID, 9) | ||
| stamped, err := time.Parse(time.RFC3339, rawUpdatedAt(t, database, b.ID)) | ||
| if err != nil { | ||
| t.Fatalf("parse updated_at: %v", err) | ||
| } | ||
|
|
||
| for _, tc := range []struct { | ||
| name string | ||
| now time.Time | ||
| want int | ||
| }{ | ||
| {"same second", stamped.Add(900 * time.Millisecond), 0}, | ||
| {"next second", stamped.Add(time.Second), 1}, | ||
| } { | ||
| got, err := svc.List(ctx, booking.ListFilter{UpdatedSince: stamped.Add(-time.Hour), Now: tc.now}) | ||
| if err != nil { | ||
| t.Fatalf("%s: List: %v", tc.name, err) | ||
| } | ||
| if len(got) != tc.want { | ||
| t.Errorf("%s: %d bookings; want %d", tc.name, len(got), tc.want) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.