diff options
Diffstat (limited to 'activerecord/test')
20 files changed, 411 insertions, 18 deletions
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 0bc345428f..546ed80894 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -9,11 +9,16 @@ require 'models/person' require 'models/reader' require 'models/owner' require 'models/pet' +require 'models/reference' +require 'models/job' +require 'models/subscriber' +require 'models/subscription' +require 'models/book' class EagerAssociationTest < ActiveRecord::TestCase fixtures :posts, :comments, :authors, :categories, :categories_posts, :companies, :accounts, :tags, :taggings, :people, :readers, - :owners, :pets, :author_favorites + :owners, :pets, :author_favorites, :jobs, :references, :subscribers, :subscriptions, :books def test_loading_with_one_association posts = Post.find(:all, :include => :comments) @@ -194,6 +199,48 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal authors(:mary), assert_no_queries { author_favorite.favorite_author } end + def test_eager_load_belongs_to_quotes_table_and_column_names + job = Job.find jobs(:unicyclist).id, :include => :ideal_reference + references(:michael_unicyclist) + assert_no_queries{ assert_equal references(:michael_unicyclist), job.ideal_reference} + end + + def test_eager_load_has_one_quotes_table_and_column_names + michael = Person.find(people(:michael), :include => :favourite_reference) + references(:michael_unicyclist) + assert_no_queries{ assert_equal references(:michael_unicyclist), michael.favourite_reference} + end + + def test_eager_load_has_many_quotes_table_and_column_names + michael = Person.find(people(:michael), :include => :references) + references(:michael_magician,:michael_unicyclist) + assert_no_queries{ assert_equal references(:michael_magician,:michael_unicyclist), michael.references.sort_by(&:id) } + end + + def test_eager_load_has_many_through_quotes_table_and_column_names + michael = Person.find(people(:michael), :include => :jobs) + jobs(:magician, :unicyclist) + assert_no_queries{ assert_equal jobs(:unicyclist, :magician), michael.jobs.sort_by(&:id) } + end + + def test_eager_load_has_many_with_string_keys + subscriptions = subscriptions(:webster_awdr, :webster_rfr) + subscriber =Subscriber.find(subscribers(:second).id, :include => :subscriptions) + assert_equal subscriptions, subscriber.subscriptions.sort_by(&:id) + end + + def test_eager_load_has_many_through_with_string_keys + books = books(:awdr, :rfr) + subscriber = Subscriber.find(subscribers(:second).id, :include => :books) + assert_equal books, subscriber.books.sort_by(&:id) + end + + def test_eager_load_belongs_to_with_string_keys + subscriber = subscribers(:second) + subscription = Subscription.find(subscriptions(:webster_awdr).id, :include => :subscriber) + assert_equal subscriber, subscription.subscriber + end + def test_eager_association_loading_with_explicit_join posts = Post.find(:all, :include => :comments, :joins => "INNER JOIN authors ON posts.author_id = authors.id AND authors.name = 'Mary'", :limit => 1, :order => 'author_id') assert_equal 1, posts.length 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 a9899102d7..5561361bca 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -115,6 +115,18 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase assert posts(:thinking).reload.people(true).collect(&:first_name).include?("Jeb") end + def test_associate_with_create_and_no_options + peeps = posts(:thinking).people.count + posts(:thinking).people.create(:first_name => 'foo') + assert_equal peeps + 1, posts(:thinking).people.count + end + + def test_associate_with_create_exclamation_and_no_options + peeps = posts(:thinking).people.count + posts(:thinking).people.create!(:first_name => 'foo') + assert_equal peeps + 1, posts(:thinking).people.count + end + def test_clear_associations assert_queries(2) { posts(:welcome);posts(:welcome).people(true) } diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 45d47837a4..e07ec50c46 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -251,6 +251,27 @@ class BasicsTest < ActiveRecord::TestCase topic = Topic.create("title" => "New Topic") topicReloaded = Topic.find(topic.id) assert_equal(topic, topicReloaded) + end + + def test_create_through_factory_with_block + topic = Topic.create("title" => "New Topic") do |t| + t.author_name = "David" + end + topicReloaded = Topic.find(topic.id) + assert_equal("New Topic", topic.title) + assert_equal("David", topic.author_name) + end + + def test_create_many_through_factory_with_block + topics = Topic.create([ { "title" => "first" }, { "title" => "second" }]) do |t| + t.author_name = "David" + end + assert_equal 2, topics.size + topic1, topic2 = Topic.find(topics[0].id), Topic.find(topics[1].id) + assert_equal "first", topic1.title + assert_equal "David", topic1.author_name + assert_equal "second", topic2.title + assert_equal "David", topic2.author_name end def test_update @@ -1630,6 +1651,10 @@ class BasicsTest < ActiveRecord::TestCase def test_last assert_equal Developer.find(:first, :order => 'id desc'), Developer.last end + + def test_all_with_conditions + assert_equal Developer.find(:all, :order => 'id desc'), Developer.all(:order => 'id desc') + end def test_find_ordered_last last = Developer.find :last, :order => 'developers.salary ASC' diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 182f4f0f0b..2787b9a39d 100755 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -96,6 +96,10 @@ class FixturesTest < ActiveRecord::TestCase second_row = ActiveRecord::Base.connection.select_one("SELECT * FROM prefix_topics_suffix WHERE author_name = 'Mary'") assert_nil(second_row["author_email_address"]) + + # This checks for a caching problem which causes a bug in the fixtures + # class-level configuration helper. + assert_not_nil topics, "Fixture data inserted, but fixture objects not returned from create" ensure # Restore prefix/suffix to its previous values ActiveRecord::Base.table_name_prefix = old_prefix diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index e80f902d0d..7db6c570b5 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -2,6 +2,7 @@ require "cases/helper" require 'models/person' require 'models/reader' require 'models/legacy_thing' +require 'models/reference' class LockWithoutDefault < ActiveRecord::Base; end @@ -15,7 +16,7 @@ class ReadonlyFirstNamePerson < Person end class OptimisticLockingTest < ActiveRecord::TestCase - fixtures :people, :legacy_things + fixtures :people, :legacy_things, :references # need to disable transactional fixtures, because otherwise the sqlite3 # adapter (at least) chokes when we try and change the schema in the middle @@ -138,6 +139,12 @@ class OptimisticLockingTest < ActiveRecord::TestCase end end end + + def test_quote_table_name + ref = references(:michael_magician) + ref.favourite = !ref.favourite + assert ref.save + end private diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index f99e736c55..d4e81827aa 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -1077,7 +1077,7 @@ if ActiveRecord::Base.connection.supports_migrations? protected def with_new_table - Person.connection.create_table :delete_me do |t| + Person.connection.create_table :delete_me, :force => true do |t| yield t end ensure @@ -1086,4 +1086,210 @@ if ActiveRecord::Base.connection.supports_migrations? end # SexyMigrationsTest end # uses_mocha + + uses_mocha 'ChangeTable migration tests' do + class ChangeTableMigrationsTest < ActiveRecord::TestCase + def setup + @connection = Person.connection + @connection.create_table :delete_me, :force => true do |t| + end + end + + def teardown + Person.connection.drop_table :delete_me rescue nil + end + + def test_references_column_type_adds_id + with_change_table do |t| + @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {}) + t.references :customer + end + end + + def test_remove_references_column_type_removes_id + with_change_table do |t| + @connection.expects(:remove_column).with(:delete_me, 'customer_id') + t.remove_references :customer + end + end + + def test_add_belongs_to_works_like_add_references + with_change_table do |t| + @connection.expects(:add_column).with(:delete_me, 'customer_id', :integer, {}) + t.belongs_to :customer + end + end + + def test_remove_belongs_to_works_like_remove_references + with_change_table do |t| + @connection.expects(:remove_column).with(:delete_me, 'customer_id') + t.remove_belongs_to :customer + end + end + + def test_references_column_type_with_polymorphic_adds_type + with_change_table do |t| + @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {}) + @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {}) + t.references :taggable, :polymorphic => true + end + end + + def test_remove_references_column_type_with_polymorphic_removes_type + with_change_table do |t| + @connection.expects(:remove_column).with(:delete_me, 'taggable_type') + @connection.expects(:remove_column).with(:delete_me, 'taggable_id') + t.remove_references :taggable, :polymorphic => true + end + end + + def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag + with_change_table do |t| + @connection.expects(:add_column).with(:delete_me, 'taggable_type', :string, {:null => false}) + @connection.expects(:add_column).with(:delete_me, 'taggable_id', :integer, {:null => false}) + t.references :taggable, :polymorphic => true, :null => false + end + end + + def test_remove_references_column_type_with_polymorphic_and_options_null_is_false_removes_table_flag + with_change_table do |t| + @connection.expects(:remove_column).with(:delete_me, 'taggable_type') + @connection.expects(:remove_column).with(:delete_me, 'taggable_id') + t.remove_references :taggable, :polymorphic => true, :null => false + end + end + + def test_timestamps_creates_updated_at_and_created_at + with_change_table do |t| + @connection.expects(:add_timestamps).with(:delete_me) + t.timestamps + end + end + + def test_remove_timestamps_creates_updated_at_and_created_at + with_change_table do |t| + @connection.expects(:remove_timestamps).with(:delete_me) + t.remove_timestamps + end + end + + def string_column + if current_adapter?(:PostgreSQLAdapter) + "character varying(255)" + else + 'varchar(255)' + end + end + + def integer_column + if current_adapter?(:SQLite3Adapter) || current_adapter?(:SQLiteAdapter) || current_adapter?(:PostgreSQLAdapter) + "integer" + else + 'int(11)' + end + end + + def test_integer_creates_integer_column + with_change_table do |t| + @connection.expects(:add_column).with(:delete_me, :foo, integer_column, {}) + @connection.expects(:add_column).with(:delete_me, :bar, integer_column, {}) + t.integer :foo, :bar + end + end + + def test_string_creates_string_column + with_change_table do |t| + @connection.expects(:add_column).with(:delete_me, :foo, string_column, {}) + @connection.expects(:add_column).with(:delete_me, :bar, string_column, {}) + t.string :foo, :bar + end + end + + def test_column_creates_column + with_change_table do |t| + @connection.expects(:add_column).with(:delete_me, :bar, :integer, {}) + t.column :bar, :integer + end + end + + def test_column_creates_column_with_options + with_change_table do |t| + @connection.expects(:add_column).with(:delete_me, :bar, :integer, {:null => false}) + t.column :bar, :integer, :null => false + end + end + + def test_index_creates_index + with_change_table do |t| + @connection.expects(:add_index).with(:delete_me, :bar, {}) + t.index :bar + end + end + + def test_index_creates_index_with_options + with_change_table do |t| + @connection.expects(:add_index).with(:delete_me, :bar, {:unique => true}) + t.index :bar, :unique => true + end + end + + def test_change_changes_column + with_change_table do |t| + @connection.expects(:change_column).with(:delete_me, :bar, :string, {}) + t.change :bar, :string + end + end + + def test_change_changes_column_with_options + with_change_table do |t| + @connection.expects(:change_column).with(:delete_me, :bar, :string, {:null => true}) + t.change :bar, :string, :null => true + end + end + + def test_change_default_changes_column + with_change_table do |t| + @connection.expects(:change_column_default).with(:delete_me, :bar, :string) + t.change_default :bar, :string + end + end + + def test_remove_drops_single_column + with_change_table do |t| + @connection.expects(:remove_column).with(:delete_me, [:bar]) + t.remove :bar + end + end + + def test_remove_drops_multiple_columns + with_change_table do |t| + @connection.expects(:remove_column).with(:delete_me, [:bar, :baz]) + t.remove :bar, :baz + end + end + + def test_remove_index_removes_index_with_options + with_change_table do |t| + @connection.expects(:remove_index).with(:delete_me, {:unique => true}) + t.remove_index :unique => true + end + end + + def test_rename_renames_column + with_change_table do |t| + @connection.expects(:rename_column).with(:delete_me, :bar, :baz) + t.rename :bar, :baz + end + end + + protected + def with_change_table + Person.connection.change_table :delete_me do |t| + yield t + end + end + + end # ChangeTable test + end # uses_mocha + end diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index 3d3cecd603..e99448c23e 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -11,15 +11,15 @@ class NamedScopeTest < ActiveRecord::TestCase def test_implements_enumerable assert !Topic.find(:all).empty? - assert_equal Topic.find(:all), Topic.all - assert_equal Topic.find(:all), Topic.all.to_a - assert_equal Topic.find(:first), Topic.all.first - assert_equal Topic.find(:all), Topic.all.each { |i| i } + assert_equal Topic.find(:all), Topic.base + assert_equal Topic.find(:all), Topic.base.to_a + assert_equal Topic.find(:first), Topic.base.first + assert_equal Topic.find(:all), Topic.base.each { |i| i } end def test_found_items_are_cached Topic.columns - all_posts = Topic.all + all_posts = Topic.base assert_queries(1) do all_posts.collect @@ -28,7 +28,7 @@ class NamedScopeTest < ActiveRecord::TestCase end def test_reload_expires_cache_of_found_items - all_posts = Topic.all + all_posts = Topic.base all_posts.inspect new_post = Topic.create! @@ -39,17 +39,17 @@ class NamedScopeTest < ActiveRecord::TestCase def test_delegates_finds_and_calculations_to_the_base_class assert !Topic.find(:all).empty? - assert_equal Topic.find(:all), Topic.all.find(:all) - assert_equal Topic.find(:first), Topic.all.find(:first) - assert_equal Topic.count, Topic.all.count - assert_equal Topic.average(:replies_count), Topic.all.average(:replies_count) + assert_equal Topic.find(:all), Topic.base.find(:all) + assert_equal Topic.find(:first), Topic.base.find(:first) + assert_equal Topic.count, Topic.base.count + assert_equal Topic.average(:replies_count), Topic.base.average(:replies_count) end def test_subclasses_inherit_scopes - assert Topic.scopes.include?(:all) + assert Topic.scopes.include?(:base) - assert Reply.scopes.include?(:all) - assert_equal Reply.find(:all), Reply.all + assert Reply.scopes.include?(:base) + assert_equal Reply.find(:all), Reply.base end def test_scopes_with_options_limit_finds_to_those_matching_the_criteria_specified @@ -104,7 +104,7 @@ class NamedScopeTest < ActiveRecord::TestCase def test_active_records_have_scope_named__all__ assert !Topic.find(:all).empty? - assert_equal Topic.find(:all), Topic.all + assert_equal Topic.find(:all), Topic.base end def test_active_records_have_scope_named__scoped__ diff --git a/activerecord/test/cases/validations_test.rb b/activerecord/test/cases/validations_test.rb index ca36ad3581..e3ca8660ac 100755 --- a/activerecord/test/cases/validations_test.rb +++ b/activerecord/test/cases/validations_test.rb @@ -133,6 +133,22 @@ class ValidationsTest < ActiveRecord::TestCase Reply.create!([ { "title" => "OK" }, { "title" => "Wrong Create" }]) end end + + def test_exception_on_create_bang_with_block + assert_raises(ActiveRecord::RecordInvalid) do + Reply.create!({ "title" => "OK" }) do |r| + r.content = nil + end + end + end + + def test_exception_on_create_bang_many_with_block + assert_raises(ActiveRecord::RecordInvalid) do + Reply.create!([{ "title" => "OK" }, { "title" => "Wrong Create" }]) do |r| + r.content = nil + end + end + end def test_scoped_create_without_attributes Reply.with_scope(:create => {}) do diff --git a/activerecord/test/fixtures/jobs.yml b/activerecord/test/fixtures/jobs.yml new file mode 100644 index 0000000000..f5775d27d3 --- /dev/null +++ b/activerecord/test/fixtures/jobs.yml @@ -0,0 +1,7 @@ +unicyclist: + id: 1 + ideal_reference_id: 2 +clown: + id: 2 +magician: + id: 3 diff --git a/activerecord/test/fixtures/references.yml b/activerecord/test/fixtures/references.yml new file mode 100644 index 0000000000..8e3953e916 --- /dev/null +++ b/activerecord/test/fixtures/references.yml @@ -0,0 +1,17 @@ +michael_magician: + id: 1 + person_id: 1 + job_id: 3 + favourite: false + +michael_unicyclist: + id: 2 + person_id: 1 + job_id: 1 + favourite: true + +david_unicyclist: + id: 3 + person_id: 2 + job_id: 1 + favourite: false diff --git a/activerecord/test/fixtures/subscriptions.yml b/activerecord/test/fixtures/subscriptions.yml new file mode 100644 index 0000000000..371bfd3422 --- /dev/null +++ b/activerecord/test/fixtures/subscriptions.yml @@ -0,0 +1,12 @@ +webster_awdr: + id: 1 + subscriber_id: webster132 + book_id: 1 +webster_rfr: + id: 2 + subscriber_id: webster132 + book_id: 2 +alterself_awdr: + id: 3 + subscriber_id: alterself + book_id: 3
\ No newline at end of file diff --git a/activerecord/test/models/job.rb b/activerecord/test/models/job.rb new file mode 100644 index 0000000000..3333a02e27 --- /dev/null +++ b/activerecord/test/models/job.rb @@ -0,0 +1,5 @@ +class Job < ActiveRecord::Base + has_many :references + has_many :people, :through => :references + belongs_to :ideal_reference, :class_name => 'Reference' +end diff --git a/activerecord/test/models/person.rb b/activerecord/test/models/person.rb index 366f9fb708..4f4d695b24 100644 --- a/activerecord/test/models/person.rb +++ b/activerecord/test/models/person.rb @@ -2,4 +2,9 @@ class Person < ActiveRecord::Base has_many :readers has_many :posts, :through => :readers has_many :posts_with_no_comments, :through => :readers, :source => :post, :include => :comments, :conditions => 'comments.id is null' + + has_many :references + has_many :jobs, :through => :references + has_one :favourite_reference, :class_name => 'Reference', :conditions => ['favourite=?', true] + end diff --git a/activerecord/test/models/reference.rb b/activerecord/test/models/reference.rb new file mode 100644 index 0000000000..479e8b72c6 --- /dev/null +++ b/activerecord/test/models/reference.rb @@ -0,0 +1,4 @@ +class Reference < ActiveRecord::Base + belongs_to :person + belongs_to :job +end diff --git a/activerecord/test/models/reply.rb b/activerecord/test/models/reply.rb index 27621bd703..812bc1f535 100755 --- a/activerecord/test/models/reply.rb +++ b/activerecord/test/models/reply.rb @@ -1,6 +1,8 @@ require 'models/topic' class Reply < Topic + named_scope :base + belongs_to :topic, :foreign_key => "parent_id", :counter_cache => true has_many :replies, :class_name => "SillyReply", :dependent => :destroy, :foreign_key => "parent_id" diff --git a/activerecord/test/models/subscriber.rb b/activerecord/test/models/subscriber.rb index 51335a8f20..5b78014e6f 100644 --- a/activerecord/test/models/subscriber.rb +++ b/activerecord/test/models/subscriber.rb @@ -1,5 +1,7 @@ class Subscriber < ActiveRecord::Base set_primary_key 'nick' + has_many :subscriptions + has_many :books, :through => :subscriptions end class SpecialSubscriber < Subscriber diff --git a/activerecord/test/models/subscription.rb b/activerecord/test/models/subscription.rb new file mode 100644 index 0000000000..4bdb36ea46 --- /dev/null +++ b/activerecord/test/models/subscription.rb @@ -0,0 +1,4 @@ +class Subscription < ActiveRecord::Base + belongs_to :subscriber + belongs_to :book +end diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index d2503b78df..f63e862cfd 100755 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -1,4 +1,5 @@ class Topic < ActiveRecord::Base + named_scope :base named_scope :written_before, lambda { |time| { :conditions => ['written_on < ?', time] } } diff --git a/activerecord/test/schema/postgresql_specific_schema.rb b/activerecord/test/schema/postgresql_specific_schema.rb index 35e9ecf64d..576a4d03c6 100644 --- a/activerecord/test/schema/postgresql_specific_schema.rb +++ b/activerecord/test/schema/postgresql_specific_schema.rb @@ -2,7 +2,7 @@ ActiveRecord::Schema.define do %w(postgresql_arrays postgresql_moneys postgresql_numbers postgresql_times postgresql_network_addresses postgresql_bit_strings postgresql_oids defaults geometrics).each do |table_name| - drop_table table_name + execute "DROP TABLE IF EXISTS #{quote_table_name table_name}" end execute 'DROP SEQUENCE IF EXISTS companies_nonstd_seq CASCADE' diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 2e78844c9b..818237f076 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -162,6 +162,11 @@ ActiveRecord::Schema.define do t.column :type, :string end + + create_table :jobs, :force => true do |t| + t.integer :ideal_reference_id + end + create_table :keyboards, :force => true, :id => false do |t| t.primary_key :key_number t.string :name @@ -197,6 +202,13 @@ ActiveRecord::Schema.define do t.string :type end + create_table :references, :force => true do |t| + t.integer :person_id + t.integer :job_id + t.boolean :favourite + t.integer :lock_version, :default => 0 + end + create_table :minimalistics, :force => true do |t| end @@ -337,6 +349,11 @@ ActiveRecord::Schema.define do end add_index :subscribers, :nick, :unique => true + create_table :subscriptions, :force => true do |t| + t.string :subscriber_id + t.integer :book_id + end + create_table :tasks, :force => true do |t| t.datetime :starting t.datetime :ending |