0
+require 'models/developer'
0
+require 'models/project'
0
+require 'models/company'
0
+require 'models/computer'
0
+require 'models/customer'
0
+require 'models/author'
0
+require 'models/tagging'
0
+require 'models/comment'
0
+class BelongsToAssociationsTest < ActiveRecord::TestCase
0
+ fixtures :accounts, :companies, :developers, :projects, :topics,
0
+ :developers_projects, :computers, :authors, :posts, :tags, :taggings, :comments
0
+ Client.find(3).firm.name
0
+ assert_equal companies(:first_firm).name, Client.find(3).firm.name
0
+ assert !Client.find(3).firm.nil?, "Microsoft should have a firm"
0
+ def test_proxy_assignment
0
+ account = Account.find(1)
0
+ assert_nothing_raised { account.firm = account.firm }
0
+ def test_triple_equality
0
+ assert Client.find(3).firm === Firm
0
+ assert Firm === Client.find(3).firm
0
+ def test_type_mismatch
0
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = 1 }
0
+ assert_raise(ActiveRecord::AssociationTypeMismatch) { Account.find(1).firm = Project.find(1) }
0
+ def test_natural_assignment
0
+ apple = Firm.create("name" => "Apple")
0
+ citibank = Account.create("credit_limit" => 10)
0
+ assert_equal apple.id, citibank.firm_id
0
+ def test_no_unexpected_aliasing
0
+ first_firm = companies(:first_firm)
0
+ another_firm = companies(:another_firm)
0
+ citibank = Account.create("credit_limit" => 10)
0
+ citibank.firm = first_firm
0
+ original_proxy = citibank.firm
0
+ citibank.firm = another_firm
0
+ assert_equal first_firm.object_id, original_proxy.object_id
0
+ assert_equal another_firm.object_id, citibank.firm.object_id
0
+ def test_creating_the_belonging_object
0
+ citibank = Account.create("credit_limit" => 10)
0
+ apple = citibank.create_firm("name" => "Apple")
0
+ assert_equal apple, citibank.firm
0
+ assert_equal apple, citibank.firm
0
+ def test_building_the_belonging_object
0
+ citibank = Account.create("credit_limit" => 10)
0
+ apple = citibank.build_firm("name" => "Apple")
0
+ assert_equal apple.id, citibank.firm_id
0
+ def test_natural_assignment_to_nil
0
+ client = Client.find(3)
0
+ assert_nil client.firm(true)
0
+ assert_nil client.client_of
0
+ def test_with_different_class_name
0
+ assert_equal Company.find(1).name, Company.find(3).firm_with_other_name.name
0
+ assert_not_nil Company.find(3).firm_with_other_name, "Microsoft should have a firm"
0
+ def test_with_condition
0
+ assert_equal Company.find(1).name, Company.find(3).firm_with_condition.name
0
+ assert_not_nil Company.find(3).firm_with_condition, "Microsoft should have a firm"
0
+ def test_belongs_to_counter
0
+ debate = Topic.create("title" => "debate")
0
+ assert_equal 0, debate.send(:read_attribute, "replies_count"), "No replies yet"
0
+ trash = debate.replies.create("title" => "blah!", "content" => "world around!")
0
+ assert_equal 1, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply created"
0
+ assert_equal 0, Topic.find(debate.id).send(:read_attribute, "replies_count"), "First reply deleted"
0
+ def test_belongs_to_counter_with_assigning_nil
0
+ assert_equal p.id, c.post_id
0
+ assert_equal 2, Post.find(p.id).comments.size
0
+ assert_equal 1, Post.find(p.id).comments.size
0
+ def test_belongs_to_counter_with_reassigning
0
+ t1 = Topic.create("title" => "t1")
0
+ t2 = Topic.create("title" => "t2")
0
+ r1 = Reply.new("title" => "r1", "content" => "r1")
0
+ assert_equal 1, Topic.find(t1.id).replies.size
0
+ assert_equal 0, Topic.find(t2.id).replies.size
0
+ r1.topic = Topic.find(t2.id)
0
+ assert_equal 0, Topic.find(t1.id).replies.size
0
+ assert_equal 1, Topic.find(t2.id).replies.size
0
+ assert_equal 0, Topic.find(t1.id).replies.size
0
+ assert_equal 0, Topic.find(t2.id).replies.size
0
+ assert_equal 1, Topic.find(t1.id).replies.size
0
+ assert_equal 0, Topic.find(t2.id).replies.size
0
+ assert_equal 0, Topic.find(t1.id).replies.size
0
+ assert_equal 0, Topic.find(t2.id).replies.size
0
+ def test_belongs_to_counter_after_save
0
+ topic = Topic.create!(:title => "monday night")
0
+ topic.replies.create!(:title => "re: monday night", :content => "football")
0
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
0
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
0
+ def test_belongs_to_counter_after_update_attributes
0
+ topic = Topic.create!(:title => "37s")
0
+ topic.replies.create!(:title => "re: 37s", :content => "rails")
0
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
0
+ topic.update_attributes(:title => "37signals")
0
+ assert_equal 1, Topic.find(topic.id)[:replies_count]
0
+ def test_belongs_to_counter_after_save
0
+ topic = Topic.create("title" => "monday night")
0
+ topic.replies.create("title" => "re: monday night", "content" => "football")
0
+ assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
0
+ assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
0
+ def test_belongs_to_counter_after_update_attributes
0
+ topic = Topic.create("title" => "37s")
0
+ topic.replies.create("title" => "re: 37s", "content" => "rails")
0
+ assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
0
+ topic.update_attributes("title" => "37signals")
0
+ assert_equal 1, Topic.find(topic.id).send(:read_attribute, "replies_count")
0
+ def test_assignment_before_parent_saved
0
+ client = Client.find(:first)
0
+ apple = Firm.new("name" => "Apple")
0
+ assert_equal apple, client.firm
0
+ assert apple.new_record?
0
+ assert !apple.new_record?
0
+ assert_equal apple, client.firm
0
+ assert_equal apple, client.firm(true)
0
+ def test_assignment_before_child_saved
0
+ final_cut = Client.new("name" => "Final Cut")
0
+ assert final_cut.new_record?
0
+ assert !final_cut.new_record?
0
+ assert !firm.new_record?
0
+ assert_equal firm, final_cut.firm
0
+ assert_equal firm, final_cut.firm(true)
0
+ def test_assignment_before_either_saved
0
+ final_cut = Client.new("name" => "Final Cut")
0
+ apple = Firm.new("name" => "Apple")
0
+ final_cut.firm = apple
0
+ assert final_cut.new_record?
0
+ assert apple.new_record?
0
+ assert !final_cut.new_record?
0
+ assert !apple.new_record?
0
+ assert_equal apple, final_cut.firm
0
+ assert_equal apple, final_cut.firm(true)
0
+ def test_new_record_with_foreign_key_but_no_object
0
+ c = Client.new("firm_id" => 1)
0
+ assert_equal Firm.find(:first), c.firm_with_basic_id
0
+ def test_forgetting_the_load_when_foreign_key_enters_late
0
+ assert_nil c.firm_with_basic_id
0
+ assert_equal Firm.find(:first), c.firm_with_basic_id
0
+ def test_field_name_same_as_foreign_key
0
+ computer = Computer.find(1)
0
+ assert_not_nil computer.developer, ":foreign key == attribute didn't lock up" # '
0
+ def test_counter_cache
0
+ topic = Topic.create :title => "Zoom-zoom-zoom"
0
+ assert_equal 0, topic[:replies_count]
0
+ reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
0
+ assert_equal 1, topic.reload[:replies_count]
0
+ assert_equal 1, topic.replies.size
0
+ topic[:replies_count] = 15
0
+ assert_equal 15, topic.replies.size
0
+ def test_custom_counter_cache
0
+ reply = Reply.create(:title => "re: zoom", :content => "speedy quick!")
0
+ assert_equal 0, reply[:replies_count]
0
+ silly = SillyReply.create(:title => "gaga", :content => "boo-boo")
0
+ assert_equal 1, reply.reload[:replies_count]
0
+ assert_equal 1, reply.replies.size
0
+ reply[:replies_count] = 17
0
+ assert_equal 17, reply.replies.size
0
+ def test_store_two_association_with_one_save
0
+ num_orders = Order.count
0
+ num_customers = Customer.count
0
+ customer1 = order.billing = Customer.new
0
+ customer2 = order.shipping = Customer.new
0
+ assert_equal customer1, order.billing
0
+ assert_equal customer2, order.shipping
0
+ assert_equal customer1, order.billing
0
+ assert_equal customer2, order.shipping
0
+ assert_equal num_orders +1, Order.count
0
+ assert_equal num_customers +2, Customer.count
0
+ def test_store_association_in_two_relations_with_one_save
0
+ num_orders = Order.count
0
+ num_customers = Customer.count
0
+ customer = order.billing = order.shipping = Customer.new
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+ assert_equal num_orders +1, Order.count
0
+ assert_equal num_customers +1, Customer.count
0
+ def test_store_association_in_two_relations_with_one_save_in_existing_object
0
+ num_orders = Order.count
0
+ num_customers = Customer.count
0
+ customer = order.billing = order.shipping = Customer.new
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+ assert_equal num_orders +1, Order.count
0
+ assert_equal num_customers +1, Customer.count
0
+ def test_store_association_in_two_relations_with_one_save_in_existing_object_with_values
0
+ num_orders = Order.count
0
+ num_customers = Customer.count
0
+ customer = order.billing = order.shipping = Customer.new
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+ customer = order.billing = order.shipping = Customer.new
0
+ assert_equal customer, order.billing
0
+ assert_equal customer, order.shipping
0
+ assert_equal num_orders +1, Order.count
0
+ assert_equal num_customers +2, Customer.count
0
+ def test_association_assignment_sticks
0
+ post = Post.find(:first)
0
+ author1, author2 = Author.find(:all, :limit => 2)
0
+ assert_not_nil author1
0
+ assert_not_nil author2
0
+ # make sure the association is loaded
0
+ # set the association by id, directly
0
+ post.author_id = author2.id
0
+ # the author id of the post should be the id we set
0
+ assert_equal post.author_id, author2.id
0
+ def test_cant_save_readonly_association
0
+ assert_raise(ActiveRecord::ReadOnlyRecord) { companies(:first_client).readonly_firm.save! }
0
+ assert companies(:first_client).readonly_firm.readonly?