diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2008-07-04 00:33:56 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-07-04 00:33:56 +0100 |
commit | 0a7923ea7a76c1136bddfe638a600c62457df751 (patch) | |
tree | 2709c19133af9342958fc51b36ad53ca56c2927a /activerecord/test | |
parent | 5dba6c024289e42c31894b7d13c1695049fcf30d (diff) | |
parent | 1a478923dc909bf7b6aea4f2ad49cbeee6dea259 (diff) | |
download | rails-0a7923ea7a76c1136bddfe638a600c62457df751.tar.gz rails-0a7923ea7a76c1136bddfe638a600c62457df751.tar.bz2 rails-0a7923ea7a76c1136bddfe638a600c62457df751.zip |
Merge commit 'mainstream/master'
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/has_many_associations_test.rb | 26 | ||||
-rw-r--r-- | activerecord/test/cases/dirty_test.rb | 27 | ||||
-rw-r--r-- | activerecord/test/cases/finder_test.rb | 17 | ||||
-rw-r--r-- | activerecord/test/cases/migration_test.rb | 24 | ||||
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 4 | ||||
-rwxr-xr-x | activerecord/test/models/topic.rb | 1 |
6 files changed, 88 insertions, 11 deletions
diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index b638143c5a..247726bc61 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -345,7 +345,7 @@ class HasManyAssociationsTest < ActiveRecord::TestCase def test_invalid_adding_with_validate_false firm = Firm.find(:first) client = Client.new - firm.unvalidated_clients_of_firm << Client.new + firm.unvalidated_clients_of_firm << client assert firm.valid? assert !client.valid? @@ -353,6 +353,23 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert client.new_record? end + def test_valid_adding_with_validate_false + no_of_clients = Client.count + + firm = Firm.find(:first) + client = Client.new("name" => "Apple") + + assert firm.valid? + assert client.valid? + assert client.new_record? + + firm.unvalidated_clients_of_firm << client + + assert firm.save + assert !client.new_record? + assert_equal no_of_clients+1, Client.count + end + def test_build company = companies(:first_firm) new_client = assert_no_queries { company.clients_of_firm.build("name" => "Another Client") } @@ -367,6 +384,13 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 2, company.clients_of_firm(true).size end + def test_collection_size_after_building + company = companies(:first_firm) # company already has one client + company.clients_of_firm.build("name" => "Another Client") + company.clients_of_firm.build("name" => "Yet Another Client") + assert_equal 3, company.clients_of_firm.size + end + def test_build_many company = companies(:first_firm) new_clients = assert_no_queries { company.clients_of_firm.build([{"name" => "Another Client"}, {"name" => "Another Client II"}]) } diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index d70a787208..e5e022050d 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -55,6 +55,33 @@ class DirtyTest < ActiveRecord::TestCase end end + def test_zero_to_blank_marked_as_changed + pirate = Pirate.new + pirate.catchphrase = "Yarrrr, me hearties" + pirate.parrot_id = 1 + pirate.save + + # check the change from 1 to '' + pirate = Pirate.find_by_catchphrase("Yarrrr, me hearties") + pirate.parrot_id = '' + assert pirate.parrot_id_changed? + assert_equal([1, nil], pirate.parrot_id_change) + pirate.save + + # check the change from nil to 0 + pirate = Pirate.find_by_catchphrase("Yarrrr, me hearties") + pirate.parrot_id = 0 + assert pirate.parrot_id_changed? + assert_equal([nil, 0], pirate.parrot_id_change) + pirate.save + + # check the change from 0 to '' + pirate = Pirate.find_by_catchphrase("Yarrrr, me hearties") + pirate.parrot_id = '' + assert pirate.parrot_id_changed? + assert_equal([0, nil], pirate.parrot_id_change) + end + def test_object_should_be_changed_if_any_attribute_is_changed pirate = Pirate.new assert !pirate.changed? diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index f48b62ba6b..b97db73b68 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -200,6 +200,23 @@ class FinderTest < ActiveRecord::TestCase assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => { 'topics.approved' => true }) } end + def test_find_on_hash_conditions_with_hashed_table_name + assert Topic.find(1, :conditions => {:topics => { :approved => false }}) + assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => {:topics => { :approved => true }}) } + end + + def test_find_with_hash_conditions_on_joined_table + firms = Firm.all :joins => :account, :conditions => {:accounts => { :credit_limit => 50 }} + assert_equal 1, firms.size + assert_equal companies(:first_firm), firms.first + end + + def test_find_with_hash_conditions_on_joined_table_and_with_range + firms = DependentFirm.all :joins => :account, :conditions => {:name => 'RailsCore', :accounts => { :credit_limit => 55..60 }} + assert_equal 1, firms.size + assert_equal companies(:rails_core), firms.first + end + def test_find_on_hash_conditions_with_explicit_table_name_and_aggregate david = customers(:david) assert Customer.find(david.id, :conditions => { 'customers.name' => david.name, :address => david.address }) diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 908951590c..4482b487dd 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -153,9 +153,10 @@ if ActiveRecord::Base.connection.supports_migrations? t.column :default_int, :integer - t.column :one_int, :integer, :limit => 1 - t.column :four_int, :integer, :limit => 4 - t.column :eight_int, :integer, :limit => 8 + t.column :one_int, :integer, :limit => 1 + t.column :four_int, :integer, :limit => 4 + t.column :eight_int, :integer, :limit => 8 + t.column :eleven_int, :integer, :limit => 11 end end @@ -167,17 +168,20 @@ if ActiveRecord::Base.connection.supports_migrations? one = columns.detect { |c| c.name == "one_int" } four = columns.detect { |c| c.name == "four_int" } eight = columns.detect { |c| c.name == "eight_int" } + eleven = columns.detect { |c| c.name == "eleven_int" } if current_adapter?(:PostgreSQLAdapter) assert_equal 'integer', default.sql_type assert_equal 'smallint', one.sql_type assert_equal 'integer', four.sql_type assert_equal 'bigint', eight.sql_type + assert_equal 'integer', eleven.sql_type elsif current_adapter?(:MysqlAdapter) - assert_match /^int\(\d+\)/, default.sql_type - assert_match /^tinyint\(\d+\)/, one.sql_type - assert_match /^int\(\d+\)/, four.sql_type - assert_match /^bigint\(\d+\)/, eight.sql_type + assert_match 'int(11)', default.sql_type + assert_match 'tinyint', one.sql_type + assert_match 'int', four.sql_type + assert_match 'bigint', eight.sql_type + assert_match 'int(11)', eleven.sql_type elsif current_adapter?(:OracleAdapter) assert_equal 'NUMBER(38)', default.sql_type assert_equal 'NUMBER(1)', one.sql_type @@ -1242,10 +1246,10 @@ if ActiveRecord::Base.connection.supports_migrations? end def integer_column - if current_adapter?(:SQLite3Adapter) || current_adapter?(:SQLiteAdapter) || current_adapter?(:PostgreSQLAdapter) - "integer" - else + if current_adapter?(:MysqlAdapter) 'int(11)' + else + 'integer' end end diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 393ba086c9..7d73541ee1 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -65,6 +65,10 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal Topic.replied.approved, Topic.replied.approved_as_string end + def test_scopes_can_be_specified_with_deep_hash_conditions + assert_equal Topic.replied.approved, Topic.replied.approved_as_hash_condition + end + def test_scopes_are_composable assert_equal (approved = Topic.find(:all, :conditions => {:approved => true})), Topic.approved assert_equal (replied = Topic.find(:all, :conditions => 'replies_count > 0')), Topic.replied diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 423b6fe52b..47b2eec938 100755 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -4,6 +4,7 @@ class Topic < ActiveRecord::Base { :conditions => ['written_on < ?', time] } } named_scope :approved, :conditions => {:approved => true} + named_scope :approved_as_hash_condition, :conditions => {:topics => {:approved => true}} named_scope 'approved_as_string', :conditions => {:approved => true} named_scope :replied, :conditions => ['replies_count > 0'] named_scope :anonymous_extension do |