PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/rails/activerecord/test/associations_test.rb

https://github.com/bricooke/my-biz-expenses
Ruby | 1809 lines | 1524 code | 271 blank | 14 comment | 7 complexity | 50e5a90b0a2c3cd00dffa3167d900fbf MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. require 'abstract_unit'
  2. require 'fixtures/developer'
  3. require 'fixtures/project'
  4. require 'fixtures/company'
  5. require 'fixtures/topic'
  6. require 'fixtures/reply'
  7. require 'fixtures/computer'
  8. require 'fixtures/customer'
  9. require 'fixtures/order'
  10. require 'fixtures/category'
  11. require 'fixtures/post'
  12. require 'fixtures/author'
  13. class AssociationsTest < Test::Unit::TestCase
  14. fixtures :accounts, :companies, :developers, :projects, :developers_projects,
  15. :computers
  16. def test_bad_collection_keys
  17. assert_raise(ArgumentError, 'ActiveRecord should have barked on bad collection keys') do
  18. Class.new(ActiveRecord::Base).has_many(:wheels, :name => 'wheels')
  19. end
  20. end
  21. def test_force_reload
  22. firm = Firm.new("name" => "A New Firm, Inc")
  23. firm.save
  24. firm.clients.each {|c|} # forcing to load all clients
  25. assert firm.clients.empty?, "New firm shouldn't have client objects"
  26. assert_deprecated do
  27. assert !firm.has_clients?, "New firm shouldn't have clients"
  28. end
  29. assert_equal 0, firm.clients.size, "New firm should have 0 clients"
  30. client = Client.new("name" => "TheClient.com", "firm_id" => firm.id)
  31. client.save
  32. assert firm.clients.empty?, "New firm should have cached no client objects"
  33. assert_deprecated do
  34. assert !firm.has_clients?, "New firm should have cached a no-clients response"
  35. end
  36. assert_equal 0, firm.clients.size, "New firm should have cached 0 clients count"
  37. assert !firm.clients(true).empty?, "New firm should have reloaded client objects"
  38. assert_equal 1, firm.clients(true).size, "New firm should have reloaded clients count"
  39. end
  40. def test_storing_in_pstore
  41. require "tmpdir"
  42. store_filename = File.join(Dir.tmpdir, "ar-pstore-association-test")
  43. File.delete(store_filename) if File.exists?(store_filename)
  44. require "pstore"
  45. apple = Firm.create("name" => "Apple")
  46. natural = Client.new("name" => "Natural Company")
  47. apple.clients << natural
  48. db = PStore.new(store_filename)
  49. db.transaction do
  50. db["apple"] = apple
  51. end
  52. db = PStore.new(store_filename)
  53. db.transaction do
  54. assert_equal "Natural Company", db["apple"].clients.first.name
  55. end
  56. end
  57. end
  58. class AssociationProxyTest < Test::Unit::TestCase
  59. fixtures :authors, :posts
  60. def test_proxy_accessors
  61. welcome = posts(:welcome)
  62. assert_equal welcome, welcome.author.proxy_owner
  63. assert_equal welcome.class.reflect_on_association(:author), welcome.author.proxy_reflection
  64. welcome.author.class # force load target
  65. assert_equal welcome.author, welcome.author.proxy_target
  66. david = authors(:david)
  67. assert_equal david, david.posts.proxy_owner
  68. assert_equal david.class.reflect_on_association(:posts), david.posts.proxy_reflection
  69. david.posts.first # force load target
  70. assert_equal david.posts, david.posts.proxy_target
  71. assert_equal david, david.posts_with_extension.testing_proxy_owner
  72. assert_equal david.class.reflect_on_association(:posts_with_extension), david.posts_with_extension.testing_proxy_reflection
  73. david.posts_with_extension.first # force load target
  74. assert_equal david.posts_with_extension, david.posts_with_extension.testing_proxy_target
  75. end
  76. end
  77. class HasOneAssociationsTest < Test::Unit::TestCase
  78. fixtures :accounts, :companies, :developers, :projects, :developers_projects
  79. def setup
  80. Account.destroyed_account_ids.clear
  81. end
  82. def test_has_one
  83. assert_equal companies(:first_firm).account, Account.find(1)
  84. assert_equal Account.find(1).credit_limit, companies(:first_firm).account.credit_limit
  85. end
  86. def test_has_one_cache_nils
  87. firm = companies(:another_firm)
  88. assert_queries(1) { assert_nil firm.account }
  89. assert_queries(0) { assert_nil firm.account }
  90. firms = Firm.find(:all, :include => :account)
  91. assert_queries(0) { firms.each(&:account) }
  92. end
  93. def test_can_marshal_has_one_association_with_nil_target
  94. firm = Firm.new
  95. assert_nothing_raised do
  96. assert_equal firm.attributes, Marshal.load(Marshal.dump(firm)).attributes
  97. end
  98. firm.account
  99. assert_nothing_raised do
  100. assert_equal firm.attributes, Marshal.load(Marshal.dump(firm)).attributes
  101. end
  102. end
  103. def test_proxy_assignment
  104. company = companies(:first_firm)
  105. assert_nothing_raised { company.account = company.account }
  106. end
  107. def test_triple_equality
  108. assert Account === companies(:first_firm).account
  109. assert companies(:first_firm).account === Account
  110. end
  111. def test_type_mismatch
  112. assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).account = 1 }
  113. assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).account = Project.find(1) }
  114. end
  115. def test_natural_assignment
  116. apple = Firm.create("name" => "Apple")
  117. citibank = Account.create("credit_limit" => 10)
  118. apple.account = citibank
  119. assert_equal apple.id, citibank.firm_id
  120. end
  121. def test_natural_assignment_to_nil
  122. old_account_id = companies(:first_firm).account.id
  123. companies(:first_firm).account = nil
  124. companies(:first_firm).save
  125. assert_nil companies(:first_firm).account
  126. # account is dependent, therefore is destroyed when reference to owner is lost
  127. assert_raises(ActiveRecord::RecordNotFound) { Account.find(old_account_id) }
  128. end
  129. def test_assignment_without_replacement
  130. apple = Firm.create("name" => "Apple")
  131. citibank = Account.create("credit_limit" => 10)
  132. apple.account = citibank
  133. assert_equal apple.id, citibank.firm_id
  134. hsbc = apple.build_account({ :credit_limit => 20}, false)
  135. assert_equal apple.id, hsbc.firm_id
  136. hsbc.save
  137. assert_equal apple.id, citibank.firm_id
  138. nykredit = apple.create_account({ :credit_limit => 30}, false)
  139. assert_equal apple.id, nykredit.firm_id
  140. assert_equal apple.id, citibank.firm_id
  141. assert_equal apple.id, hsbc.firm_id
  142. end
  143. def test_assignment_without_replacement_on_create
  144. apple = Firm.create("name" => "Apple")
  145. citibank = Account.create("credit_limit" => 10)
  146. apple.account = citibank
  147. assert_equal apple.id, citibank.firm_id
  148. hsbc = apple.create_account({:credit_limit => 10}, false)
  149. assert_equal apple.id, hsbc.firm_id
  150. hsbc.save
  151. assert_equal apple.id, citibank.firm_id
  152. end
  153. def test_dependence
  154. num_accounts = Account.count
  155. firm = Firm.find(1)
  156. assert !firm.account.nil?
  157. account_id = firm.account.id
  158. assert_equal [], Account.destroyed_account_ids[firm.id]
  159. firm.destroy
  160. assert_equal num_accounts - 1, Account.count
  161. assert_equal [account_id], Account.destroyed_account_ids[firm.id]
  162. end
  163. def test_deprecated_exclusive_dependence
  164. assert_deprecated(/:exclusively_dependent.*:dependent => :delete_all/) do
  165. Firm.has_many :deprecated_exclusively_dependent_clients, :class_name => 'Client', :exclusively_dependent => true
  166. end
  167. end
  168. def test_exclusive_dependence
  169. num_accounts = Account.count
  170. firm = ExclusivelyDependentFirm.find(9)
  171. assert !firm.account.nil?
  172. account_id = firm.account.id
  173. assert_equal [], Account.destroyed_account_ids[firm.id]
  174. firm.destroy
  175. assert_equal num_accounts - 1, Account.count
  176. assert_equal [], Account.destroyed_account_ids[firm.id]
  177. end
  178. def test_dependence_with_nil_associate
  179. firm = DependentFirm.new(:name => 'nullify')
  180. firm.save!
  181. assert_nothing_raised { firm.destroy }
  182. end
  183. def test_succesful_build_association
  184. firm = Firm.new("name" => "GlobalMegaCorp")
  185. firm.save
  186. account = firm.build_account("credit_limit" => 1000)
  187. assert account.save
  188. assert_equal account, firm.account
  189. end
  190. def test_failing_build_association
  191. firm = Firm.new("name" => "GlobalMegaCorp")
  192. firm.save
  193. account = firm.build_account
  194. assert !account.save
  195. assert_equal "can't be empty", account.errors.on("credit_limit")
  196. end
  197. def test_build_association_twice_without_saving_affects_nothing
  198. count_of_account = Account.count
  199. firm = Firm.find(:first)
  200. account1 = firm.build_account("credit_limit" => 1000)
  201. account2 = firm.build_account("credit_limit" => 2000)
  202. assert_equal count_of_account, Account.count
  203. end
  204. def test_create_association
  205. firm = Firm.new("name" => "GlobalMegaCorp")
  206. firm.save
  207. assert_equal firm.create_account("credit_limit" => 1000), firm.account
  208. end
  209. def test_build
  210. firm = Firm.new("name" => "GlobalMegaCorp")
  211. firm.save
  212. firm.account = account = Account.new("credit_limit" => 1000)
  213. assert_equal account, firm.account
  214. assert account.save
  215. assert_equal account, firm.account
  216. end
  217. def test_build_before_child_saved
  218. firm = Firm.find(1)
  219. account = firm.account.build("credit_limit" => 1000)
  220. assert_equal account, firm.account
  221. assert account.new_record?
  222. assert firm.save
  223. assert_equal account, firm.account
  224. assert !account.new_record?
  225. end
  226. def test_build_before_either_saved
  227. firm = Firm.new("name" => "GlobalMegaCorp")
  228. firm.account = account = Account.new("credit_limit" => 1000)
  229. assert_equal account, firm.account
  230. assert account.new_record?
  231. assert firm.save
  232. assert_equal account, firm.account
  233. assert !account.new_record?
  234. end
  235. def test_failing_build_association
  236. firm = Firm.new("name" => "GlobalMegaCorp")
  237. firm.save
  238. firm.account = account = Account.new
  239. assert_equal account, firm.account
  240. assert !account.save
  241. assert_equal account, firm.account
  242. assert_equal "can't be empty", account.errors.on("credit_limit")
  243. end
  244. def test_create
  245. firm = Firm.new("name" => "GlobalMegaCorp")
  246. firm.save
  247. firm.account = account = Account.create("credit_limit" => 1000)
  248. assert_equal account, firm.account
  249. end
  250. def test_create_before_save
  251. firm = Firm.new("name" => "GlobalMegaCorp")
  252. firm.account = account = Account.create("credit_limit" => 1000)
  253. assert_equal account, firm.account
  254. end
  255. def test_dependence_with_missing_association
  256. Account.destroy_all
  257. firm = Firm.find(1)
  258. assert firm.account.nil?
  259. firm.destroy
  260. end
  261. def test_assignment_before_parent_saved
  262. firm = Firm.new("name" => "GlobalMegaCorp")
  263. firm.account = a = Account.find(1)
  264. assert firm.new_record?
  265. assert_equal a, firm.account
  266. assert firm.save
  267. assert_equal a, firm.account
  268. assert_equal a, firm.account(true)
  269. end
  270. def test_finding_with_interpolated_condition
  271. firm = Firm.find(:first)
  272. superior = firm.clients.create(:name => 'SuperiorCo')
  273. superior.rating = 10
  274. superior.save
  275. assert_equal 10, firm.clients_with_interpolated_conditions.first.rating
  276. end
  277. def test_assignment_before_child_saved
  278. firm = Firm.find(1)
  279. firm.account = a = Account.new("credit_limit" => 1000)
  280. assert !a.new_record?
  281. assert_equal a, firm.account
  282. assert_equal a, firm.account
  283. assert_equal a, firm.account(true)
  284. end
  285. def test_assignment_before_either_saved
  286. firm = Firm.new("name" => "GlobalMegaCorp")
  287. firm.account = a = Account.new("credit_limit" => 1000)
  288. assert firm.new_record?
  289. assert a.new_record?
  290. assert_equal a, firm.account
  291. assert firm.save
  292. assert !firm.new_record?
  293. assert !a.new_record?
  294. assert_equal a, firm.account
  295. assert_equal a, firm.account(true)
  296. end
  297. def test_not_resaved_when_unchanged
  298. firm = Firm.find(:first, :include => :account)
  299. assert_queries(1) { firm.save! }
  300. firm = Firm.find(:first)
  301. firm.account = Account.find(:first)
  302. assert_queries(1) { firm.save! }
  303. firm = Firm.find(:first).clone
  304. firm.account = Account.find(:first)
  305. assert_queries(2) { firm.save! }
  306. firm = Firm.find(:first).clone
  307. firm.account = Account.find(:first).clone
  308. assert_queries(2) { firm.save! }
  309. end
  310. def test_save_still_works_after_accessing_nil_has_one
  311. jp = Company.new :name => 'Jaded Pixel'
  312. jp.dummy_account.nil?
  313. assert_nothing_raised do
  314. jp.save!
  315. end
  316. end
  317. def test_deprecated_inferred_foreign_key
  318. assert_not_deprecated { Company.belongs_to :firm }
  319. assert_not_deprecated { Company.belongs_to :client, :foreign_key => "firm_id" }
  320. assert_not_deprecated { Company.belongs_to :firm, :class_name => "Firm", :foreign_key => "client_of" }
  321. assert_deprecated("inferred foreign_key name") { Company.belongs_to :client, :class_name => "Firm" }
  322. end
  323. end
  324. class HasManyAssociationsTest < Test::Unit::TestCase
  325. fixtures :accounts, :companies, :developers, :projects,
  326. :developers_projects, :topics
  327. def setup
  328. Client.destroyed_client_ids.clear
  329. end
  330. def force_signal37_to_load_all_clients_of_firm
  331. companies(:first_firm).clients_of_firm.each {|f| }
  332. end
  333. def test_counting_with_counter_sql
  334. assert_equal 2, Firm.find(:first).clients.count
  335. end
  336. def test_counting
  337. assert_equal 2, Firm.find(:first).plain_clients.count
  338. end
  339. def test_counting_with_single_conditions
  340. assert_deprecated 'count' do
  341. assert_equal 2, Firm.find(:first).plain_clients.count('1=1')
  342. end
  343. end
  344. def test_counting_with_single_hash
  345. assert_equal 2, Firm.find(:first).plain_clients.count(:conditions => '1=1')
  346. end
  347. def test_counting_with_column_name_and_hash
  348. assert_equal 2, Firm.find(:first).plain_clients.count(:all, :conditions => '1=1')
  349. end
  350. def test_finding
  351. assert_equal 2, Firm.find(:first).clients.length
  352. end
  353. def test_find_many_with_merged_options
  354. assert_equal 1, companies(:first_firm).limited_clients.size
  355. assert_equal 1, companies(:first_firm).limited_clients.find(:all).size
  356. assert_equal 2, companies(:first_firm).limited_clients.find(:all, :limit => nil).size
  357. end
  358. def test_triple_equality
  359. assert !(Array === Firm.find(:first).clients)
  360. assert Firm.find(:first).clients === Array
  361. end
  362. def test_finding_default_orders
  363. assert_equal "Summit", Firm.find(:first).clients.first.name
  364. end
  365. def test_finding_with_different_class_name_and_order
  366. assert_equal "Microsoft", Firm.find(:first).clients_sorted_desc.first.name
  367. end
  368. def test_finding_with_foreign_key
  369. assert_equal "Microsoft", Firm.find(:first).clients_of_firm.first.name
  370. end
  371. def test_finding_with_condition
  372. assert_equal "Microsoft", Firm.find(:first).clients_like_ms.first.name
  373. end
  374. def test_finding_with_condition_hash
  375. assert_equal "Microsoft", Firm.find(:first).clients_like_ms_with_hash_conditions.first.name
  376. end
  377. def test_finding_using_sql
  378. firm = Firm.find(:first)
  379. first_client = firm.clients_using_sql.first
  380. assert_not_nil first_client
  381. assert_equal "Microsoft", first_client.name
  382. assert_equal 1, firm.clients_using_sql.size
  383. assert_equal 1, Firm.find(:first).clients_using_sql.size
  384. end
  385. def test_counting_using_sql
  386. assert_equal 1, Firm.find(:first).clients_using_counter_sql.size
  387. assert_equal 0, Firm.find(:first).clients_using_zero_counter_sql.size
  388. end
  389. def test_counting_non_existant_items_using_sql
  390. assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size
  391. end
  392. def test_belongs_to_sanity
  393. c = Client.new
  394. assert_nil c.firm
  395. if c.firm
  396. assert false, "belongs_to failed if check"
  397. end
  398. unless c.firm
  399. else
  400. assert false, "belongs_to failed unless check"
  401. end
  402. end
  403. def test_find_ids
  404. firm = Firm.find(:first)
  405. assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find }
  406. client = firm.clients.find(2)
  407. assert_kind_of Client, client
  408. client_ary = firm.clients.find([2])
  409. assert_kind_of Array, client_ary
  410. assert_equal client, client_ary.first
  411. client_ary = firm.clients.find(2, 3)
  412. assert_kind_of Array, client_ary
  413. assert_equal 2, client_ary.size
  414. assert_equal client, client_ary.first
  415. assert_raises(ActiveRecord::RecordNotFound) { firm.clients.find(2, 99) }
  416. end
  417. def test_find_all
  418. assert_deprecated 'find_all' do
  419. firm = Firm.find_first
  420. assert_equal firm.clients, firm.clients.find_all
  421. assert_equal 2, firm.clients.find(:all, :conditions => "#{QUOTED_TYPE} = 'Client'").length
  422. assert_equal 1, firm.clients.find(:all, :conditions => "name = 'Summit'").length
  423. end
  424. end
  425. def test_find_all_sanitized
  426. assert_deprecated 'find_all' do
  427. firm = Firm.find_first
  428. assert_equal firm.clients.find_all("name = 'Summit'"), firm.clients.find_all(["name = '%s'", "Summit"])
  429. summit = firm.clients.find(:all, :conditions => "name = 'Summit'")
  430. assert_equal summit, firm.clients.find(:all, :conditions => ["name = ?", "Summit"])
  431. assert_equal summit, firm.clients.find(:all, :conditions => ["name = :name", { :name => "Summit" }])
  432. end
  433. end
  434. def test_find_first
  435. assert_deprecated 'find_first' do
  436. firm = Firm.find_first
  437. client2 = Client.find(2)
  438. assert_equal firm.clients.first, firm.clients.find_first
  439. assert_equal client2, firm.clients.find_first("#{QUOTED_TYPE} = 'Client'")
  440. assert_equal client2, firm.clients.find(:first, :conditions => "#{QUOTED_TYPE} = 'Client'")
  441. end
  442. end
  443. def test_find_first_sanitized
  444. assert_deprecated 'find_first' do
  445. firm = Firm.find_first
  446. client2 = Client.find(2)
  447. assert_deprecated(/find_first/) do
  448. assert_equal client2, firm.clients.find_first(["#{QUOTED_TYPE} = ?", "Client"])
  449. end
  450. assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = ?", 'Client'])
  451. assert_equal client2, firm.clients.find(:first, :conditions => ["#{QUOTED_TYPE} = :type", { :type => 'Client' }])
  452. end
  453. end
  454. def test_find_in_collection
  455. assert_equal Client.find(2).name, companies(:first_firm).clients.find(2).name
  456. assert_raises(ActiveRecord::RecordNotFound) { companies(:first_firm).clients.find(6) }
  457. end
  458. def test_find_grouped
  459. all_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1")
  460. grouped_clients_of_firm1 = Client.find(:all, :conditions => "firm_id = 1", :group => "firm_id", :select => 'firm_id, count(id) as clients_count')
  461. assert_equal 2, all_clients_of_firm1.size
  462. assert_equal 1, grouped_clients_of_firm1.size
  463. end
  464. def test_adding
  465. force_signal37_to_load_all_clients_of_firm
  466. natural = Client.new("name" => "Natural Company")
  467. companies(:first_firm).clients_of_firm << natural
  468. assert_equal 2, companies(:first_firm).clients_of_firm.size # checking via the collection
  469. assert_equal 2, companies(:first_firm).clients_of_firm(true).size # checking using the db
  470. assert_equal natural, companies(:first_firm).clients_of_firm.last
  471. end
  472. def test_adding_using_create
  473. first_firm = companies(:first_firm)
  474. assert_equal 2, first_firm.plain_clients.size
  475. natural = first_firm.plain_clients.create(:name => "Natural Company")
  476. assert_equal 3, first_firm.plain_clients.length
  477. assert_equal 3, first_firm.plain_clients.size
  478. end
  479. def test_adding_a_mismatch_class
  480. assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << nil }
  481. assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << 1 }
  482. assert_raises(ActiveRecord::AssociationTypeMismatch) { companies(:first_firm).clients_of_firm << Topic.find(1) }
  483. end
  484. def test_adding_a_collection
  485. force_signal37_to_load_all_clients_of_firm
  486. companies(:first_firm).clients_of_firm.concat([Client.new("name" => "Natural Company"), Client.new("name" => "Apple")])
  487. assert_equal 3, companies(:first_firm).clients_of_firm.size
  488. assert_equal 3, companies(:first_firm).clients_of_firm(true).size
  489. end
  490. def test_adding_before_save
  491. no_of_firms = Firm.count
  492. no_of_clients = Client.count
  493. new_firm = Firm.new("name" => "A New Firm, Inc")
  494. new_firm.clients_of_firm.push Client.new("name" => "Natural Company")
  495. new_firm.clients_of_firm << (c = Client.new("name" => "Apple"))
  496. assert new_firm.new_record?
  497. assert c.new_record?
  498. assert_equal 2, new_firm.clients_of_firm.size
  499. assert_equal no_of_firms, Firm.count # Firm was not saved to database.
  500. assert_equal no_of_clients, Client.count # Clients were not saved to database.
  501. assert new_firm.save
  502. assert !new_firm.new_record?
  503. assert !c.new_record?
  504. assert_equal new_firm, c.firm
  505. assert_equal no_of_firms+1, Firm.count # Firm was saved to database.
  506. assert_equal no_of_clients+2, Client.count # Clients were saved to database.
  507. assert_equal 2, new_firm.clients_of_firm.size
  508. assert_equal 2, new_firm.clients_of_firm(true).size
  509. end
  510. def test_invalid_adding
  511. firm = Firm.find(1)
  512. assert !(firm.clients_of_firm << c = Client.new)
  513. assert c.new_record?
  514. assert !firm.valid?
  515. assert !firm.save
  516. assert c.new_record?
  517. end
  518. def test_invalid_adding_before_save
  519. no_of_firms = Firm.count
  520. no_of_clients = Client.count
  521. new_firm = Firm.new("name" => "A New Firm, Inc")
  522. new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")])
  523. assert c.new_record?
  524. assert !c.valid?
  525. assert !new_firm.valid?
  526. assert !new_firm.save
  527. assert c.new_record?
  528. assert new_firm.new_record?
  529. end
  530. def test_build
  531. new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client")
  532. assert_equal "Another Client", new_client.name
  533. assert new_client.new_record?
  534. assert_equal new_client, companies(:first_firm).clients_of_firm.last
  535. assert companies(:first_firm).save
  536. assert !new_client.new_record?
  537. assert_equal 2, companies(:first_firm).clients_of_firm(true).size
  538. end
  539. def test_build_many
  540. new_clients = companies(:first_firm).clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}])
  541. assert_equal 2, new_clients.size
  542. assert companies(:first_firm).save
  543. assert_equal 3, companies(:first_firm).clients_of_firm(true).size
  544. end
  545. def test_build_without_loading_association
  546. first_topic = topics(:first)
  547. Reply.column_names
  548. assert_equal 1, first_topic.replies.length
  549. assert_no_queries do
  550. first_topic.replies.build(:title => "Not saved", :content => "Superstars")
  551. assert_equal 2, first_topic.replies.size
  552. end
  553. assert_equal 2, first_topic.replies.to_ary.size
  554. end
  555. def test_create_without_loading_association
  556. first_firm = companies(:first_firm)
  557. Firm.column_names
  558. Client.column_names
  559. assert_equal 1, first_firm.clients_of_firm.size
  560. first_firm.clients_of_firm.reset
  561. assert_queries(1) do
  562. first_firm.clients_of_firm.create(:name => "Superstars")
  563. end
  564. assert_equal 2, first_firm.clients_of_firm.size
  565. end
  566. def test_invalid_build
  567. new_client = companies(:first_firm).clients_of_firm.build
  568. assert new_client.new_record?
  569. assert !new_client.valid?
  570. assert_equal new_client, companies(:first_firm).clients_of_firm.last
  571. assert !companies(:first_firm).save
  572. assert new_client.new_record?
  573. assert_equal 1, companies(:first_firm).clients_of_firm(true).size
  574. end
  575. def test_create
  576. force_signal37_to_load_all_clients_of_firm
  577. new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
  578. assert !new_client.new_record?
  579. assert_equal new_client, companies(:first_firm).clients_of_firm.last
  580. assert_equal new_client, companies(:first_firm).clients_of_firm(true).last
  581. end
  582. def test_create_many
  583. companies(:first_firm).clients_of_firm.create([{"name" => "Another Client"}, {"name" => "Another Client II"}])
  584. assert_equal 3, companies(:first_firm).clients_of_firm(true).size
  585. end
  586. def test_find_or_create
  587. number_of_clients = companies(:first_firm).clients.size
  588. the_client = companies(:first_firm).clients.find_or_create_by_name("Yet another client")
  589. assert_equal number_of_clients + 1, companies(:first_firm, :refresh).clients.size
  590. assert_equal the_client, companies(:first_firm).clients.find_or_create_by_name("Yet another client")
  591. assert_equal number_of_clients + 1, companies(:first_firm, :refresh).clients.size
  592. end
  593. def test_deleting
  594. force_signal37_to_load_all_clients_of_firm
  595. companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
  596. assert_equal 0, companies(:first_firm).clients_of_firm.size
  597. assert_equal 0, companies(:first_firm).clients_of_firm(true).size
  598. end
  599. def test_deleting_before_save
  600. new_firm = Firm.new("name" => "A New Firm, Inc.")
  601. new_client = new_firm.clients_of_firm.build("name" => "Another Client")
  602. assert_equal 1, new_firm.clients_of_firm.size
  603. new_firm.clients_of_firm.delete(new_client)
  604. assert_equal 0, new_firm.clients_of_firm.size
  605. end
  606. def test_deleting_a_collection
  607. force_signal37_to_load_all_clients_of_firm
  608. companies(:first_firm).clients_of_firm.create("name" => "Another Client")
  609. assert_equal 2, companies(:first_firm).clients_of_firm.size
  610. companies(:first_firm).clients_of_firm.delete([companies(:first_firm).clients_of_firm[0], companies(:first_firm).clients_of_firm[1]])
  611. assert_equal 0, companies(:first_firm).clients_of_firm.size
  612. assert_equal 0, companies(:first_firm).clients_of_firm(true).size
  613. end
  614. def test_delete_all
  615. force_signal37_to_load_all_clients_of_firm
  616. companies(:first_firm).clients_of_firm.create("name" => "Another Client")
  617. assert_equal 2, companies(:first_firm).clients_of_firm.size
  618. companies(:first_firm).clients_of_firm.delete_all
  619. assert_equal 0, companies(:first_firm).clients_of_firm.size
  620. assert_equal 0, companies(:first_firm).clients_of_firm(true).size
  621. end
  622. def test_delete_all_with_not_yet_loaded_association_collection
  623. force_signal37_to_load_all_clients_of_firm
  624. companies(:first_firm).clients_of_firm.create("name" => "Another Client")
  625. assert_equal 2, companies(:first_firm).clients_of_firm.size
  626. companies(:first_firm).clients_of_firm.reset
  627. companies(:first_firm).clients_of_firm.delete_all
  628. assert_equal 0, companies(:first_firm).clients_of_firm.size
  629. assert_equal 0, companies(:first_firm).clients_of_firm(true).size
  630. end
  631. def test_clearing_an_association_collection
  632. firm = companies(:first_firm)
  633. client_id = firm.clients_of_firm.first.id
  634. assert_equal 1, firm.clients_of_firm.size
  635. firm.clients_of_firm.clear
  636. assert_equal 0, firm.clients_of_firm.size
  637. assert_equal 0, firm.clients_of_firm(true).size
  638. assert_equal [], Client.destroyed_client_ids[firm.id]
  639. # Should not be destroyed since the association is not dependent.
  640. assert_nothing_raised do
  641. assert Client.find(client_id).firm.nil?
  642. end
  643. end
  644. def test_clearing_a_dependent_association_collection
  645. firm = companies(:first_firm)
  646. client_id = firm.dependent_clients_of_firm.first.id
  647. assert_equal 1, firm.dependent_clients_of_firm.size
  648. # :dependent means destroy is called on each client
  649. firm.dependent_clients_of_firm.clear
  650. assert_equal 0, firm.dependent_clients_of_firm.size
  651. assert_equal 0, firm.dependent_clients_of_firm(true).size
  652. assert_equal [client_id], Client.destroyed_client_ids[firm.id]
  653. # Should be destroyed since the association is dependent.
  654. assert Client.find_by_id(client_id).nil?
  655. end
  656. def test_clearing_an_exclusively_dependent_association_collection
  657. firm = companies(:first_firm)
  658. client_id = firm.exclusively_dependent_clients_of_firm.first.id
  659. assert_equal 1, firm.exclusively_dependent_clients_of_firm.size
  660. assert_equal [], Client.destroyed_client_ids[firm.id]
  661. # :exclusively_dependent means each client is deleted directly from
  662. # the database without looping through them calling destroy.
  663. firm.exclusively_dependent_clients_of_firm.clear
  664. assert_equal 0, firm.exclusively_dependent_clients_of_firm.size
  665. assert_equal 0, firm.exclusively_dependent_clients_of_firm(true).size
  666. assert_equal [3], Client.destroyed_client_ids[firm.id]
  667. # Should be destroyed since the association is exclusively dependent.
  668. assert Client.find_by_id(client_id).nil?
  669. end
  670. def test_clearing_without_initial_access
  671. firm = companies(:first_firm)
  672. firm.clients_of_firm.clear
  673. assert_equal 0, firm.clients_of_firm.size
  674. assert_equal 0, firm.clients_of_firm(true).size
  675. end
  676. def test_deleting_a_item_which_is_not_in_the_collection
  677. force_signal37_to_load_all_clients_of_firm
  678. summit = Client.find_by_name('Summit')
  679. companies(:first_firm).clients_of_firm.delete(summit)
  680. assert_equal 1, companies(:first_firm).clients_of_firm.size
  681. assert_equal 1, companies(:first_firm).clients_of_firm(true).size
  682. assert_equal 2, summit.client_of
  683. end
  684. def test_deleting_type_mismatch
  685. david = Developer.find(1)
  686. david.projects.reload
  687. assert_raises(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(1) }
  688. end
  689. def test_deleting_self_type_mismatch
  690. david = Developer.find(1)
  691. david.projects.reload
  692. assert_raises(ActiveRecord::AssociationTypeMismatch) { david.projects.delete(Project.find(1).developers) }
  693. end
  694. def test_destroy_all
  695. force_signal37_to_load_all_clients_of_firm
  696. assert !companies(:first_firm).clients_of_firm.empty?, "37signals has clients after load"
  697. companies(:first_firm).clients_of_firm.destroy_all
  698. assert companies(:first_firm).clients_of_firm.empty?, "37signals has no clients after destroy all"
  699. assert companies(:first_firm).clients_of_firm(true).empty?, "37signals has no clients after destroy all and refresh"
  700. end
  701. def test_dependence
  702. firm = companies(:first_firm)
  703. assert_equal 2, firm.clients.size
  704. firm.destroy
  705. assert Client.find(:all, :conditions => "firm_id=#{firm.id}").empty?
  706. end
  707. def test_destroy_dependent_when_deleted_from_association
  708. firm = Firm.find(:first)
  709. assert_equal 2, firm.clients.size
  710. client = firm.clients.first
  711. firm.clients.delete(client)
  712. assert_raise(ActiveRecord::RecordNotFound) { Client.find(client.id) }
  713. assert_raise(ActiveRecord::RecordNotFound) { firm.clients.find(client.id) }
  714. assert_equal 1, firm.clients.size
  715. end
  716. def test_three_levels_of_dependence
  717. topic = Topic.create "title" => "neat and simple"
  718. reply = topic.replies.create "title" => "neat and simple", "content" => "still digging it"
  719. silly_reply = reply.replies.create "title" => "neat and simple", "content" => "ain't complaining"
  720. assert_nothing_raised { topic.destroy }
  721. end
  722. uses_transaction :test_dependence_with_transaction_support_on_failure
  723. def test_dependence_with_transaction_support_on_failure
  724. firm = companies(:first_firm)
  725. clients = firm.clients
  726. assert_equal 2, clients.length
  727. clients.last.instance_eval { def before_destroy() raise "Trigger rollback" end }
  728. firm.destroy rescue "do nothing"
  729. assert_equal 2, Client.find(:all, :conditions => "firm_id=#{firm.id}").size
  730. end
  731. def test_dependence_on_account
  732. num_accounts = Account.count
  733. companies(:first_firm).destroy
  734. assert_equal num_accounts - 1, Account.count
  735. end
  736. def test_depends_and_nullify
  737. num_accounts = Account.count
  738. num_companies = Company.count
  739. core = companies(:rails_core)
  740. assert_equal accounts(:rails_core_account), core.account
  741. assert_equal [companies(:leetsoft), companies(:jadedpixel)], core.companies
  742. core.destroy
  743. assert_nil accounts(:rails_core_account).reload.firm_id
  744. assert_nil companies(:leetsoft).reload.client_of
  745. assert_nil companies(:jadedpixel).reload.client_of
  746. assert_equal num_accounts, Account.count
  747. end
  748. def test_included_in_collection
  749. assert companies(:first_firm).clients.include?(Client.find(2))
  750. end
  751. def test_adding_array_and_collection
  752. assert_nothing_raised { Firm.find(:first).clients + Firm.find(:all).last.clients }
  753. end
  754. def test_find_all_without_conditions
  755. firm = companies(:first_firm)
  756. assert_equal 2, firm.clients.find(:all).length
  757. end
  758. def test_replace_with_less
  759. firm = Firm.find(:first)
  760. firm.clients = [companies(:first_client)]
  761. assert firm.save, "Could not save firm"
  762. firm.reload
  763. assert_equal 1, firm.clients.length
  764. end
  765. def test_replace_with_new
  766. firm = Firm.find(:first)
  767. new_client = Client.new("name" => "New Client")
  768. firm.clients = [companies(:second_client),new_client]
  769. firm.save
  770. firm.reload
  771. assert_equal 2, firm.clients.length
  772. assert !firm.clients.include?(:first_client)
  773. end
  774. def test_replace_on_new_object
  775. firm = Firm.new("name" => "New Firm")
  776. firm.clients = [companies(:second_client), Client.new("name" => "New Client")]
  777. assert firm.save
  778. firm.reload
  779. assert_equal 2, firm.clients.length
  780. assert firm.clients.include?(Client.find_by_name("New Client"))
  781. end
  782. def test_get_ids
  783. assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids
  784. end
  785. def test_assign_ids
  786. firm = Firm.new("name" => "Apple")
  787. firm.client_ids = [companies(:first_client).id, companies(:second_client).id]
  788. firm.save
  789. firm.reload
  790. assert_equal 2, firm.clients.length
  791. assert firm.clients.include?(companies(:second_client))
  792. end
  793. def test_assign_ids_ignoring_blanks
  794. firm = Firm.new("name" => "Apple")
  795. firm.client_ids = [companies(:first_client).id, nil, companies(:second_client).id, '']
  796. firm.save
  797. firm.reload
  798. assert_equal 2, firm.clients.length
  799. assert firm.clients.include?(companies(:second_client))
  800. end
  801. end
  802. class BelongsToAssociationsTest < Test::Unit::TestCase
  803. fixtures :accounts, :companies, :developers, :projects, :topics,
  804. :developers_projects, :computers, :authors, :posts
  805. def test_belongs_to
  806. Client.find(3).firm.name
  807. assert_equal companies(:first_firm).name, Client.find(3).firm.name
  808. assert !Client.find(3).firm.nil?, "Microsoft should have a firm"
  809. end
  810. def test_proxy_assignment
  811. account = Account.find(1)
  812. assert_nothing_raised { account.firm = account.firm }
  813. end
  814. def test_triple_equality
  815. assert Client.find(3).firm === Firm
  816. assert Firm === Client.find(3).firm
  817. end
  818. def test_type_mismatch
  819. assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 }
  820. assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) }
  821. end
  822. def test_natural_assignment
  823. apple = Firm.create("name" => "Apple")
  824. citibank = Account.create("credit_limit" => 10)
  825. citibank.firm = apple
  826. assert_equal apple.id, citibank.firm_id
  827. end
  828. def test_creating_the_belonging_object
  829. citibank = Account.create("credit_limit" => 10)
  830. apple = citibank.create_firm("name" => "Apple")
  831. assert_equal apple, citibank.firm
  832. citibank.save
  833. citibank.reload
  834. assert_equal apple, citibank.firm
  835. end
  836. def test_building_the_belonging_object
  837. citibank = Account.create("credit_limit" => 10)
  838. apple = citibank.build_firm("name" => "Apple")
  839. citibank.save
  840. assert_equal apple.id, citibank.firm_id
  841. end
  842. def test_natural_assignment_to_nil
  843. client = Client.find(3)
  844. client.firm = nil
  845. client.save
  846. assert_nil client.firm(true)
  847. assert_nil client.client_of
  848. end
  849. def test_with_different_class_name
  850. assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
  851. assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm"
  852. end
  853. def test_with_condition
  854. assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
  855. assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
  856. end
  857. def test_belongs_to_counter
  858. debate = Topic.create("title" => "debate")
  859. assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
  860. trash = debate.replies.create("title" => "blah!", "content" => "world around!")
  861. assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
  862. trash.destroy
  863. assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
  864. end
  865. def test_belongs_to_counter_with_reassigning
  866. t1 = Topic.create("title" => "t1")
  867. t2 = Topic.create("title" => "t2")
  868. r1 = Reply.new("title" => "r1", "content" => "r1")
  869. r1.topic = t1
  870. assert r1.save
  871. assert_equal 1, Topic.find(t1.id).replies.size
  872. assert_equal 0, Topic.find(t2.id).replies.size
  873. r1.topic = Topic.find(t2.id)
  874. assert r1.save
  875. assert_equal 0, Topic.find(t1.id).replies.size
  876. assert_equal 1, Topic.find(t2.id).replies.size
  877. r1.topic = nil
  878. assert_equal 0, Topic.find(t1.id).replies.size
  879. assert_equal 0, Topic.find(t2.id).replies.size
  880. r1.topic = t1
  881. assert_equal 1, Topic.find(t1.id).replies.size
  882. assert_equal 0, Topic.find(t2.id).replies.size
  883. r1.destroy
  884. assert_equal 0, Topic.find(t1.id).replies.size
  885. assert_equal 0, Topic.find(t2.id).replies.size
  886. end
  887. def test_assignment_before_parent_saved
  888. client = Client.find(:first)
  889. apple = Firm.new("name" => "Apple")
  890. client.firm = apple
  891. assert_equal apple, client.firm
  892. assert apple.new_record?
  893. assert client.save
  894. assert apple.save
  895. assert !apple.new_record?
  896. assert_equal apple, client.firm
  897. assert_equal apple, client.firm(true)
  898. end
  899. def test_assignment_before_child_saved
  900. final_cut = Client.new("name" => "Final Cut")
  901. firm = Firm.find(1)
  902. final_cut.firm = firm
  903. assert final_cut.new_record?
  904. assert final_cut.save
  905. assert !final_cut.new_record?
  906. assert !firm.new_record?
  907. assert_equal firm, final_cut.firm
  908. assert_equal firm, final_cut.firm(true)
  909. end
  910. def test_assignment_before_either_saved
  911. final_cut = Client.new("name" => "Final Cut")
  912. apple = Firm.new("name" => "Apple")
  913. final_cut.firm = apple
  914. assert final_cut.new_record?
  915. assert apple.new_record?
  916. assert final_cut.save
  917. assert !final_cut.new_record?
  918. assert !apple.new_record?
  919. assert_equal apple, final_cut.firm
  920. assert_equal apple, final_cut.firm(true)
  921. end
  922. def test_new_record_with_foreign_key_but_no_object
  923. c = Client.new("firm_id" => 1)
  924. assert_equal Firm.find(:first), c.firm_with_basic_id
  925. end
  926. def test_forgetting_the_load_when_foreign_key_enters_late
  927. c = Client.new
  928. assert_nil c.firm_with_basic_id
  929. c.firm_id = 1
  930. assert_equal Firm.find(:first), c.firm_with_basic_id
  931. end
  932. def test_field_name_same_as_foreign_key
  933. computer = Computer.find(1)
  934. assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
  935. end
  936. def test_counter_cache
  937. topic = Topic.create :title => "Zoom-zoom-zoom"
  938. assert_equal 0, topic[:replies_count]
  939. reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
  940. reply.topic = topic
  941. assert_equal 1, topic.reload[:replies_count]
  942. assert_equal 1, topic.replies.size
  943. topic[:replies_count] = 15
  944. assert_equal 15, topic.replies.size
  945. end
  946. def test_custom_counter_cache
  947. reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
  948. assert_equal 0, reply[:replies_count]
  949. silly = SillyReply.create(:title => "gaga", :content => "boo-boo")
  950. silly.reply = reply
  951. assert_equal 1, reply.reload[:replies_count]
  952. assert_equal 1, reply.replies.size
  953. reply[:replies_count] = 17
  954. assert_equal 17, reply.replies.size
  955. end
  956. def test_store_two_association_with_one_save
  957. num_orders = Order.count
  958. num_customers = Customer.count
  959. order = Order.new
  960. customer1 = order.billing = Customer.new
  961. customer2 = order.shipping = Customer.new
  962. assert order.save
  963. assert_equal customer1, order.billing
  964. assert_equal customer2, order.shipping
  965. order.reload
  966. assert_equal customer1, order.billing
  967. assert_equal customer2, order.shipping
  968. assert_equal num_orders +1, Order.count
  969. assert_equal num_customers +2, Customer.count
  970. end
  971. def test_store_association_in_two_relations_with_one_save
  972. num_orders = Order.count
  973. num_customers = Customer.count
  974. order = Order.new
  975. customer = order.billing = order.shipping = Customer.new
  976. assert order.save
  977. assert_equal customer, order.billing
  978. assert_equal customer, order.shipping
  979. order.reload
  980. assert_equal customer, order.billing
  981. assert_equal customer, order.shipping
  982. assert_equal num_orders +1, Order.count
  983. assert_equal num_customers +1, Customer.count
  984. end
  985. def test_store_association_in_two_relations_with_one_save_in_existing_object
  986. num_orders = Order.count
  987. num_customers = Customer.count
  988. order = Order.create
  989. customer = order.billing = order.shipping = Customer.new
  990. assert order.save
  991. assert_equal customer, order.billing
  992. assert_equal customer, order.shipping
  993. order.reload
  994. assert_equal customer, order.billing
  995. assert_equal customer, order.shipping
  996. assert_equal num_orders +1, Order.count
  997. assert_equal num_customers +1, Customer.count
  998. end
  999. def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
  1000. num_orders = Order.count
  1001. num_customers = Customer.count
  1002. order = Order.create
  1003. customer = order.billing = order.shipping = Customer.new
  1004. assert order.save
  1005. assert_equal customer, order.billing
  1006. assert_equal customer, order.shipping
  1007. order.reload
  1008. customer = order.billing = order.shipping = Customer.new
  1009. assert order.save
  1010. order.reload
  1011. assert_equal customer, order.billing
  1012. assert_equal customer, order.shipping
  1013. assert_equal num_orders +1, Order.count
  1014. assert_equal num_customers +2, Customer.count
  1015. end
  1016. def test_association_assignment_sticks
  1017. post = Post.find(:first)
  1018. author1, author2 = Author.find(:all, :limit => 2)
  1019. assert_not_nil author1
  1020. assert_not_nil author2
  1021. # make sure the association is loaded
  1022. post.author
  1023. # set the association by id, directly
  1024. post.author_id = author2.id
  1025. # save and reload
  1026. post.save!
  1027. post.reload
  1028. # the author id of the post should be the id we set
  1029. assert_equal post.author_id, author2.id
  1030. end
  1031. end
  1032. class ProjectWithAfterCreateHook < ActiveRecord::Base
  1033. set_table_name 'projects'
  1034. has_and_belongs_to_many :developers,
  1035. :class_name => "DeveloperForProjectWithAfterCreateHook",
  1036. :join_table => "developers_projects",
  1037. :foreign_key => "project_id",
  1038. :association_foreign_key => "developer_id"
  1039. after_create :add_david
  1040. def add_david
  1041. david = DeveloperForProjectWithAfterCreateHook.find_by_name('David')
  1042. david.projects << self
  1043. end
  1044. end
  1045. class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
  1046. set_table_name 'developers'
  1047. has_and_belongs_to_many :projects,
  1048. :class_name => "ProjectWithAfterCreateHook",
  1049. :join_table => "developers_projects",
  1050. :association_foreign_key => "project_id",
  1051. :foreign_key => "developer_id"
  1052. end
  1053. class HasAndBelongsToManyAssociationsTest < Test::Unit::TestCase
  1054. fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects
  1055. def test_has_and_belongs_to_many
  1056. david = Developer.find(1)
  1057. assert !david.projects.empty?
  1058. assert_equal 2, david.projects.size
  1059. active_record = Project.find(1)
  1060. assert !active_record.developers.empty?
  1061. assert_equal 3, active_record.developers.size
  1062. assert active_record.developers.include?(david)
  1063. end
  1064. def test_triple_equality
  1065. assert !(Array === Developer.find(1).projects)
  1066. assert Developer.find(1).projects === Array
  1067. end
  1068. def test_adding_single
  1069. jamis = Developer.find(2)
  1070. jamis.projects.reload # causing the collection to load
  1071. action_controller = Project.find(2)
  1072. assert_equal 1, jamis.projects.size
  1073. assert_equal 1, action_controller.developers.size
  1074. jamis.projects << action_controller
  1075. assert_equal 2, jamis.projects.size
  1076. assert_equal 2, jamis.projects(true).size
  1077. assert_equal 2, action_controller.developers(true).size
  1078. end
  1079. def test_adding_type_mismatch
  1080. jamis = Developer.find(2)
  1081. assert_raise(ActiveRecord::AssociationTypeMismatch) { jamis.projects << nil }
  1082. assert_raise(ActiveRecord::AssociationTypeMismatch) { jamis.projects << 1 }
  1083. end
  1084. def test_adding_from_the_project
  1085. jamis = Developer.find(2)
  1086. action_controller = Project.find(2)
  1087. action_controller.developers.reload
  1088. assert_equal 1, jamis.projects.size
  1089. assert_equal 1, action_controller.developers.size
  1090. action_controller.developers << jamis
  1091. assert_equal 2, jamis.projects(true).size
  1092. assert_equal 2, action_controller.developers.size
  1093. assert_equal 2, action_controller.developers(true).size
  1094. end
  1095. def test_adding_from_the_project_fixed_timestamp
  1096. jamis = Developer.find(2)
  1097. action_controller = Project.find(2)
  1098. action_controller.developers.reload
  1099. assert_equal 1, jamis.projects.size
  1100. assert_equal 1, action_controller.developers.size
  1101. updated_at = jamis.updated_at
  1102. action_controller.developers << jamis
  1103. assert_equal updated_at, jamis.updated_at
  1104. assert_equal 2, jamis.projects(true).size
  1105. assert_equal 2, action_controller.developers.size
  1106. assert_equal 2, action_controller.developers(true).size
  1107. end
  1108. def test_adding_multiple
  1109. aredridel = Developer.new("name" => "Aredridel")
  1110. aredridel.save
  1111. aredridel.projects.reload
  1112. aredridel.projects.push(Project.find(1), Project.find(2))
  1113. assert_equal 2, aredridel.projects.size
  1114. assert_equal 2, aredridel.projects(true).size
  1115. end
  1116. def test_adding_a_collection
  1117. aredridel = Developer.new("name" => "Aredridel")
  1118. aredridel.save
  1119. aredridel.projects.reload
  1120. aredridel.projects.concat([Project.find(1), Project.find(2)])
  1121. assert_equal 2, aredridel.projects.size
  1122. assert_equal 2, aredridel.projects(true).size
  1123. end
  1124. def test_adding_uses_default_values_on_join_table
  1125. ac = projects(:action_controller)
  1126. assert !developers(:jamis).projects.include?(ac)
  1127. developers(:jamis).projects << ac
  1128. assert developers(:jamis, :reload).projects.include?(ac)
  1129. project = developers(:jamis).projects.detect { |p| p == ac }
  1130. assert_equal 1, project.access_level.to_i
  1131. end
  1132. def test_adding_uses_explicit_values_on_join_table
  1133. ac = projects(:action_controller)
  1134. assert !developers(:jamis).projects.include?(ac)
  1135. assert_deprecated do
  1136. developers(:jamis).projects.push_with_attributes(ac, :access_level => 3)
  1137. end
  1138. assert developers(:jamis, :reload).projects.include?(ac)
  1139. project = developers(:jamis).projects.detect { |p| p == ac }
  1140. assert_equal 3, project.access_level.to_i
  1141. end
  1142. def test_hatbm_attribute_access_and_respond_to
  1143. project = developers(:jamis).projects[0]
  1144. assert project.has_attribute?("name")
  1145. assert project.has_attribute?("joined_on")
  1146. assert project.has_attribute?("access_level")
  1147. assert project.respond_to?("name")
  1148. assert project.respond_to?("name=")
  1149. assert project.respond_to?("name?")
  1150. assert project.respond_to?("joined_on")
  1151. assert project.respond_to?("joined_on=")
  1152. assert project.respond_to?("joined_on?")
  1153. assert project.respond_to?("access_level")
  1154. assert project.respond_to?("access_level=")
  1155. assert project.respond_to?("access_level?")
  1156. end
  1157. def test_habtm_adding_before_save
  1158. no_of_devels = Developer.count
  1159. no_of_projects = Project.count
  1160. aredridel = Developer.new("name" => "Aredridel")
  1161. aredridel.projects.concat([Project.find(1), p = Project.new("name" => "Projekt")])
  1162. assert aredridel.new_record?
  1163. assert p.new_record?
  1164. assert aredridel.save
  1165. assert !aredridel.new_record?
  1166. assert_equal no_of_devels+1, Developer.count
  1167. assert_equal no_of_projects+1, Project.count
  1168. assert_equal 2, aredridel.projects.size
  1169. assert_equal 2, aredridel.projects(true).size
  1170. end
  1171. def test_habtm_adding_before_save_with_join_attributes
  1172. no_of_devels = Developer.count
  1173. no_of_projects = Project.count
  1174. now = Date.today
  1175. ken = Developer.new("name" => "Ken")
  1176. assert_deprecated do
  1177. ken.projects.push_with_attributes( Project.find(1), :joined_on => now )
  1178. end
  1179. p = Project.new("name" => "Foomatic")
  1180. assert_deprecated do
  1181. ken.projects.push_with_attributes( p, :joined_on => now )
  1182. end
  1183. assert ken.new_record?
  1184. assert p.new_record?
  1185. assert ken.save
  1186. assert !ken.new_record?
  1187. assert_equal no_of_devels+1, Developer.count
  1188. assert_equal no_of_projects+1, Project.count
  1189. assert_equal 2, ken.projects.size
  1190. assert_equal 2, ken.projects(true).size
  1191. kenReloaded = Developer.find_by_name 'Ken'
  1192. kenReloaded.projects.each {|prj| assert_date_from_db(now, prj.joined_on)}
  1193. end
  1194. def test_habtm_saving_multiple_relationships
  1195. new_project = Project.new("name" => "Grimetime")
  1196. amount_of_developers = 4
  1197. developers = (0...amount_of_developers).collect {|i| Developer.create(:name => "JME #{i}") }.reverse
  1198. new_project.developer_ids = [developers[0].id, developers[1].id]
  1199. new_project.developers_with_callback_ids = [developers[2].id, developers[3].id]
  1200. assert new_project.save
  1201. new_project.reload
  1202. assert_equal amount_of_developers, new_project.developers.size
  1203. assert_equal developers, new_project.developers
  1204. end
  1205. def test_habtm_unique_order_preserved
  1206. assert_equal [developers(:poor_jamis), developers(:jamis), developers(:david)], projects(:active_record).non_unique_developers
  1207. assert_equal [developers(:poor_jamis), developers(:jamis), developers(:david)], projects(:active_record).developers
  1208. end
  1209. def test_build
  1210. devel = Developer.find(1)
  1211. proj = devel.projects.build("name" => "Projekt")
  1212. assert_equal devel.projects.last, proj
  1213. assert proj.new_record?
  1214. devel.save
  1215. assert !proj.new_record?
  1216. assert_equal devel.projects.last, proj
  1217. assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated
  1218. end
  1219. def test_build_by_new_record

Large files files are truncated, but you can click here to view the full file