Skip to content

Commit 292198c

Browse files
committed
Add test for refreshAccesses update path and fix db.Find syntax
- Fix db.Find syntax error by using db.GetEngine().Where().Find() instead of db.Find() with builder.Eq directly - Add TestRepository_RecalculateAccesses_UpdateMode to test the update optimization path when user access mode changes - Improves test coverage for refreshAccesses from 69.4% to 75.0% - Validates that access mode updates use UPDATE instead of DELETE+INSERT
1 parent 250b266 commit 292198c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

models/perm/access/access.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ func refreshAccesses(ctx context.Context, repo *repo_model.Repository, accessMap
9797
}
9898

9999
// Query existing accesses for cross-comparison
100-
existingAccesses, err := db.Find[Access](ctx, builder.Eq{"repo_id": repo.ID})
101-
if err != nil {
100+
var existingAccesses []Access
101+
if err := db.GetEngine(ctx).Where(builder.Eq{"repo_id": repo.ID}).Find(&existingAccesses); err != nil {
102102
return fmt.Errorf("find existing accesses: %w", err)
103103
}
104104
existingMap := make(map[int64]perm.AccessMode, len(existingAccesses))

models/perm/access/access_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,38 @@ func TestRepository_RecalculateAccesses2(t *testing.T) {
124124
assert.NoError(t, err)
125125
assert.False(t, has)
126126
}
127+
128+
func TestRepository_RecalculateAccesses_UpdateMode(t *testing.T) {
129+
// Test the update path in refreshAccesses optimization
130+
// Scenario: User's access mode changes from Read to Write
131+
assert.NoError(t, unittest.PrepareTestDatabase())
132+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
133+
assert.NoError(t, repo.LoadOwner(t.Context()))
134+
135+
// Verify initial access mode
136+
initialAccess := &access_model.Access{UserID: 4, RepoID: 4}
137+
has, err := db.GetEngine(t.Context()).Get(initialAccess)
138+
assert.NoError(t, err)
139+
assert.True(t, has)
140+
initialMode := initialAccess.Mode
141+
142+
// Change collaboration mode to trigger update path
143+
newMode := perm_model.AccessModeAdmin
144+
assert.NotEqual(t, initialMode, newMode, "New mode should differ from initial mode")
145+
146+
_, err = db.GetEngine(t.Context()).
147+
Where("user_id = ? AND repo_id = ?", 4, 4).
148+
Cols("mode").
149+
Update(&repo_model.Collaboration{Mode: newMode})
150+
assert.NoError(t, err)
151+
152+
// Recalculate accesses - should UPDATE existing access, not delete+insert
153+
assert.NoError(t, access_model.RecalculateAccesses(t.Context(), repo))
154+
155+
// Verify access was updated, not deleted and re-inserted
156+
updatedAccess := &access_model.Access{UserID: 4, RepoID: 4}
157+
has, err = db.GetEngine(t.Context()).Get(updatedAccess)
158+
assert.NoError(t, err)
159+
assert.True(t, has, "Access should still exist")
160+
assert.Equal(t, newMode, updatedAccess.Mode, "Access mode should be updated to new collaboration mode")
161+
}

0 commit comments

Comments
 (0)