summaryrefslogtreecommitdiffstats
path: root/store/sql_status_store_test.go
blob: dce9738508425752782d649d1d8371ce06a324e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package store

import (
	"testing"

	"github.com/mattermost/platform/model"
)

func TestSqlStatusStore(t *testing.T) {
	Setup()

	status := &model.Status{model.NewId(), model.STATUS_ONLINE, false, 0, ""}

	if err := (<-store.Status().SaveOrUpdate(status)).Err; err != nil {
		t.Fatal(err)
	}

	status.LastActivityAt = 10

	if err := (<-store.Status().SaveOrUpdate(status)).Err; err != nil {
		t.Fatal(err)
	}

	if err := (<-store.Status().Get(status.UserId)).Err; err != nil {
		t.Fatal(err)
	}

	status2 := &model.Status{model.NewId(), model.STATUS_AWAY, false, 0, ""}
	if err := (<-store.Status().SaveOrUpdate(status2)).Err; err != nil {
		t.Fatal(err)
	}

	status3 := &model.Status{model.NewId(), model.STATUS_OFFLINE, false, 0, ""}
	if err := (<-store.Status().SaveOrUpdate(status3)).Err; err != nil {
		t.Fatal(err)
	}

	if result := <-store.Status().GetOnlineAway(); result.Err != nil {
		t.Fatal(result.Err)
	} else {
		statuses := result.Data.([]*model.Status)
		for _, status := range statuses {
			if status.Status == model.STATUS_OFFLINE {
				t.Fatal("should not have returned offline statuses")
			}
		}
	}

	if result := <-store.Status().GetOnline(); result.Err != nil {
		t.Fatal(result.Err)
	} else {
		statuses := result.Data.([]*model.Status)
		for _, status := range statuses {
			if status.Status != model.STATUS_ONLINE {
				t.Fatal("should not have returned offline statuses")
			}
		}
	}

	if result := <-store.Status().GetByIds([]string{status.UserId, "junk"}); result.Err != nil {
		t.Fatal(result.Err)
	} else {
		statuses := result.Data.([]*model.Status)
		if len(statuses) != 1 {
			t.Fatal("should only have 1 status")
		}
	}

	if err := (<-store.Status().ResetAll()).Err; err != nil {
		t.Fatal(err)
	}

	if result := <-store.Status().Get(status.UserId); result.Err != nil {
		t.Fatal(result.Err)
	} else {
		status := result.Data.(*model.Status)
		if status.Status != model.STATUS_OFFLINE {
			t.Fatal("should be offline")
		}
	}

	if result := <-store.Status().UpdateLastActivityAt(status.UserId, 10); result.Err != nil {
		t.Fatal(result.Err)
	}
}

func TestActiveUserCount(t *testing.T) {
	Setup()

	status := &model.Status{model.NewId(), model.STATUS_ONLINE, false, model.GetMillis(), ""}
	Must(store.Status().SaveOrUpdate(status))

	if result := <-store.Status().GetTotalActiveUsersCount(); result.Err != nil {
		t.Fatal(result.Err)
	} else {
		count := result.Data.(int64)
		if count <= 0 {
			t.Fatal()
		}
	}
}