You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
1.4 KiB

  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package convert
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/structs"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func TestPullRequest_APIFormat(t *testing.T) {
  12. //with HeadRepo
  13. assert.NoError(t, models.PrepareTestDatabase())
  14. headRepo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository)
  15. pr := models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
  16. assert.NoError(t, pr.LoadAttributes())
  17. assert.NoError(t, pr.LoadIssue())
  18. apiPullRequest := ToAPIPullRequest(pr)
  19. assert.NotNil(t, apiPullRequest)
  20. assert.EqualValues(t, &structs.PRBranchInfo{
  21. Name: "branch1",
  22. Ref: "refs/pull/2/head",
  23. Sha: "4a357436d925b5c974181ff12a994538ddc5a269",
  24. RepoID: 1,
  25. Repository: headRepo.APIFormat(models.AccessModeNone),
  26. }, apiPullRequest.Head)
  27. //withOut HeadRepo
  28. pr = models.AssertExistsAndLoadBean(t, &models.PullRequest{ID: 1}).(*models.PullRequest)
  29. assert.NoError(t, pr.LoadIssue())
  30. assert.NoError(t, pr.LoadAttributes())
  31. // simulate fork deletion
  32. pr.HeadRepo = nil
  33. pr.HeadRepoID = 100000
  34. apiPullRequest = ToAPIPullRequest(pr)
  35. assert.NotNil(t, apiPullRequest)
  36. assert.Nil(t, apiPullRequest.Head.Repository)
  37. assert.EqualValues(t, -1, apiPullRequest.Head.RepoID)
  38. }