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.

111 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. assert.Len(t, notices, 2)
  46. assert.Equal(t, int64(3), notices[0].ID)
  47. assert.Equal(t, int64(2), notices[1].ID)
  48. notices, err = Notices(2, 2)
  49. assert.NoError(t, err)
  50. assert.Len(t, notices, 1)
  51. assert.Equal(t, int64(1), notices[0].ID)
  52. }
  53. func TestDeleteNotice(t *testing.T) {
  54. assert.NoError(t, PrepareTestDatabase())
  55. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  56. assert.NoError(t, DeleteNotice(3))
  57. AssertNotExistsBean(t, &Notice{ID: 3})
  58. }
  59. func TestDeleteNotices(t *testing.T) {
  60. // delete a non-empty range
  61. assert.NoError(t, PrepareTestDatabase())
  62. AssertExistsAndLoadBean(t, &Notice{ID: 1})
  63. AssertExistsAndLoadBean(t, &Notice{ID: 2})
  64. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  65. assert.NoError(t, DeleteNotices(1, 2))
  66. AssertNotExistsBean(t, &Notice{ID: 1})
  67. AssertNotExistsBean(t, &Notice{ID: 2})
  68. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  69. }
  70. func TestDeleteNotices2(t *testing.T) {
  71. // delete an empty range
  72. assert.NoError(t, PrepareTestDatabase())
  73. AssertExistsAndLoadBean(t, &Notice{ID: 1})
  74. AssertExistsAndLoadBean(t, &Notice{ID: 2})
  75. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  76. assert.NoError(t, DeleteNotices(3, 2))
  77. AssertExistsAndLoadBean(t, &Notice{ID: 1})
  78. AssertExistsAndLoadBean(t, &Notice{ID: 2})
  79. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  80. }
  81. func TestDeleteNoticesByIDs(t *testing.T) {
  82. assert.NoError(t, PrepareTestDatabase())
  83. AssertExistsAndLoadBean(t, &Notice{ID: 1})
  84. AssertExistsAndLoadBean(t, &Notice{ID: 2})
  85. AssertExistsAndLoadBean(t, &Notice{ID: 3})
  86. assert.NoError(t, DeleteNoticesByIDs([]int64{1, 3}))
  87. AssertNotExistsBean(t, &Notice{ID: 1})
  88. AssertExistsAndLoadBean(t, &Notice{ID: 2})
  89. AssertNotExistsBean(t, &Notice{ID: 3})
  90. }