github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/storetest/status_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package storetest
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/mattermost/mattermost-server/v5/model"
    12  	"github.com/mattermost/mattermost-server/v5/store"
    13  )
    14  
    15  func TestStatusStore(t *testing.T, ss store.Store) {
    16  	t.Run("", func(t *testing.T) { testStatusStore(t, ss) })
    17  	t.Run("ActiveUserCount", func(t *testing.T) { testActiveUserCount(t, ss) })
    18  }
    19  
    20  func testStatusStore(t *testing.T, ss store.Store) {
    21  	status := &model.Status{UserId: model.NewId(), Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: 0, ActiveChannel: ""}
    22  	require.NoError(t, ss.Status().SaveOrUpdate(status))
    23  
    24  	status.LastActivityAt = 10
    25  
    26  	_, err := ss.Status().Get(status.UserId)
    27  	require.NoError(t, err)
    28  
    29  	status2 := &model.Status{UserId: model.NewId(), Status: model.STATUS_AWAY, Manual: false, LastActivityAt: 0, ActiveChannel: ""}
    30  	require.NoError(t, ss.Status().SaveOrUpdate(status2))
    31  
    32  	status3 := &model.Status{UserId: model.NewId(), Status: model.STATUS_OFFLINE, Manual: false, LastActivityAt: 0, ActiveChannel: ""}
    33  	require.NoError(t, ss.Status().SaveOrUpdate(status3))
    34  
    35  	statuses, err := ss.Status().GetByIds([]string{status.UserId, "junk"})
    36  	require.NoError(t, err)
    37  	require.Len(t, statuses, 1, "should only have 1 status")
    38  
    39  	err = ss.Status().ResetAll()
    40  	require.NoError(t, err)
    41  
    42  	statusParameter, err := ss.Status().Get(status.UserId)
    43  	require.NoError(t, err)
    44  	require.Equal(t, statusParameter.Status, model.STATUS_OFFLINE, "should be offline")
    45  
    46  	err = ss.Status().UpdateLastActivityAt(status.UserId, 10)
    47  	require.NoError(t, err)
    48  }
    49  
    50  func testActiveUserCount(t *testing.T, ss store.Store) {
    51  	status := &model.Status{UserId: model.NewId(), Status: model.STATUS_ONLINE, Manual: false, LastActivityAt: model.GetMillis(), ActiveChannel: ""}
    52  	require.NoError(t, ss.Status().SaveOrUpdate(status))
    53  
    54  	count, err := ss.Status().GetTotalActiveUsersCount()
    55  	require.NoError(t, err)
    56  	require.True(t, count > 0, "expected count > 0, got %d", count)
    57  }
    58  
    59  type ByUserId []*model.Status
    60  
    61  func (s ByUserId) Len() int           { return len(s) }
    62  func (s ByUserId) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
    63  func (s ByUserId) Less(i, j int) bool { return s[i].UserId < s[j].UserId }