code.gitea.io/gitea@v1.21.7/models/issues/content_history_test.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package issues_test 5 6 import ( 7 "testing" 8 9 "code.gitea.io/gitea/models/db" 10 issues_model "code.gitea.io/gitea/models/issues" 11 "code.gitea.io/gitea/models/unittest" 12 "code.gitea.io/gitea/modules/timeutil" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestContentHistory(t *testing.T) { 18 assert.NoError(t, unittest.PrepareTestDatabase()) 19 20 dbCtx := db.DefaultContext 21 timeStampNow := timeutil.TimeStampNow() 22 23 _ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow, "i-a", true) 24 _ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow.Add(2), "i-b", false) 25 _ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow.Add(7), "i-c", false) 26 27 _ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow, "c-a", true) 28 _ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(5), "c-b", false) 29 _ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(20), "c-c", false) 30 _ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(50), "c-d", false) 31 _ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(51), "c-e", false) 32 33 h1, _ := issues_model.GetIssueContentHistoryByID(dbCtx, 1) 34 assert.EqualValues(t, 1, h1.ID) 35 36 m, _ := issues_model.QueryIssueContentHistoryEditedCountMap(dbCtx, 10) 37 assert.Equal(t, 3, m[0]) 38 assert.Equal(t, 5, m[100]) 39 40 /* 41 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 42 when the refactor of models are done, this test will be possible to be run then with a real `User` model. 43 */ 44 type User struct { 45 ID int64 46 Name string 47 FullName string 48 } 49 _ = db.GetEngine(dbCtx).Sync(&User{}) 50 51 list1, _ := issues_model.FetchIssueContentHistoryList(dbCtx, 10, 0) 52 assert.Len(t, list1, 3) 53 list2, _ := issues_model.FetchIssueContentHistoryList(dbCtx, 10, 100) 54 assert.Len(t, list2, 5) 55 56 hasHistory1, _ := issues_model.HasIssueContentHistory(dbCtx, 10, 0) 57 assert.True(t, hasHistory1) 58 hasHistory2, _ := issues_model.HasIssueContentHistory(dbCtx, 10, 1) 59 assert.False(t, hasHistory2) 60 61 h6, h6Prev, _ := issues_model.GetIssueContentHistoryAndPrev(dbCtx, 10, 6) 62 assert.EqualValues(t, 6, h6.ID) 63 assert.EqualValues(t, 5, h6Prev.ID) 64 65 // soft-delete 66 _ = issues_model.SoftDeleteIssueContentHistory(dbCtx, 5) 67 h6, h6Prev, _ = issues_model.GetIssueContentHistoryAndPrev(dbCtx, 10, 6) 68 assert.EqualValues(t, 6, h6.ID) 69 assert.EqualValues(t, 4, h6Prev.ID) 70 71 // only keep 3 history revisions for comment_id=100, the first and the last should never be deleted 72 issues_model.KeepLimitedContentHistory(dbCtx, 10, 100, 3) 73 list1, _ = issues_model.FetchIssueContentHistoryList(dbCtx, 10, 0) 74 assert.Len(t, list1, 3) 75 list2, _ = issues_model.FetchIssueContentHistoryList(dbCtx, 10, 100) 76 assert.Len(t, list2, 3) 77 assert.EqualValues(t, 8, list2[0].HistoryID) 78 assert.EqualValues(t, 7, list2[1].HistoryID) 79 assert.EqualValues(t, 4, list2[2].HistoryID) 80 }