瀏覽代碼

Add tests

Joe Chen 1 年之前
父節點
當前提交
f0fed90a0a
共有 2 個文件被更改,包括 71 次插入15 次删除
  1. 14 12
      internal/db/organizations.go
  2. 57 3
      internal/db/organizations_test.go

+ 14 - 12
internal/db/organizations.go

@@ -238,28 +238,30 @@ func (*organizations) accessibleRepositoriesByUser(tx *gorm.DB, orgID, userID in
 		Equivalent SQL for PostgreSQL:
 
 		SELECT * FROM "repository"
-		JOIN team_repo ON repository.id = team_repo.repo_id
 		WHERE
 			owner_id = @orgID
 		AND (
-				team_repo.team_id IN (
-					SELECT team_id FROM "team_user"
-					WHERE team_user.org_id = @orgID AND uid = @userID)
+				id IN (
+					SELECT repo_id
+					FROM "team_repo"
+					JOIN "team_user" ON team_user.org_id = team_repo.org_id
+					WHERE team_repo.org_id = @orgID AND team_user.uid = @userID
 				)
-			OR  (repository.is_private = FALSE AND repository.is_unlisted = FALSE)
+			OR  (is_private = FALSE AND is_unlisted = FALSE)
 		)
 		[ORDER BY updated_unix DESC]
 		[LIMIT @limit OFFSET @offset]
 	*/
 	conds := tx.
 		Table("repository").
-		Joins("JOIN team_repo ON repository.id = team_repo.repo_id").
-		Where("owner_id = ? AND (team_repo.team_id IN (?) OR (repository.is_private = ? AND repository.is_unlisted = ?))",
+		Where("owner_id = ? AND (id IN (?) OR (is_private = ? AND is_unlisted = ?))",
 			orgID,
-			tx.Select("team_id").
-				Table("team_user").
-				Where("team_user.org_id = ? AND uid = ?", orgID, userID),
-			false, false,
+			tx.Select("repo_id").
+				Table("team_repo").
+				Joins("JOIN team_user ON team_user.org_id = team_repo.org_id").
+				Where("team_repo.org_id = ? AND team_user.uid = ?", orgID, userID),
+			false,
+			false,
 		)
 	if opts.orderBy == OrderByUpdatedDesc {
 		conds.Order("updated_unix DESC")
@@ -297,7 +299,7 @@ func (db *organizations) AccessibleRepositoriesByUser(ctx context.Context, orgID
 		return repos, 0, nil
 	}
 	var count int64
-	err = conds.Model(&Repository{}).Count(&count).Error
+	err = conds.Count(&count).Error
 	if err != nil {
 		return nil, 0, errors.Wrap(err, "count repositories")
 	}

+ 57 - 3
internal/db/organizations_test.go

@@ -52,6 +52,9 @@ func TestOrganizations(t *testing.T) {
 		{"ListMembers", orgsListMembers},
 		{"IsOwnedBy", orgsIsOwnedBy},
 		{"SetMemberVisibility", orgsSetMemberVisibility},
+		{"GetTeamByName", orgsGetTeamByName},
+		{"AccessibleRepositoriesByUser", orgsAccessibleRepositoriesByUser},
+		{"MirrorRepositoriesByUser", orgsMirrorRepositoriesByUser},
 	} {
 		t.Run(tc.name, func(t *testing.T) {
 			t.Cleanup(func() {
@@ -472,7 +475,7 @@ func orgsRemoveMember(t *testing.T, db *organizations) {
 	err = db.AddMember(ctx, org1.ID, bob.ID)
 	require.NoError(t, err)
 
-	// Mock repository, watches, accesses and collaborations
+	// Mock repository, watches and collaborations
 	reposStore := NewRepositoriesStore(db.DB)
 	repo1, err := reposStore.Create(ctx, org1.ID, CreateRepoOptions{Name: "repo1", Private: true})
 	require.NoError(t, err)
@@ -515,6 +518,7 @@ func orgsRemoveMember(t *testing.T, db *organizations) {
 	).Error
 	require.NoError(t, err)
 
+	// Mock accesses
 	permsStore := NewPermsStore(db.DB)
 	err = permsStore.SetRepoPerms(ctx, repo1.ID, map[int64]AccessMode{bob.ID: AccessModeRead})
 	require.NoError(t, err)
@@ -609,7 +613,7 @@ func orgsIsOwnedBy(t *testing.T, db *organizations) {
 	cindy, err := usersStore.Create(ctx, "cindy", "cindy@exmaple.com", CreateUserOptions{})
 	require.NoError(t, err)
 
-	tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsListMembers-tempPictureAvatarUploadPath")
+	tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsIsOwnedBy-tempPictureAvatarUploadPath")
 	conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
 
 	org1, err := db.Create(ctx, "org1", alice.ID, CreateOrganizationOptions{})
@@ -632,7 +636,7 @@ func orgsSetMemberVisibility(t *testing.T, db *organizations) {
 	alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
 	require.NoError(t, err)
 
-	tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsListMembers-tempPictureAvatarUploadPath")
+	tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsSetMemberVisibility-tempPictureAvatarUploadPath")
 	conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
 
 	org1, err := db.Create(ctx, "org1", alice.ID, CreateOrganizationOptions{})
@@ -648,3 +652,53 @@ func orgsSetMemberVisibility(t *testing.T, db *organizations) {
 	require.NoError(t, err)
 	assert.Len(t, got, 1)
 }
+
+func orgsGetTeamByName(t *testing.T, db *organizations) {
+	ctx := context.Background()
+
+	usersStore := NewUsersStore(db.DB)
+	alice, err := usersStore.Create(ctx, "alice", "alice@example.com", CreateUserOptions{})
+	require.NoError(t, err)
+
+	tempPictureAvatarUploadPath := filepath.Join(os.TempDir(), "orgsGetTeamByName-tempPictureAvatarUploadPath")
+	conf.SetMockPicture(t, conf.PictureOpts{AvatarUploadPath: tempPictureAvatarUploadPath})
+
+	org1, err := db.Create(ctx, "org1", alice.ID, CreateOrganizationOptions{})
+	require.NoError(t, err)
+
+	t.Run("non-existent team", func(t *testing.T) {
+		_, err := db.GetTeamByName(ctx, org1.ID, "non-existent")
+		wantErr := ErrTeamNotExist{errutil.Args{"orgID": org1.ID, "name": "non-existent"}}
+		assert.Equal(t, wantErr, err)
+	})
+
+	t.Run("team of another organization", func(t *testing.T) {
+		org2, err := db.Create(ctx, "org2", alice.ID, CreateOrganizationOptions{})
+		require.NoError(t, err)
+
+		// TODO: Use Organizations.CreateTeam to replace SQL hack when the method is available.
+		team1 := &Team{
+			OrgID:      org2.ID,
+			LowerName:  "team1",
+			Name:       "team1",
+			NumMembers: 1,
+		}
+		err = db.DB.Create(team1).Error
+		require.NoError(t, err)
+
+		_, err = db.GetTeamByName(ctx, org1.ID, team1.Name)
+		wantErr := ErrTeamNotExist{errutil.Args{"orgID": org1.ID, "name": team1.Name}}
+		assert.Equal(t, wantErr, err)
+	})
+
+	_, err = db.GetTeamByName(ctx, org1.ID, TeamNameOwners)
+	require.NoError(t, err)
+}
+
+func orgsAccessibleRepositoriesByUser(t *testing.T, db *organizations) {
+	// todo
+}
+
+func orgsMirrorRepositoriesByUser(t *testing.T, db *organizations) {
+	// todo
+}