summaryrefslogtreecommitdiffstats
path: root/models/issues/stopwatch_test.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2022-06-13 17:37:59 +0800
committerGitHub <noreply@github.com>2022-06-13 17:37:59 +0800
commit1a9821f57a0293db3adc0eab8aff08ca5fa1026c (patch)
tree3c3d02813eb63c0d0827ef6d9745f6dcdd2636cb /models/issues/stopwatch_test.go
parent3708ca8e2849ca7e36e6bd15ec6935a2a2d81e55 (diff)
downloadgitea-1a9821f57a0293db3adc0eab8aff08ca5fa1026c.tar.gz
gitea-1a9821f57a0293db3adc0eab8aff08ca5fa1026c.zip
Move issues related files into models/issues (#19931)
* Move access and repo permission to models/perm/access * fix test * fix git test * Move functions sequence * Some improvements per @KN4CK3R and @delvh * Move issues related code to models/issues * Move some issues related sub package * Merge * Fix test * Fix test * Fix test * Fix test * Rename some files
Diffstat (limited to 'models/issues/stopwatch_test.go')
-rw-r--r--models/issues/stopwatch_test.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/models/issues/stopwatch_test.go b/models/issues/stopwatch_test.go
new file mode 100644
index 0000000000..c0573964d5
--- /dev/null
+++ b/models/issues/stopwatch_test.go
@@ -0,0 +1,79 @@
+// 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.
+
+package issues_test
+
+import (
+ "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"
+ "code.gitea.io/gitea/modules/timeutil"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestCancelStopwatch(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ user1, err := user_model.GetUserByID(1)
+ assert.NoError(t, err)
+
+ issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1)
+ assert.NoError(t, err)
+ issue2, err := issues_model.GetIssueByID(db.DefaultContext, 2)
+ assert.NoError(t, err)
+
+ err = issues_model.CancelStopwatch(user1, issue1)
+ assert.NoError(t, err)
+ 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})
+
+ assert.Nil(t, issues_model.CancelStopwatch(user1, issue2))
+}
+
+func TestStopwatchExists(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ assert.True(t, issues_model.StopwatchExists(1, 1))
+ assert.False(t, issues_model.StopwatchExists(1, 2))
+}
+
+func TestHasUserStopwatch(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ exists, sw, err := issues_model.HasUserStopwatch(db.DefaultContext, 1)
+ assert.NoError(t, err)
+ assert.True(t, exists)
+ assert.Equal(t, int64(1), sw.ID)
+
+ exists, _, err = issues_model.HasUserStopwatch(db.DefaultContext, 3)
+ assert.NoError(t, err)
+ assert.False(t, exists)
+}
+
+func TestCreateOrStopIssueStopwatch(t *testing.T) {
+ assert.NoError(t, unittest.PrepareTestDatabase())
+
+ user2, err := user_model.GetUserByID(2)
+ assert.NoError(t, err)
+ user3, err := user_model.GetUserByID(3)
+ assert.NoError(t, err)
+
+ issue1, err := issues_model.GetIssueByID(db.DefaultContext, 1)
+ assert.NoError(t, err)
+ issue2, err := issues_model.GetIssueByID(db.DefaultContext, 2)
+ assert.NoError(t, err)
+
+ assert.NoError(t, issues_model.CreateOrStopIssueStopwatch(user3, issue1))
+ sw := unittest.AssertExistsAndLoadBean(t, &issues_model.Stopwatch{UserID: 3, IssueID: 1}).(*issues_model.Stopwatch)
+ assert.LessOrEqual(t, sw.CreatedUnix, timeutil.TimeStampNow())
+
+ assert.NoError(t, issues_model.CreateOrStopIssueStopwatch(user2, issue2))
+ unittest.AssertNotExistsBean(t, &issues_model.Stopwatch{UserID: 2, IssueID: 2})
+ unittest.AssertExistsAndLoadBean(t, &issues_model.TrackedTime{UserID: 2, IssueID: 2})
+}