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.

94 lines
2.6 KiB

  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. "code.gitea.io/gitea/modules/markdown"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestRepo(t *testing.T) {
  11. repo := &Repository{Name: "testRepo"}
  12. repo.Owner = &User{Name: "testOwner"}
  13. repo.Units = nil
  14. assert.Nil(t, repo.ComposeMetas())
  15. externalTracker := RepoUnit{
  16. Type: UnitTypeExternalTracker,
  17. Config: &ExternalTrackerConfig{
  18. ExternalTrackerFormat: "https://someurl.com/{user}/{repo}/{issue}",
  19. },
  20. }
  21. testSuccess := func(expectedStyle string) {
  22. repo.Units = []*RepoUnit{&externalTracker}
  23. repo.ExternalMetas = nil
  24. metas := repo.ComposeMetas()
  25. assert.Equal(t, expectedStyle, metas["style"])
  26. assert.Equal(t, "testRepo", metas["repo"])
  27. assert.Equal(t, "testOwner", metas["user"])
  28. assert.Equal(t, "https://someurl.com/{user}/{repo}/{issue}", metas["format"])
  29. }
  30. testSuccess(markdown.IssueNameStyleNumeric)
  31. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markdown.IssueNameStyleAlphanumeric
  32. testSuccess(markdown.IssueNameStyleAlphanumeric)
  33. externalTracker.ExternalTrackerConfig().ExternalTrackerStyle = markdown.IssueNameStyleNumeric
  34. testSuccess(markdown.IssueNameStyleNumeric)
  35. }
  36. func TestGetRepositoryCount(t *testing.T) {
  37. assert.NoError(t, PrepareTestDatabase())
  38. count, err1 := GetRepositoryCount(&User{ID: int64(10)})
  39. privateCount, err2 := GetPrivateRepositoryCount(&User{ID: int64(10)})
  40. publicCount, err3 := GetPublicRepositoryCount(&User{ID: int64(10)})
  41. assert.NoError(t, err1)
  42. assert.NoError(t, err2)
  43. assert.NoError(t, err3)
  44. assert.Equal(t, int64(3), count)
  45. assert.Equal(t, (privateCount + publicCount), count)
  46. }
  47. func TestGetPublicRepositoryCount(t *testing.T) {
  48. assert.NoError(t, PrepareTestDatabase())
  49. count, err := GetPublicRepositoryCount(&User{ID: int64(10)})
  50. assert.NoError(t, err)
  51. assert.Equal(t, int64(1), count)
  52. }
  53. func TestGetPrivateRepositoryCount(t *testing.T) {
  54. assert.NoError(t, PrepareTestDatabase())
  55. count, err := GetPrivateRepositoryCount(&User{ID: int64(10)})
  56. assert.NoError(t, err)
  57. assert.Equal(t, int64(2), count)
  58. }
  59. func TestUpdateRepositoryVisibilityChanged(t *testing.T) {
  60. assert.NoError(t, PrepareTestDatabase())
  61. // Get sample repo and change visibility
  62. repo, err := GetRepositoryByID(9)
  63. repo.IsPrivate = true
  64. // Update it
  65. err = UpdateRepository(repo, true)
  66. assert.NoError(t, err)
  67. // Check visibility of action has become private
  68. act := Action{}
  69. _, err = x.ID(3).Get(&act)
  70. assert.NoError(t, err)
  71. assert.Equal(t, true, act.IsPrivate)
  72. }