diff options
Diffstat (limited to 'activerecord/test')
18 files changed, 173 insertions, 48 deletions
diff --git a/activerecord/test/cases/adapter_test.rb b/activerecord/test/cases/adapter_test.rb index 04770646b2..80530194ff 100644 --- a/activerecord/test/cases/adapter_test.rb +++ b/activerecord/test/cases/adapter_test.rb @@ -130,4 +130,19 @@ class AdapterTest < ActiveRecord::TestCase assert_equal " LIMIT 1,7 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7) end end + + def test_uniqueness_violations_are_translated_to_specific_exception + @connection.execute "INSERT INTO subscribers(nick) VALUES('me')" + assert_raises(ActiveRecord::RecordNotUnique) do + @connection.execute "INSERT INTO subscribers(nick) VALUES('me')" + end + end + + def test_foreign_key_violations_are_translated_to_specific_exception + unless @connection.adapter_name == 'SQLite' + assert_raises(ActiveRecord::InvalidForeignKey) do + @connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)" + end + end + end end diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 8dc95806b9..14b96caaae 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -803,6 +803,13 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, developer.projects.count end + unless current_adapter?(:PostgreSQLAdapter) + def test_count_with_finder_sql + assert_equal 3, projects(:active_record).developers_with_finder_sql.count + assert_equal 3, projects(:active_record).developers_with_multiline_finder_sql.count + end + end + def test_association_proxy_transaction_method_starts_transaction_in_association_class Post.expects(:transaction) Category.find(:first).posts.transaction do diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index d99424f9cd..15919e2289 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -163,6 +163,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 0, Firm.find(:first).no_clients_using_counter_sql.size end + def test_counting_using_finder_sql + assert_equal 2, Firm.find(4).clients_using_sql.count + assert_equal 2, Firm.find(4).clients_using_multiline_sql.count + end + def test_belongs_to_sanity c = Client.new assert_nil c.firm diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 4254badef2..7a4712d7c8 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -157,6 +157,12 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert_equal peeps + 1, posts(:thinking).people.count end + def test_associate_with_create_with_through_having_conditions + impatient_people = posts(:thinking).impatient_people.count + posts(:thinking).impatient_people.create!(:first_name => 'foo') + assert_equal impatient_people + 1, posts(:thinking).impatient_people.count + end + def test_associate_with_create_exclamation_and_no_options peeps = posts(:thinking).people.count posts(:thinking).people.create!(:first_name => 'foo') diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 59aa6953e3..f9ac37cc87 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -146,7 +146,12 @@ class BasicsTest < ActiveRecord::TestCase def test_read_attributes_before_type_cast_on_datetime developer = Developer.find(:first) - assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"] + # Oracle adapter returns Time before type cast + unless current_adapter?(:OracleAdapter) + assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"] + else + assert_equal developer.created_at.to_s(:db) , developer.attributes_before_type_cast["created_at"].to_s(:db) + end end def test_hash_content @@ -594,9 +599,9 @@ class BasicsTest < ActiveRecord::TestCase end def test_destroy_many - assert_equal 3, Client.count - Client.destroy([2, 3]) - assert_equal 1, Client.count + assert_difference('Client.count', -2) do + Client.destroy([2, 3]) + end end def test_delete_many @@ -682,21 +687,24 @@ class BasicsTest < ActiveRecord::TestCase end end - def test_update_all_ignores_order_without_limit_from_association - author = authors(:david) - assert_nothing_raised do - assert_equal author.posts_with_comments_and_categories.length, author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ]) + # Oracle UPDATE does not support ORDER BY + unless current_adapter?(:OracleAdapter) + def test_update_all_ignores_order_without_limit_from_association + author = authors(:david) + assert_nothing_raised do + assert_equal author.posts_with_comments_and_categories.length, author.posts_with_comments_and_categories.update_all([ "body = ?", "bulk update!" ]) + end end - end - def test_update_all_with_order_and_limit_updates_subset_only - author = authors(:david) - assert_nothing_raised do - assert_equal 1, author.posts_sorted_by_id_limited.size - assert_equal 2, author.posts_sorted_by_id_limited.find(:all, :limit => 2).size - assert_equal 1, author.posts_sorted_by_id_limited.update_all([ "body = ?", "bulk update!" ]) - assert_equal "bulk update!", posts(:welcome).body - assert_not_equal "bulk update!", posts(:thinking).body + def test_update_all_with_order_and_limit_updates_subset_only + author = authors(:david) + assert_nothing_raised do + assert_equal 1, author.posts_sorted_by_id_limited.size + assert_equal 2, author.posts_sorted_by_id_limited.find(:all, :limit => 2).size + assert_equal 1, author.posts_sorted_by_id_limited.update_all([ "body = ?", "bulk update!" ]) + assert_equal "bulk update!", posts(:welcome).body + assert_not_equal "bulk update!", posts(:thinking).body + end end end @@ -1117,22 +1125,25 @@ class BasicsTest < ActiveRecord::TestCase Topic.skip_time_zone_conversion_for_attributes = [] end - def test_multiparameter_attributes_on_time_only_column_with_time_zone_aware_attributes_does_not_do_time_zone_conversion - ActiveRecord::Base.time_zone_aware_attributes = true - ActiveRecord::Base.default_timezone = :utc - Time.zone = ActiveSupport::TimeZone[-28800] - attributes = { - "bonus_time(1i)" => "2000", "bonus_time(2i)" => "1", "bonus_time(3i)" => "1", - "bonus_time(4i)" => "16", "bonus_time(5i)" => "24" - } - topic = Topic.find(1) - topic.attributes = attributes - assert_equal Time.utc(2000, 1, 1, 16, 24, 0), topic.bonus_time - assert topic.bonus_time.utc? - ensure - ActiveRecord::Base.time_zone_aware_attributes = false - ActiveRecord::Base.default_timezone = :local - Time.zone = nil + # Oracle, and Sybase do not have a TIME datatype. + unless current_adapter?(:OracleAdapter, :SybaseAdapter) + def test_multiparameter_attributes_on_time_only_column_with_time_zone_aware_attributes_does_not_do_time_zone_conversion + ActiveRecord::Base.time_zone_aware_attributes = true + ActiveRecord::Base.default_timezone = :utc + Time.zone = ActiveSupport::TimeZone[-28800] + attributes = { + "bonus_time(1i)" => "2000", "bonus_time(2i)" => "1", "bonus_time(3i)" => "1", + "bonus_time(4i)" => "16", "bonus_time(5i)" => "24" + } + topic = Topic.find(1) + topic.attributes = attributes + assert_equal Time.utc(2000, 1, 1, 16, 24, 0), topic.bonus_time + assert topic.bonus_time.utc? + ensure + ActiveRecord::Base.time_zone_aware_attributes = false + ActiveRecord::Base.default_timezone = :local + Time.zone = nil + end end def test_multiparameter_attributes_on_time_with_empty_seconds @@ -2105,4 +2116,8 @@ class BasicsTest < ActiveRecord::TestCase assert_equal custom_datetime, parrot[attribute] end end + + def test_dup + assert !Minimalistic.new.freeze.dup.frozen? + end end diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index fd455e0864..75f52dfa4a 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -203,7 +203,7 @@ class CalculationsTest < ActiveRecord::TestCase c = Company.count(:all, :group => "UPPER(#{QUOTED_TYPE})") assert_equal 2, c[nil] assert_equal 1, c['DEPENDENTFIRM'] - assert_equal 3, c['CLIENT'] + assert_equal 4, c['CLIENT'] assert_equal 2, c['FIRM'] end @@ -211,7 +211,7 @@ class CalculationsTest < ActiveRecord::TestCase c = Company.count(:all, :group => "UPPER(companies.#{QUOTED_TYPE})") assert_equal 2, c[nil] assert_equal 1, c['DEPENDENTFIRM'] - assert_equal 3, c['CLIENT'] + assert_equal 4, c['CLIENT'] assert_equal 2, c['FIRM'] end diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 037b67ec84..d8f5695a0f 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -1044,8 +1044,8 @@ class FinderTest < ActiveRecord::TestCase end def test_select_values - assert_equal ["1","2","3","4","5","6","7","8","9"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s } - assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy"], Company.connection.select_values("SELECT name FROM companies ORDER BY id") + assert_equal ["1","2","3","4","5","6","7","8","9", "10"], Company.connection.select_values("SELECT id FROM companies ORDER BY id").map! { |i| i.to_s } + assert_equal ["37signals","Summit","Microsoft", "Flamboyant Software", "Ex Nihilo", "RailsCore", "Leetsoft", "Jadedpixel", "Odegy", "Ex Nihilo Part Deux"], Company.connection.select_values("SELECT name FROM companies ORDER BY id") end def test_select_rows diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index c30186286d..d1e7caed89 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -6,8 +6,6 @@ require 'config' require 'rubygems' require 'test/unit' require 'stringio' -gem 'mocha', '>= 0.9.5' -require 'mocha' require 'active_record' require 'active_record/test_case' diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index eae5a60829..167d3abad9 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -112,9 +112,9 @@ class InheritanceTest < ActiveRecord::TestCase end def test_inheritance_condition - assert_equal 9, Company.count + assert_equal 10, Company.count assert_equal 2, Firm.count - assert_equal 3, Client.count + assert_equal 4, Client.count end def test_alt_inheritance_condition diff --git a/activerecord/test/cases/json_serialization_test.rb b/activerecord/test/cases/json_serialization_test.rb index 0ffbc9bf3b..54bc8e2343 100644 --- a/activerecord/test/cases/json_serialization_test.rb +++ b/activerecord/test/cases/json_serialization_test.rb @@ -170,18 +170,18 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase def test_should_allow_only_option_for_list_of_authors authors = [@david, @mary] - assert_equal %([{"name":"David"},{"name":"Mary"}]), authors.to_json(:only => :name) + assert_equal %([{"name":"David"},{"name":"Mary"}]), ActiveSupport::JSON.encode(authors, :only => :name) end def test_should_allow_except_option_for_list_of_authors authors = [@david, @mary] - assert_equal %([{"id":1},{"id":2}]), authors.to_json(:except => [:name, :author_address_id, :author_address_extra_id]) + assert_equal %([{"id":1},{"id":2}]), ActiveSupport::JSON.encode(authors, :except => [:name, :author_address_id, :author_address_extra_id]) end def test_should_allow_includes_for_list_of_authors authors = [@david, @mary] - json = authors.to_json( + json = ActiveSupport::JSON.encode(authors, :only => :name, :include => { :posts => { :only => :id } @@ -200,6 +200,6 @@ class DatabaseConnectedJsonEncodingTest < ActiveRecord::TestCase 2 => @mary } - assert_equal %({"1":{"name":"David"}}), authors_hash.to_json(:only => [1, :name]) + assert_equal %({"1":{"name":"David"}}), ActiveSupport::JSON.encode(authors_hash, :only => [1, :name]) end end diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 16861f21b1..215b5a427a 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -293,6 +293,13 @@ if ActiveRecord::Base.connection.supports_migrations? Person.connection.drop_table table_name rescue nil end + def test_create_table_without_a_block + table_name = :testings + Person.connection.create_table table_name + ensure + Person.connection.drop_table table_name rescue nil + end + # Sybase, and SQLite3 will not allow you to add a NOT NULL # column to a table without a default value. unless current_adapter?(:SybaseAdapter, :SQLiteAdapter) @@ -635,6 +642,32 @@ if ActiveRecord::Base.connection.supports_migrations? end end + if current_adapter?(:SQLiteAdapter) + def test_rename_table_for_sqlite_should_work_with_reserved_words + begin + assert_nothing_raised do + ActiveRecord::Base.connection.rename_table :references, :old_references + ActiveRecord::Base.connection.create_table :octopuses do |t| + t.column :url, :string + end + end + + assert_nothing_raised { ActiveRecord::Base.connection.rename_table :octopuses, :references } + + # Using explicit id in insert for compatibility across all databases + con = ActiveRecord::Base.connection + assert_nothing_raised do + con.execute "INSERT INTO 'references' (#{con.quote_column_name('id')}, #{con.quote_column_name('url')}) VALUES (1, 'http://rubyonrails.com')" + end + assert_equal 'http://rubyonrails.com', ActiveRecord::Base.connection.select_value("SELECT url FROM 'references' WHERE id=1") + + ensure + ActiveRecord::Base.connection.drop_table :references + ActiveRecord::Base.connection.rename_table :old_references, :references + end + end + end + def test_rename_table begin ActiveRecord::Base.connection.create_table :octopuses do |t| diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index db64bbb806..194d5e9dff 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -5,14 +5,20 @@ require 'models/company' require 'models/company_in_module' require 'models/subscriber' require 'models/pirate' +require 'models/price_estimate' class ReflectionTest < ActiveRecord::TestCase - fixtures :topics, :customers, :companies, :subscribers + fixtures :topics, :customers, :companies, :subscribers, :price_estimates def setup @first = Topic.find(1) end + def test_human_name + assert_equal "Price estimate", PriceEstimate.human_name + assert_equal "Subscriber", Subscriber.human_name + end + def test_column_null_not_null subscriber = Subscriber.find(:first) assert subscriber.column_for_attribute("name").null @@ -170,8 +176,8 @@ class ReflectionTest < ActiveRecord::TestCase def test_reflection_of_all_associations # FIXME these assertions bust a lot - assert_equal 28, Firm.reflect_on_all_associations.size - assert_equal 21, Firm.reflect_on_all_associations(:has_many).size + assert_equal 29, Firm.reflect_on_all_associations.size + assert_equal 22, Firm.reflect_on_all_associations(:has_many).size assert_equal 7, Firm.reflect_on_all_associations(:has_one).size assert_equal 0, Firm.reflect_on_all_associations(:belongs_to).size end diff --git a/activerecord/test/fixtures/companies.yml b/activerecord/test/fixtures/companies.yml index e7691fde46..9ad68fbe11 100644 --- a/activerecord/test/fixtures/companies.yml +++ b/activerecord/test/fixtures/companies.yml @@ -35,6 +35,14 @@ another_client: name: Ex Nihilo ruby_type: Client +a_third_client: + id: 10 + type: Client + firm_id: 4 + client_of: 4 + name: Ex Nihilo Part Deux + ruby_type: Client + rails_core: id: 6 name: RailsCore diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 4c2514b0b3..840527ddeb 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -48,6 +48,10 @@ class Firm < Company has_many :clients_with_interpolated_conditions, :class_name => "Client", :conditions => 'rating > #{rating}' has_many :clients_like_ms_with_hash_conditions, :conditions => { :name => 'Microsoft' }, :class_name => "Client", :order => "id" has_many :clients_using_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}' + has_many :clients_using_multiline_sql, :class_name => "Client", :finder_sql => ' + SELECT + companies.* + FROM companies WHERE companies.client_of = #{id}' has_many :clients_using_counter_sql, :class_name => "Client", :finder_sql => 'SELECT * FROM companies WHERE client_of = #{id}', :counter_sql => 'SELECT COUNT(*) FROM companies WHERE client_of = #{id}' diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 374e536a5b..7392515ec7 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -69,6 +69,8 @@ class Post < ActiveRecord::Base :after_add => lambda {|owner, reader| log(:added, :after, reader.first_name) }, :before_remove => lambda {|owner, reader| log(:removed, :before, reader.first_name) }, :after_remove => lambda {|owner, reader| log(:removed, :after, reader.first_name) } + has_many :skimmers, :class_name => 'Reader', :conditions => { :skimmer => true } + has_many :impatient_people, :through => :skimmers, :source => :person def self.top(limit) ranked_by_comments.limit(limit) diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index f25b2ddf77..422b12dc83 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -8,6 +8,12 @@ class Project < ActiveRecord::Base has_and_belongs_to_many :developers_named_david_with_hash_conditions, :class_name => "Developer", :conditions => { :name => 'David' }, :uniq => true has_and_belongs_to_many :salaried_developers, :class_name => "Developer", :conditions => "salary > 0" has_and_belongs_to_many :developers_with_finder_sql, :class_name => "Developer", :finder_sql => 'SELECT t.*, j.* FROM developers_projects j, developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id' + has_and_belongs_to_many :developers_with_multiline_finder_sql, :class_name => "Developer", :finder_sql => ' + SELECT + t.*, j.* + FROM + developers_projects j, + developers t WHERE t.id = j.developer_id AND j.project_id = #{id} ORDER BY t.id' has_and_belongs_to_many :developers_by_sql, :class_name => "Developer", :delete_sql => "DELETE FROM developers_projects WHERE project_id = \#{id} AND developer_id = \#{record.id}" has_and_belongs_to_many :developers_with_callbacks, :class_name => "Developer", :before_add => Proc.new {|o, r| o.developers_log << "before_adding#{r.id || '<new>'}"}, :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || '<new>'}"}, diff --git a/activerecord/test/schema/oracle_specific_schema.rb b/activerecord/test/schema/oracle_specific_schema.rb new file mode 100644 index 0000000000..2d87f34625 --- /dev/null +++ b/activerecord/test/schema/oracle_specific_schema.rb @@ -0,0 +1,19 @@ +ActiveRecord::Schema.define do + + execute "drop table test_oracle_defaults" rescue nil + execute "drop sequence test_oracle_defaults_seq" rescue nil + + execute <<-SQL +create table test_oracle_defaults ( + id integer not null primary key, + test_char char(1) default 'X' not null, + test_string varchar2(20) default 'hello' not null, + test_int integer default 3 not null +) + SQL + + execute <<-SQL +create sequence test_oracle_defaults_seq minvalue 10000 + SQL + +end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index b2aaccb352..d2d6d1f4b3 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -364,6 +364,7 @@ ActiveRecord::Schema.define do create_table :readers, :force => true do |t| t.integer :post_id, :null => false t.integer :person_id, :null => false + t.boolean :skimmer, :default => false end create_table :shape_expressions, :force => true do |t| |