@@ -5,13 +5,13 @@ defmodule CodeCorps.MessagesTest do
5
5
6
6
import Ecto.Query , only: [ where: 2 ]
7
7
8
- alias CodeCorps . { Message , Messages }
8
+ alias CodeCorps . { Conversation , Message , Messages }
9
9
10
- describe "list" do
11
- defp get_and_sort_ids ( records ) do
12
- records |> Enum . map ( & Map . get ( & 1 , :id ) ) |> Enum . sort
13
- end
10
+ defp get_and_sort_ids ( records ) do
11
+ records |> Enum . map ( & Map . get ( & 1 , :id ) ) |> Enum . sort
12
+ end
14
13
14
+ describe "list" do
15
15
test "returns all records by default" do
16
16
insert_list ( 3 , :message )
17
17
assert Message |> Messages . list ( % { } ) |> Enum . count == 3
@@ -121,4 +121,165 @@ defmodule CodeCorps.MessagesTest do
121
121
refute message_p2_a2 . id in result_ids
122
122
end
123
123
end
124
+
125
+ describe "list_conversations/2" do
126
+ test "returns all records by default" do
127
+ insert_list ( 3 , :conversation )
128
+ assert Conversation |> Messages . list_conversations ( % { } ) |> Enum . count == 3
129
+ end
130
+
131
+ test "can filter by project" do
132
+ [ % { project: project_1 } = message_1 , % { project: project_2 } = message_2 ] =
133
+ insert_pair ( :message )
134
+
135
+ conversation_1 = insert ( :conversation , message: message_1 )
136
+ conversation_2 = insert ( :conversation , message: message_2 )
137
+
138
+ result_ids =
139
+ Conversation
140
+ |> Messages . list_conversations ( % { "project_id" => project_1 . id } )
141
+ |> get_and_sort_ids ( )
142
+
143
+ assert result_ids |> Enum . count == 1
144
+ assert conversation_1 . id in result_ids
145
+ refute conversation_2 . id in result_ids
146
+
147
+ result_ids =
148
+ Conversation
149
+ |> Messages . list_conversations ( % { "project_id" => project_2 . id } )
150
+ |> get_and_sort_ids ( )
151
+
152
+ assert result_ids |> Enum . count == 1
153
+ refute conversation_1 . id in result_ids
154
+ assert conversation_2 . id in result_ids
155
+ end
156
+
157
+ test "can filter by status" do
158
+ message_started_by_admin = insert ( :message , initiated_by: "admin" )
159
+ message_started_by_user = insert ( :message , initiated_by: "user" )
160
+
161
+ conversation_started_by_admin_without_reply =
162
+ insert ( :conversation , message: message_started_by_admin )
163
+ conversation_started_by_admin_with_reply =
164
+ insert ( :conversation , message: message_started_by_admin )
165
+ insert (
166
+ :conversation_part ,
167
+ conversation: conversation_started_by_admin_with_reply
168
+ )
169
+
170
+ conversation_started_by_user_without_reply =
171
+ insert ( :conversation , message: message_started_by_user )
172
+ conversation_started_by_user_with_reply =
173
+ insert ( :conversation , message: message_started_by_user )
174
+ insert (
175
+ :conversation_part ,
176
+ conversation: conversation_started_by_user_with_reply
177
+ )
178
+
179
+ result_ids =
180
+ Conversation
181
+ |> Messages . list_conversations ( % { "status" => "active" } )
182
+ |> get_and_sort_ids ( )
183
+
184
+ refute conversation_started_by_admin_without_reply . id in result_ids
185
+ assert conversation_started_by_admin_with_reply . id in result_ids
186
+ assert conversation_started_by_user_without_reply . id in result_ids
187
+ assert conversation_started_by_user_with_reply . id in result_ids
188
+
189
+ result_ids =
190
+ Conversation
191
+ |> Messages . list_conversations ( % { "status" => "any" } )
192
+ |> get_and_sort_ids ( )
193
+
194
+ assert conversation_started_by_admin_without_reply . id in result_ids
195
+ assert conversation_started_by_admin_with_reply . id in result_ids
196
+ assert conversation_started_by_user_without_reply . id in result_ids
197
+ assert conversation_started_by_user_with_reply . id in result_ids
198
+ end
199
+
200
+ test "builds upon the provided scope" do
201
+ [ project_1 , project_2 ] = insert_pair ( :project )
202
+ [ user_1 , user_2 ] = insert_pair ( :user )
203
+
204
+ message_p1 = insert ( :message , project: project_1 )
205
+ message_p2 = insert ( :message , project: project_2 )
206
+
207
+ conversation_u1_p1 =
208
+ insert ( :conversation , user: user_1 , message: message_p1 )
209
+ conversation_u1_p2 =
210
+ insert ( :conversation , user: user_1 , message: message_p2 )
211
+ conversation_u2_p1 =
212
+ insert ( :conversation , user: user_2 , message: message_p1 )
213
+ conversation_u2_p2 =
214
+ insert ( :conversation , user: user_2 , message: message_p2 )
215
+
216
+ params = % { "project_id" => project_1 . id }
217
+ result_ids =
218
+ Conversation
219
+ |> where ( user_id: ^ user_1 . id )
220
+ |> Messages . list_conversations ( params )
221
+ |> get_and_sort_ids ( )
222
+
223
+ assert conversation_u1_p1 . id in result_ids
224
+ refute conversation_u1_p2 . id in result_ids
225
+ refute conversation_u2_p1 . id in result_ids
226
+ refute conversation_u2_p2 . id in result_ids
227
+ end
228
+
229
+ test "supports multiple filters at once" do
230
+ ## we create two messages started by admin, each on a different project
231
+ % { project: project_1 } = message_1_started_by_admin =
232
+ insert ( :message , initiated_by: "admin" )
233
+ % { project: project_2 } = message_2_started_by_admin =
234
+ insert ( :message , initiated_by: "admin" )
235
+
236
+ # we create one conversation without a reply, to test the "status" filter
237
+
238
+ conversation_started_by_admin_without_reply =
239
+ insert ( :conversation , message: message_1_started_by_admin )
240
+
241
+ # we create two conversations with replies, on on each message
242
+ # since the messages are on different projects, this allows us to
243
+ # test the project filter
244
+
245
+ conversation_started_by_admin_with_reply =
246
+ insert ( :conversation , message: message_1_started_by_admin )
247
+ insert (
248
+ :conversation_part ,
249
+ conversation: conversation_started_by_admin_with_reply
250
+ )
251
+ other_conversation_started_by_admin_with_reply =
252
+ insert ( :conversation , message: message_2_started_by_admin )
253
+ insert (
254
+ :conversation_part ,
255
+ conversation: other_conversation_started_by_admin_with_reply
256
+ )
257
+
258
+ params = % { "status" => "active" , "project_id" => project_1 . id }
259
+ result_ids =
260
+ Conversation
261
+ |> Messages . list_conversations ( params )
262
+ |> get_and_sort_ids ( )
263
+
264
+ # this means the status filter worked, because the first conv. belongs to
265
+ # the message with the correct project
266
+ refute conversation_started_by_admin_without_reply . id in result_ids
267
+ # this conversation is active and belongs to the message with the
268
+ # correct project
269
+ assert conversation_started_by_admin_with_reply . id in result_ids
270
+ # this conversation is active, but belongs to a message with a different
271
+ # project
272
+ refute other_conversation_started_by_admin_with_reply . id in result_ids
273
+
274
+ params = % { "status" => "active" , "project_id" => project_2 . id }
275
+ result_ids =
276
+ Conversation
277
+ |> Messages . list_conversations ( params )
278
+ |> get_and_sort_ids ( )
279
+
280
+ refute conversation_started_by_admin_without_reply . id in result_ids
281
+ refute conversation_started_by_admin_with_reply . id in result_ids
282
+ assert other_conversation_started_by_admin_with_reply . id in result_ids
283
+ end
284
+ end
124
285
end
0 commit comments