2021-10-10 22:40:03 +00:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-10-10 22:40:03 +00:00
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
package issues_test
|
2021-10-10 22:40:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-06-13 09:37:59 +00:00
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
2021-11-12 14:36:47 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-10-10 22:40:03 +00:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 19:41:27 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-10-10 22:40:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContentHistory(t *testing.T) {
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2021-10-10 22:40:03 +00:00
|
|
|
|
|
|
|
dbCtx := db.DefaultContext
|
|
|
|
timeStampNow := timeutil.TimeStampNow()
|
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
_ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow, "i-a", true)
|
|
|
|
_ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow.Add(2), "i-b", false)
|
|
|
|
_ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 0, timeStampNow.Add(7), "i-c", false)
|
2021-10-10 22:40:03 +00:00
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
_ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow, "c-a", true)
|
|
|
|
_ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(5), "c-b", false)
|
|
|
|
_ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(20), "c-c", false)
|
|
|
|
_ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(50), "c-d", false)
|
|
|
|
_ = issues_model.SaveIssueContentHistory(dbCtx, 1, 10, 100, timeStampNow.Add(51), "c-e", false)
|
2021-10-10 22:40:03 +00:00
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
h1, _ := issues_model.GetIssueContentHistoryByID(dbCtx, 1)
|
2021-10-10 22:40:03 +00:00
|
|
|
assert.EqualValues(t, 1, h1.ID)
|
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
m, _ := issues_model.QueryIssueContentHistoryEditedCountMap(dbCtx, 10)
|
2021-10-10 22:40:03 +00:00
|
|
|
assert.Equal(t, 3, m[0])
|
|
|
|
assert.Equal(t, 5, m[100])
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
when the refactor of models are done, this test will be possible to be run then with a real `User` model.
|
|
|
|
*/
|
|
|
|
type User struct {
|
2022-02-20 19:50:11 +00:00
|
|
|
ID int64
|
|
|
|
Name string
|
|
|
|
FullName string
|
2021-10-10 22:40:03 +00:00
|
|
|
}
|
2023-08-13 19:17:21 +00:00
|
|
|
_ = db.GetEngine(dbCtx).Sync(&User{})
|
2021-10-10 22:40:03 +00:00
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
list1, _ := issues_model.FetchIssueContentHistoryList(dbCtx, 10, 0)
|
2021-10-10 22:40:03 +00:00
|
|
|
assert.Len(t, list1, 3)
|
2022-06-13 09:37:59 +00:00
|
|
|
list2, _ := issues_model.FetchIssueContentHistoryList(dbCtx, 10, 100)
|
2021-10-10 22:40:03 +00:00
|
|
|
assert.Len(t, list2, 5)
|
|
|
|
|
2022-06-13 09:37:59 +00:00
|
|
|
hasHistory1, _ := issues_model.HasIssueContentHistory(dbCtx, 10, 0)
|
2021-11-22 12:20:16 +00:00
|
|
|
assert.True(t, hasHistory1)
|
2022-06-13 09:37:59 +00:00
|
|
|
hasHistory2, _ := issues_model.HasIssueContentHistory(dbCtx, 10, 1)
|
2021-11-22 12:20:16 +00:00
|
|
|
assert.False(t, hasHistory2)
|
|
|
|
|
2023-11-25 17:21:21 +00:00
|
|
|
h6, h6Prev, _ := issues_model.GetIssueContentHistoryAndPrev(dbCtx, 10, 6)
|
2021-10-10 22:40:03 +00:00
|
|
|
assert.EqualValues(t, 6, h6.ID)
|
|
|
|
assert.EqualValues(t, 5, h6Prev.ID)
|
|
|
|
|
|
|
|
// soft-delete
|
2022-06-13 09:37:59 +00:00
|
|
|
_ = issues_model.SoftDeleteIssueContentHistory(dbCtx, 5)
|
2023-11-25 17:21:21 +00:00
|
|
|
h6, h6Prev, _ = issues_model.GetIssueContentHistoryAndPrev(dbCtx, 10, 6)
|
2021-10-10 22:40:03 +00:00
|
|
|
assert.EqualValues(t, 6, h6.ID)
|
|
|
|
assert.EqualValues(t, 4, h6Prev.ID)
|
|
|
|
|
2021-11-22 12:20:16 +00:00
|
|
|
// only keep 3 history revisions for comment_id=100, the first and the last should never be deleted
|
2022-06-13 09:37:59 +00:00
|
|
|
issues_model.KeepLimitedContentHistory(dbCtx, 10, 100, 3)
|
|
|
|
list1, _ = issues_model.FetchIssueContentHistoryList(dbCtx, 10, 0)
|
2021-10-10 22:40:03 +00:00
|
|
|
assert.Len(t, list1, 3)
|
2022-06-13 09:37:59 +00:00
|
|
|
list2, _ = issues_model.FetchIssueContentHistoryList(dbCtx, 10, 100)
|
2021-10-10 22:40:03 +00:00
|
|
|
assert.Len(t, list2, 3)
|
2021-11-22 12:20:16 +00:00
|
|
|
assert.EqualValues(t, 8, list2[0].HistoryID)
|
|
|
|
assert.EqualValues(t, 7, list2[1].HistoryID)
|
2021-10-10 22:40:03 +00:00
|
|
|
assert.EqualValues(t, 4, list2[2].HistoryID)
|
|
|
|
}
|
2024-02-28 18:23:14 +00:00
|
|
|
|
|
|
|
func TestHasIssueContentHistory(t *testing.T) {
|
2024-07-30 19:41:27 +00:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2024-02-28 18:23:14 +00:00
|
|
|
|
|
|
|
// Ensures that comment_id is into taken account even if it's zero.
|
|
|
|
_ = issues_model.SaveIssueContentHistory(db.DefaultContext, 1, 11, 100, timeutil.TimeStampNow(), "c-a", true)
|
|
|
|
_ = issues_model.SaveIssueContentHistory(db.DefaultContext, 1, 11, 100, timeutil.TimeStampNow().Add(5), "c-b", false)
|
|
|
|
|
|
|
|
hasHistory1, _ := issues_model.HasIssueContentHistory(db.DefaultContext, 11, 0)
|
|
|
|
assert.False(t, hasHistory1)
|
|
|
|
hasHistory2, _ := issues_model.HasIssueContentHistory(db.DefaultContext, 11, 100)
|
|
|
|
assert.True(t, hasHistory2)
|
|
|
|
}
|