PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/contenter_gui/spec/models/content_query_spec.rb

https://github.com/kstephens/contenter
Ruby | 568 lines | 448 code | 52 blank | 68 comment | 27 complexity | d0badad9db8b92883e644b92e120bc80 MD5 | raw file
Possible License(s): JSON
  1. # -*- ruby -*-
  2. #require 'test_helper'
  3. require 'spec/spec_helper'
  4. describe "Content::Query" do
  5. COMMON_TABLES = "content_keys, languages, countries, brands, applications, mime_types, content_statuses, users AS updater_users, users AS creator_users".freeze
  6. JOINS = <<"END"
  7. (content_keys.id = contents.content_key_id)
  8. AND (languages.id = contents.language_id)
  9. AND (countries.id = contents.country_id)
  10. AND (brands.id = contents.brand_id)
  11. AND (applications.id = contents.application_id)
  12. AND (mime_types.id = contents.mime_type_id)
  13. AND (content_statuses.id = contents.content_status_id)
  14. AND (updater_users.id = contents.updater_user_id)
  15. AND (creator_users.id = contents.creator_user_id)
  16. AND (content_types.id = content_keys.content_type_id)
  17. END
  18. ORDER_BY = <<"END".chomp.freeze
  19. ORDER BY
  20. (SELECT content_keys.code FROM content_keys WHERE content_keys.id = contents.content_key_id),
  21. (SELECT languages.code FROM languages WHERE languages.id = contents.language_id),
  22. (SELECT countries.code FROM countries WHERE countries.id = contents.country_id),
  23. (SELECT brands.code FROM brands WHERE brands.id = contents.brand_id),
  24. (SELECT applications.code FROM applications WHERE applications.id = contents.application_id),
  25. (SELECT mime_types.code FROM mime_types WHERE mime_types.id = contents.mime_type_id)
  26. END
  27. ORDER_BY_CV = ORDER_BY.gsub(/\.id = contents\./, '.id = cv.').freeze
  28. DATA = "(CASE WHEN (content_types.mime_type_id NOT IN (SELECT id FROM mime_types WHERE code LIKE 'text/%')) THEN '' ELSE convert_from(contents.data, 'UTF8') END)".freeze
  29. it 'should create SQL to query all Content' do
  30. q = Content::Query.new()
  31. # $stderr.puts(q.sql + "\n")
  32. (q.sql + "\n").should == <<"END"
  33. SELECT contents.*
  34. FROM contents AS contents, #{COMMON_TABLES}, content_types
  35. WHERE
  36. #{JOINS}
  37. #{ORDER_BY}
  38. END
  39. end
  40. it 'should create SQL to query by ContentType' do
  41. q = Content::Query.new(:params => { :content_type => 'phrase' })
  42. (q.sql + "\n").should == <<"END"
  43. SELECT contents.*
  44. FROM contents AS contents, #{COMMON_TABLES}, content_types
  45. WHERE
  46. #{JOINS}
  47. AND (
  48. (content_types.code = E'phrase')
  49. )
  50. #{ORDER_BY}
  51. END
  52. end
  53. it 'should create SQL to query by ContentKey' do
  54. q = Content::Query.new(:params => { :content_key => 'content_key' })
  55. (q.sql + "\n").should == <<"END"
  56. SELECT contents.*
  57. FROM contents AS contents, #{COMMON_TABLES}, content_types
  58. WHERE
  59. #{JOINS}
  60. AND (
  61. (content_keys.code = E'content_key')
  62. )
  63. #{ORDER_BY}
  64. END
  65. end
  66. it 'should create SQL to query by content_id ' do
  67. q = Content::Query.new(:params => { :content_id => '123' })
  68. (q.sql + "\n").should == <<"END"
  69. SELECT contents.*
  70. FROM contents AS contents, #{COMMON_TABLES}, content_types
  71. WHERE
  72. #{JOINS}
  73. AND (
  74. (contents.id = 123)
  75. )
  76. #{ORDER_BY}
  77. END
  78. end
  79. it 'should create SQL to query by UUID ' do
  80. q = Content::Query.new(:params => { :uuid => '123' })
  81. (q.sql + "\n").should == <<"END"
  82. SELECT contents.*
  83. FROM contents AS contents, #{COMMON_TABLES}, content_types
  84. WHERE
  85. #{JOINS}
  86. AND (
  87. (contents.uuid = E'123')
  88. )
  89. #{ORDER_BY}
  90. END
  91. end
  92. it 'should create SQL to query by md5sum ' do
  93. q = Content::Query.new(:params => { :md5sum => '123' })
  94. (q.sql + "\n").should == <<"END"
  95. SELECT contents.*
  96. FROM contents AS contents, #{COMMON_TABLES}, content_types
  97. WHERE
  98. #{JOINS}
  99. AND (
  100. (contents.md5sum = E'123')
  101. )
  102. #{ORDER_BY}
  103. END
  104. end
  105. [ :updater, :creator ].each do | field |
  106. it "should query by #{field}" do
  107. q = Content::Query.new(:params => { field => 'joeuser' })
  108. # $stderr.puts (q.sql + "\n")
  109. (q.sql + "\n").should == <<"END"
  110. SELECT contents.*
  111. FROM contents AS contents, #{COMMON_TABLES}, content_types
  112. WHERE
  113. #{JOINS}
  114. AND (
  115. (#{field}_users.login = E'joeuser')
  116. )
  117. #{ORDER_BY}
  118. END
  119. end
  120. end # each
  121. it 'should search for data using LIKE' do
  122. q = Content::Query.new(:params => { :data => '%123%' }, :like => true)
  123. (q.sql + "\n").should == <<"END"
  124. SELECT contents.*
  125. FROM contents AS contents, #{COMMON_TABLES}, content_types
  126. WHERE
  127. #{JOINS}
  128. AND (
  129. (#{DATA} LIKE E'%123%')
  130. )
  131. #{ORDER_BY}
  132. END
  133. end
  134. it 'should search for data using case-insensitive LIKE' do
  135. q = Content::Query.new(:params => { :data => '%123%' }, :ilike => true)
  136. (q.sql + "\n").should == <<"END"
  137. SELECT contents.*
  138. FROM contents AS contents, #{COMMON_TABLES}, content_types
  139. WHERE
  140. #{JOINS}
  141. AND (
  142. (#{DATA} ILIKE E'%123%')
  143. )
  144. #{ORDER_BY}
  145. END
  146. end
  147. it 'should search using a version_list_id' do
  148. q = Content::Query.
  149. new(:params => { :version_list_id => 123 }
  150. )
  151. (q.sql + "\n").should == <<"END"
  152. SELECT contents.*
  153. FROM content_versions AS contents, #{COMMON_TABLES}, version_list_contents, content_types
  154. WHERE
  155. #{JOINS}
  156. AND (version_list_contents.version_list_id = 123)
  157. AND (version_list_contents.content_version_id = contents.id)
  158. #{ORDER_BY}
  159. END
  160. end
  161. [ :content_key_id, :content_type_id, :country_id, :content_status_id ].each do | column |
  162. it "should search using a #{column}." do
  163. q = Content::Query.
  164. new(:params => { column => 123 }
  165. )
  166. # $stderr.puts ("\n" + q.sql + "\n")
  167. (q.sql + "\n").should == <<"END"
  168. SELECT contents.*
  169. FROM contents AS contents, #{COMMON_TABLES}, content_types
  170. WHERE
  171. #{JOINS}
  172. AND (
  173. (#{column.to_s.sub(/_id\Z/, '').pluralize}.id = 123)
  174. )
  175. #{ORDER_BY}
  176. END
  177. end
  178. end
  179. it "should search using tasks." do
  180. q = Content::Query.
  181. new(:params => { :tasks => 123 }
  182. )
  183. # $stderr.puts ("\n" + q.sql + "\n")
  184. (q.sql + "\n").should =~ /\(contents.tasks LIKE E'% 123 %'\)/
  185. end
  186. it 'should search by version_list_id' do
  187. q = Content::Query.
  188. new(
  189. :params => { :version_list_id => 123 }
  190. )
  191. (q.sql + "\n").should == <<"END"
  192. SELECT contents.*
  193. FROM content_versions AS contents, #{COMMON_TABLES}, version_list_contents, content_types
  194. WHERE
  195. #{JOINS}
  196. AND (version_list_contents.version_list_id = 123)
  197. AND (version_list_contents.content_version_id = contents.id)
  198. #{ORDER_BY}
  199. END
  200. end
  201. it 'should search by version_list_name' do
  202. q = Content::Query.
  203. new(
  204. :params => { :version_list_name => 'production' }
  205. )
  206. (q.sql + "\n").should == <<"END"
  207. SELECT contents.*
  208. FROM content_versions AS contents, #{COMMON_TABLES}, version_list_names, version_lists, version_list_contents, content_types
  209. WHERE
  210. #{JOINS}
  211. AND (version_list_names.name = E'production')
  212. AND (version_list_contents.version_list_id = version_list_names.version_list_id)
  213. AND (version_list_contents.content_version_id = contents.id)
  214. #{ORDER_BY}
  215. END
  216. end
  217. it 'should search using a simple user_query' do
  218. q = Content::Query.
  219. new(:user_query => ' %foo_bar% '
  220. )
  221. (q.sql + "\n").should == <<"END"
  222. SELECT contents.*
  223. FROM contents AS contents, #{COMMON_TABLES}, content_types
  224. WHERE
  225. #{JOINS}
  226. AND (
  227. (#{DATA} ILIKE E'%foo_bar%')
  228. OR (content_keys.code ILIKE E'%foo_bar%')
  229. )
  230. #{ORDER_BY}
  231. END
  232. end
  233. [ :content_key_id, :content_type_id, :language_id ].each do | column |
  234. it "should search using a simple user_query with #{column}:123" do
  235. q = Content::Query.
  236. new(:user_query => " #{column}:321 %foo_bar% #{column}:123"
  237. )
  238. # $stderr.puts("\n" + q.sql + "\n")
  239. (q.sql + "\n").should == <<"END"
  240. SELECT contents.*
  241. FROM contents AS contents, #{COMMON_TABLES}, content_types
  242. WHERE
  243. #{JOINS}
  244. AND (
  245. (#{DATA} ILIKE E'%foo_bar%')
  246. OR (content_keys.code ILIKE E'%foo_bar%')
  247. )
  248. AND (
  249. (#{column.to_s.sub(/_id\Z/, '').pluralize}.id = 123)
  250. )
  251. #{ORDER_BY}
  252. END
  253. end
  254. end
  255. it 'should search using a user_query with additional selection AND clauses' do
  256. q = Content::Query.
  257. new(:user_query => ' content_type:phrase language:en %foo_bar% ',
  258. :params => { :id => 123 }
  259. )
  260. (q.sql + "\n").should == <<"END"
  261. SELECT contents.*
  262. FROM contents AS contents, #{COMMON_TABLES}, content_types
  263. WHERE
  264. #{JOINS}
  265. AND (
  266. (contents.id = 123)
  267. )
  268. AND (
  269. (#{DATA} ILIKE E'%foo_bar%')
  270. OR (content_keys.code ILIKE E'%foo_bar%')
  271. )
  272. AND (
  273. (content_types.code = E'phrase')
  274. AND (languages.code = E'en')
  275. )
  276. #{ORDER_BY}
  277. END
  278. end
  279. it 'should search using a user_query with content_key' do
  280. q = Content::Query.
  281. new({
  282. :user_query => ' content_key:key '
  283. })
  284. # $stderr.puts("\n" + q.sql)
  285. (q.sql + "\n").should == <<"END"
  286. SELECT contents.*
  287. FROM contents AS contents, #{COMMON_TABLES}, content_types
  288. WHERE
  289. #{JOINS}
  290. AND (
  291. (content_keys.code = E'key')
  292. )
  293. #{ORDER_BY}
  294. END
  295. end
  296. it 'should count using a user_query with content_key' do
  297. q = Content::Query.
  298. new({
  299. :user_query => ' content_key:key ',
  300. })
  301. # $stderr.puts("\n" + q.sql(:count => true))
  302. (q.sql(:count => true) + "\n").should == <<"END"
  303. SELECT COUNT(contents.*)
  304. FROM contents AS contents, #{COMMON_TABLES}, content_types
  305. WHERE
  306. #{JOINS}
  307. AND (
  308. (content_keys.code = E'key')
  309. )
  310. END
  311. end
  312. it 'should search all versions using a user_query with content_key' do
  313. q = Content::Query.
  314. new({
  315. :user_query => ' content_key:key ',
  316. :versions => true,
  317. })
  318. # $stderr.puts("\n" + q.sql)
  319. (q.sql + "\n").should == <<"END"
  320. SELECT cv.* FROM content_versions AS cv
  321. WHERE cv.id IN (
  322. SELECT contents.id
  323. FROM content_versions AS contents, #{COMMON_TABLES}, content_types
  324. WHERE
  325. #{JOINS}
  326. AND (
  327. (content_keys.code = E'key')
  328. )
  329. AND contents.content_id = cv.content_id
  330. )
  331. #{ORDER_BY_CV},
  332. version DESC
  333. END
  334. end
  335. it 'should search using a user_query and version_list_id' do
  336. q = Content::Query.
  337. new(:user_query => ' content_type:email country:US %foo_bar% ',
  338. :params => { :version_list_id => 123 }
  339. )
  340. (q.sql + "\n").should == <<"END"
  341. SELECT contents.*
  342. FROM content_versions AS contents, #{COMMON_TABLES}, version_list_contents, content_types
  343. WHERE
  344. #{JOINS}
  345. AND (version_list_contents.version_list_id = 123)
  346. AND (version_list_contents.content_version_id = contents.id)
  347. AND (
  348. (#{DATA} ILIKE E'%foo_bar%')
  349. OR (content_keys.code ILIKE E'%foo_bar%')
  350. )
  351. AND (
  352. (content_types.code = E'email')
  353. AND (countries.code = E'US')
  354. )
  355. #{ORDER_BY}
  356. END
  357. end
  358. it 'should search using a user_query content_status and latest version' do
  359. q = Content::Query.
  360. new(:user_query => ' content_status:initial ',
  361. :latest => true
  362. )
  363. # $stderr.puts("\n" + q.sql)
  364. (q.sql + "\n").should == <<"END"
  365. SELECT cv.* FROM content_versions AS cv
  366. WHERE cv.id IN (
  367. SELECT MAX(contents.id)
  368. FROM content_versions AS contents, #{COMMON_TABLES}, content_types
  369. WHERE
  370. #{JOINS}
  371. AND (
  372. (content_statuses.code = E'initial')
  373. )
  374. AND contents.content_id = cv.content_id
  375. )
  376. #{ORDER_BY_CV}
  377. END
  378. end
  379. it 'should count using a user_query content_status OR expression and latest version' do
  380. q = Content::Query.
  381. new(:user_query => ' latest:true content_status:approved|released '
  382. )
  383. # $stderr.puts("\n" + q.sql(:count => true))
  384. (q.sql(:count => true) + "\n").should == <<"END"
  385. SELECT COUNT(cv.*) FROM content_versions AS cv
  386. WHERE cv.id IN (
  387. SELECT MAX(contents.id)
  388. FROM content_versions AS contents, #{COMMON_TABLES}, content_types
  389. WHERE
  390. #{JOINS}
  391. AND (
  392. ((content_statuses.code = E'approved') OR (content_statuses.code = E'released'))
  393. )
  394. AND contents.content_id = cv.content_id
  395. )
  396. END
  397. end
  398. it 'should count using a user_query content_status AND expression and latest version' do
  399. q = Content::Query.
  400. new(:user_query => ' latest:true content_status:!approved&released '
  401. )
  402. # $stderr.puts("\n" + q.sql(:count => true))
  403. (q.sql(:count => true) + "\n").should == <<"END"
  404. SELECT COUNT(cv.*) FROM content_versions AS cv
  405. WHERE cv.id IN (
  406. SELECT MAX(contents.id)
  407. FROM content_versions AS contents, #{COMMON_TABLES}, content_types
  408. WHERE
  409. #{JOINS}
  410. AND (
  411. (((content_statuses.code IS NULL) OR (content_statuses.code <> E'approved')) AND (content_statuses.code = E'released'))
  412. )
  413. AND contents.content_id = cv.content_id
  414. )
  415. END
  416. end
  417. it 'should query using a content_status AND expression and latest version using :exact match and not parse AND and OR expressions' do
  418. q = Content::Query.
  419. new({
  420. :latest => true,
  421. :exact => true,
  422. :params => {
  423. :content_key => "something&somethingelse",
  424. :content_status => "!approved&released",
  425. },
  426. }
  427. )
  428. # $stderr.puts("\n" + q.sql())
  429. (q.sql() + "\n").should == <<"END"
  430. SELECT cv.* FROM content_versions AS cv
  431. WHERE cv.id IN (
  432. SELECT MAX(contents.id)
  433. FROM content_versions AS contents, #{COMMON_TABLES}, content_types
  434. WHERE
  435. #{JOINS}
  436. AND (
  437. (content_statuses.code = E'!approved&released')
  438. AND (content_keys.code = E'something&somethingelse')
  439. )
  440. AND contents.content_id = cv.content_id
  441. )
  442. #{ORDER_BY_CV}
  443. END
  444. end
  445. [ 123, nil ].each do | b_id |
  446. [ :like, :ilike ].each do | opt |
  447. it "should search where brand_id=>#{b_id.inspect} and #{opt.inspect}" do
  448. q = Content::Query.
  449. new({
  450. :params => {
  451. :brand_id => b_id,
  452. },
  453. opt => true,
  454. })
  455. # $stderr.puts("\n" + q.sql())
  456. (q.sql() + "\n").should == <<"END"
  457. SELECT contents.*
  458. FROM contents AS contents, #{COMMON_TABLES}, content_types
  459. WHERE
  460. #{JOINS}
  461. AND (
  462. (brands.id #{opt.to_s.upcase} E'#{b_id}')
  463. )
  464. #{ORDER_BY}
  465. END
  466. end # it
  467. end # each
  468. end # each
  469. it 'should search all versions with a :conditions option' do
  470. q = Content::Query.
  471. new({
  472. :user_query => ' content_key:key ',
  473. :versions => true,
  474. :conditions => ' brands.id = 123 ',
  475. })
  476. # $stderr.puts("\n" + q.sql)
  477. (q.sql + "\n").should == <<"END"
  478. SELECT cv.* FROM content_versions AS cv
  479. WHERE cv.id IN (
  480. SELECT contents.id
  481. FROM content_versions AS contents, #{COMMON_TABLES}, content_types
  482. WHERE
  483. #{JOINS}
  484. AND ( brands.id = 123 )
  485. AND (
  486. (content_keys.code = E'key')
  487. )
  488. AND contents.content_id = cv.content_id
  489. )
  490. #{ORDER_BY_CV},
  491. version DESC
  492. END
  493. end
  494. it 'should by filename: and versions:t' do
  495. q = Content::Query.
  496. new({
  497. :user_query => ' filename:abc.png versions:t ',
  498. })
  499. # $stderr.puts("\n" + q.sql)
  500. (q.sql + "\n").should == <<"END"
  501. SELECT cv.* FROM content_versions AS cv
  502. WHERE cv.id IN (
  503. SELECT contents.id
  504. FROM content_versions AS contents, #{COMMON_TABLES}, content_types
  505. WHERE
  506. #{JOINS}
  507. AND (
  508. (contents.filename = E'abc.png')
  509. )
  510. AND contents.content_id = cv.content_id
  511. )
  512. #{ORDER_BY_CV},
  513. version DESC
  514. END
  515. end
  516. end # describe