do not reload slice if already loaded

This commit is contained in:
Nicolas Gourdon 2019-04-30 22:01:16 +02:00
parent 8216a2a210
commit f6983856de
3 changed files with 10 additions and 2 deletions

View file

@ -53,7 +53,9 @@ func (org *User) GetOwnerTeam() (*Team, error) {
}
func (org *User) getTeams(e Engine) error {
org.Teams = nil
if org.Teams != nil {
return nil
}
return e.
Where("org_id=?", org.ID).
OrderBy("CASE WHEN name LIKE '" + ownerTeamName + "' THEN '' ELSE name END").

View file

@ -88,7 +88,9 @@ func (t *Team) IsMember(userID int64) bool {
}
func (t *Team) getRepositories(e Engine) error {
t.Repos = nil
if t.Repos != nil {
return nil
}
return e.Join("INNER", "team_repo", "repository.id = team_repo.repo_id").
Where("team_repo.team_id=?", t.ID).
OrderBy("repository.name").

View file

@ -452,11 +452,13 @@ func TestAllRepositoriesTeams(t *testing.T) {
repoCounts[4] = 3
for i, team := range teams {
assert.NoError(t, UpdateTeam(team, false), "team %d: UpdateTeam", i)
team.Repos = nil // Reset repos to allow their reloading.
assert.NoError(t, team.GetRepositories(), "team %d: GetRepositories", i)
assert.Equal(t, repoCounts[i], len(team.Repos), "team %d: repo count", i)
}
// Create repo and check teams repo count.
org.Teams = nil // Reset teams to allow their reloading.
r, err := CreateRepository(user, org, CreateRepoOptions{Name: "repo-last"})
assert.NoError(t, err, "CreateRepository last")
if r != nil {
@ -466,6 +468,7 @@ func TestAllRepositoriesTeams(t *testing.T) {
repoCounts[1] = 4
repoCounts[4] = 4
for i, team := range teams {
team.Repos = nil // Reset repos to allow their reloading.
assert.NoError(t, team.GetRepositories(), "team %d: GetRepositories", i)
assert.Equal(t, repoCounts[i], len(team.Repos), "team %d: repo count", i)
}
@ -477,6 +480,7 @@ func TestAllRepositoriesTeams(t *testing.T) {
repoCounts[3] = 2
repoCounts[4] = 3
for i, team := range teams {
team.Repos = nil // Reset repos to allow their reloading.
assert.NoError(t, team.GetRepositories(), "team %d: GetRepositories", i)
assert.Equal(t, repoCounts[i], len(team.Repos), "team %d: repo count", i)
}