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.

admin_test.go 3.1KB

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