-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrating_filter_test.go
92 lines (73 loc) · 2.85 KB
/
rating_filter_test.go
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
package scopes_test
import (
. "gopkg.in/check.v1"
"launchpad.net/go-unityscopes/v2"
)
func (s *S) TestRatingFilter(c *C) {
filter1 := scopes.NewRatingFilter("f1", "Options")
c.Check("f1", Equals, filter1.Id)
c.Check("Options", Equals, filter1.Label)
c.Check(filter1.DisplayHints, Equals, scopes.FilterDisplayDefault)
filter1.DisplayHints = scopes.FilterDisplayPrimary
filter1.AddOption("1", "Option 1", false)
filter1.AddOption("2", "Option 2", false)
c.Check(filter1.DisplayHints, Equals, scopes.FilterDisplayPrimary)
c.Check(2, Equals, len(filter1.Options))
c.Check("1", Equals, filter1.Options[0].Id)
c.Check("Option 1", Equals, filter1.Options[0].Label)
c.Check("2", Equals, filter1.Options[1].Id)
c.Check("Option 2", Equals, filter1.Options[1].Label)
// verify the list of options
c.Check(len(filter1.Options), Equals, 2)
c.Check(filter1.Options, DeepEquals, []scopes.FilterOption{scopes.FilterOption{"1", "Option 1", false}, scopes.FilterOption{"2", "Option 2", false}})
}
func (s *S) TestRatingFilterMultiSelection(c *C) {
filter1 := scopes.NewRatingFilter("f1", "Options")
filter1.AddOption("1", "Option 1", false)
filter1.AddOption("2", "Option 2", false)
filter1.AddOption("3", "Option 3", false)
fstate := make(scopes.FilterState)
// enable option1 & option2
filter1.UpdateState(fstate, "1", true)
_, ok := fstate["f1"]
c.Check(ok, Equals, true)
active, ok := filter1.ActiveRating(fstate)
c.Check(active, Equals, "1")
c.Check(ok, Equals, true)
// disable option1
filter1.UpdateState(fstate, "1", false)
active, ok = filter1.ActiveRating(fstate)
c.Check(active, Equals, "")
c.Check(ok, Equals, false)
filter1.UpdateState(fstate, "3", true)
active, ok = filter1.ActiveRating(fstate)
c.Check(active, Equals, "3")
c.Check(ok, Equals, true)
// select another one
filter1.UpdateState(fstate, "1", true)
active, ok = filter1.ActiveRating(fstate)
c.Check(active, Equals, "1")
c.Check(ok, Equals, true)
// select another one
filter1.UpdateState(fstate, "2", true)
// erase not selected
filter1.UpdateState(fstate, "1", false)
active, ok = filter1.ActiveRating(fstate)
c.Check(active, Equals, "2")
c.Check(ok, Equals, true)
// erase the active one
filter1.UpdateState(fstate, "2", false)
active, ok = filter1.ActiveRating(fstate)
c.Check(ok, Equals, false)
c.Check(active, Equals, "")
}
func (s *S) TestRatingFilterBadOption(c *C) {
filter1 := scopes.NewRatingFilter("f1", "Options")
filter1.AddOption("1", "Option 1", false)
filter1.AddOption("2", "Option 2", false)
filter1.AddOption("3", "Option 3", false)
fstate := make(scopes.FilterState)
c.Assert(func() { filter1.UpdateState(fstate, "5", true) }, PanicMatches, "invalid option ID")
c.Assert(func() { filter1.UpdateState(fstate, "5", false) }, PanicMatches, "invalid option ID")
c.Assert(func() { filter1.UpdateState(fstate, "", false) }, PanicMatches, "invalid option ID")
}