|
14 | 14 | require 'models/contract' |
15 | 15 | require 'models/company' |
16 | 16 | require 'models/developer' |
| 17 | +require 'models/subscriber' |
| 18 | +require 'models/book' |
| 19 | +require 'models/subscription' |
17 | 20 |
|
18 | 21 | class HasManyThroughAssociationsTest < ActiveRecord::TestCase |
19 | | - fixtures :posts, :readers, :people, :comments, :authors, :owners, :pets, :toys, :jobs, :references, :companies |
| 22 | + fixtures :posts, :readers, :people, :comments, :authors, |
| 23 | + :owners, :pets, :toys, :jobs, :references, :companies, |
| 24 | + :subscribers, :books, :subscriptions |
20 | 25 |
|
21 | 26 | # Dummies to force column loads so query counts are clean. |
22 | 27 | def setup |
@@ -383,4 +388,37 @@ def test_modifying_has_many_through_has_one_reflection_should_raise |
383 | 388 | lambda { authors(:david).very_special_comments.delete(authors(:david).very_special_comments.first) }, |
384 | 389 | ].each {|block| assert_raise(ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection, &block) } |
385 | 390 | end |
| 391 | + |
| 392 | + def test_collection_singular_ids_getter_with_string_primary_keys |
| 393 | + book = books(:awdr) |
| 394 | + assert_equal 2, book.subscriber_ids.size |
| 395 | + assert_equal [subscribers(:first).nick, subscribers(:second).nick].sort, book.subscriber_ids.sort |
| 396 | + end |
| 397 | + |
| 398 | + def test_collection_singular_ids_setter |
| 399 | + company = companies(:rails_core) |
| 400 | + dev = Developer.find(:first) |
| 401 | + |
| 402 | + company.developer_ids = [dev.id] |
| 403 | + assert_equal [dev], company.developers |
| 404 | + end |
| 405 | + |
| 406 | + def test_collection_singular_ids_setter_with_string_primary_keys |
| 407 | + assert_nothing_raised do |
| 408 | + book = books(:awdr) |
| 409 | + book.subscriber_ids = [subscribers(:second).nick] |
| 410 | + assert_equal [subscribers(:second)], book.subscribers(true) |
| 411 | + |
| 412 | + book.subscriber_ids = [] |
| 413 | + assert_equal [], book.subscribers(true) |
| 414 | + end |
| 415 | + |
| 416 | + end |
| 417 | + |
| 418 | + def test_collection_singular_ids_setter_raises_exception_when_invalid_ids_set |
| 419 | + company = companies(:rails_core) |
| 420 | + ids = [Developer.find(:first).id, -9999] |
| 421 | + assert_raises(ActiveRecord::RecordNotFound) {company.developer_ids= ids} |
| 422 | + end |
| 423 | + |
386 | 424 | end |
0 commit comments