本站源代码
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.

81 lines
2.6KB

  1. // Copyright 2017 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 models
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestAddDeletedBranch(t *testing.T) {
  10. assert.NoError(t, PrepareTestDatabase())
  11. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  12. firstBranch := AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch)
  13. assert.Error(t, repo.AddDeletedBranch(firstBranch.Name, firstBranch.Commit, firstBranch.DeletedByID))
  14. assert.NoError(t, repo.AddDeletedBranch("test", "5655464564554545466464656", int64(1)))
  15. }
  16. func TestGetDeletedBranches(t *testing.T) {
  17. assert.NoError(t, PrepareTestDatabase())
  18. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  19. branches, err := repo.GetDeletedBranches()
  20. assert.NoError(t, err)
  21. assert.Len(t, branches, 2)
  22. }
  23. func TestGetDeletedBranch(t *testing.T) {
  24. assert.NoError(t, PrepareTestDatabase())
  25. firstBranch := AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch)
  26. assert.NotNil(t, getDeletedBranch(t, firstBranch))
  27. }
  28. func TestDeletedBranchLoadUser(t *testing.T) {
  29. assert.NoError(t, PrepareTestDatabase())
  30. firstBranch := AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch)
  31. secondBranch := AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2}).(*DeletedBranch)
  32. branch := getDeletedBranch(t, firstBranch)
  33. assert.Nil(t, branch.DeletedBy)
  34. branch.LoadUser()
  35. assert.NotNil(t, branch.DeletedBy)
  36. assert.Equal(t, "user1", branch.DeletedBy.Name)
  37. branch = getDeletedBranch(t, secondBranch)
  38. assert.Nil(t, branch.DeletedBy)
  39. branch.LoadUser()
  40. assert.NotNil(t, branch.DeletedBy)
  41. assert.Equal(t, "Ghost", branch.DeletedBy.Name)
  42. }
  43. func TestRemoveDeletedBranch(t *testing.T) {
  44. assert.NoError(t, PrepareTestDatabase())
  45. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  46. firstBranch := AssertExistsAndLoadBean(t, &DeletedBranch{ID: 1}).(*DeletedBranch)
  47. err := repo.RemoveDeletedBranch(1)
  48. assert.NoError(t, err)
  49. AssertNotExistsBean(t, firstBranch)
  50. AssertExistsAndLoadBean(t, &DeletedBranch{ID: 2})
  51. }
  52. func getDeletedBranch(t *testing.T, branch *DeletedBranch) *DeletedBranch {
  53. repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
  54. deletedBranch, err := repo.GetDeletedBranchByID(branch.ID)
  55. assert.NoError(t, err)
  56. assert.Equal(t, branch.ID, deletedBranch.ID)
  57. assert.Equal(t, branch.Name, deletedBranch.Name)
  58. assert.Equal(t, branch.Commit, deletedBranch.Commit)
  59. assert.Equal(t, branch.DeletedByID, deletedBranch.DeletedByID)
  60. return deletedBranch
  61. }
上海开阖软件有限公司 沪ICP备12045867号-1