Debian Codegraph @

client/session/seenstate/seenstate_test.go

raw
1 /*
2 Copyright 2013-2014 Canonical Ltd.
3
4 This program is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License version 3, as published
6 by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful, but
9 WITHOUT ANY WARRANTY; without even the implied warranties of
10 MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11 PURPOSE. See the GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License along
14 with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 package seenstate
18
19 import (
20 "testing"
21
22 . "gopkg.in/check.v1"
23
24 "gitlab.com/ubports/development/core/lomiri-push-service/protocol"
25 )
26
27 func TestSeenState(t *testing.T) { TestingT(t) }
28
29 type ssSuite struct {
30 constructor func() (SeenState, error)
31 }
32
33 var _ = Suite(&ssSuite{})
34
35 func (s *ssSuite) SetUpSuite(c *C) {
36 s.constructor = NewSeenState
37 }
38
39 func (s *ssSuite) TestAllTheLevelThings(c *C) {
40 var err error
41 var ss SeenState
42 // checks NewSeenState returns a SeenState
43 ss, err = s.constructor()
44 // and that it works
45 c.Assert(err, IsNil)
46 // setting a couple of things, sets them
47 c.Check(ss.SetLevel("this", 12), IsNil)
48 c.Check(ss.SetLevel("that", 42), IsNil)
49 all, err := ss.GetAllLevels()
50 c.Check(err, IsNil)
51 c.Check(all, DeepEquals, map[string]int64{"this": 12, "that": 42})
52 // re-setting one of them, resets it
53 c.Check(ss.SetLevel("this", 999), IsNil)
54 all, err = ss.GetAllLevels()
55 c.Check(err, IsNil)
56 c.Check(all, DeepEquals, map[string]int64{"this": 999, "that": 42})
57 // huzzah
58 }
59
60 func (s *ssSuite) TestFilterBySeen(c *C) {
61 var err error
62 var ss SeenState
63 ss, err = s.constructor()
64 // and that it works
65 c.Assert(err, IsNil)
66 n1 := protocol.Notification{MsgId: "m1"}
67 n2 := protocol.Notification{MsgId: "m2"}
68 n3 := protocol.Notification{MsgId: "m3"}
69 n4 := protocol.Notification{MsgId: "m4"}
70 n5 := protocol.Notification{MsgId: "m5"}
71
72 res, err := ss.FilterBySeen([]protocol.Notification{n1, n2, n3})
73 c.Assert(err, IsNil)
74 // everything wasn't seen yet
75 c.Check(res, DeepEquals, []protocol.Notification{n1, n2, n3})
76
77 res, err = ss.FilterBySeen([]protocol.Notification{n1, n3, n4, n5})
78 c.Assert(err, IsNil)
79 // already seen n1-n3 removed
80 c.Check(res, DeepEquals, []protocol.Notification{n4, n5})
81
82 // corner case
83 res, err = ss.FilterBySeen([]protocol.Notification{})
84 c.Assert(err, IsNil)
85 c.Assert(res, HasLen, 0)
86 }