code.gitea.io/gitea@v1.21.7/services/convert/pull_test.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package convert
     5  
     6  import (
     7  	"testing"
     8  
     9  	"code.gitea.io/gitea/models/db"
    10  	issues_model "code.gitea.io/gitea/models/issues"
    11  	"code.gitea.io/gitea/models/perm"
    12  	access_model "code.gitea.io/gitea/models/perm/access"
    13  	repo_model "code.gitea.io/gitea/models/repo"
    14  	"code.gitea.io/gitea/models/unittest"
    15  	"code.gitea.io/gitea/modules/git"
    16  	"code.gitea.io/gitea/modules/structs"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func TestPullRequest_APIFormat(t *testing.T) {
    22  	// with HeadRepo
    23  	assert.NoError(t, unittest.PrepareTestDatabase())
    24  	headRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
    25  	pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
    26  	assert.NoError(t, pr.LoadAttributes(db.DefaultContext))
    27  	assert.NoError(t, pr.LoadIssue(db.DefaultContext))
    28  	apiPullRequest := ToAPIPullRequest(git.DefaultContext, pr, nil)
    29  	assert.NotNil(t, apiPullRequest)
    30  	assert.EqualValues(t, &structs.PRBranchInfo{
    31  		Name:       "branch1",
    32  		Ref:        "refs/pull/2/head",
    33  		Sha:        "4a357436d925b5c974181ff12a994538ddc5a269",
    34  		RepoID:     1,
    35  		Repository: ToRepo(db.DefaultContext, headRepo, access_model.Permission{AccessMode: perm.AccessModeRead}),
    36  	}, apiPullRequest.Head)
    37  
    38  	// withOut HeadRepo
    39  	pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 1})
    40  	assert.NoError(t, pr.LoadIssue(db.DefaultContext))
    41  	assert.NoError(t, pr.LoadAttributes(db.DefaultContext))
    42  	// simulate fork deletion
    43  	pr.HeadRepo = nil
    44  	pr.HeadRepoID = 100000
    45  	apiPullRequest = ToAPIPullRequest(git.DefaultContext, pr, nil)
    46  	assert.NotNil(t, apiPullRequest)
    47  	assert.Nil(t, apiPullRequest.Head.Repository)
    48  	assert.EqualValues(t, -1, apiPullRequest.Head.RepoID)
    49  }