2017-10-25 18:49:16 -06:00
|
|
|
// Copyright 2017 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2017-10-25 18:49:16 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddDeletedBranch(t *testing.T) {
|
2021-09-19 05:49:59 -06:00
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
|
|
|
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
|
|
|
firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch)
|
2017-11-04 07:31:59 -06:00
|
|
|
|
2017-10-25 18:49:16 -06:00
|
|
|
assert.Error(t, repo.AddDeletedBranch(firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID))
|
2017-11-04 07:31:59 -06:00
|
|
|
assert.NoError(t, repo.AddDeletedBranch("test", "5655464564554545466464656", int64(1)))
|
2017-10-25 18:49:16 -06:00
|
|
|
}
|
2017-11-04 07:31:59 -06:00
|
|
|
|
2017-10-25 18:49:16 -06:00
|
|
|
func TestGetDeletedBranches(t *testing.T) {
|
2021-09-19 05:49:59 -06:00
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
|
|
|
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
2017-10-25 18:49:16 -06:00
|
|
|
|
|
|
|
branches, err := repo.GetDeletedBranches()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, branches, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetDeletedBranch(t *testing.T) {
|
2021-09-19 05:49:59 -06:00
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
|
|
|
firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch)
|
2017-11-04 07:31:59 -06:00
|
|
|
|
2017-10-25 18:49:16 -06:00
|
|
|
assert.NotNil(t, getDeletedBranch(t, firstBranch))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeletedBranchLoadUser(t *testing.T) {
|
2021-09-19 05:49:59 -06:00
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
2017-11-04 07:31:59 -06:00
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch)
|
|
|
|
secondBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}).(*DeletedBranch)
|
2017-11-04 07:31:59 -06:00
|
|
|
|
2017-10-25 18:49:16 -06:00
|
|
|
branch := getDeletedBranch(t, firstBranch)
|
|
|
|
assert.Nil(t, branch.DeletedBy)
|
|
|
|
branch.LoadUser()
|
|
|
|
assert.NotNil(t, branch.DeletedBy)
|
|
|
|
assert.Equal(t, "user1", branch.DeletedBy.Name)
|
|
|
|
|
|
|
|
branch = getDeletedBranch(t, secondBranch)
|
|
|
|
assert.Nil(t, branch.DeletedBy)
|
|
|
|
branch.LoadUser()
|
|
|
|
assert.NotNil(t, branch.DeletedBy)
|
|
|
|
assert.Equal(t, "Ghost", branch.DeletedBy.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRemoveDeletedBranch(t *testing.T) {
|
2021-09-19 05:49:59 -06:00
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
|
|
|
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
2017-10-25 18:49:16 -06:00
|
|
|
|
2021-09-19 05:49:59 -06:00
|
|
|
firstBranch := db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch)
|
2017-11-04 07:31:59 -06:00
|
|
|
|
2017-10-25 18:49:16 -06:00
|
|
|
err := repo.RemoveDeletedBranch(1)
|
|
|
|
assert.NoError(t, err)
|
2021-09-19 05:49:59 -06:00
|
|
|
db.AssertNotExistsBean(t, firstBranch)
|
|
|
|
db.AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2})
|
2017-10-25 18:49:16 -06:00
|
|
|
}
|
|
|
|
|
2017-11-04 07:31:59 -06:00
|
|
|
func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch {
|
2021-09-19 05:49:59 -06:00
|
|
|
repo := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
2017-10-25 18:49:16 -06:00
|
|
|
|
|
|
|
deletedBranch, err := repo.GetDeletedBranchByID(branch.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, branch.ID, deletedBranch.ID)
|
|
|
|
assert.Equal(t, branch.Name, deletedBranch.Name)
|
|
|
|
assert.Equal(t, branch.Commit, deletedBranch.Commit)
|
|
|
|
assert.Equal(t, branch.DeletedByID, deletedBranch.DeletedByID)
|
|
|
|
|
|
|
|
return deletedBranch
|
|
|
|
}
|
2021-10-08 11:03:04 -06:00
|
|
|
|
|
|
|
func TestFindRenamedBranch(t *testing.T) {
|
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
|
|
|
branch, exist, err := FindRenamedBranch(1, "dev")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, true, exist)
|
|
|
|
assert.Equal(t, "master", branch.To)
|
|
|
|
|
|
|
|
_, exist, err = FindRenamedBranch(1, "unknow")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, false, exist)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRenameBranch(t *testing.T) {
|
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
|
|
|
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
|
|
|
_isDefault := false
|
|
|
|
|
|
|
|
err := UpdateProtectBranch(repo1, &ProtectedBranch{
|
|
|
|
RepoID: repo1.ID,
|
|
|
|
BranchName: "master",
|
|
|
|
}, WhitelistOptions{})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NoError(t, repo1.RenameBranch("master", "main", func(isDefault bool) error {
|
|
|
|
_isDefault = isDefault
|
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
assert.Equal(t, true, _isDefault)
|
|
|
|
repo1 = db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
|
|
|
assert.Equal(t, "main", repo1.DefaultBranch)
|
|
|
|
|
|
|
|
pull := db.AssertExistsAndLoadBean(t, &PullRequest{ID: 1}).(*PullRequest) // merged
|
|
|
|
assert.Equal(t, "master", pull.BaseBranch)
|
|
|
|
|
|
|
|
pull = db.AssertExistsAndLoadBean(t, &PullRequest{ID: 2}).(*PullRequest) // open
|
|
|
|
assert.Equal(t, "main", pull.BaseBranch)
|
|
|
|
|
|
|
|
renamedBranch := db.AssertExistsAndLoadBean(t, &RenamedBranch{ID: 2}).(*RenamedBranch)
|
|
|
|
assert.Equal(t, "master", renamedBranch.From)
|
|
|
|
assert.Equal(t, "main", renamedBranch.To)
|
|
|
|
assert.Equal(t, int64(1), renamedBranch.RepoID)
|
|
|
|
|
|
|
|
db.AssertExistsAndLoadBean(t, &ProtectedBranch{
|
|
|
|
RepoID: repo1.ID,
|
|
|
|
BranchName: "main",
|
|
|
|
})
|
|
|
|
}
|
2021-11-08 08:45:37 -07:00
|
|
|
|
|
|
|
func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
|
|
|
|
assert.NoError(t, db.PrepareTestDatabase())
|
|
|
|
|
|
|
|
// Get deletedBranch with ID of 1 on repo with ID 2.
|
|
|
|
// This should return a nil branch as this deleted branch
|
|
|
|
// is actually on repo with ID 1.
|
|
|
|
repo2 := db.AssertExistsAndLoadBean(t, &Repository{ID: 2}).(*Repository)
|
|
|
|
|
|
|
|
deletedBranch, err := repo2.GetDeletedBranchByID(1)
|
|
|
|
|
|
|
|
// Expect no error, and the returned branch is nil.
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, deletedBranch)
|
|
|
|
|
|
|
|
// Now get the deletedBranch with ID of 1 on repo with ID 1.
|
|
|
|
// This should return the deletedBranch.
|
|
|
|
repo1 := db.AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
|
|
|
|
|
|
|
|
deletedBranch, err = repo1.GetDeletedBranchByID(1)
|
|
|
|
|
|
|
|
// Expect no error, and the returned branch to be not nil.
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, deletedBranch)
|
|
|
|
}
|