Skip to content

Commit 26063f9

Browse files
committed
test(api): make scheduled-revoke mock explicit + deterministic reservation test
CodeRabbit round 4: TestRevokePurchase_ScheduledExecution_AdminFreeCancel relied on the shared mock default for its CAS return and carried a comment naming the wrong method (CancelExecutionAtomic), while a scheduled row is actually cancelled via CancelScheduledExecutionAtomic. Stub that method explicitly with Return(true, "cancelled", nil).Once() plus DeleteSuppressionsByExecutionTx, matching the sibling scheduled-revoke tests. AssertExpectations now enforces the stub, so the setup and the asserted status can never drift. TestBuildReservationName_NilFieldsReturnsFallback rebuilt its expected value via SanitizeReservationID("", prefix), which calls time.Now() a second time -- a second-boundary cross between the actual call and the rebuild would flake the equality assertion. Replace it with deterministic structural assertions: regexp ^rds-reserved-[0-9]+$ (sanitized prefix + unix timestamp), no trailing hyphen, and the length cap. No second time.Now() comparison. Verified by execution: go test ./internal/api/... (1636) and -run Revoke (33) both green; reservation_name nil tests pass.
1 parent 756ca62 commit 26063f9

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

internal/api/handler_purchases_revoke_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,13 @@ func TestRevokePurchase_ScheduledExecution_AdminFreeCancel(t *testing.T) {
580580
mockAuth.On("ValidateSession", ctx, "tok").Return(adminSess, nil)
581581
exec := scheduledExecution(execID, "")
582582
mockStore.On("GetExecutionByID", ctx, execID).Return(exec, nil)
583-
// CancelExecutionAtomic and DeleteSuppressionsByExecutionTx use mock defaults
584-
// (WithTx calls fn(nil), CancelExecutionAtomic returns canceled=true plus the
585-
// persisted cancel status and a nil error).
583+
// A scheduled row is canceled via CancelScheduledExecutionAtomic. Stub its
584+
// return explicitly (matching the assertion below) rather than leaning on the
585+
// shared mock default, so the setup and the asserted status can never drift.
586+
mockStore.On("CancelScheduledExecutionAtomic", ctx, mock.Anything, execID, mock.Anything).
587+
//nolint:misspell // DB schema value 'cancelled' -- see migration 000001_initial_schema.up.sql
588+
Return(true, "cancelled", nil).Once()
589+
mockStore.On("DeleteSuppressionsByExecutionTx", ctx, mock.Anything, execID).Return(nil).Once()
586590

587591
h := &Handler{config: mockStore, auth: mockAuth}
588592
result, err := h.revokePurchase(ctx, sessionReq(), execID)

pkg/common/reservation_name_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ func testFixedRand() []byte { return []byte{0xa1, 0xb2, 0xc3, 0xd4} }
1616

1717
// TestBuildReservationName_NilFieldsReturnsFallback is the CR #1276 guard: the
1818
// exported helper must return the fallback prefix (a safe, non-empty no-op) on
19-
// a nil pointer argument rather than panicking.
19+
// a nil pointer argument rather than panicking. Asserts structural properties
20+
// (prefix + timestamp shape + cap) rather than equality against a value rebuilt
21+
// with a second time.Now() call, which would flake on a second-boundary cross.
2022
func TestBuildReservationName_NilFieldsReturnsFallback(t *testing.T) {
2123
got := BuildReservationName(nil, "rds-reserved-")
2224
assert.NotEmpty(t, got, "nil fields must not panic and must yield a non-empty name")
23-
// Under the cap, the nil path equals the plain sanitized fallback.
24-
assert.Equal(t, SanitizeReservationID("", "rds-reserved-"), got)
25+
// SanitizeReservationID("", prefix) returns prefix + unix-seconds, so the
26+
// output is the sanitized prefix followed by digits only.
27+
assert.Regexp(t, `^rds-reserved-[0-9]+$`, got,
28+
"nil path must yield the sanitized fallback prefix plus a unix timestamp")
29+
assert.False(t, strings.HasSuffix(got, "-"), "name must not end in a hyphen")
2530
assert.LessOrEqual(t, len(got), awsReservationNameMaxLen)
2631
}
2732

0 commit comments

Comments
 (0)