2017-10-26 02:49:16 +02: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"
2022-04-28 13:48:48 +02:00
"code.gitea.io/gitea/models/db"
2021-12-10 09:27:50 +08:00
repo_model "code.gitea.io/gitea/models/repo"
2021-11-12 22:36:47 +08:00
"code.gitea.io/gitea/models/unittest"
2021-11-17 20:34:35 +08:00
2017-10-26 02:49:16 +02:00
"github.com/stretchr/testify/assert"
)
func TestAddDeletedBranch ( t * testing . T ) {
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2021-12-10 09:27:50 +08:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } ) . ( * repo_model . Repository )
2021-11-16 16:53:21 +08:00
firstBranch := unittest . AssertExistsAndLoadBean ( t , & DeletedBranch { ID : 1 } ) . ( * DeletedBranch )
2017-11-04 15:31:59 +02:00
2021-12-10 09:27:50 +08:00
assert . Error ( t , AddDeletedBranch ( repo . ID , firstBranch . Name , firstBranch . Commit , firstBranch . DeletedByID ) )
assert . NoError ( t , AddDeletedBranch ( repo . ID , "test" , "5655464564554545466464656" , int64 ( 1 ) ) )
2017-10-26 02:49:16 +02:00
}
2017-11-04 15:31:59 +02:00
2017-10-26 02:49:16 +02:00
func TestGetDeletedBranches ( t * testing . T ) {
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2021-12-10 09:27:50 +08:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } ) . ( * repo_model . Repository )
2017-10-26 02:49:16 +02:00
2021-12-10 09:27:50 +08:00
branches , err := GetDeletedBranches ( repo . ID )
2017-10-26 02:49:16 +02:00
assert . NoError ( t , err )
assert . Len ( t , branches , 2 )
}
func TestGetDeletedBranch ( t * testing . T ) {
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2021-11-16 16:53:21 +08:00
firstBranch := unittest . AssertExistsAndLoadBean ( t , & DeletedBranch { ID : 1 } ) . ( * DeletedBranch )
2017-11-04 15:31:59 +02:00
2017-10-26 02:49:16 +02:00
assert . NotNil ( t , getDeletedBranch ( t , firstBranch ) )
}
func TestDeletedBranchLoadUser ( t * testing . T ) {
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2017-11-04 15:31:59 +02:00
2021-11-16 16:53:21 +08:00
firstBranch := unittest . AssertExistsAndLoadBean ( t , & DeletedBranch { ID : 1 } ) . ( * DeletedBranch )
secondBranch := unittest . AssertExistsAndLoadBean ( t , & DeletedBranch { ID : 2 } ) . ( * DeletedBranch )
2017-11-04 15:31:59 +02:00
2017-10-26 02:49:16 +02: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-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2021-12-10 09:27:50 +08:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } ) . ( * repo_model . Repository )
2017-10-26 02:49:16 +02:00
2021-11-16 16:53:21 +08:00
firstBranch := unittest . AssertExistsAndLoadBean ( t , & DeletedBranch { ID : 1 } ) . ( * DeletedBranch )
2017-11-04 15:31:59 +02:00
2021-12-10 09:27:50 +08:00
err := RemoveDeletedBranchByID ( repo . ID , 1 )
2017-10-26 02:49:16 +02:00
assert . NoError ( t , err )
2021-11-16 16:53:21 +08:00
unittest . AssertNotExistsBean ( t , firstBranch )
unittest . AssertExistsAndLoadBean ( t , & DeletedBranch { ID : 2 } )
2017-10-26 02:49:16 +02:00
}
2017-11-04 15:31:59 +02:00
func getDeletedBranch ( t * testing . T , branch * DeletedBranch ) * DeletedBranch {
2021-12-10 09:27:50 +08:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } ) . ( * repo_model . Repository )
2017-10-26 02:49:16 +02:00
2021-12-10 09:27:50 +08:00
deletedBranch , err := GetDeletedBranchByID ( repo . ID , branch . ID )
2017-10-26 02:49:16 +02:00
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-09 01:03:04 +08:00
func TestFindRenamedBranch ( t * testing . T ) {
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2021-10-09 01:03:04 +08:00
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 ) {
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2021-12-10 09:27:50 +08:00
repo1 := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } ) . ( * repo_model . Repository )
2021-10-09 01:03:04 +08:00
_isDefault := false
2022-04-28 13:48:48 +02:00
ctx , committer , err := db . TxContext ( )
defer committer . Close ( )
assert . NoError ( t , err )
assert . NoError ( t , UpdateProtectBranch ( ctx , repo1 , & ProtectedBranch {
2021-10-09 01:03:04 +08:00
RepoID : repo1 . ID ,
BranchName : "master" ,
2022-04-28 13:48:48 +02:00
} , WhitelistOptions { } ) )
assert . NoError ( t , committer . Commit ( ) )
2021-10-09 01:03:04 +08:00
2021-12-10 09:27:50 +08:00
assert . NoError ( t , RenameBranch ( repo1 , "master" , "main" , func ( isDefault bool ) error {
2021-10-09 01:03:04 +08:00
_isDefault = isDefault
return nil
} ) )
assert . Equal ( t , true , _isDefault )
2021-12-10 09:27:50 +08:00
repo1 = unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } ) . ( * repo_model . Repository )
2021-10-09 01:03:04 +08:00
assert . Equal ( t , "main" , repo1 . DefaultBranch )
2021-11-16 16:53:21 +08:00
pull := unittest . AssertExistsAndLoadBean ( t , & PullRequest { ID : 1 } ) . ( * PullRequest ) // merged
2021-10-09 01:03:04 +08:00
assert . Equal ( t , "master" , pull . BaseBranch )
2021-11-16 16:53:21 +08:00
pull = unittest . AssertExistsAndLoadBean ( t , & PullRequest { ID : 2 } ) . ( * PullRequest ) // open
2021-10-09 01:03:04 +08:00
assert . Equal ( t , "main" , pull . BaseBranch )
2021-11-16 16:53:21 +08:00
renamedBranch := unittest . AssertExistsAndLoadBean ( t , & RenamedBranch { ID : 2 } ) . ( * RenamedBranch )
2021-10-09 01:03:04 +08:00
assert . Equal ( t , "master" , renamedBranch . From )
assert . Equal ( t , "main" , renamedBranch . To )
assert . Equal ( t , int64 ( 1 ) , renamedBranch . RepoID )
2021-11-16 16:53:21 +08:00
unittest . AssertExistsAndLoadBean ( t , & ProtectedBranch {
2021-10-09 01:03:04 +08:00
RepoID : repo1 . ID ,
BranchName : "main" ,
} )
}
2021-11-08 16:45:37 +01:00
func TestOnlyGetDeletedBranchOnCorrectRepo ( t * testing . T ) {
2021-11-12 22:36:47 +08:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2021-11-08 16:45:37 +01:00
// 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.
2021-12-10 09:27:50 +08:00
repo2 := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 2 } ) . ( * repo_model . Repository )
2021-11-08 16:45:37 +01:00
2021-12-10 09:27:50 +08:00
deletedBranch , err := GetDeletedBranchByID ( repo2 . ID , 1 )
2021-11-08 16:45:37 +01:00
// 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.
2021-12-10 09:27:50 +08:00
repo1 := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : 1 } ) . ( * repo_model . Repository )
2021-11-08 16:45:37 +01:00
2021-12-10 09:27:50 +08:00
deletedBranch , err = GetDeletedBranchByID ( repo1 . ID , 1 )
2021-11-08 16:45:37 +01:00
// Expect no error, and the returned branch to be not nil.
assert . NoError ( t , err )
assert . NotNil ( t , deletedBranch )
}