PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/activerecord/test/finder_test.rb

http://monkeycharger.googlecode.com/
Ruby | 567 lines | 495 code | 67 blank | 5 comment | 1 complexity | d9b92d5220760374c55dabf1af5921d7 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'fixtures/comment'
  3. require 'fixtures/company'
  4. require 'fixtures/topic'
  5. require 'fixtures/reply'
  6. require 'fixtures/entrant'
  7. require 'fixtures/developer'
  8. require 'fixtures/post'
  9. class FinderTest < Test::Unit::TestCase
  10. fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors
  11. def test_find
  12. assert_equal(topics(:first).title, Topic.find(1).title)
  13. end
  14. # find should handle strings that come from URLs
  15. # (example: Category.find(params[:id]))
  16. def test_find_with_string
  17. assert_equal(Topic.find(1).title,Topic.find("1").title)
  18. end
  19. def test_exists
  20. assert Topic.exists?(1)
  21. assert Topic.exists?("1")
  22. assert Topic.exists?(:author_name => "David")
  23. assert Topic.exists?(:author_name => "Mary", :approved => true)
  24. assert Topic.exists?(["parent_id = ?", 1])
  25. assert !Topic.exists?(45)
  26. assert !Topic.exists?("foo")
  27. assert_raise(NoMethodError) { Topic.exists?([1,2]) }
  28. end
  29. def test_find_by_array_of_one_id
  30. assert_kind_of(Array, Topic.find([ 1 ]))
  31. assert_equal(1, Topic.find([ 1 ]).length)
  32. end
  33. def test_find_by_ids
  34. assert_equal 2, Topic.find(1, 2).size
  35. assert_equal topics(:second).title, Topic.find([2]).first.title
  36. end
  37. def test_find_by_ids_with_limit_and_offset
  38. assert_equal 2, Entrant.find([1,3,2], :limit => 2).size
  39. assert_equal 1, Entrant.find([1,3,2], :limit => 3, :offset => 2).size
  40. # Also test an edge case: If you have 11 results, and you set a
  41. # limit of 3 and offset of 9, then you should find that there
  42. # will be only 2 results, regardless of the limit.
  43. devs = Developer.find :all
  44. last_devs = Developer.find devs.map(&:id), :limit => 3, :offset => 9
  45. assert_equal 2, last_devs.size
  46. end
  47. def test_find_an_empty_array
  48. assert_equal [], Topic.find([])
  49. end
  50. def test_find_by_ids_missing_one
  51. assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, 2, 45) }
  52. end
  53. def test_find_all_with_limit
  54. entrants = Entrant.find(:all, :order => "id ASC", :limit => 2)
  55. assert_equal(2, entrants.size)
  56. assert_equal(entrants(:first).name, entrants.first.name)
  57. end
  58. def test_find_all_with_prepared_limit_and_offset
  59. entrants = Entrant.find(:all, :order => "id ASC", :limit => 2, :offset => 1)
  60. assert_equal(2, entrants.size)
  61. assert_equal(entrants(:second).name, entrants.first.name)
  62. entrants = Entrant.find(:all, :order => "id ASC", :limit => 2, :offset => 2)
  63. assert_equal(1, entrants.size)
  64. assert_equal(entrants(:third).name, entrants.first.name)
  65. end
  66. def test_find_all_with_limit_and_offset_and_multiple_orderings
  67. developers = Developer.find(:all, :order => "salary ASC, id DESC", :limit => 3, :offset => 1)
  68. assert_equal ["David", "fixture_10", "fixture_9"], developers.collect {|d| d.name}
  69. end
  70. def test_find_with_limit_and_condition
  71. developers = Developer.find(:all, :order => "id DESC", :conditions => "salary = 100000", :limit => 3, :offset =>7)
  72. assert_equal(1, developers.size)
  73. assert_equal("fixture_3", developers.first.name)
  74. end
  75. def test_find_with_entire_select_statement
  76. topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'"
  77. assert_equal(1, topics.size)
  78. assert_equal(topics(:second).title, topics.first.title)
  79. end
  80. def test_find_with_prepared_select_statement
  81. topics = Topic.find_by_sql ["SELECT * FROM topics WHERE author_name = ?", "Mary"]
  82. assert_equal(1, topics.size)
  83. assert_equal(topics(:second).title, topics.first.title)
  84. end
  85. def test_find_by_sql_with_sti_on_joined_table
  86. accounts = Account.find_by_sql("SELECT * FROM accounts INNER JOIN companies ON companies.id = accounts.firm_id")
  87. assert_equal [Account], accounts.collect(&:class).uniq
  88. end
  89. def test_find_first
  90. first = Topic.find(:first, :conditions => "title = 'The First Topic'")
  91. assert_equal(topics(:first).title, first.title)
  92. end
  93. def test_find_first_failing
  94. first = Topic.find(:first, :conditions => "title = 'The First Topic!'")
  95. assert_nil(first)
  96. end
  97. def test_unexisting_record_exception_handling
  98. assert_raises(ActiveRecord::RecordNotFound) {
  99. Topic.find(1).parent
  100. }
  101. Topic.find(2).topic
  102. end
  103. def test_find_only_some_columns
  104. topic = Topic.find(1, :select => "author_name")
  105. assert_raises(NoMethodError) { topic.title }
  106. assert_equal "David", topic.author_name
  107. assert !topic.attribute_present?("title")
  108. assert !topic.respond_to?("title")
  109. assert topic.attribute_present?("author_name")
  110. assert topic.respond_to?("author_name")
  111. end
  112. def test_find_on_blank_conditions
  113. [nil, " ", [], {}].each do |blank|
  114. assert_nothing_raised { Topic.find(:first, :conditions => blank) }
  115. end
  116. end
  117. def test_find_on_blank_bind_conditions
  118. [ [""], ["",{}] ].each do |blank|
  119. assert_nothing_raised { Topic.find(:first, :conditions => blank) }
  120. end
  121. end
  122. def test_find_on_array_conditions
  123. assert Topic.find(1, :conditions => ["approved = ?", false])
  124. assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => ["approved = ?", true]) }
  125. end
  126. def test_find_on_hash_conditions
  127. assert Topic.find(1, :conditions => { :approved => false })
  128. assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :approved => true }) }
  129. end
  130. def test_find_on_association_proxy_conditions
  131. assert_equal [1, 2, 3, 5, 6, 7, 8, 9, 10], Comment.find_all_by_post_id(authors(:david).posts).map(&:id).sort
  132. end
  133. def test_find_on_hash_conditions_with_range
  134. assert_equal [1,2], Topic.find(:all, :conditions => { :id => 1..2 }).map(&:id).sort
  135. assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :id => 2..3 }) }
  136. end
  137. def test_find_on_hash_conditions_with_multiple_ranges
  138. assert_equal [1,2,3], Comment.find(:all, :conditions => { :id => 1..3, :post_id => 1..2 }).map(&:id).sort
  139. assert_equal [1], Comment.find(:all, :conditions => { :id => 1..1, :post_id => 1..10 }).map(&:id).sort
  140. end
  141. def test_find_on_multiple_hash_conditions
  142. assert Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => false })
  143. assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
  144. assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "HHC", :replies_count => 1, :approved => false }) }
  145. assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { :author_name => "David", :title => "The First Topic", :replies_count => 1, :approved => true }) }
  146. end
  147. def test_condition_interpolation
  148. assert_kind_of Firm, Company.find(:first, :conditions => ["name = '%s'", "37signals"])
  149. assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!"])
  150. assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!' OR 1=1"])
  151. assert_kind_of Time, Topic.find(:first, :conditions => ["id = %d", 1]).written_on
  152. end
  153. def test_condition_array_interpolation
  154. assert_kind_of Firm, Company.find(:first, :conditions => ["name = '%s'", "37signals"])
  155. assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!"])
  156. assert_nil Company.find(:first, :conditions => ["name = '%s'", "37signals!' OR 1=1"])
  157. assert_kind_of Time, Topic.find(:first, :conditions => ["id = %d", 1]).written_on
  158. end
  159. def test_condition_hash_interpolation
  160. assert_kind_of Firm, Company.find(:first, :conditions => { :name => "37signals"})
  161. assert_nil Company.find(:first, :conditions => { :name => "37signals!"})
  162. assert_kind_of Time, Topic.find(:first, :conditions => {:id => 1}).written_on
  163. end
  164. def test_hash_condition_find_malformed
  165. assert_raises(ActiveRecord::StatementInvalid) {
  166. Company.find(:first, :conditions => { :id => 2, :dhh => true })
  167. }
  168. end
  169. def test_hash_condition_find_with_escaped_characters
  170. Company.create("name" => "Ain't noth'n like' \#stuff")
  171. assert Company.find(:first, :conditions => { :name => "Ain't noth'n like' \#stuff" })
  172. end
  173. def test_hash_condition_find_with_array
  174. p1, p2 = Post.find(:all, :limit => 2, :order => 'id asc')
  175. assert_equal [p1, p2], Post.find(:all, :conditions => { :id => [p1, p2] }, :order => 'id asc')
  176. assert_equal [p1, p2], Post.find(:all, :conditions => { :id => [p1, p2.id] }, :order => 'id asc')
  177. end
  178. def test_hash_condition_find_with_nil
  179. topic = Topic.find(:first, :conditions => { :last_read => nil } )
  180. assert_not_nil topic
  181. assert_nil topic.last_read
  182. end
  183. def test_bind_variables
  184. assert_kind_of Firm, Company.find(:first, :conditions => ["name = ?", "37signals"])
  185. assert_nil Company.find(:first, :conditions => ["name = ?", "37signals!"])
  186. assert_nil Company.find(:first, :conditions => ["name = ?", "37signals!' OR 1=1"])
  187. assert_kind_of Time, Topic.find(:first, :conditions => ["id = ?", 1]).written_on
  188. assert_raises(ActiveRecord::PreparedStatementInvalid) {
  189. Company.find(:first, :conditions => ["id=? AND name = ?", 2])
  190. }
  191. assert_raises(ActiveRecord::PreparedStatementInvalid) {
  192. Company.find(:first, :conditions => ["id=?", 2, 3, 4])
  193. }
  194. end
  195. def test_bind_variables_with_quotes
  196. Company.create("name" => "37signals' go'es agains")
  197. assert Company.find(:first, :conditions => ["name = ?", "37signals' go'es agains"])
  198. end
  199. def test_named_bind_variables_with_quotes
  200. Company.create("name" => "37signals' go'es agains")
  201. assert Company.find(:first, :conditions => ["name = :name", {:name => "37signals' go'es agains"}])
  202. end
  203. def test_bind_arity
  204. assert_nothing_raised { bind '' }
  205. assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '', 1 }
  206. assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?' }
  207. assert_nothing_raised { bind '?', 1 }
  208. assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 }
  209. end
  210. def test_named_bind_variables
  211. assert_equal '1', bind(':a', :a => 1) # ' ruby-mode
  212. assert_equal '1 1', bind(':a :a', :a => 1) # ' ruby-mode
  213. assert_kind_of Firm, Company.find(:first, :conditions => ["name = :name", { :name => "37signals" }])
  214. assert_nil Company.find(:first, :conditions => ["name = :name", { :name => "37signals!" }])
  215. assert_nil Company.find(:first, :conditions => ["name = :name", { :name => "37signals!' OR 1=1" }])
  216. assert_kind_of Time, Topic.find(:first, :conditions => ["id = :id", { :id => 1 }]).written_on
  217. end
  218. def test_bind_enumerable
  219. assert_equal '1,2,3', bind('?', [1, 2, 3])
  220. assert_equal %('a','b','c'), bind('?', %w(a b c))
  221. assert_equal '1,2,3', bind(':a', :a => [1, 2, 3])
  222. assert_equal %('a','b','c'), bind(':a', :a => %w(a b c)) # '
  223. require 'set'
  224. assert_equal '1,2,3', bind('?', Set.new([1, 2, 3]))
  225. assert_equal %('a','b','c'), bind('?', Set.new(%w(a b c)))
  226. assert_equal '1,2,3', bind(':a', :a => Set.new([1, 2, 3]))
  227. assert_equal %('a','b','c'), bind(':a', :a => Set.new(%w(a b c))) # '
  228. end
  229. def test_bind_empty_enumerable
  230. quoted_nil = ActiveRecord::Base.connection.quote(nil)
  231. assert_equal quoted_nil, bind('?', [])
  232. assert_equal " in (#{quoted_nil})", bind(' in (?)', [])
  233. assert_equal "foo in (#{quoted_nil})", bind('foo in (?)', [])
  234. end
  235. def test_bind_string
  236. assert_equal "''", bind('?', '')
  237. end
  238. def test_bind_record
  239. o = Struct.new(:quoted_id).new(1)
  240. assert_equal '1', bind('?', o)
  241. os = [o] * 3
  242. assert_equal '1,1,1', bind('?', os)
  243. end
  244. def test_string_sanitation
  245. assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1")
  246. assert_equal "'something; select table'", ActiveRecord::Base.sanitize("something; select table")
  247. end
  248. def test_count
  249. assert_equal(0, Entrant.count(:conditions => "id > 3"))
  250. assert_equal(1, Entrant.count(:conditions => ["id > ?", 2]))
  251. assert_equal(2, Entrant.count(:conditions => ["id > ?", 1]))
  252. end
  253. def test_count_by_sql
  254. assert_equal(0, Entrant.count_by_sql("SELECT COUNT(*) FROM entrants WHERE id > 3"))
  255. assert_equal(1, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 2]))
  256. assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1]))
  257. end
  258. def test_find_by_one_attribute
  259. assert_equal topics(:first), Topic.find_by_title("The First Topic")
  260. assert_nil Topic.find_by_title("The First Topic!")
  261. end
  262. def test_find_by_one_attribute_with_order_option
  263. assert_equal accounts(:signals37), Account.find_by_credit_limit(50, :order => 'id')
  264. assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :order => 'id DESC')
  265. end
  266. def test_find_by_one_attribute_with_conditions
  267. assert_equal accounts(:rails_core_account), Account.find_by_credit_limit(50, :conditions => ['firm_id = ?', 6])
  268. end
  269. def test_find_by_one_attribute_with_several_options
  270. assert_equal accounts(:unknown), Account.find_by_credit_limit(50, :order => 'id DESC', :conditions => ['id != ?', 3])
  271. end
  272. def test_find_by_one_missing_attribute
  273. assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") }
  274. end
  275. def test_find_by_invalid_method_syntax
  276. assert_raises(NoMethodError) { Topic.fail_to_find_by_title("The First Topic") }
  277. assert_raises(NoMethodError) { Topic.find_by_title?("The First Topic") }
  278. assert_raises(NoMethodError) { Topic.fail_to_find_or_create_by_title("Nonexistent Title") }
  279. assert_raises(NoMethodError) { Topic.find_or_create_by_title?("Nonexistent Title") }
  280. end
  281. def test_find_by_two_attributes
  282. assert_equal topics(:first), Topic.find_by_title_and_author_name("The First Topic", "David")
  283. assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary")
  284. end
  285. def test_find_all_by_one_attribute
  286. topics = Topic.find_all_by_content("Have a nice day")
  287. assert_equal 2, topics.size
  288. assert topics.include?(topics(:first))
  289. assert_equal [], Topic.find_all_by_title("The First Topic!!")
  290. end
  291. def test_find_all_by_one_attribute_with_options
  292. topics = Topic.find_all_by_content("Have a nice day", :order => "id DESC")
  293. assert topics(:first), topics.last
  294. topics = Topic.find_all_by_content("Have a nice day", :order => "id")
  295. assert topics(:first), topics.first
  296. end
  297. def test_find_all_by_array_attribute
  298. assert_equal 2, Topic.find_all_by_title(["The First Topic", "The Second Topic's of the day"]).size
  299. end
  300. def test_find_all_by_boolean_attribute
  301. topics = Topic.find_all_by_approved(false)
  302. assert_equal 1, topics.size
  303. assert topics.include?(topics(:first))
  304. topics = Topic.find_all_by_approved(true)
  305. assert_equal 1, topics.size
  306. assert topics.include?(topics(:second))
  307. end
  308. def test_find_by_nil_attribute
  309. topic = Topic.find_by_last_read nil
  310. assert_not_nil topic
  311. assert_nil topic.last_read
  312. end
  313. def test_find_all_by_nil_attribute
  314. topics = Topic.find_all_by_last_read nil
  315. assert_equal 1, topics.size
  316. assert_nil topics[0].last_read
  317. end
  318. def test_find_by_nil_and_not_nil_attributes
  319. topic = Topic.find_by_last_read_and_author_name nil, "Mary"
  320. assert_equal "Mary", topic.author_name
  321. end
  322. def test_find_all_by_nil_and_not_nil_attributes
  323. topics = Topic.find_all_by_last_read_and_author_name nil, "Mary"
  324. assert_equal 1, topics.size
  325. assert_equal "Mary", topics[0].author_name
  326. end
  327. def test_find_or_create_from_one_attribute
  328. number_of_companies = Company.count
  329. sig38 = Company.find_or_create_by_name("38signals")
  330. assert_equal number_of_companies + 1, Company.count
  331. assert_equal sig38, Company.find_or_create_by_name("38signals")
  332. assert !sig38.new_record?
  333. end
  334. def test_find_or_create_from_two_attributes
  335. number_of_topics = Topic.count
  336. another = Topic.find_or_create_by_title_and_author_name("Another topic","John")
  337. assert_equal number_of_topics + 1, Topic.count
  338. assert_equal another, Topic.find_or_create_by_title_and_author_name("Another topic", "John")
  339. assert !another.new_record?
  340. end
  341. def test_find_or_create_from_one_attribute_and_hash
  342. number_of_companies = Company.count
  343. sig38 = Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
  344. assert_equal number_of_companies + 1, Company.count
  345. assert_equal sig38, Company.find_or_create_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
  346. assert !sig38.new_record?
  347. assert_equal "38signals", sig38.name
  348. assert_equal 17, sig38.firm_id
  349. assert_equal 23, sig38.client_of
  350. end
  351. def test_find_or_initialize_from_one_attribute
  352. sig38 = Company.find_or_initialize_by_name("38signals")
  353. assert_equal "38signals", sig38.name
  354. assert sig38.new_record?
  355. end
  356. def test_find_or_initialize_from_two_attributes
  357. another = Topic.find_or_initialize_by_title_and_author_name("Another topic","John")
  358. assert_equal "Another topic", another.title
  359. assert_equal "John", another.author_name
  360. assert another.new_record?
  361. end
  362. def test_find_or_initialize_from_one_attribute_and_hash
  363. sig38 = Company.find_or_initialize_by_name({:name => "38signals", :firm_id => 17, :client_of => 23})
  364. assert_equal "38signals", sig38.name
  365. assert_equal 17, sig38.firm_id
  366. assert_equal 23, sig38.client_of
  367. assert sig38.new_record?
  368. end
  369. def test_find_with_bad_sql
  370. assert_raises(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
  371. end
  372. def test_find_with_invalid_params
  373. assert_raises(ArgumentError) { Topic.find :first, :join => "It should be `joins'" }
  374. assert_raises(ArgumentError) { Topic.find :first, :conditions => '1 = 1', :join => "It should be `joins'" }
  375. end
  376. def test_find_all_with_limit
  377. first_five_developers = Developer.find :all, :order => 'id ASC', :limit => 5
  378. assert_equal 5, first_five_developers.length
  379. assert_equal 'David', first_five_developers.first.name
  380. assert_equal 'fixture_5', first_five_developers.last.name
  381. no_developers = Developer.find :all, :order => 'id ASC', :limit => 0
  382. assert_equal 0, no_developers.length
  383. end
  384. def test_find_all_with_limit_and_offset
  385. first_three_developers = Developer.find :all, :order => 'id ASC', :limit => 3, :offset => 0
  386. second_three_developers = Developer.find :all, :order => 'id ASC', :limit => 3, :offset => 3
  387. last_two_developers = Developer.find :all, :order => 'id ASC', :limit => 2, :offset => 8
  388. assert_equal 3, first_three_developers.length
  389. assert_equal 3, second_three_developers.length
  390. assert_equal 2, last_two_developers.length
  391. assert_equal 'David', first_three_developers.first.name
  392. assert_equal 'fixture_4', second_three_developers.first.name
  393. assert_equal 'fixture_9', last_two_developers.first.name
  394. end
  395. def test_find_all_with_limit_and_offset_and_multiple_order_clauses
  396. first_three_posts = Post.find :all, :order => 'author_id, id', :limit => 3, :offset => 0
  397. second_three_posts = Post.find :all, :order => ' author_id,id ', :limit => 3, :offset => 3
  398. last_posts = Post.find :all, :order => ' author_id, id ', :limit => 3, :offset => 6
  399. assert_equal [[0,3],[1,1],[1,2]], first_three_posts.map { |p| [p.author_id, p.id] }
  400. assert_equal [[1,4],[1,5],[1,6]], second_three_posts.map { |p| [p.author_id, p.id] }
  401. assert_equal [[2,7]], last_posts.map { |p| [p.author_id, p.id] }
  402. end
  403. def test_find_all_with_join
  404. developers_on_project_one = Developer.find(
  405. :all,
  406. :joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
  407. :conditions => 'project_id=1'
  408. )
  409. assert_equal 3, developers_on_project_one.length
  410. developer_names = developers_on_project_one.map { |d| d.name }
  411. assert developer_names.include?('David')
  412. assert developer_names.include?('Jamis')
  413. end
  414. def test_joins_dont_clobber_id
  415. first = Firm.find(
  416. :first,
  417. :joins => 'INNER JOIN companies AS clients ON clients.firm_id = companies.id',
  418. :conditions => 'companies.id = 1'
  419. )
  420. assert_equal 1, first.id
  421. end
  422. def test_find_by_id_with_conditions_with_or
  423. assert_nothing_raised do
  424. Post.find([1,2,3],
  425. :conditions => "posts.id <= 3 OR posts.#{QUOTED_TYPE} = 'Post'")
  426. end
  427. end
  428. # http://dev.rubyonrails.org/ticket/6778
  429. def test_find_ignores_previously_inserted_record
  430. post = Post.create!(:title => 'test', :body => 'it out')
  431. assert_equal [], Post.find_all_by_id(nil)
  432. end
  433. def test_find_by_empty_ids
  434. assert_equal [], Post.find([])
  435. end
  436. def test_find_by_empty_in_condition
  437. assert_equal [], Post.find(:all, :conditions => ['id in (?)', []])
  438. end
  439. def test_find_by_records
  440. p1, p2 = Post.find(:all, :limit => 2, :order => 'id asc')
  441. assert_equal [p1, p2], Post.find(:all, :conditions => ['id in (?)', [p1, p2]], :order => 'id asc')
  442. assert_equal [p1, p2], Post.find(:all, :conditions => ['id in (?)', [p1, p2.id]], :order => 'id asc')
  443. end
  444. def test_select_value
  445. assert_equal "37signals", Company.connection.select_value("SELECT name FROM companies WHERE id = 1")
  446. assert_nil Company.connection.select_value("SELECT name FROM companies WHERE id = -1")
  447. # make sure we didn't break count...
  448. assert_equal 0, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = 'Halliburton'")
  449. assert_equal 1, Company.count_by_sql("SELECT COUNT(*) FROM companies WHERE name = '37signals'")
  450. end
  451. def test_select_values
  452. assert_equal ["1","2","3","4","5","6","7","8","9"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s }
  453. assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy"], Company.connection.select_values("SELECT name FROM companies ORDER BY id")
  454. end
  455. protected
  456. def bind(statement, *vars)
  457. if vars.first.is_a?(Hash)
  458. ActiveRecord::Base.send(:replace_named_bind_variables, statement, vars.first)
  459. else
  460. ActiveRecord::Base.send(:replace_bind_variables, statement, vars)
  461. end
  462. end
  463. end