store: scope activities to a user_id
garmin_activity_id uniqueness becomes per-user (UNIQUE(user_id, garmin_activity_id), added in Task 1) so two users' Garmin accounts can never collide even in the unlikely event their activity ids coincide.
This commit is contained in:
@@ -41,7 +41,7 @@ func TestMigrateIsIdempotent(t *testing.T) {
|
||||
func TestUpsertActivity_InsertThenUpdateIsIdempotent(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
ctx := context.Background()
|
||||
userID, err := db.ProvisionUser(ctx, "test-sub", "Test User")
|
||||
userID, err := db.ProvisionUser(ctx, "test-sub", "Test")
|
||||
if err != nil {
|
||||
t.Fatalf("ProvisionUser: %v", err)
|
||||
}
|
||||
@@ -69,7 +69,7 @@ func TestUpsertActivity_InsertThenUpdateIsIdempotent(t *testing.T) {
|
||||
t.Fatalf("expected same internal id across upserts, got %d then %d", id, id2)
|
||||
}
|
||||
|
||||
got, ok, err := db.GetActivity(ctx, id)
|
||||
got, ok, err := db.GetActivity(ctx, userID, id)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("GetActivity: ok=%v err=%v", ok, err)
|
||||
}
|
||||
@@ -86,10 +86,40 @@ func TestUpsertActivity_InsertThenUpdateIsIdempotent(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpsertActivity_SameGarminActivityIDAllowedAcrossDifferentUsers(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
ctx := context.Background()
|
||||
userA, err := db.ProvisionUser(ctx, "sub-a", "A")
|
||||
if err != nil {
|
||||
t.Fatalf("ProvisionUser(a): %v", err)
|
||||
}
|
||||
userB, err := db.ProvisionUser(ctx, "sub-b", "B")
|
||||
if err != nil {
|
||||
t.Fatalf("ProvisionUser(b): %v", err)
|
||||
}
|
||||
|
||||
activity := Activity{GarminActivityID: 999, StartTimeUTC: "2026-07-11 05:00:00", RawJSON: "{}"}
|
||||
if _, err := db.UpsertActivity(ctx, userA, activity); err != nil {
|
||||
t.Fatalf("UpsertActivity(a): %v", err)
|
||||
}
|
||||
if _, err := db.UpsertActivity(ctx, userB, activity); err != nil {
|
||||
t.Fatalf("expected the same garmin_activity_id to be allowed for a different user, got: %v", err)
|
||||
}
|
||||
|
||||
aActivities, err := db.ListActivities(ctx, userA, ActivityFilter{})
|
||||
if err != nil || len(aActivities) != 1 {
|
||||
t.Fatalf("ListActivities(a): got %d, err=%v", len(aActivities), err)
|
||||
}
|
||||
bActivities, err := db.ListActivities(ctx, userB, ActivityFilter{})
|
||||
if err != nil || len(bActivities) != 1 {
|
||||
t.Fatalf("ListActivities(b): got %d, err=%v", len(bActivities), err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKindAssignment_AppendOnlyHistoryAndCurrentView(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
ctx := context.Background()
|
||||
userID, err := db.ProvisionUser(ctx, "test-sub", "Test User")
|
||||
userID, err := db.ProvisionUser(ctx, "test-sub", "Test")
|
||||
if err != nil {
|
||||
t.Fatalf("ProvisionUser: %v", err)
|
||||
}
|
||||
@@ -177,7 +207,7 @@ func TestKindAssignment_AppendOnlyHistoryAndCurrentView(t *testing.T) {
|
||||
func TestReplaceLapsIsIdempotent(t *testing.T) {
|
||||
db := openTestDB(t)
|
||||
ctx := context.Background()
|
||||
userID, err := db.ProvisionUser(ctx, "test-sub", "Test User")
|
||||
userID, err := db.ProvisionUser(ctx, "test-sub", "Test")
|
||||
if err != nil {
|
||||
t.Fatalf("ProvisionUser: %v", err)
|
||||
}
|
||||
@@ -257,7 +287,7 @@ func TestForeignKeyEnforcementPostMigration(t *testing.T) {
|
||||
// migrations 0023/0024/0025) is properly scoped and re-enabled after each
|
||||
// table rebuild.
|
||||
|
||||
userID, err := db.ProvisionUser(ctx, "test-sub", "Test User")
|
||||
userID, err := db.ProvisionUser(ctx, "test-sub", "Test")
|
||||
if err != nil {
|
||||
t.Fatalf("ProvisionUser: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user