Codebase list golang-github-nlopes-slack / f114674
Add Tests, Clean Up Embedded Types, Unexport Them Joe Fitzgerald 8 years ago
5 changed file(s) with 208 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
1717
1818 // Channel contains information about the channel
1919 type Channel struct {
20 BaseGroupConversation
20 groupConversation
2121 IsChannel bool `json:"is_channel"`
2222 IsGeneral bool `json:"is_general"`
2323 IsMember bool `json:"is_member"`
00 package slack
11
2 // BaseConversation is the foundation for IM and BaseGroupConversation
3 type BaseConversation struct {
2 // Conversation is the foundation for IM and BaseGroupConversation
3 type conversation struct {
44 ID string `json:"id"`
55 Created JSONTime `json:"created"`
66 IsOpen bool `json:"is_open"`
1010 UnreadCountDisplay int `json:"unread_count_display,omitempty"`
1111 }
1212
13 // BaseGroupConversation is the foundation for Group and Channel
14 type BaseGroupConversation struct {
15 BaseConversation
13 // GroupConversation is the foundation for Group and Channel
14 type groupConversation struct {
15 conversation
1616 Name string `json:"name"`
1717 Creator string `json:"creator"`
1818 IsArchived bool `json:"is_archived"`
0 package slack
1
2 import (
3 "encoding/json"
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 // Channel
10 var simpleChannel = `{
11 "id": "C024BE91L",
12 "name": "fun",
13 "is_channel": true,
14 "created": 1360782804,
15 "creator": "U024BE7LH",
16 "is_archived": false,
17 "is_general": false,
18 "members": [
19 "U024BE7LH"
20 ],
21 "topic": {
22 "value": "Fun times",
23 "creator": "U024BE7LV",
24 "last_set": 1369677212
25 },
26 "purpose": {
27 "value": "This channel is for fun",
28 "creator": "U024BE7LH",
29 "last_set": 1360782804
30 },
31 "is_member": true,
32 "last_read": "1401383885.000061",
33 "unread_count": 0,
34 "unread_count_display": 0
35 }`
36
37 func unmarshalChannel(j string) (*Channel, error) {
38 channel := &Channel{}
39 if err := json.Unmarshal([]byte(j), &channel); err != nil {
40 return nil, err
41 }
42 return channel, nil
43 }
44
45 func TestSimpleChannel(t *testing.T) {
46 channel, err := unmarshalChannel(simpleChannel)
47 assert.Nil(t, err)
48 assertSimpleChannel(t, channel)
49 }
50
51 func assertSimpleChannel(t *testing.T, channel *Channel) {
52 assert.NotNil(t, channel)
53 assert.Equal(t, "C024BE91L", channel.ID)
54 assert.Equal(t, "fun", channel.Name)
55 assert.Equal(t, true, channel.IsChannel)
56 assert.Equal(t, JSONTime(1360782804), channel.Created)
57 assert.Equal(t, "U024BE7LH", channel.Creator)
58 assert.Equal(t, false, channel.IsArchived)
59 assert.Equal(t, false, channel.IsGeneral)
60 assert.Equal(t, true, channel.IsMember)
61 assert.Equal(t, "1401383885.000061", channel.LastRead)
62 assert.Equal(t, 0, channel.UnreadCount)
63 assert.Equal(t, 0, channel.UnreadCountDisplay)
64 }
65
66 func TestCreateSimpleChannel(t *testing.T) {
67 channel := &Channel{}
68 channel.ID = "C024BE91L"
69 channel.Name = "fun"
70 channel.IsChannel = true
71 channel.Created = JSONTime(1360782804)
72 channel.Creator = "U024BE7LH"
73 channel.IsArchived = false
74 channel.IsGeneral = false
75 channel.IsMember = true
76 channel.LastRead = "1401383885.000061"
77 channel.UnreadCount = 0
78 channel.UnreadCountDisplay = 0
79 assertSimpleChannel(t, channel)
80 }
81
82 // Group
83 var simpleGroup = `{
84 "id": "G024BE91L",
85 "name": "secretplans",
86 "is_group": true,
87 "created": 1360782804,
88 "creator": "U024BE7LH",
89 "is_archived": false,
90 "members": [
91 "U024BE7LH"
92 ],
93 "topic": {
94 "value": "Secret plans on hold",
95 "creator": "U024BE7LV",
96 "last_set": 1369677212
97 },
98 "purpose": {
99 "value": "Discuss secret plans that no-one else should know",
100 "creator": "U024BE7LH",
101 "last_set": 1360782804
102 },
103 "last_read": "1401383885.000061",
104 "unread_count": 0,
105 "unread_count_display": 0
106 }`
107
108 func unmarshalGroup(j string) (*Group, error) {
109 group := &Group{}
110 if err := json.Unmarshal([]byte(j), &group); err != nil {
111 return nil, err
112 }
113 return group, nil
114 }
115
116 func TestSimpleGroup(t *testing.T) {
117 group, err := unmarshalGroup(simpleGroup)
118 assert.Nil(t, err)
119 assertSimpleGroup(t, group)
120 }
121
122 func assertSimpleGroup(t *testing.T, group *Group) {
123 assert.NotNil(t, group)
124 assert.Equal(t, "G024BE91L", group.ID)
125 assert.Equal(t, "secretplans", group.Name)
126 assert.Equal(t, true, group.IsGroup)
127 assert.Equal(t, JSONTime(1360782804), group.Created)
128 assert.Equal(t, "U024BE7LH", group.Creator)
129 assert.Equal(t, false, group.IsArchived)
130 assert.Equal(t, "1401383885.000061", group.LastRead)
131 assert.Equal(t, 0, group.UnreadCount)
132 assert.Equal(t, 0, group.UnreadCountDisplay)
133 }
134
135 func TestCreateSimpleGroup(t *testing.T) {
136 group := &Group{}
137 group.ID = "G024BE91L"
138 group.Name = "secretplans"
139 group.IsGroup = true
140 group.Created = JSONTime(1360782804)
141 group.Creator = "U024BE7LH"
142 group.IsArchived = false
143 group.LastRead = "1401383885.000061"
144 group.UnreadCount = 0
145 group.UnreadCountDisplay = 0
146 assertSimpleGroup(t, group)
147 }
148
149 // IM
150 var simpleIM = `{
151 "id": "D024BFF1M",
152 "is_im": true,
153 "user": "U024BE7LH",
154 "created": 1360782804,
155 "is_user_deleted": false,
156 "is_open": true,
157 "last_read": "1401383885.000061",
158 "unread_count": 0,
159 "unread_count_display": 0
160 }`
161
162 func unmarshalIM(j string) (*IM, error) {
163 im := &IM{}
164 if err := json.Unmarshal([]byte(j), &im); err != nil {
165 return nil, err
166 }
167 return im, nil
168 }
169
170 func TestSimpleIM(t *testing.T) {
171 im, err := unmarshalIM(simpleIM)
172 assert.Nil(t, err)
173 assertSimpleIM(t, im)
174 }
175
176 func assertSimpleIM(t *testing.T, im *IM) {
177 assert.NotNil(t, im)
178 assert.Equal(t, "D024BFF1M", im.ID)
179 assert.Equal(t, true, im.IsIM)
180 assert.Equal(t, JSONTime(1360782804), im.Created)
181 assert.Equal(t, false, im.IsUserDeleted)
182 assert.Equal(t, true, im.IsOpen)
183 assert.Equal(t, "1401383885.000061", im.LastRead)
184 assert.Equal(t, 0, im.UnreadCount)
185 assert.Equal(t, 0, im.UnreadCountDisplay)
186 }
187
188 func TestCreateSimpleIM(t *testing.T) {
189 im := &IM{}
190 im.ID = "D024BFF1M"
191 im.IsIM = true
192 im.Created = JSONTime(1360782804)
193 im.IsUserDeleted = false
194 im.IsOpen = true
195 im.LastRead = "1401383885.000061"
196 im.UnreadCount = 0
197 im.UnreadCountDisplay = 0
198 assertSimpleIM(t, im)
199 }
77
88 // Group contains all the information for a group
99 type Group struct {
10 BaseGroupConversation
10 groupConversation
1111 IsGroup bool `json:"is_group"`
1212 }
1313
2121
2222 // IM contains information related to the Direct Message channel
2323 type IM struct {
24 BaseConversation
24 conversation
2525 IsIM bool `json:"is_im"`
2626 User string `json:"user"`
2727 IsUserDeleted bool `json:"is_user_deleted"`