2022-06-13 12:37:59 +03:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 21:20:29 +03:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
package issues_test
|
|
|
|
|
|
|
|
import (
|
2024-11-16 17:51:59 +03:00
|
|
|
"path/filepath"
|
2022-06-13 12:37:59 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
|
|
"code.gitea.io/gitea/models/unittest"
|
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2024-11-16 17:51:59 +03:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2022-06-13 12:37:59 +03:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-30 22:41:10 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-13 12:37:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCancelStopwatch(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
|
2022-12-03 05:48:26 +03:00
|
|
|
user1, err := user_model.GetUserByID(db.DefaultContext, 1)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
issue2, err := issues_model.GetIssueByID(db.DefaultContext, 2)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
|
2023-09-16 17:39:12 +03:00
|
|
|
err = issues_model.CancelStopwatch(db.DefaultContext, user1, issue1)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.Stopwatch{UserID: user1.ID, IssueID: issue1.ID})
|
|
|
|
|
|
|
|
_ = unittest.AssertExistsAndLoadBean(t, &issues_model.Comment{Type: issues_model.CommentTypeCancelTracking, PosterID: user1.ID, IssueID: issue1.ID})
|
|
|
|
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.CancelStopwatch(db.DefaultContext, user1, issue2))
|
2022-06-13 12:37:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStopwatchExists(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
|
2023-09-16 17:39:12 +03:00
|
|
|
assert.True(t, issues_model.StopwatchExists(db.DefaultContext, 1, 1))
|
|
|
|
assert.False(t, issues_model.StopwatchExists(db.DefaultContext, 1, 2))
|
2022-06-13 12:37:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasUserStopwatch(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
|
2023-02-27 21:46:00 +03:00
|
|
|
exists, sw, _, err := issues_model.HasUserStopwatch(db.DefaultContext, 1)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.True(t, exists)
|
|
|
|
assert.Equal(t, int64(1), sw.ID)
|
|
|
|
|
2023-02-27 21:46:00 +03:00
|
|
|
exists, _, _, err = issues_model.HasUserStopwatch(db.DefaultContext, 3)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.False(t, exists)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateOrStopIssueStopwatch(t *testing.T) {
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
2022-06-13 12:37:59 +03:00
|
|
|
|
2022-12-03 05:48:26 +03:00
|
|
|
user2, err := user_model.GetUserByID(db.DefaultContext, 2)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2023-09-14 05:59:53 +03:00
|
|
|
org3, err := user_model.GetUserByID(db.DefaultContext, 3)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
|
|
|
|
issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
issue2, err := issues_model.GetIssueByID(db.DefaultContext, 2)
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, err)
|
2022-06-13 12:37:59 +03:00
|
|
|
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.CreateOrStopIssueStopwatch(db.DefaultContext, org3, issue1))
|
2022-08-16 05:22:25 +03:00
|
|
|
sw := unittest.AssertExistsAndLoadBean(t, &issues_model.Stopwatch{UserID: 3, IssueID: 1})
|
2022-06-13 12:37:59 +03:00
|
|
|
assert.LessOrEqual(t, sw.CreatedUnix, timeutil.TimeStampNow())
|
|
|
|
|
2024-07-30 22:41:10 +03:00
|
|
|
require.NoError(t, issues_model.CreateOrStopIssueStopwatch(db.DefaultContext, user2, issue2))
|
2022-06-13 12:37:59 +03:00
|
|
|
unittest.AssertNotExistsBean(t, &issues_model.Stopwatch{UserID: 2, IssueID: 2})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &issues_model.TrackedTime{UserID: 2, IssueID: 2})
|
|
|
|
}
|
2024-11-16 17:51:59 +03:00
|
|
|
|
|
|
|
func TestGetUIDsAndStopwatch(t *testing.T) {
|
|
|
|
defer unittest.OverrideFixtures(
|
|
|
|
unittest.FixturesOptions{
|
|
|
|
Dir: filepath.Join(setting.AppWorkPath, "models/fixtures/"),
|
|
|
|
Base: setting.AppWorkPath,
|
|
|
|
Dirs: []string{"models/issues/TestGetUIDsAndStopwatch/"},
|
|
|
|
},
|
|
|
|
)()
|
|
|
|
require.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
|
|
|
|
uidStopwatches, err := issues_model.GetUIDsAndStopwatch(db.DefaultContext)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.EqualValues(t, map[int64][]*issues_model.Stopwatch{
|
|
|
|
1: {
|
|
|
|
{
|
|
|
|
ID: 1,
|
|
|
|
UserID: 1,
|
|
|
|
IssueID: 1,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(1500988001),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: 3,
|
|
|
|
UserID: 1,
|
|
|
|
IssueID: 2,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(1500988004),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
2: {
|
|
|
|
{
|
|
|
|
ID: 2,
|
|
|
|
UserID: 2,
|
|
|
|
IssueID: 2,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(1500988002),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, uidStopwatches)
|
|
|
|
}
|