code.gitea.io/gitea@v1.21.7/models/issues/issue_watch_test.go (about)

     1  // Copyright 2017 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  
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestCreateOrUpdateIssueWatch(t *testing.T) {
    17  	assert.NoError(t, unittest.PrepareTestDatabase())
    18  
    19  	assert.NoError(t, issues_model.CreateOrUpdateIssueWatch(db.DefaultContext, 3, 1, true))
    20  	iw := unittest.AssertExistsAndLoadBean(t, &issues_model.IssueWatch{UserID: 3, IssueID: 1})
    21  	assert.True(t, iw.IsWatching)
    22  
    23  	assert.NoError(t, issues_model.CreateOrUpdateIssueWatch(db.DefaultContext, 1, 1, false))
    24  	iw = unittest.AssertExistsAndLoadBean(t, &issues_model.IssueWatch{UserID: 1, IssueID: 1})
    25  	assert.False(t, iw.IsWatching)
    26  }
    27  
    28  func TestGetIssueWatch(t *testing.T) {
    29  	assert.NoError(t, unittest.PrepareTestDatabase())
    30  
    31  	_, exists, err := issues_model.GetIssueWatch(db.DefaultContext, 9, 1)
    32  	assert.True(t, exists)
    33  	assert.NoError(t, err)
    34  
    35  	iw, exists, err := issues_model.GetIssueWatch(db.DefaultContext, 2, 2)
    36  	assert.True(t, exists)
    37  	assert.NoError(t, err)
    38  	assert.False(t, iw.IsWatching)
    39  
    40  	_, exists, err = issues_model.GetIssueWatch(db.DefaultContext, 3, 1)
    41  	assert.False(t, exists)
    42  	assert.NoError(t, err)
    43  }
    44  
    45  func TestGetIssueWatchers(t *testing.T) {
    46  	assert.NoError(t, unittest.PrepareTestDatabase())
    47  
    48  	iws, err := issues_model.GetIssueWatchers(db.DefaultContext, 1, db.ListOptions{})
    49  	assert.NoError(t, err)
    50  	// Watcher is inactive, thus 0
    51  	assert.Len(t, iws, 0)
    52  
    53  	iws, err = issues_model.GetIssueWatchers(db.DefaultContext, 2, db.ListOptions{})
    54  	assert.NoError(t, err)
    55  	// Watcher is explicit not watching
    56  	assert.Len(t, iws, 0)
    57  
    58  	iws, err = issues_model.GetIssueWatchers(db.DefaultContext, 5, db.ListOptions{})
    59  	assert.NoError(t, err)
    60  	// Issue has no Watchers
    61  	assert.Len(t, iws, 0)
    62  
    63  	iws, err = issues_model.GetIssueWatchers(db.DefaultContext, 7, db.ListOptions{})
    64  	assert.NoError(t, err)
    65  	// Issue has one watcher
    66  	assert.Len(t, iws, 1)
    67  }