Update to Rails 2.3.9 and itextomml 1.3.27

This commit is contained in:
Jacques Distler 2010-09-05 15:24:15 -05:00
parent 113e0af736
commit ef30cc22df
200 changed files with 3065 additions and 1204 deletions

View file

@ -65,15 +65,14 @@ class AdapterTest < ActiveRecord::TestCase
end
def test_not_specifying_database_name_for_cross_database_selects
assert_nothing_raised do
ActiveRecord::Base.establish_connection({
:adapter => 'mysql',
:username => 'rails'
})
ActiveRecord::Base.connection.execute "SELECT activerecord_unittest.pirates.*, activerecord_unittest2.courses.* FROM activerecord_unittest.pirates, activerecord_unittest2.courses"
begin
assert_nothing_raised do
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['arunit'].except(:database))
ActiveRecord::Base.connection.execute "SELECT activerecord_unittest.pirates.*, activerecord_unittest2.courses.* FROM activerecord_unittest.pirates, activerecord_unittest2.courses"
end
ensure
ActiveRecord::Base.establish_connection 'arunit'
end
ActiveRecord::Base.establish_connection 'arunit'
end
end

View file

@ -18,7 +18,7 @@ module Remembered
module ClassMethods
def remembered; @@remembered ||= []; end
def random_element; @@remembered.random_element; end
def sample; @@remembered.sample; end
end
end
@ -82,14 +82,14 @@ class EagerLoadPolyAssocsTest < ActiveRecord::TestCase
[Circle, Square, Triangle, NonPolyOne, NonPolyTwo].map(&:create!)
end
1.upto(NUM_SIMPLE_OBJS) do
PaintColor.create!(:non_poly_one_id => NonPolyOne.random_element.id)
PaintTexture.create!(:non_poly_two_id => NonPolyTwo.random_element.id)
PaintColor.create!(:non_poly_one_id => NonPolyOne.sample.id)
PaintTexture.create!(:non_poly_two_id => NonPolyTwo.sample.id)
end
1.upto(NUM_SHAPE_EXPRESSIONS) do
shape_type = [Circle, Square, Triangle].random_element
paint_type = [PaintColor, PaintTexture].random_element
ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.random_element.id,
:paint_type => paint_type.to_s, :paint_id => paint_type.random_element.id)
shape_type = [Circle, Square, Triangle].sample
paint_type = [PaintColor, PaintTexture].sample
ShapeExpression.create!(:shape_type => shape_type.to_s, :shape_id => shape_type.sample.id,
:paint_type => paint_type.to_s, :paint_id => paint_type.sample.id)
end
end

View file

@ -0,0 +1,19 @@
require 'cases/helper'
require 'models/tee'
require 'models/tie'
require 'models/polymorphic_design'
require 'models/polymorphic_price'
class EagerLoadNestedPolymorphicIncludeTest < ActiveRecord::TestCase
fixtures :tees, :ties, :polymorphic_designs, :polymorphic_prices
def test_eager_load_polymorphic_has_one_nested_under_polymorphic_belongs_to
designs = PolymorphicDesign.scoped(:include => {:designable => :polymorphic_price})
associated_price_ids = designs.map{|design| design.designable.polymorphic_price.id}
expected_price_ids = [1, 2, 3, 4]
assert expected_price_ids.all?{|expected_id| associated_price_ids.include?(expected_id)},
"Expected associated prices to be #{expected_price_ids.inspect} but they were #{associated_price_ids.sort.inspect}"
end
end

View file

@ -357,6 +357,13 @@ class EagerAssociationTest < ActiveRecord::TestCase
assert_equal comments(:more_greetings), Author.find(authors(:david).id, :include => :comments_with_order_and_conditions).comments_with_order_and_conditions.first
end
def test_eager_with_has_many_through_with_conditions_join_model_with_include
post_tags = Post.find(posts(:welcome).id).misc_tags
eager_post_tags = Post.find(1, :include => :misc_tags).misc_tags
assert_equal post_tags, eager_post_tags
end
def test_eager_with_has_many_through_join_model_with_include
author_comments = Author.find(authors(:david).id, :include => :comments_with_include).comments_with_include.to_a
assert_no_queries do

View file

@ -21,6 +21,68 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
Client.destroyed_client_ids.clear
end
def test_create_by
person = Person.create! :first_name => 'tenderlove'
post = Post.find :first
assert_equal [], person.readers
assert_nil person.readers.find_by_post_id(post.id)
reader = person.readers.create_by_post_id post.id
assert_equal 1, person.readers.count
assert_equal 1, person.readers.length
assert_equal post, person.readers.first.post
assert_equal person, person.readers.first.person
end
def test_create_by_multi
person = Person.create! :first_name => 'tenderlove'
post = Post.find :first
assert_equal [], person.readers
reader = person.readers.create_by_post_id_and_skimmer post.id, false
assert_equal 1, person.readers.count
assert_equal 1, person.readers.length
assert_equal post, person.readers.first.post
assert_equal person, person.readers.first.person
end
def test_find_or_create_by
person = Person.create! :first_name => 'tenderlove'
post = Post.find :first
assert_equal [], person.readers
assert_nil person.readers.find_by_post_id(post.id)
reader = person.readers.find_or_create_by_post_id post.id
assert_equal 1, person.readers.count
assert_equal 1, person.readers.length
assert_equal post, person.readers.first.post
assert_equal person, person.readers.first.person
end
def test_find_or_create
person = Person.create! :first_name => 'tenderlove'
post = Post.find :first
assert_equal [], person.readers
assert_nil person.readers.find(:first, :conditions => {
:post_id => post.id
})
reader = person.readers.find_or_create :post_id => post.id
assert_equal 1, person.readers.count
assert_equal 1, person.readers.length
assert_equal post, person.readers.first.post
assert_equal person, person.readers.first.person
end
def force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.each {|f| }
end
@ -486,7 +548,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
assert the_client.new_record?
end
def test_find_or_create
def test_find_or_create_updates_size
number_of_clients = companies(:first_firm).clients.size
the_client = companies(:first_firm).clients.find_or_create_by_name("Yet another client")
assert_equal number_of_clients + 1, companies(:first_firm, :reload).clients.size
@ -772,8 +834,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase
def test_destroy_all
force_signal37_to_load_all_clients_of_firm
assert !companies(:first_firm).clients_of_firm.empty?, "37signals has clients after load"
companies(:first_firm).clients_of_firm.destroy_all
clients = companies(:first_firm).clients_of_firm.to_a
assert !clients.empty?, "37signals has clients after load"
destroyed = companies(:first_firm).clients_of_firm.destroy_all
assert_equal clients.sort_by(&:id), destroyed.sort_by(&:id)
assert destroyed.all? { |client| client.frozen? }, "destroyed clients should be frozen"
assert companies(:first_firm).clients_of_firm.empty?, "37signals has no clients after destroy all"
assert companies(:first_firm).clients_of_firm(true).empty?, "37signals has no clients after destroy all and refresh"
end

View file

@ -18,6 +18,8 @@ require 'models/person'
require 'models/reader'
require 'models/parrot'
require 'models/pirate'
require 'models/ship'
require 'models/ship_part'
require 'models/treasure'
require 'models/price_estimate'
require 'models/club'
@ -29,6 +31,23 @@ class AssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects, :developers_projects,
:computers, :people, :readers
def test_loading_the_association_target_should_keep_child_records_marked_for_destruction
ship = Ship.create!(:name => "The good ship Dollypop")
part = ship.parts.create!(:name => "Mast")
part.mark_for_destruction
ship.parts.send(:load_target)
assert ship.parts[0].marked_for_destruction?
end
def test_loading_the_association_target_should_load_most_recent_attributes_for_child_records_marked_for_destruction
ship = Ship.create!(:name => "The good ship Dollypop")
part = ship.parts.create!(:name => "Mast")
part.mark_for_destruction
ShipPart.find(part.id).update_attribute(:name, 'Deck')
ship.parts.send(:load_target)
assert_equal 'Deck', ship.parts[0].name
end
def test_include_with_order_works
assert_nothing_raised {Account.find(:first, :order => 'id', :include => :firm)}
assert_nothing_raised {Account.find(:first, :order => :id, :include => :firm)}
@ -74,6 +93,16 @@ class AssociationsTest < ActiveRecord::TestCase
assert_queries(1) { assert_not_nil firm.clients(true).each {} }
end
end
def test_using_limitable_reflections_helper
using_limitable_reflections = lambda { |reflections| ActiveRecord::Base.send :using_limitable_reflections?, reflections }
belongs_to_reflections = [Tagging.reflect_on_association(:tag), Tagging.reflect_on_association(:super_tag)]
has_many_reflections = [Tag.reflect_on_association(:taggings), Developer.reflect_on_association(:projects)]
mixed_reflections = (belongs_to_reflections + has_many_reflections).uniq
assert using_limitable_reflections.call(belongs_to_reflections), "Belong to associations are limitable"
assert !using_limitable_reflections.call(has_many_reflections), "All has many style associations are not limitable"
assert !using_limitable_reflections.call(mixed_reflections), "No collection associations (has many style) should pass"
end
def test_storing_in_pstore
require "tmpdir"

View file

@ -1,6 +1,5 @@
require "cases/helper"
require 'models/post'
require 'models/author'
require 'models/event_author'
require 'models/topic'
require 'models/reply'
@ -588,17 +587,25 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_destroy_all
original_count = Topic.count
topics_by_mary = Topic.count(:conditions => mary = "author_name = 'Mary'")
conditions = "author_name = 'Mary'"
topics_by_mary = Topic.all(:conditions => conditions, :order => 'id')
assert ! topics_by_mary.empty?
Topic.destroy_all mary
assert_equal original_count - topics_by_mary, Topic.count
assert_difference('Topic.count', -topics_by_mary.size) do
destroyed = Topic.destroy_all(conditions).sort_by(&:id)
assert_equal topics_by_mary, destroyed
assert destroyed.all? { |topic| topic.frozen? }
end
end
def test_destroy_many
assert_equal 3, Client.count
Client.destroy([2, 3])
assert_equal 1, Client.count
clients = Client.find([2, 3], :order => 'id')
assert_difference('Client.count', -2) do
destroyed = Client.destroy([2, 3]).sort_by(&:id)
assert_equal clients, destroyed
assert destroyed.all? { |client| client.frozen? }
end
end
def test_delete_many
@ -612,55 +619,6 @@ class BasicsTest < ActiveRecord::TestCase
assert Topic.find(2).approved?
end
def test_increment_counter
Topic.increment_counter("replies_count", 1)
assert_equal 2, Topic.find(1).replies_count
Topic.increment_counter("replies_count", 1)
assert_equal 3, Topic.find(1).replies_count
end
def test_decrement_counter
Topic.decrement_counter("replies_count", 2)
assert_equal -1, Topic.find(2).replies_count
Topic.decrement_counter("replies_count", 2)
assert_equal -2, Topic.find(2).replies_count
end
def test_reset_counters
assert_equal 1, Topic.find(1).replies_count
Topic.increment_counter("replies_count", 1)
assert_equal 2, Topic.find(1).replies_count
Topic.reset_counters(1, :replies)
assert_equal 1, Topic.find(1).replies_count
end
def test_update_counter
category = categories(:general)
assert_nil category.categorizations_count
assert_equal 2, category.categorizations.count
Category.update_counters(category.id, "categorizations_count" => category.categorizations.count)
category.reload
assert_not_nil category.categorizations_count
assert_equal 2, category.categorizations_count
Category.update_counters(category.id, "categorizations_count" => category.categorizations.count)
category.reload
assert_not_nil category.categorizations_count
assert_equal 4, category.categorizations_count
category_2 = categories(:technology)
count_1, count_2 = (category.categorizations_count || 0), (category_2.categorizations_count || 0)
Category.update_counters([category.id, category_2.id], "categorizations_count" => 2)
category.reload; category_2.reload
assert_equal count_1 + 2, category.categorizations_count
assert_equal count_2 + 2, category_2.categorizations_count
end
def test_update_all
assert_equal Topic.count, Topic.update_all("content = 'bulk updated!'")
assert_equal "bulk updated!", Topic.find(1).content
@ -749,22 +707,24 @@ class BasicsTest < ActiveRecord::TestCase
end
def test_class_name
assert_equal "Firm", ActiveRecord::Base.class_name("firms")
assert_equal "Category", ActiveRecord::Base.class_name("categories")
assert_equal "AccountHolder", ActiveRecord::Base.class_name("account_holder")
ActiveSupport::Deprecation.silence do
assert_equal "Firm", ActiveRecord::Base.class_name("firms")
assert_equal "Category", ActiveRecord::Base.class_name("categories")
assert_equal "AccountHolder", ActiveRecord::Base.class_name("account_holder")
ActiveRecord::Base.pluralize_table_names = false
assert_equal "Firms", ActiveRecord::Base.class_name( "firms" )
ActiveRecord::Base.pluralize_table_names = true
ActiveRecord::Base.pluralize_table_names = false
assert_equal "Firms", ActiveRecord::Base.class_name( "firms" )
ActiveRecord::Base.pluralize_table_names = true
ActiveRecord::Base.table_name_prefix = "test_"
assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms" )
ActiveRecord::Base.table_name_suffix = "_tests"
assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms_tests" )
ActiveRecord::Base.table_name_prefix = ""
assert_equal "Firm", ActiveRecord::Base.class_name( "firms_tests" )
ActiveRecord::Base.table_name_suffix = ""
assert_equal "Firm", ActiveRecord::Base.class_name( "firms" )
ActiveRecord::Base.table_name_prefix = "test_"
assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms" )
ActiveRecord::Base.table_name_suffix = "_tests"
assert_equal "Firm", ActiveRecord::Base.class_name( "test_firms_tests" )
ActiveRecord::Base.table_name_prefix = ""
assert_equal "Firm", ActiveRecord::Base.class_name( "firms_tests" )
ActiveRecord::Base.table_name_suffix = ""
assert_equal "Firm", ActiveRecord::Base.class_name( "firms" )
end
end
def test_null_fields
@ -2090,7 +2050,7 @@ class BasicsTest < ActiveRecord::TestCase
def test_inspect_instance
topic = topics(:first)
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_s(:db)}", bonus_time: "#{topic.bonus_time.to_s(:db)}", last_read: "#{topic.last_read.to_s(:db)}", content: "Have a nice day", approved: false, replies_count: 1, parent_id: nil, parent_title: nil, type: nil>), topic.inspect
assert_equal %(#<Topic id: 1, title: "The First Topic", author_name: "David", author_email_address: "david@loudthinking.com", written_on: "#{topic.written_on.to_s(:db)}", bonus_time: "#{topic.bonus_time.to_s(:db)}", last_read: "#{topic.last_read.to_s(:db)}", content: "Have a nice day", approved: false, replies_count: 1, parent_id: nil, parent_title: nil, type: nil, group: nil>), topic.inspect
end
def test_inspect_new_instance

View file

@ -48,6 +48,7 @@ class MysqlConnectionTest < ActiveRecord::TestCase
def test_multi_results
rows = ActiveRecord::Base.connection.select_rows('CALL ten();')
assert_equal 10, rows[0][0].to_i, "ten() did not return 10 as expected: #{rows.inspect}"
assert @connection.active?, "Bad connection use by 'MysqlAdapter.select_rows'"
end
end

View file

@ -0,0 +1,84 @@
require 'cases/helper'
require 'models/topic'
require 'models/reply'
require 'models/category'
require 'models/categorization'
class CounterCacheTest < ActiveRecord::TestCase
fixtures :topics, :categories, :categorizations
class SpecialTopic < ::Topic
has_many :special_replies, :foreign_key => 'parent_id'
end
class SpecialReply < ::Reply
belongs_to :special_topic, :foreign_key => 'parent_id', :counter_cache => 'replies_count'
end
test "increment counter" do
topic = Topic.find(1)
assert_difference 'topic.reload.replies_count' do
Topic.increment_counter(:replies_count, topic.id)
end
end
test "decrement counter" do
topic = Topic.find(1)
assert_difference 'topic.reload.replies_count', -1 do
Topic.decrement_counter(:replies_count, topic.id)
end
end
test "reset counters" do
topic = Topic.find(1)
# throw the count off by 1
Topic.increment_counter(:replies_count, topic.id)
# check that it gets reset
assert_difference 'topic.reload.replies_count', -1 do
Topic.reset_counters(topic.id, :replies)
end
end
test "reset counters with string argument" do
topic = Topic.find(1)
Topic.increment_counter('replies_count', topic.id)
assert_difference 'topic.reload.replies_count', -1 do
Topic.reset_counters(topic.id, 'replies')
end
end
test "reset counters with modularized and camelized classnames" do
special = SpecialTopic.create!(:title => 'Special')
SpecialTopic.increment_counter(:replies_count, special.id)
assert_difference 'special.reload.replies_count', -1 do
SpecialTopic.reset_counters(special.id, :special_replies)
end
end
test "update counter with initial null value" do
category = categories(:general)
assert_equal 2, category.categorizations.count
assert_nil category.categorizations_count
Category.update_counters(category.id, :categorizations_count => category.categorizations.count)
assert_equal 2, category.reload.categorizations_count
end
test "update counter for decrement" do
topic = Topic.find(1)
assert_difference 'topic.reload.replies_count', -3 do
Topic.update_counters(topic.id, :replies_count => -3)
end
end
test "update counters of multiple records" do
t1, t2 = topics(:first, :second)
assert_difference ['t1.reload.replies_count', 't2.reload.replies_count'], 2 do
Topic.update_counters([t1.id, t2.id], :replies_count => 2)
end
end
end

View file

@ -53,7 +53,8 @@ class OptimisticLockingTest < ActiveRecord::TestCase
assert_raises(ActiveRecord::StaleObjectError) { p2.destroy }
assert p1.destroy
assert_equal true, p1.frozen?
assert p1.frozen?
assert p1.destroyed?
assert_raises(ActiveRecord::RecordNotFound) { Person.find(1) }
end

View file

@ -739,6 +739,10 @@ if ActiveRecord::Base.connection.supports_migrations?
ActiveRecord::Base.connection.drop_table(:hats)
end
def test_remove_column_no_second_parameter_raises_exception
assert_raise(ArgumentError) { Person.connection.remove_column("funny") }
end
def test_change_type_of_not_null_column
assert_nothing_raised do
Topic.connection.change_column "topics", "written_on", :datetime, :null => false

View file

@ -9,6 +9,11 @@ require 'models/developer'
class NamedScopeTest < ActiveRecord::TestCase
fixtures :posts, :authors, :topics, :comments, :author_addresses
def test_named_scope_with_STI
assert_equal 5,Post.with_type_self.count
assert_equal 1,SpecialPost.with_type_self.count
end
def test_implements_enumerable
assert !Topic.find(:all).empty?
@ -265,7 +270,7 @@ class NamedScopeTest < ActiveRecord::TestCase
end
def test_rand_should_select_a_random_object_from_proxy
assert Topic.approved.random_element.is_a?(Topic)
assert Topic.approved.sample.is_a?(Topic)
end
def test_should_use_where_in_query_for_named_scope

View file

@ -175,12 +175,6 @@ class TestNestedAttributesOnAHasOneAssociation < ActiveRecord::TestCase
assert_equal 'Davy Jones Gold Dagger', @pirate.ship.name
end
def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
assert_raise_with_message ActiveRecord::RecordNotFound, "Couldn't find Ship with ID=1234567890 for Pirate with ID=#{@pirate.id}" do
@pirate.ship_attributes = { :id => 1234567890 }
end
end
def test_should_take_a_hash_with_string_keys_and_update_the_associated_model
@pirate.reload.ship_attributes = { 'id' => @ship.id, 'name' => 'Davy Jones Gold Dagger' }
@ -330,10 +324,13 @@ class TestNestedAttributesOnABelongsToAssociation < ActiveRecord::TestCase
assert_equal 'Arr', @ship.pirate.catchphrase
end
def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
assert_raise_with_message ActiveRecord::RecordNotFound, "Couldn't find Pirate with ID=1234567890 for Ship with ID=#{@ship.id}" do
@ship.pirate_attributes = { :id => 1234567890 }
end
def test_should_associate_with_record_if_parent_record_is_not_saved
@ship.destroy
@pirate = Pirate.create(:catchphrase => 'Arr')
@ship = Ship.new(:name => 'Nights Dirty Lightning', :pirate_attributes => { :id => @pirate.id, :catchphrase => @pirate.catchphrase})
assert_equal @ship.name, 'Nights Dirty Lightning'
assert_equal @pirate, @ship.pirate
end
def test_should_take_a_hash_with_string_keys_and_update_the_associated_model
@ -437,6 +434,11 @@ module NestedAttributesOnACollectionAssociationTests
assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.reload.name, @child_2.reload.name]
end
def test_should_assign_existing_children_if_parent_is_new
@pirate = Pirate.new({:catchphrase => "Don' botharr talkin' like one, savvy?"}.merge(@alternate_params))
assert_equal ['Grace OMalley', 'Privateers Greed'], [@pirate.send(@association_name)[0].name, @pirate.send(@association_name)[1].name]
end
def test_should_take_an_array_and_assign_the_attributes_to_the_associated_models
@pirate.send(association_setter, @alternate_params[association_getter].values)
@pirate.save
@ -465,6 +467,33 @@ module NestedAttributesOnACollectionAssociationTests
assert_equal 'Grace OMalley', @child_1.reload.name
end
def test_should_not_overwrite_unsaved_updates_when_loading_association
@pirate.reload
@pirate.send(association_setter, [{ :id => @child_1.id, :name => 'Grace OMalley' }])
assert_equal 'Grace OMalley', @pirate.send(@association_name).send(:load_target).find { |r| r.id == @child_1.id }.name
end
def test_should_preserve_order_when_not_overwriting_unsaved_updates
@pirate.reload
@pirate.send(association_setter, [{ :id => @child_1.id, :name => 'Grace OMalley' }])
assert_equal @child_1.id, @pirate.send(@association_name).send(:load_target).first.id
end
def test_should_refresh_saved_records_when_not_overwriting_unsaved_updates
@pirate.reload
record = @pirate.class.reflect_on_association(@association_name).klass.new(:name => 'Grace OMalley')
@pirate.send(@association_name) << record
record.save!
@pirate.send(@association_name).last.update_attributes!(:name => 'Polly')
assert_equal 'Polly', @pirate.send(@association_name).send(:load_target).last.name
end
def test_should_not_remove_scheduled_destroys_when_loading_association
@pirate.reload
@pirate.send(association_setter, [{ :id => @child_1.id, :_destroy => '1' }])
assert @pirate.send(@association_name).send(:load_target).find { |r| r.id == @child_1.id }.marked_for_destruction?
end
def test_should_take_a_hash_with_composite_id_keys_and_assign_the_attributes_to_the_associated_models
@child_1.stubs(:id).returns('ABC1X')
@child_2.stubs(:id).returns('ABC2X')
@ -479,8 +508,8 @@ module NestedAttributesOnACollectionAssociationTests
assert_equal ['Grace OMalley', 'Privateers Greed'], [@child_1.name, @child_2.name]
end
def test_should_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
assert_raise_with_message ActiveRecord::RecordNotFound, "Couldn't find #{@child_1.class.name} with ID=1234567890 for Pirate with ID=#{@pirate.id}" do
def test_should_not_raise_RecordNotFound_if_an_id_is_given_but_doesnt_return_a_record
assert_nothing_raised ActiveRecord::RecordNotFound do
@pirate.attributes = { association_getter => [{ :id => 1234567890 }] }
end
end
@ -781,7 +810,13 @@ class TestHasManyAutosaveAssoictaionWhichItselfHasAutosaveAssociations < ActiveR
@part = @ship.parts.create!(:name => "Mast")
@trinket = @part.trinkets.create!(:name => "Necklace")
end
test "if association is not loaded and association record is saved and then in memory record attributes should be saved" do
@ship.parts_attributes=[{:id => @part.id,:name =>'Deck'}]
assert_equal 1, @ship.parts.proxy_target.size
assert_equal 'Deck', @ship.parts[0].name
end
test "when grandchild changed in memory, saving parent should save grandchild" do
@trinket.name = "changed"
@ship.save
@ -810,4 +845,4 @@ class TestHasManyAutosaveAssoictaionWhichItselfHasAutosaveAssociations < ActiveR
ShipPart.create!(:ship => @ship, :name => "Stern")
assert_no_queries { @ship.valid? }
end
end
end

View file

@ -24,25 +24,25 @@ class ReflectionTest < ActiveRecord::TestCase
def test_read_attribute_names
assert_equal(
%w( id title author_name author_email_address bonus_time written_on last_read content approved replies_count parent_id parent_title type ).sort,
%w( id title author_name author_email_address bonus_time written_on last_read content group approved replies_count parent_id parent_title type ).sort,
@first.attribute_names
)
end
def test_columns
assert_equal 13, Topic.columns.length
assert_equal 14, Topic.columns.length
end
def test_columns_are_returned_in_the_order_they_were_declared
column_names = Topic.columns.map { |column| column.name }
assert_equal %w(id title author_name author_email_address written_on bonus_time last_read content approved replies_count parent_id parent_title type), column_names
assert_equal %w(id title author_name author_email_address written_on bonus_time last_read content approved replies_count parent_id parent_title type group), column_names
end
def test_content_columns
content_columns = Topic.content_columns
content_column_names = content_columns.map {|column| column.name}
assert_equal 9, content_columns.length
assert_equal %w(title author_name author_email_address written_on bonus_time last_read content approved parent_title).sort, content_column_names.sort
assert_equal 10, content_columns.length
assert_equal %w(title author_name author_email_address written_on bonus_time last_read content group approved parent_title).sort, content_column_names.sort
end
def test_column_string_type_and_limit

View file

@ -0,0 +1,16 @@
require "cases/helper"
require 'models/topic'
require 'models/minimalistic'
class StoredProcedureTest < ActiveRecord::TestCase
fixtures :topics
# Test that MySQL allows multiple results for stored procedures
if Mysql.const_defined?(:CLIENT_MULTI_RESULTS)
def test_multi_results_from_find_by_sql
topics = Topic.find_by_sql 'CALL topics();'
assert_equal 1, topics.size
assert ActiveRecord::Base.connection.active?, "Bad connection use by 'MysqlAdapter.select'"
end
end
end

View file

@ -3,10 +3,12 @@ require 'models/topic'
require 'models/reply'
require 'models/developer'
require 'models/book'
require 'models/author'
require 'models/post'
class TransactionTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
fixtures :topics, :developers
fixtures :topics, :developers, :authors, :posts
def setup
@first, @second = Topic.find(1, 2).sort_by { |t| t.id }
@ -34,6 +36,25 @@ class TransactionTest < ActiveRecord::TestCase
end
end
def test_update_attributes_should_rollback_on_failure
author = Author.find(1)
posts_count = author.posts.size
assert posts_count > 0
status = author.update_attributes(:name => nil, :post_ids => [])
assert !status
assert_equal posts_count, author.posts(true).size
end
def test_update_attributes_should_rollback_on_failure!
author = Author.find(1)
posts_count = author.posts.size
assert posts_count > 0
assert_raise(ActiveRecord::RecordInvalid) do
author.update_attributes!(:name => nil, :post_ids => [])
end
assert_equal posts_count, author.posts(true).size
end
def test_successful_with_return
class << Topic.connection
alias :real_commit_db_transaction :commit_db_transaction

View file

@ -486,20 +486,20 @@ class ActiveRecordErrorI18nTests < ActiveSupport::TestCase
end
test ":default is only given to message if a symbol is supplied" do
store_translations(:errors => { :messages => { :"foo bar" => "You fooed: {{value}}." } })
store_translations(:errors => { :messages => { :"foo bar" => "You fooed: %{value}." } })
@reply.errors.add(:title, :inexistent, :default => "foo bar")
assert_equal "foo bar", @reply.errors[:title]
end
test "#generate_message passes the model attribute value for interpolation" do
store_translations(:errors => { :messages => { :foo => "You fooed: {{value}}." } })
store_translations(:errors => { :messages => { :foo => "You fooed: %{value}." } })
@reply.title = "da title"
assert_error_message 'You fooed: da title.', :title, :foo
end
test "#generate_message passes the human_name of the model for interpolation" do
store_translations(
:errors => { :messages => { :foo => "You fooed: {{model}}." } },
:errors => { :messages => { :foo => "You fooed: %{model}." } },
:models => { :topic => 'da topic' }
)
assert_error_message 'You fooed: da topic.', :title, :foo
@ -507,7 +507,7 @@ class ActiveRecordErrorI18nTests < ActiveSupport::TestCase
test "#generate_message passes the human_name of the attribute for interpolation" do
store_translations(
:errors => { :messages => { :foo => "You fooed: {{attribute}}." } },
:errors => { :messages => { :foo => "You fooed: %{attribute}." } },
:attributes => { :topic => { :title => 'da topic title' } }
)
assert_error_message 'You fooed: da topic title.', :title, :foo
@ -607,17 +607,17 @@ class ActiveRecordErrorI18nTests < ActiveSupport::TestCase
end
test "#full_message with a format present" do
store_translations(:errors => { :messages => { :kaputt => 'is kaputt' }, :full_messages => { :format => '{{attribute}}: {{message}}' } })
store_translations(:errors => { :messages => { :kaputt => 'is kaputt' }, :full_messages => { :format => '%{attribute}: %{message}' } })
assert_full_message 'Title: is kaputt', :title, :kaputt
end
test "#full_message with a type specific format present" do
store_translations(:errors => { :messages => { :kaputt => 'is kaputt' }, :full_messages => { :kaputt => '{{attribute}} {{message}}!' } })
store_translations(:errors => { :messages => { :kaputt => 'is kaputt' }, :full_messages => { :kaputt => '%{attribute} %{message}!' } })
assert_full_message 'Title is kaputt!', :title, :kaputt
end
test "#full_message with class-level specified custom message" do
store_translations(:errors => { :messages => { :broken => 'is kaputt' }, :full_messages => { :broken => '{{attribute}} {{message}}?!' } })
store_translations(:errors => { :messages => { :broken => 'is kaputt' }, :full_messages => { :broken => '%{attribute} %{message}?!' } })
assert_full_message 'Title is kaputt?!', :title, :kaputt, :message => :broken
end
@ -625,7 +625,7 @@ class ActiveRecordErrorI18nTests < ActiveSupport::TestCase
store_translations(:my_errors => { :messages => { :kaputt => 'is kaputt' } })
assert_full_message 'Title is kaputt', :title, :kaputt, :scope => [:activerecord, :my_errors]
store_translations(:my_errors => { :full_messages => { :kaputt => '{{attribute}} {{message}}!' } })
store_translations(:my_errors => { :full_messages => { :kaputt => '%{attribute} %{message}!' } })
assert_full_message 'Title is kaputt!', :title, :kaputt, :scope => [:activerecord, :my_errors]
end
@ -763,7 +763,7 @@ class ActiveRecordDefaultErrorMessagesI18nTests < ActiveSupport::TestCase
end
test "custom message string interpolation" do
assert_equal 'custom message title', error_message(:invalid, :default => 'custom message {{value}}', :value => 'title')
assert_equal 'custom message title', error_message(:invalid, :default => 'custom message %{value}', :value => 'title')
end
end
@ -897,14 +897,14 @@ class ActiveRecordValidationsI18nFullMessagesFullStackTests < ActiveSupport::Tes
test "full_message format stored per custom error message key" do
assert_full_message("Name is broken!") do
store_translations :errors => { :messages => { :broken => 'is broken' }, :full_messages => { :broken => '{{attribute}} {{message}}!' } }
store_translations :errors => { :messages => { :broken => 'is broken' }, :full_messages => { :broken => '%{attribute} %{message}!' } }
I18nPerson.validates_presence_of :name, :message => :broken
end
end
test "full_message format stored per error type" do
assert_full_message("Name can't be blank!") do
store_translations :errors => { :full_messages => { :blank => '{{attribute}} {{message}}!' } }
store_translations :errors => { :full_messages => { :blank => '%{attribute} %{message}!' } }
I18nPerson.validates_presence_of :name
end
end
@ -912,7 +912,7 @@ class ActiveRecordValidationsI18nFullMessagesFullStackTests < ActiveSupport::Tes
test "full_message format stored as default" do
assert_full_message("Name: can't be blank") do
store_translations :errors => { :full_messages => { :format => '{{attribute}}: {{message}}' } }
store_translations :errors => { :full_messages => { :format => '%{attribute}: %{message}' } }
I18nPerson.validates_presence_of :name
end
end

View file

@ -434,6 +434,18 @@ class ValidationsTest < ActiveRecord::TestCase
end
end
def test_validate_uniqueness_with_reserved_word_as_scope
repair_validations(Reply) do
Topic.validates_uniqueness_of(:content, :scope => "group")
t1 = Topic.create "title" => "t1", "content" => "hello world2"
assert t1.valid?
t2 = Topic.create "title" => "t2", "content" => "hello world2"
assert !t2.valid?
end
end
def test_validate_uniqueness_scoped_to_defining_class
t = Topic.create("title" => "What, me worry?")
@ -678,7 +690,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validate_format_with_formatted_message
Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => "can't be {{value}}")
Topic.validates_format_of(:title, :with => /^Valid Title$/, :message => "can't be %{value}")
t = Topic.create(:title => 'Invalid title')
assert_equal "can't be Invalid title", t.errors.on(:title)
end
@ -741,7 +753,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validates_inclusion_of_with_formatted_message
Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :message => "option {{value}} is not in the list" )
Topic.validates_inclusion_of( :title, :in => %w( a b c d e f g ), :message => "option %{value} is not in the list" )
assert Topic.create("title" => "a", "content" => "abc").valid?
@ -768,7 +780,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validates_exclusion_of_with_formatted_message
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ), :message => "option {{value}} is restricted" )
Topic.validates_exclusion_of( :title, :in => %w( abe monkey ), :message => "option %{value} is restricted" )
assert Topic.create("title" => "something", "content" => "abc")
@ -868,7 +880,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_optionally_validates_length_of_using_within_on_create
Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_long => "my string is too long: {{count}}"
Topic.validates_length_of :title, :content, :within => 5..10, :on => :create, :too_long => "my string is too long: %{count}"
t = Topic.create("title" => "thisisnotvalid", "content" => "whatever")
assert !t.save
@ -889,7 +901,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_optionally_validates_length_of_using_within_on_update
Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => "my string is too short: {{count}}"
Topic.validates_length_of :title, :content, :within => 5..10, :on => :update, :too_short => "my string is too short: %{count}"
t = Topic.create("title" => "vali", "content" => "whatever")
assert !t.save
@ -953,7 +965,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_validates_length_with_globally_modified_error_message
defaults = ActiveSupport::Deprecation.silence { ActiveRecord::Errors.default_error_messages }
original_message = defaults[:too_short]
defaults[:too_short] = 'tu est trops petit hombre {{count}}'
defaults[:too_short] = 'tu est trops petit hombre %{count}'
Topic.validates_length_of :title, :minimum => 10
t = Topic.create(:title => 'too short')
@ -1004,7 +1016,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validates_length_of_custom_errors_for_minimum_with_message
Topic.validates_length_of( :title, :minimum=>5, :message=>"boo {{count}}" )
Topic.validates_length_of( :title, :minimum=>5, :message=>"boo %{count}" )
t = Topic.create("title" => "uhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1012,7 +1024,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validates_length_of_custom_errors_for_minimum_with_too_short
Topic.validates_length_of( :title, :minimum=>5, :too_short=>"hoo {{count}}" )
Topic.validates_length_of( :title, :minimum=>5, :too_short=>"hoo %{count}" )
t = Topic.create("title" => "uhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1020,7 +1032,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validates_length_of_custom_errors_for_maximum_with_message
Topic.validates_length_of( :title, :maximum=>5, :message=>"boo {{count}}" )
Topic.validates_length_of( :title, :maximum=>5, :message=>"boo %{count}" )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1028,7 +1040,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validates_length_of_custom_errors_for_in
Topic.validates_length_of(:title, :in => 10..20, :message => "hoo {{count}}")
Topic.validates_length_of(:title, :in => 10..20, :message => "hoo %{count}")
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1041,7 +1053,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validates_length_of_custom_errors_for_maximum_with_too_long
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}" )
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}" )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1049,7 +1061,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validates_length_of_custom_errors_for_is_with_message
Topic.validates_length_of( :title, :is=>5, :message=>"boo {{count}}" )
Topic.validates_length_of( :title, :is=>5, :message=>"boo %{count}" )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1057,7 +1069,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validates_length_of_custom_errors_for_is_with_wrong_length
Topic.validates_length_of( :title, :is=>5, :wrong_length=>"hoo {{count}}" )
Topic.validates_length_of( :title, :is=>5, :wrong_length=>"hoo %{count}" )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1123,7 +1135,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_optionally_validates_length_of_using_within_on_create_utf8
with_kcode('UTF8') do
Topic.validates_length_of :title, :within => 5..10, :on => :create, :too_long => "長すぎます: {{count}}"
Topic.validates_length_of :title, :within => 5..10, :on => :create, :too_long => "長すぎます: %{count}"
t = Topic.create("title" => "一二三四五六七八九十A", "content" => "whatever")
assert !t.save
@ -1146,7 +1158,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_optionally_validates_length_of_using_within_on_update_utf8
with_kcode('UTF8') do
Topic.validates_length_of :title, :within => 5..10, :on => :update, :too_short => "短すぎます: {{count}}"
Topic.validates_length_of :title, :within => 5..10, :on => :update, :too_short => "短すぎます: %{count}"
t = Topic.create("title" => "一二三4", "content" => "whatever")
assert !t.save
@ -1181,7 +1193,7 @@ class ValidationsTest < ActiveRecord::TestCase
end
def test_validates_length_of_with_block
Topic.validates_length_of :content, :minimum => 5, :too_short=>"Your essay must be at least {{count}} words.",
Topic.validates_length_of :content, :minimum => 5, :too_short=>"Your essay must be at least %{count} words.",
:tokenizer => lambda {|str| str.scan(/\w+/) }
t = Topic.create!(:content => "this content should be long enough")
assert t.valid?
@ -1356,7 +1368,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_if_validation_using_method_true
# When the method returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => :condition_is_true )
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => :condition_is_true )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1365,7 +1377,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_unless_validation_using_method_true
# When the method returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => :condition_is_true )
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => :condition_is_true )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert !t.errors.on(:title)
@ -1373,7 +1385,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_if_validation_using_method_false
# When the method returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => :condition_is_true_but_its_not )
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => :condition_is_true_but_its_not )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert !t.errors.on(:title)
@ -1381,7 +1393,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_unless_validation_using_method_false
# When the method returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => :condition_is_true_but_its_not )
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => :condition_is_true_but_its_not )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1390,7 +1402,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_if_validation_using_string_true
# When the evaluated string returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => "a = 1; a == 1" )
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => "a = 1; a == 1" )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1399,7 +1411,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_unless_validation_using_string_true
# When the evaluated string returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => "a = 1; a == 1" )
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => "a = 1; a == 1" )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert !t.errors.on(:title)
@ -1407,7 +1419,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_if_validation_using_string_false
# When the evaluated string returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :if => "false")
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :if => "false")
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
assert !t.errors.on(:title)
@ -1415,7 +1427,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_unless_validation_using_string_false
# When the evaluated string returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}", :unless => "false")
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}", :unless => "false")
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
assert t.errors.on(:title)
@ -1424,7 +1436,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_if_validation_using_block_true
# When the block returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:if => Proc.new { |r| r.content.size > 4 } )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
@ -1434,7 +1446,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_unless_validation_using_block_true
# When the block returns true
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:unless => Proc.new { |r| r.content.size > 4 } )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
@ -1443,7 +1455,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_if_validation_using_block_false
# When the block returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:if => Proc.new { |r| r.title != "uhohuhoh"} )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert t.valid?
@ -1452,7 +1464,7 @@ class ValidationsTest < ActiveRecord::TestCase
def test_unless_validation_using_block_false
# When the block returns false
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo {{count}}",
Topic.validates_length_of( :title, :maximum=>5, :too_long=>"hoo %{count}",
:unless => Proc.new { |r| r.title != "uhohuhoh"} )
t = Topic.create("title" => "uhohuhoh", "content" => "whatever")
assert !t.valid?
@ -1634,13 +1646,13 @@ class ValidatesNumericalityTest < ActiveRecord::TestCase
end
def test_validates_numericality_with_numeric_message
Topic.validates_numericality_of :approved, :less_than => 4, :message => "smaller than {{count}}"
Topic.validates_numericality_of :approved, :less_than => 4, :message => "smaller than %{count}"
topic = Topic.new("title" => "numeric test", "approved" => 10)
assert !topic.valid?
assert_equal "smaller than 4", topic.errors.on(:approved)
Topic.validates_numericality_of :approved, :greater_than => 4, :message => "greater than {{count}}"
Topic.validates_numericality_of :approved, :greater_than => 4, :message => "greater than %{count}"
topic = Topic.new("title" => "numeric test", "approved" => 1)
assert !topic.valid?