PageRenderTime 327ms CodeModel.GetById 100ms app.highlight 165ms RepoModel.GetById 57ms app.codeStats 0ms

/ucengine/src/tests/search_tests.erl

http://github.com/AF83/ucengine
Erlang | 424 lines | 353 code | 54 blank | 17 comment | 0 complexity | 9537f61026f3be386b331e84bdf368aa MD5 | raw file
  1%%
  2%%  U.C.Engine - Unified Collaboration Engine
  3%%  Copyright (C) 2011 af83
  4%%
  5%%  This program is free software: you can redistribute it and/or modify
  6%%  it under the terms of the GNU Affero General Public License as published by
  7%%  the Free Software Foundation, either version 3 of the License, or
  8%%  (at your option) any later version.
  9%%
 10%%  This program is distributed in the hope that it will be useful,
 11%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13%%  GNU Affero General Public License for more details.
 14%%
 15%%  You should have received a copy of the GNU Affero General Public License
 16%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17%%
 18-module(search_tests).
 19
 20-include("uce.hrl").
 21-include_lib("eunit/include/eunit.hrl").
 22
 23setup_events(Domain) ->
 24    {ok, Participant} = uce_user:get_by_name(Domain, "participant.user@af83.com"),
 25    uce_event:add(Domain, #uce_event{ id=none,
 26                              type="test_event_1",
 27                              location="testmeeting",
 28                              from=Participant#uce_user.id}),
 29    timer:sleep(10),
 30    {ok, User2} = uce_user:get_by_name(Domain, "user_2"),
 31    uce_event:add(Domain, #uce_event{ id=none,
 32                              type="test_event_2",
 33                              location="testmeeting",
 34                              from=User2#uce_user.id}),
 35    timer:sleep(10),
 36    {ok, User3} = uce_user:get_by_name(Domain, "user_3"),
 37    uce_event:add(Domain, #uce_event{ id=none,
 38                              type="test_event_3",
 39                              location="testmeeting",
 40                              from=User3#uce_user.id,
 41                              metadata=[{"description", "test"}]}),
 42
 43    timer:sleep(2000),
 44
 45    {ok, [Event1]} = uce_event:list(Domain,
 46                                    "",
 47                                    [],
 48                                    "",
 49                                    ["test_event_1"],
 50                                    "",
 51                                    0, infinity, "", 0, 1, asc),
 52
 53    {ok, [Event2]} = uce_event:list(Domain,
 54                                    "",
 55                                    [],
 56                                    "",
 57                                    ["test_event_2"],
 58                                    "",
 59                                    0, infinity, "", 0, 1, asc),
 60
 61    {ok, [Event3]} = uce_event:list(Domain,
 62                                    "",
 63                                    [],
 64                                    "",
 65                                    ["test_event_3"],
 66                                    "",
 67                                    0, infinity, "", 0, 1, asc),
 68
 69    [Event1, Event2, Event3].
 70
 71search_test_() ->
 72    { setup
 73      , fun() ->
 74                [Domain, BaseUrl, Testers] = fixtures:setup(),
 75                Events = setup_events(Domain),
 76                [Domain, BaseUrl, Testers, Events]
 77        end
 78      , fun([Domain, BaseUrl, Testers, _Events]) ->
 79                fixtures:teardown([Domain, BaseUrl, Testers])
 80        end
 81      , fun([_, BaseUrl, [Root|_], Events]) ->
 82                [?_test(test_search(BaseUrl, Root)),
 83                 ?_test(test_search_first(BaseUrl, Root)),
 84                 ?_test(test_search_second(BaseUrl, Root)),
 85                 ?_test(test_search_second_page(BaseUrl, Root)),
 86                 ?_test(test_search_overflow(BaseUrl, Root)),
 87                 ?_test(test_search_with_keywords(BaseUrl, Root)),
 88                 ?_test(test_search_with_keywords_without_meeting(BaseUrl, Root)),
 89                 ?_test(test_search_with_keywords_with_from(BaseUrl, Root)),
 90                 ?_test(test_search_with_keywords_and_timestart_and_timeend(BaseUrl, Root, Events)),
 91                 ?_test(test_search_with_type(BaseUrl, Root, Events)),
 92                 ?_test(test_search_with_types(BaseUrl, Root, Events)),
 93                 ?_test(test_search_with_type_and_timestart(BaseUrl, Root, Events)),
 94                 ?_test(test_search_with_type_and_timestart_and_timeend(BaseUrl, Root, Events)),
 95                 ?_test(test_search_with_type_and_timeend(BaseUrl, Root, Events))
 96                ]
 97        end}.
 98
 99-define(MATCH_SEARCH_RESULTS(TotalResults, StartIndex, ItemsPerPage, SearchTerms, StartPage, Entries, Results),
100        ?assertMatch({struct, [{"result", {struct,
101                                           [{"link", _},
102                                            {"totalResults", TotalResults},
103                                            {"startIndex", StartIndex},
104                                            {"itemsPerPage", ItemsPerPage},
105                                            {"Query", {struct, [{"role", "request"},
106                                                                {"searchTerms", SearchTerms},
107                                                                {"startPage", StartPage}]}},
108                                            {"entries", Entries}]}}]},
109                     Results)).
110
111test_search(BaseUrl, {RootUid, RootSid}) ->
112    Params = [{"uid", RootUid},
113              {"sid", RootSid},
114              {"count", "3"}],
115
116    ?MATCH_SEARCH_RESULTS(3, 0, 3, "", 1, {array, [{struct, [{"type", "test_event_1"}
117                                                             , {"domain", _}
118                                                             , {"datetime", _}
119                                                             , {"id", _}
120                                                             , {"location", "testmeeting"}
121                                                             , {"from", _} %%"participant.user@af83.com"}
122                                                             , {"metadata", {struct, []}}
123                                                            ]},
124                                                   {struct, [{"type", "test_event_2"}
125                                                             , {"domain", _}
126                                                             , {"datetime", _}
127                                                             , {"id", _}
128                                                             , {"location", "testmeeting"}
129                                                             , {"from", _} %%"user_2"}
130                                                             , {"metadata", {struct, []}}
131                                                            ]},
132                                                   {struct, [{"type", "test_event_3"}
133                                                             , {"domain", _}
134                                                             , {"datetime", _}
135                                                             , {"id", _}
136                                                             , {"location", "testmeeting"}
137                                                             , {"from", _} %%"user_3"}
138                                                             , {"metadata", {struct, [{"description", "test"}]}}
139                                                            ]}]
140                                          },
141                          tests_utils:get(BaseUrl, "/search/event", Params)).
142
143test_search_first(BaseUrl, {RootUid, RootSid}) ->
144    Params = [{"uid", RootUid},
145              {"sid", RootSid},
146              {"count", "1"}],
147
148    ?MATCH_SEARCH_RESULTS(3, 0, 1, "", 1, {array, [{struct, [{"type", "test_event_1"}
149                                                             , {"domain", _}
150                                                             , {"datetime", _}
151                                                             , {"id", _}
152                                                             , {"location", "testmeeting"}
153                                                             , {"from", _} %%"participant.user@af83.com"}
154                                                             , {"metadata", {struct, []}}
155                                                            ]}]},
156                          tests_utils:get(BaseUrl, "/search/event", Params)).
157
158test_search_second(BaseUrl, {RootUid, RootSid}) ->
159    Params = [{"uid", RootUid},
160              {"sid", RootSid},
161              {"count", "1"},
162              {"startIndex", "1"}],
163
164    ?MATCH_SEARCH_RESULTS(3, 1, 1, "", 1, {array, [{struct, [{"type", "test_event_2"}
165                                                             , {"domain", _}
166                                                             , {"datetime", _}
167                                                             , {"id", _}
168                                                             , {"location", "testmeeting"}
169                                                             , {"from", _} %%"user_2"}
170                                                             , {"metadata", {struct, []}}
171                                                            ]}]},
172                          tests_utils:get(BaseUrl, "/search/event", Params)).
173
174test_search_second_page(BaseUrl, {RootUid, RootSid}) ->
175    Params = [{"uid", RootUid},
176              {"sid", RootSid},
177              {"count", "1"},
178              {"startPage", "2"}],
179
180    ?MATCH_SEARCH_RESULTS(3, 0, 1, "", 2, {array, [{struct, [{"type", "test_event_2"}
181                                                             , {"domain", _}
182                                                             , {"datetime", _}
183                                                             , {"id", _}
184                                                             , {"location", "testmeeting"}
185                                                             , {"from", _} %%"user_2"}
186                                                             , {"metadata", {struct, []}}
187                                                            ]}]},
188                          tests_utils:get(BaseUrl, "/search/event", Params)).
189
190test_search_overflow(BaseUrl, {RootUid, RootSid}) ->
191    Params = [{"uid", RootUid},
192              {"sid", RootSid},
193              {"count", "2"},
194              {"startPage", "2"},
195              {"startIndex", "1"}],
196
197    ?MATCH_SEARCH_RESULTS(3, 1, 2, "", 2, {array, []},
198                          tests_utils:get(BaseUrl, "/search/event", Params)).
199
200test_search_with_keywords(BaseUrl, {RootUid, RootSid}) ->
201    Params = [{"uid", RootUid},
202              {"sid", RootSid},
203              {"type", "search_event"},
204              {"metadata[description]", "lonely event"}],
205    {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params),
206
207    timer:sleep(2000),
208
209    SearchTerms = lists:concat([" type:search_event",
210                                " location:testmeeting",
211                                " lonely event"]),
212
213    ParamsGet = [{"uid", RootUid},
214                 {"sid", RootSid},
215                 {"count", "1"},
216                 {"searchTerms", SearchTerms}],
217
218
219    ?MATCH_SEARCH_RESULTS(1, 0, 1, SearchTerms, 1, {array, [{struct, [{"type", "search_event"}
220                                                                       , {"domain", _}
221                                                                       , {"datetime", _}
222                                                                       , {"id", _}
223                                                                       , {"location", "testmeeting"}
224                                                                       , {"from", RootUid}
225                                                                       , {"metadata", {struct, [{"description", "lonely event"}]}}
226                                                                      ]}]}, tests_utils:get(BaseUrl, "/search/event", ParamsGet)).
227
228test_search_with_keywords_without_meeting(BaseUrl, {RootUid, RootSid}) ->
229    Params = [{"uid", RootUid},
230              {"sid", RootSid},
231              {"type", "search_event"},
232              {"metadata[description]", "lonely hungry event"}],
233    {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params),
234
235    timer:sleep(2000),
236
237    SearchTerms = lists:concat([" type:search_event",
238                                " hungry"]),
239
240    ParamsGet = [{"uid", RootUid},
241                 {"sid", RootSid},
242                 {"count", "1"},
243                 {"searchTerms", SearchTerms}],
244
245    ?MATCH_SEARCH_RESULTS(1, 0, 1, SearchTerms, 1, {array,
246                                                   [{struct, [{"type", "search_event"}
247                                                              , {"domain", _}
248                                                              , {"datetime", _}
249                                                              , {"id", _}
250                                                              , {"location", "testmeeting"}
251                                                              , {"from", RootUid}
252                                                              , {"metadata", {struct, [{"description", "lonely hungry event"}]}}
253                                                             ]}]},
254                         tests_utils:get(BaseUrl, "/search/event", ParamsGet)).
255
256test_search_with_keywords_with_from(BaseUrl, {RootUid, RootSid}) ->
257    Params = [{"uid", RootUid},
258              {"sid", RootSid},
259              {"type", "search_event"},
260              {"metadata[description]", "lonely event"}],
261    {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params),
262
263    timer:sleep(2000),
264
265    SearchTerms = lists:concat([" from:", RootUid,
266                                " lonely"]),
267
268    ParamsGet = [{"uid", RootUid},
269                 {"sid", RootSid},
270                 {"count", "1"},
271                 {"searchTerms", SearchTerms}],
272
273    ?MATCH_SEARCH_RESULTS(3, 0, 1, SearchTerms, 1, {array,
274                                                    [{struct, [ {"type", "search_event"}
275                                                                , {"domain", _}
276                                                                , {"datetime", _}
277                                                                , {"id", _}
278                                                                , {"location", "testmeeting"}
279                                                                , {"from", RootUid}
280                                                                , {"metadata", {struct, [{"description", "lonely event"}]}}
281                                                              ]}]},
282                          tests_utils:get(BaseUrl, "/search/event", ParamsGet)).
283
284test_search_with_keywords_and_timestart_and_timeend(BaseUrl,
285                                                    {RootUid, RootSid},
286                                                    [_, _, #uce_event{datetime = Datetime}]) ->
287    SearchTerms = lists:concat([" start:", Datetime,
288                                " end:", Datetime + 1,
289                                " test"]),
290
291    ParamsGetStart = [{"uid", RootUid},
292                      {"sid", RootSid},
293                      {"searchTerms", SearchTerms}],
294    ?MATCH_SEARCH_RESULTS(1, 0, 10, SearchTerms, 1, {array,
295                                                     [ {struct, [{"type", "test_event_3"}
296                                                                 , {"domain", _}
297                                                                 , {"datetime", Datetime}
298                                                                 , {"id", _}
299                                                                 , {"location", "testmeeting"}
300                                                                 , {"from", _} %%"user_3"}
301                                                                 , {"metadata", {struct, [{"description", "test"}]}}
302                                                                ]}|_]
303                                                    },
304                          tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)),
305
306    SearchTermsNothing = lists:concat([" start:", Datetime - 2,
307                                       " end:", Datetime - 1,
308                                       " test"]),
309
310    ParamsGetNothing = [{"uid", RootUid},
311                        {"sid", RootSid},
312                        {"searchTerms", SearchTermsNothing}],
313
314    ?MATCH_SEARCH_RESULTS(0, 0, 10, SearchTermsNothing, 1, {array, []},
315                          tests_utils:get(BaseUrl, "/search/event", ParamsGetNothing)).
316
317test_search_with_type(BaseUrl, {RootUid, RootSid}, [_, _, #uce_event{datetime=Datetime}]) ->
318     SearchTerms = lists:concat([" type:test_event_3"]),
319
320     ParamsGetStart = [{"uid", RootUid},
321                       {"sid", RootSid},
322                       {"searchTerms", SearchTerms}],
323
324     ?MATCH_SEARCH_RESULTS(_, 0, 10, SearchTerms, 1, {array,
325                                                      [ {struct, [{"type", "test_event_3"}
326                                                                  , {"domain", _}
327                                                                  , {"datetime", Datetime}
328                                                                  , {"id", _}
329                                                                  , {"location", "testmeeting"}
330                                                                  , {"from", _} %%"user_3"}
331                                                                  , {"metadata", {struct, [{"description", "test"}]}}
332                                                                 ]}|_]},
333                           tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)).
334
335test_search_with_types(BaseUrl, {RootUid, RootSid}, [_, _, #uce_event{datetime=Datetime}]) ->
336    SearchTerms = lists:concat([" type:test_event_3,test_event_1"]),
337
338    ParamsGetStart = [{"uid", RootUid},
339                      {"sid", RootSid},
340                      {"searchTerms", SearchTerms}],
341
342    ?MATCH_SEARCH_RESULTS(_, 0, 10, SearchTerms, 1, {array,
343                                                     [ {struct, [{"type", "test_event_1"}
344                                                                 , {"domain", _}
345                                                                 , {"datetime", _}
346                                                                 , {"id", _}
347                                                                 , {"location", "testmeeting"}
348                                                                 , {"from", _} %%"participant.user@af83.com"}
349                                                                 , {"metadata", {struct, []}}
350                                                                ]},
351                                                       {struct, [{"type", "test_event_3"}
352                                                                 , {"domain", _}
353                                                                 , {"datetime", Datetime}
354                                                                 , {"id", _}
355                                                                 , {"location", "testmeeting"}
356                                                                 , {"from", _} %%"user_3"}
357                                                                 , {"metadata", {struct, [{"description", "test"}]}}
358                                                                ]}|_]
359                                                    },
360                          tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)).
361
362test_search_with_type_and_timestart(BaseUrl, {RootUid, RootSid}, [_, _, #uce_event{datetime=Datetime}]) ->
363    SearchTerms = lists:concat([" start:", Datetime,
364                                " type:test_event_3",
365                                " test"]),
366
367    ParamsGetStart = [{"uid", RootUid},
368                      {"sid", RootSid},
369                      {"searchTerms", SearchTerms}],
370
371    ?MATCH_SEARCH_RESULTS(1, 0, 10, SearchTerms, 1, {array,
372                                                     [ {struct, [{"type", "test_event_3"}
373                                                                 , {"domain", _}
374                                                                 , {"datetime", Datetime}
375                                                                 , {"id", _}
376                                                                 , {"location", "testmeeting"}
377                                                                 , {"from", _} %%"user_3"}
378                                                                 , {"metadata", {struct, [{"description", "test"}]}}
379                                                                ]}|_]
380                                                    },
381                          tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)).
382
383test_search_with_type_and_timestart_and_timeend(BaseUrl, {RootUid, RootSid}, [_, _, #uce_event{datetime=Datetime}]) ->
384
385    SearchTerms = lists:concat([" start:", Datetime,
386                                " end:", Datetime + 1,
387                                " type:test_event_3",
388                                " test"]),
389    ParamsGetStart = [{"uid", RootUid},
390                      {"sid", RootSid},
391                      {"searchTerms", SearchTerms}],
392
393        ?MATCH_SEARCH_RESULTS(1, 0, 10, SearchTerms, 1, {array,
394                                                     [ {struct, [{"type", "test_event_3"}
395                                                                 , {"domain", _}
396                                                                 , {"datetime", Datetime}
397                                                                 , {"id", _}
398                                                                 , {"location", "testmeeting"}
399                                                                 , {"from", _} %%"user_3"}
400                                                                 , {"metadata", {struct, [{"description", "test"}]}}
401                                                                ]}|_]
402                                                    },
403                          tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)).
404
405test_search_with_type_and_timeend(BaseUrl, {RootUid, RootSid}, [_,
406                                                                #uce_event{datetime=Datetime1},
407                                                                #uce_event{datetime=Datetime2}]) ->
408    SearchTerms = lists:concat([" end:", Datetime2 - 1,
409                                " type:test_event_2"]),
410
411    ParamsGetStart = [{"uid", RootUid},
412                      {"sid", RootSid},
413                      {"searchTerms", SearchTerms}],
414    ?MATCH_SEARCH_RESULTS(1, 0, 10, SearchTerms, 1, {array,
415                                                     [ {struct, [{"type", "test_event_2"}
416                                                                 , {"domain", _}
417                                                                 , {"datetime", Datetime1}
418                                                                 , {"id", _}
419                                                                 , {"location", "testmeeting"}
420                                                                 , {"from", _} %%"user_2"}
421                                                                 , {"metadata", {struct, []}}
422                                                                ]}]
423                                                    },
424                          tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)).