1
0

notices_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package database
  2. import (
  3. "context"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. "github.com/stretchr/testify/require"
  8. "gorm.io/gorm"
  9. )
  10. func TestNotice_BeforeCreate(t *testing.T) {
  11. now := time.Now()
  12. db := &gorm.DB{
  13. Config: &gorm.Config{
  14. SkipDefaultTransaction: true,
  15. NowFunc: func() time.Time {
  16. return now
  17. },
  18. },
  19. }
  20. t.Run("CreatedUnix has been set", func(t *testing.T) {
  21. notice := &Notice{
  22. CreatedUnix: 1,
  23. }
  24. _ = notice.BeforeCreate(db)
  25. assert.Equal(t, int64(1), notice.CreatedUnix)
  26. })
  27. t.Run("CreatedUnix has not been set", func(t *testing.T) {
  28. notice := &Notice{}
  29. _ = notice.BeforeCreate(db)
  30. assert.Equal(t, db.NowFunc().Unix(), notice.CreatedUnix)
  31. })
  32. }
  33. func TestNotice_AfterFind(t *testing.T) {
  34. now := time.Now()
  35. db := &gorm.DB{
  36. Config: &gorm.Config{
  37. SkipDefaultTransaction: true,
  38. NowFunc: func() time.Time {
  39. return now
  40. },
  41. },
  42. }
  43. notice := &Notice{
  44. CreatedUnix: now.Unix(),
  45. }
  46. _ = notice.AfterFind(db)
  47. assert.Equal(t, notice.CreatedUnix, notice.Created.Unix())
  48. }
  49. func TestNotices(t *testing.T) {
  50. if testing.Short() {
  51. t.Skip()
  52. }
  53. t.Parallel()
  54. ctx := context.Background()
  55. s := &NoticesStore{
  56. db: newTestDB(t, "NoticesStore"),
  57. }
  58. for _, tc := range []struct {
  59. name string
  60. test func(t *testing.T, ctx context.Context, s *NoticesStore)
  61. }{
  62. {"Create", noticesCreate},
  63. {"DeleteByIDs", noticesDeleteByIDs},
  64. {"DeleteAll", noticesDeleteAll},
  65. {"List", noticesList},
  66. {"Count", noticesCount},
  67. } {
  68. t.Run(tc.name, func(t *testing.T) {
  69. t.Cleanup(func() {
  70. err := clearTables(t, s.db)
  71. require.NoError(t, err)
  72. })
  73. tc.test(t, ctx, s)
  74. })
  75. if t.Failed() {
  76. break
  77. }
  78. }
  79. }
  80. func noticesCreate(t *testing.T, ctx context.Context, s *NoticesStore) {
  81. err := s.Create(ctx, NoticeTypeRepository, "test")
  82. require.NoError(t, err)
  83. count := s.Count(ctx)
  84. assert.Equal(t, int64(1), count)
  85. }
  86. func noticesDeleteByIDs(t *testing.T, ctx context.Context, s *NoticesStore) {
  87. err := s.Create(ctx, NoticeTypeRepository, "test")
  88. require.NoError(t, err)
  89. notices, err := s.List(ctx, 1, 10)
  90. require.NoError(t, err)
  91. ids := make([]int64, 0, len(notices))
  92. for _, notice := range notices {
  93. ids = append(ids, notice.ID)
  94. }
  95. // Non-existing IDs should be ignored
  96. ids = append(ids, 404)
  97. err = s.DeleteByIDs(ctx, ids...)
  98. require.NoError(t, err)
  99. count := s.Count(ctx)
  100. assert.Equal(t, int64(0), count)
  101. }
  102. func noticesDeleteAll(t *testing.T, ctx context.Context, s *NoticesStore) {
  103. err := s.Create(ctx, NoticeTypeRepository, "test")
  104. require.NoError(t, err)
  105. err = s.DeleteAll(ctx)
  106. require.NoError(t, err)
  107. count := s.Count(ctx)
  108. assert.Equal(t, int64(0), count)
  109. }
  110. func noticesList(t *testing.T, ctx context.Context, s *NoticesStore) {
  111. err := s.Create(ctx, NoticeTypeRepository, "test 1")
  112. require.NoError(t, err)
  113. err = s.Create(ctx, NoticeTypeRepository, "test 2")
  114. require.NoError(t, err)
  115. got1, err := s.List(ctx, 1, 1)
  116. require.NoError(t, err)
  117. require.Len(t, got1, 1)
  118. got2, err := s.List(ctx, 2, 1)
  119. require.NoError(t, err)
  120. require.Len(t, got2, 1)
  121. assert.True(t, got1[0].ID > got2[0].ID)
  122. got, err := s.List(ctx, 1, 3)
  123. require.NoError(t, err)
  124. require.Len(t, got, 2)
  125. }
  126. func noticesCount(t *testing.T, ctx context.Context, s *NoticesStore) {
  127. count := s.Count(ctx)
  128. assert.Equal(t, int64(0), count)
  129. err := s.Create(ctx, NoticeTypeRepository, "test")
  130. require.NoError(t, err)
  131. count = s.Count(ctx)
  132. assert.Equal(t, int64(1), count)
  133. }