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.

113 lines
3.0 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. "github.com/stretchr/testify/assert"
  8. )
  9. func TestNotice_TrStr(t *testing.T) {
  10. notice := &Notice{
  11. Type: NoticeRepository,
  12. Description: "test description",
  13. }
  14. assert.Equal(t, "admin.notices.type_1", notice.TrStr())
  15. }
  16. func TestCreateNotice(t *testing.T) {
  17. assert.NoError(t, PrepareTestDatabase())
  18. noticeBean := &Notice{
  19. Type: NoticeRepository,
  20. Description: "test description",
  21. }
  22. AssertNotExistsBean(t, noticeBean)
  23. assert.NoError(t, CreateNotice(noticeBean.Type, noticeBean.Description))
  24. AssertExistsAndLoadBean(t, noticeBean)
  25. }
  26. func TestCreateRepositoryNotice(t *testing.T) {
  27. assert.NoError(t, PrepareTestDatabase())
  28. noticeBean := &Notice{
  29. Type: NoticeRepository,
  30. Description: "test description",
  31. }
  32. AssertNotExistsBean(t, noticeBean)
  33. assert.NoError(t, CreateRepositoryNotice(noticeBean.Description))
  34. AssertExistsAndLoadBean(t, noticeBean)
  35. }
  36. // TODO TestRemoveAllWithNotice
  37. func TestCountNotices(t *testing.T) {
  38. assert.NoError(t, PrepareTestDatabase())
  39. assert.Equal(t, int64(3), CountNotices())
  40. }
  41. func TestNotices(t *testing.T) {
  42. assert.NoError(t, PrepareTestDatabase())
  43. notices, err := Notices(1, 2)
  44. assert.NoError(t, err)
  45. if assert.Len(t, notices, 2) {
  46. assert.Equal(t, int64(3), notices[0].ID)
  47. assert.Equal(t, int64(2), notices[1].ID)
  48. }
  49. notices, err = Notices(2, 2)
  50. assert.NoError(t, err)
  51. if assert.Len(t, notices, 1) {
  52. assert.Equal(t, int64(1), notices[0].ID)
  53. }
  54. }
  55. func TestDeleteNotice(t *testing.T) {
  56. assert.NoError(t, PrepareTestDatabase())
  57. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  58. assert.NoError(t, DeleteNotice(3))
  59. AssertNotExistsBean(t, &Notice{ID: 3})
  60. }
  61. func TestDeleteNotices(t *testing.T) {
  62. // delete a non-empty range
  63. assert.NoError(t, PrepareTestDatabase())
  64. AssertExistsAndLoadBean(t, &Notice{ID: 1})
  65. AssertExistsAndLoadBean(t, &Notice{ID: 2})
  66. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  67. assert.NoError(t, DeleteNotices(1, 2))
  68. AssertNotExistsBean(t, &Notice{ID: 1})
  69. AssertNotExistsBean(t, &Notice{ID: 2})
  70. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  71. }
  72. func TestDeleteNotices2(t *testing.T) {
  73. // delete an empty range
  74. assert.NoError(t, PrepareTestDatabase())
  75. AssertExistsAndLoadBean(t, &Notice{ID: 1})
  76. AssertExistsAndLoadBean(t, &Notice{ID: 2})
  77. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  78. assert.NoError(t, DeleteNotices(3, 2))
  79. AssertExistsAndLoadBean(t, &Notice{ID: 1})
  80. AssertExistsAndLoadBean(t, &Notice{ID: 2})
  81. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  82. }
  83. func TestDeleteNoticesByIDs(t *testing.T) {
  84. assert.NoError(t, PrepareTestDatabase())
  85. AssertExistsAndLoadBean(t, &Notice{ID: 1})
  86. AssertExistsAndLoadBean(t, &Notice{ID: 2})
  87. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  88. assert.NoError(t, DeleteNoticesByIDs([]int64{1, 3}))
  89. AssertNotExistsBean(t, &Notice{ID: 1})
  90. AssertExistsAndLoadBean(t, &Notice{ID: 2})
  91. AssertNotExistsBean(t, &Notice{ID: 3})
  92. }