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.

content_history_test.go 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2021 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 issues
  5. import (
  6. "testing"
  7. "code.gitea.io/gitea/models/db"
  8. "code.gitea.io/gitea/models/unittest"
  9. "code.gitea.io/gitea/modules/timeutil"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func TestContentHistory(t *testing.T) {
  13. assert.NoError(t, unittest.PrepareTestDatabase())
  14. dbCtx := db.DefaultContext
  15. timeStampNow := timeutil.TimeStampNow()
  16. _ = SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow, "i-a", true)
  17. _ = SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow.Add(2), "i-b", false)
  18. _ = SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow.Add(7), "i-c", false)
  19. _ = SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow, "c-a", true)
  20. _ = SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(5), "c-b", false)
  21. _ = SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(20), "c-c", false)
  22. _ = SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(50), "c-d", false)
  23. _ = SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(51), "c-e", false)
  24. h1, _ := GetIssueContentHistoryByID(dbCtx, 1)
  25. assert.EqualValues(t, 1, h1.ID)
  26. m, _ := QueryIssueContentHistoryEditedCountMap(dbCtx, 10)
  27. assert.Equal(t, 3, m[0])
  28. assert.Equal(t, 5, m[100])
  29. /*
  30. we can not have this test with real `User` now, because we can not depend on `User` model (circle-import), so there is no `user` table
  31. when the refactor of models are done, this test will be possible to be run then with a real `User` model.
  32. */
  33. type User struct {
  34. ID int64
  35. Name string
  36. FullName string
  37. }
  38. _ = db.GetEngine(dbCtx).Sync2(&User{})
  39. list1, _ := FetchIssueContentHistoryList(dbCtx, 10, 0)
  40. assert.Len(t, list1, 3)
  41. list2, _ := FetchIssueContentHistoryList(dbCtx, 10, 100)
  42. assert.Len(t, list2, 5)
  43. hasHistory1, _ := HasIssueContentHistory(dbCtx, 10, 0)
  44. assert.True(t, hasHistory1)
  45. hasHistory2, _ := HasIssueContentHistory(dbCtx, 10, 1)
  46. assert.False(t, hasHistory2)
  47. h6, h6Prev, _ := GetIssueContentHistoryAndPrev(dbCtx, 6)
  48. assert.EqualValues(t, 6, h6.ID)
  49. assert.EqualValues(t, 5, h6Prev.ID)
  50. // soft-delete
  51. _ = SoftDeleteIssueContentHistory(dbCtx, 5)
  52. h6, h6Prev, _ = GetIssueContentHistoryAndPrev(dbCtx, 6)
  53. assert.EqualValues(t, 6, h6.ID)
  54. assert.EqualValues(t, 4, h6Prev.ID)
  55. // only keep 3 history revisions for comment_id=100, the first and the last should never be deleted
  56. keepLimitedContentHistory(dbCtx, 10, 100, 3)
  57. list1, _ = FetchIssueContentHistoryList(dbCtx, 10, 0)
  58. assert.Len(t, list1, 3)
  59. list2, _ = FetchIssueContentHistoryList(dbCtx, 10, 100)
  60. assert.Len(t, list2, 3)
  61. assert.EqualValues(t, 8, list2[0].HistoryID)
  62. assert.EqualValues(t, 7, list2[1].HistoryID)
  63. assert.EqualValues(t, 4, list2[2].HistoryID)
  64. }