forked from golift/subscribe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events_test.go
65 lines (49 loc) · 1.8 KB
/
events_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
package subscribe
import (
"testing"
"github.com/stretchr/testify/assert"
)
const testFile3 = "/tmp/this_is_a_testfile_for_subtscribe_test3.go.json"
func TestNew(t *testing.T) {
t.Parallel()
a := assert.New(t)
sub, err := GetDB(testFile3)
a.NotNil(sub.Events.Names(), "the events slice must not be nil")
a.Nil(err, "getting a db must produce no error")
a.EqualValues(0, len(sub.Events.Names()), "event count must be 0 since none have been added")
a.Nil(sub.Events.New("event_test", nil))
a.EqualValues(1, len(sub.Events.Names()), "event count must be 1 since 1 was added")
}
func TestGetEvent(t *testing.T) {
t.Parallel()
a := assert.New(t)
sub, err := GetDB("")
a.NotNil(sub.Events.Names(), "the events map must not be nil")
a.Nil(err, "getting a db must produce no error")
a.Nil(sub.Events.New("event_test", nil))
a.True(sub.Events.Exists("event_test"), "this event exists so the method must return true")
a.EqualValues(1, len(sub.Events.Names()), "event count must be 1 since 1 was added")
a.False(sub.Events.Exists("missing_event"), "this event does not exists")
}
func TestNewEvent(t *testing.T) {
t.Parallel()
a := assert.New(t)
sub := &Subscribe{Events: &Events{Map: make(map[string]*Rules)}}
a.Nil(sub.Events.New("event_test", nil))
a.NotNil(sub.Events.Map["event_test"], "the event rules map must not be nil")
}
func TestRemoveEvent(t *testing.T) {
t.Parallel()
a := assert.New(t)
sub := &Subscribe{Events: &Events{Map: make(map[string]*Rules)}}
sub.Events.Remove("no_event")
// Make two events to remove.
sub.Events.Map["some_event"] = nil
sub.Events.Map["some_event2"] = nil
// Subscribe a user to one of them.
s := sub.CreateSub("test_contact", "api", true, false)
a.Nil(s.Subscribe("some_event2"))
sub.EventRemove("some_event2")
sub.EventRemove("some_event")
// TODO: count them
}