PageRenderTime 15ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/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. -include("uce.hrl").
  20. -include_lib("eunit/include/eunit.hrl").
  21. setup_events(Domain) ->
  22. {ok, Participant} = uce_user:get_by_name(Domain, "participant.user@af83.com"),
  23. uce_event:add(Domain, #uce_event{ id=none,
  24. type="test_event_1",
  25. location="testmeeting",
  26. from=Participant#uce_user.id}),
  27. timer:sleep(10),
  28. {ok, User2} = uce_user:get_by_name(Domain, "user_2"),
  29. uce_event:add(Domain, #uce_event{ id=none,
  30. type="test_event_2",
  31. location="testmeeting",
  32. from=User2#uce_user.id}),
  33. timer:sleep(10),
  34. {ok, User3} = uce_user:get_by_name(Domain, "user_3"),
  35. uce_event:add(Domain, #uce_event{ id=none,
  36. type="test_event_3",
  37. location="testmeeting",
  38. from=User3#uce_user.id,
  39. metadata=[{"description", "test"}]}),
  40. timer:sleep(2000),
  41. {ok, [Event1]} = uce_event:list(Domain,
  42. "",
  43. [],
  44. "",
  45. ["test_event_1"],
  46. "",
  47. 0, infinity, "", 0, 1, asc),
  48. {ok, [Event2]} = uce_event:list(Domain,
  49. "",
  50. [],
  51. "",
  52. ["test_event_2"],
  53. "",
  54. 0, infinity, "", 0, 1, asc),
  55. {ok, [Event3]} = uce_event:list(Domain,
  56. "",
  57. [],
  58. "",
  59. ["test_event_3"],
  60. "",
  61. 0, infinity, "", 0, 1, asc),
  62. [Event1, Event2, Event3].
  63. search_test_() ->
  64. { setup
  65. , fun() ->
  66. [Domain, BaseUrl, Testers] = fixtures:setup(),
  67. Events = setup_events(Domain),
  68. [Domain, BaseUrl, Testers, Events]
  69. end
  70. , fun([Domain, BaseUrl, Testers, _Events]) ->
  71. fixtures:teardown([Domain, BaseUrl, Testers])
  72. end
  73. , fun([_, BaseUrl, [Root|_], Events]) ->
  74. [?_test(test_search(BaseUrl, Root)),
  75. ?_test(test_search_first(BaseUrl, Root)),
  76. ?_test(test_search_second(BaseUrl, Root)),
  77. ?_test(test_search_second_page(BaseUrl, Root)),
  78. ?_test(test_search_overflow(BaseUrl, Root)),
  79. ?_test(test_search_with_keywords(BaseUrl, Root)),
  80. ?_test(test_search_with_keywords_without_meeting(BaseUrl, Root)),
  81. ?_test(test_search_with_keywords_with_from(BaseUrl, Root)),
  82. ?_test(test_search_with_keywords_and_timestart_and_timeend(BaseUrl, Root, Events)),
  83. ?_test(test_search_with_type(BaseUrl, Root, Events)),
  84. ?_test(test_search_with_types(BaseUrl, Root, Events)),
  85. ?_test(test_search_with_type_and_timestart(BaseUrl, Root, Events)),
  86. ?_test(test_search_with_type_and_timestart_and_timeend(BaseUrl, Root, Events)),
  87. ?_test(test_search_with_type_and_timeend(BaseUrl, Root, Events))
  88. ]
  89. end}.
  90. -define(MATCH_SEARCH_RESULTS(TotalResults, StartIndex, ItemsPerPage, SearchTerms, StartPage, Entries, Results),
  91. ?assertMatch({struct, [{"result", {struct,
  92. [{"link", _},
  93. {"totalResults", TotalResults},
  94. {"startIndex", StartIndex},
  95. {"itemsPerPage", ItemsPerPage},
  96. {"Query", {struct, [{"role", "request"},
  97. {"searchTerms", SearchTerms},
  98. {"startPage", StartPage}]}},
  99. {"entries", Entries}]}}]},
  100. Results)).
  101. test_search(BaseUrl, {RootUid, RootSid}) ->
  102. Params = [{"uid", RootUid},
  103. {"sid", RootSid},
  104. {"count", "3"}],
  105. ?MATCH_SEARCH_RESULTS(3, 0, 3, "", 1, {array, [{struct, [{"type", "test_event_1"}
  106. , {"domain", _}
  107. , {"datetime", _}
  108. , {"id", _}
  109. , {"location", "testmeeting"}
  110. , {"from", _} %%"participant.user@af83.com"}
  111. , {"metadata", {struct, []}}
  112. ]},
  113. {struct, [{"type", "test_event_2"}
  114. , {"domain", _}
  115. , {"datetime", _}
  116. , {"id", _}
  117. , {"location", "testmeeting"}
  118. , {"from", _} %%"user_2"}
  119. , {"metadata", {struct, []}}
  120. ]},
  121. {struct, [{"type", "test_event_3"}
  122. , {"domain", _}
  123. , {"datetime", _}
  124. , {"id", _}
  125. , {"location", "testmeeting"}
  126. , {"from", _} %%"user_3"}
  127. , {"metadata", {struct, [{"description", "test"}]}}
  128. ]}]
  129. },
  130. tests_utils:get(BaseUrl, "/search/event", Params)).
  131. test_search_first(BaseUrl, {RootUid, RootSid}) ->
  132. Params = [{"uid", RootUid},
  133. {"sid", RootSid},
  134. {"count", "1"}],
  135. ?MATCH_SEARCH_RESULTS(3, 0, 1, "", 1, {array, [{struct, [{"type", "test_event_1"}
  136. , {"domain", _}
  137. , {"datetime", _}
  138. , {"id", _}
  139. , {"location", "testmeeting"}
  140. , {"from", _} %%"participant.user@af83.com"}
  141. , {"metadata", {struct, []}}
  142. ]}]},
  143. tests_utils:get(BaseUrl, "/search/event", Params)).
  144. test_search_second(BaseUrl, {RootUid, RootSid}) ->
  145. Params = [{"uid", RootUid},
  146. {"sid", RootSid},
  147. {"count", "1"},
  148. {"startIndex", "1"}],
  149. ?MATCH_SEARCH_RESULTS(3, 1, 1, "", 1, {array, [{struct, [{"type", "test_event_2"}
  150. , {"domain", _}
  151. , {"datetime", _}
  152. , {"id", _}
  153. , {"location", "testmeeting"}
  154. , {"from", _} %%"user_2"}
  155. , {"metadata", {struct, []}}
  156. ]}]},
  157. tests_utils:get(BaseUrl, "/search/event", Params)).
  158. test_search_second_page(BaseUrl, {RootUid, RootSid}) ->
  159. Params = [{"uid", RootUid},
  160. {"sid", RootSid},
  161. {"count", "1"},
  162. {"startPage", "2"}],
  163. ?MATCH_SEARCH_RESULTS(3, 0, 1, "", 2, {array, [{struct, [{"type", "test_event_2"}
  164. , {"domain", _}
  165. , {"datetime", _}
  166. , {"id", _}
  167. , {"location", "testmeeting"}
  168. , {"from", _} %%"user_2"}
  169. , {"metadata", {struct, []}}
  170. ]}]},
  171. tests_utils:get(BaseUrl, "/search/event", Params)).
  172. test_search_overflow(BaseUrl, {RootUid, RootSid}) ->
  173. Params = [{"uid", RootUid},
  174. {"sid", RootSid},
  175. {"count", "2"},
  176. {"startPage", "2"},
  177. {"startIndex", "1"}],
  178. ?MATCH_SEARCH_RESULTS(3, 1, 2, "", 2, {array, []},
  179. tests_utils:get(BaseUrl, "/search/event", Params)).
  180. test_search_with_keywords(BaseUrl, {RootUid, RootSid}) ->
  181. Params = [{"uid", RootUid},
  182. {"sid", RootSid},
  183. {"type", "search_event"},
  184. {"metadata[description]", "lonely event"}],
  185. {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params),
  186. timer:sleep(2000),
  187. SearchTerms = lists:concat([" type:search_event",
  188. " location:testmeeting",
  189. " lonely event"]),
  190. ParamsGet = [{"uid", RootUid},
  191. {"sid", RootSid},
  192. {"count", "1"},
  193. {"searchTerms", SearchTerms}],
  194. ?MATCH_SEARCH_RESULTS(1, 0, 1, SearchTerms, 1, {array, [{struct, [{"type", "search_event"}
  195. , {"domain", _}
  196. , {"datetime", _}
  197. , {"id", _}
  198. , {"location", "testmeeting"}
  199. , {"from", RootUid}
  200. , {"metadata", {struct, [{"description", "lonely event"}]}}
  201. ]}]}, tests_utils:get(BaseUrl, "/search/event", ParamsGet)).
  202. test_search_with_keywords_without_meeting(BaseUrl, {RootUid, RootSid}) ->
  203. Params = [{"uid", RootUid},
  204. {"sid", RootSid},
  205. {"type", "search_event"},
  206. {"metadata[description]", "lonely hungry event"}],
  207. {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params),
  208. timer:sleep(2000),
  209. SearchTerms = lists:concat([" type:search_event",
  210. " hungry"]),
  211. ParamsGet = [{"uid", RootUid},
  212. {"sid", RootSid},
  213. {"count", "1"},
  214. {"searchTerms", SearchTerms}],
  215. ?MATCH_SEARCH_RESULTS(1, 0, 1, SearchTerms, 1, {array,
  216. [{struct, [{"type", "search_event"}
  217. , {"domain", _}
  218. , {"datetime", _}
  219. , {"id", _}
  220. , {"location", "testmeeting"}
  221. , {"from", RootUid}
  222. , {"metadata", {struct, [{"description", "lonely hungry event"}]}}
  223. ]}]},
  224. tests_utils:get(BaseUrl, "/search/event", ParamsGet)).
  225. test_search_with_keywords_with_from(BaseUrl, {RootUid, RootSid}) ->
  226. Params = [{"uid", RootUid},
  227. {"sid", RootSid},
  228. {"type", "search_event"},
  229. {"metadata[description]", "lonely event"}],
  230. {struct, [{"result", _}]} = tests_utils:post(BaseUrl, "/event/testmeeting", Params),
  231. timer:sleep(2000),
  232. SearchTerms = lists:concat([" from:", RootUid,
  233. " lonely"]),
  234. ParamsGet = [{"uid", RootUid},
  235. {"sid", RootSid},
  236. {"count", "1"},
  237. {"searchTerms", SearchTerms}],
  238. ?MATCH_SEARCH_RESULTS(3, 0, 1, SearchTerms, 1, {array,
  239. [{struct, [ {"type", "search_event"}
  240. , {"domain", _}
  241. , {"datetime", _}
  242. , {"id", _}
  243. , {"location", "testmeeting"}
  244. , {"from", RootUid}
  245. , {"metadata", {struct, [{"description", "lonely event"}]}}
  246. ]}]},
  247. tests_utils:get(BaseUrl, "/search/event", ParamsGet)).
  248. test_search_with_keywords_and_timestart_and_timeend(BaseUrl,
  249. {RootUid, RootSid},
  250. [_, _, #uce_event{datetime = Datetime}]) ->
  251. SearchTerms = lists:concat([" start:", Datetime,
  252. " end:", Datetime + 1,
  253. " test"]),
  254. ParamsGetStart = [{"uid", RootUid},
  255. {"sid", RootSid},
  256. {"searchTerms", SearchTerms}],
  257. ?MATCH_SEARCH_RESULTS(1, 0, 10, SearchTerms, 1, {array,
  258. [ {struct, [{"type", "test_event_3"}
  259. , {"domain", _}
  260. , {"datetime", Datetime}
  261. , {"id", _}
  262. , {"location", "testmeeting"}
  263. , {"from", _} %%"user_3"}
  264. , {"metadata", {struct, [{"description", "test"}]}}
  265. ]}|_]
  266. },
  267. tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)),
  268. SearchTermsNothing = lists:concat([" start:", Datetime - 2,
  269. " end:", Datetime - 1,
  270. " test"]),
  271. ParamsGetNothing = [{"uid", RootUid},
  272. {"sid", RootSid},
  273. {"searchTerms", SearchTermsNothing}],
  274. ?MATCH_SEARCH_RESULTS(0, 0, 10, SearchTermsNothing, 1, {array, []},
  275. tests_utils:get(BaseUrl, "/search/event", ParamsGetNothing)).
  276. test_search_with_type(BaseUrl, {RootUid, RootSid}, [_, _, #uce_event{datetime=Datetime}]) ->
  277. SearchTerms = lists:concat([" type:test_event_3"]),
  278. ParamsGetStart = [{"uid", RootUid},
  279. {"sid", RootSid},
  280. {"searchTerms", SearchTerms}],
  281. ?MATCH_SEARCH_RESULTS(_, 0, 10, SearchTerms, 1, {array,
  282. [ {struct, [{"type", "test_event_3"}
  283. , {"domain", _}
  284. , {"datetime", Datetime}
  285. , {"id", _}
  286. , {"location", "testmeeting"}
  287. , {"from", _} %%"user_3"}
  288. , {"metadata", {struct, [{"description", "test"}]}}
  289. ]}|_]},
  290. tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)).
  291. test_search_with_types(BaseUrl, {RootUid, RootSid}, [_, _, #uce_event{datetime=Datetime}]) ->
  292. SearchTerms = lists:concat([" type:test_event_3,test_event_1"]),
  293. ParamsGetStart = [{"uid", RootUid},
  294. {"sid", RootSid},
  295. {"searchTerms", SearchTerms}],
  296. ?MATCH_SEARCH_RESULTS(_, 0, 10, SearchTerms, 1, {array,
  297. [ {struct, [{"type", "test_event_1"}
  298. , {"domain", _}
  299. , {"datetime", _}
  300. , {"id", _}
  301. , {"location", "testmeeting"}
  302. , {"from", _} %%"participant.user@af83.com"}
  303. , {"metadata", {struct, []}}
  304. ]},
  305. {struct, [{"type", "test_event_3"}
  306. , {"domain", _}
  307. , {"datetime", Datetime}
  308. , {"id", _}
  309. , {"location", "testmeeting"}
  310. , {"from", _} %%"user_3"}
  311. , {"metadata", {struct, [{"description", "test"}]}}
  312. ]}|_]
  313. },
  314. tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)).
  315. test_search_with_type_and_timestart(BaseUrl, {RootUid, RootSid}, [_, _, #uce_event{datetime=Datetime}]) ->
  316. SearchTerms = lists:concat([" start:", Datetime,
  317. " type:test_event_3",
  318. " test"]),
  319. ParamsGetStart = [{"uid", RootUid},
  320. {"sid", RootSid},
  321. {"searchTerms", SearchTerms}],
  322. ?MATCH_SEARCH_RESULTS(1, 0, 10, SearchTerms, 1, {array,
  323. [ {struct, [{"type", "test_event_3"}
  324. , {"domain", _}
  325. , {"datetime", Datetime}
  326. , {"id", _}
  327. , {"location", "testmeeting"}
  328. , {"from", _} %%"user_3"}
  329. , {"metadata", {struct, [{"description", "test"}]}}
  330. ]}|_]
  331. },
  332. tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)).
  333. test_search_with_type_and_timestart_and_timeend(BaseUrl, {RootUid, RootSid}, [_, _, #uce_event{datetime=Datetime}]) ->
  334. SearchTerms = lists:concat([" start:", Datetime,
  335. " end:", Datetime + 1,
  336. " type:test_event_3",
  337. " test"]),
  338. ParamsGetStart = [{"uid", RootUid},
  339. {"sid", RootSid},
  340. {"searchTerms", SearchTerms}],
  341. ?MATCH_SEARCH_RESULTS(1, 0, 10, SearchTerms, 1, {array,
  342. [ {struct, [{"type", "test_event_3"}
  343. , {"domain", _}
  344. , {"datetime", Datetime}
  345. , {"id", _}
  346. , {"location", "testmeeting"}
  347. , {"from", _} %%"user_3"}
  348. , {"metadata", {struct, [{"description", "test"}]}}
  349. ]}|_]
  350. },
  351. tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)).
  352. test_search_with_type_and_timeend(BaseUrl, {RootUid, RootSid}, [_,
  353. #uce_event{datetime=Datetime1},
  354. #uce_event{datetime=Datetime2}]) ->
  355. SearchTerms = lists:concat([" end:", Datetime2 - 1,
  356. " type:test_event_2"]),
  357. ParamsGetStart = [{"uid", RootUid},
  358. {"sid", RootSid},
  359. {"searchTerms", SearchTerms}],
  360. ?MATCH_SEARCH_RESULTS(1, 0, 10, SearchTerms, 1, {array,
  361. [ {struct, [{"type", "test_event_2"}
  362. , {"domain", _}
  363. , {"datetime", Datetime1}
  364. , {"id", _}
  365. , {"location", "testmeeting"}
  366. , {"from", _} %%"user_2"}
  367. , {"metadata", {struct, []}}
  368. ]}]
  369. },
  370. tests_utils:get(BaseUrl, "/search/event", ParamsGetStart)).