2020-04-05 01:20:50 -05:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2017-09-12 01:48:13 -05:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2017-12-10 23:37:04 -05:00
|
|
|
|
2022-05-20 09:08:52 -05:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-11-12 09:36:47 -05:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2021-11-24 04:49:20 -05:00
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2019-08-15 09:46:21 -05:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2017-09-12 01:48:13 -05:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCancelStopwatch(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-12 01:48:13 -05:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
user1, err := user_model.GetUserByID(1)
|
2017-09-12 01:48:13 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
issue1, err := GetIssueByID(1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
issue2, err := GetIssueByID(2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = CancelStopwatch(user1, issue1)
|
|
|
|
assert.NoError(t, err)
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, &Stopwatch{UserID: user1.ID, IssueID: issue1.ID})
|
2017-09-12 01:48:13 -05:00
|
|
|
|
2021-11-16 03:53:21 -05:00
|
|
|
_ = unittest.AssertExistsAndLoadBean(t, &Comment{Type: CommentTypeCancelTracking, PosterID: user1.ID, IssueID: issue1.ID})
|
2017-09-12 01:48:13 -05:00
|
|
|
|
|
|
|
assert.Nil(t, CancelStopwatch(user1, issue2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStopwatchExists(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-12 01:48:13 -05:00
|
|
|
|
|
|
|
assert.True(t, StopwatchExists(1, 1))
|
|
|
|
assert.False(t, StopwatchExists(1, 2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHasUserStopwatch(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-12 01:48:13 -05:00
|
|
|
|
2022-05-20 09:08:52 -05:00
|
|
|
exists, sw, err := HasUserStopwatch(db.DefaultContext, 1)
|
2017-09-12 01:48:13 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, exists)
|
|
|
|
assert.Equal(t, int64(1), sw.ID)
|
|
|
|
|
2022-05-20 09:08:52 -05:00
|
|
|
exists, _, err = HasUserStopwatch(db.DefaultContext, 3)
|
2017-09-12 01:48:13 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.False(t, exists)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCreateOrStopIssueStopwatch(t *testing.T) {
|
2021-11-12 09:36:47 -05:00
|
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
2017-09-12 01:48:13 -05:00
|
|
|
|
2021-11-24 04:49:20 -05:00
|
|
|
user2, err := user_model.GetUserByID(2)
|
2017-09-12 01:48:13 -05:00
|
|
|
assert.NoError(t, err)
|
2021-11-24 04:49:20 -05:00
|
|
|
user3, err := user_model.GetUserByID(3)
|
2017-09-12 01:48:13 -05:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
issue1, err := GetIssueByID(1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
issue2, err := GetIssueByID(2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
assert.NoError(t, CreateOrStopIssueStopwatch(user3, issue1))
|
2021-11-16 03:53:21 -05:00
|
|
|
sw := unittest.AssertExistsAndLoadBean(t, &Stopwatch{UserID: 3, IssueID: 1}).(*Stopwatch)
|
2021-06-07 00:27:09 -05:00
|
|
|
assert.LessOrEqual(t, sw.CreatedUnix, timeutil.TimeStampNow())
|
2017-09-12 01:48:13 -05:00
|
|
|
|
|
|
|
assert.NoError(t, CreateOrStopIssueStopwatch(user2, issue2))
|
2021-11-16 03:53:21 -05:00
|
|
|
unittest.AssertNotExistsBean(t, &Stopwatch{UserID: 2, IssueID: 2})
|
|
|
|
unittest.AssertExistsAndLoadBean(t, &TrackedTime{UserID: 2, IssueID: 2})
|
2017-09-12 01:48:13 -05:00
|
|
|
}
|