diff options
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/callbacks_test.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/autosave_association_test.rb | 20 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/named_scope_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/relation_scoping_test.rb | 46 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 7 | ||||
-rw-r--r-- | activerecord/test/models/eye.rb | 37 | ||||
-rw-r--r-- | activerecord/test/models/post.rb | 6 | ||||
-rw-r--r-- | activerecord/test/models/topic.rb | 7 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 111 |
10 files changed, 191 insertions, 60 deletions
diff --git a/activerecord/test/cases/associations/callbacks_test.rb b/activerecord/test/cases/associations/callbacks_test.rb index 15537d6940..6a30e2905b 100644 --- a/activerecord/test/cases/associations/callbacks_test.rb +++ b/activerecord/test/cases/associations/callbacks_test.rb @@ -72,7 +72,7 @@ class AssociationCallbacksTest < ActiveRecord::TestCase def test_has_many_callbacks_for_save_on_parent jack = Author.new :name => "Jack" - post = jack.posts_with_callbacks.build :title => "Call me back!", :body => "Before you wake up and after you sleep" + jack.posts_with_callbacks.build :title => "Call me back!", :body => "Before you wake up and after you sleep" callback_log = ["before_adding<new>", "after_adding#{jack.posts_with_callbacks.first.id}"] assert_equal callback_log, jack.post_log @@ -149,7 +149,7 @@ class AssociationCallbacksTest < ActiveRecord::TestCase assert !@david.unchangable_posts.include?(@authorless) begin @david.unchangable_posts << @authorless - rescue Exception => e + rescue Exception end assert @david.post_log.empty? assert !@david.unchangable_posts.include?(@authorless) diff --git a/activerecord/test/cases/autosave_association_test.rb b/activerecord/test/cases/autosave_association_test.rb index 52382f5afc..89be94c81f 100644 --- a/activerecord/test/cases/autosave_association_test.rb +++ b/activerecord/test/cases/autosave_association_test.rb @@ -17,6 +17,7 @@ require 'models/tag' require 'models/tagging' require 'models/treasure' require 'models/company' +require 'models/eye' class TestAutosaveAssociationsInGeneral < ActiveRecord::TestCase def test_autosave_should_be_a_valid_option_for_has_one @@ -170,6 +171,25 @@ class TestDefaultAutosaveAssociationOnAHasOneAssociation < ActiveRecord::TestCas firm.account = Account.find(:first).clone assert_queries(2) { firm.save! } end + + def test_callbacks_firing_order_on_create + eye = Eye.create(:iris_attributes => {:color => 'honey'}) + assert_equal [true, false], eye.after_create_callbacks_stack + end + + def test_callbacks_firing_order_on_update + eye = Eye.create(:iris_attributes => {:color => 'honey'}) + eye.update_attributes(:iris_attributes => {:color => 'green'}) + assert_equal [true, false], eye.after_update_callbacks_stack + end + + def test_callbacks_firing_order_on_save + eye = Eye.create(:iris_attributes => {:color => 'honey'}) + assert_equal [false, false], eye.after_save_callbacks_stack + + eye.update_attributes(:iris_attributes => {:color => 'blue'}) + assert_equal [false, false, false, false], eye.after_save_callbacks_stack + end end class TestDefaultAutosaveAssociationOnABelongsToAssociation < ActiveRecord::TestCase diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 8acee9ac71..e63e1fbe09 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1150,6 +1150,12 @@ class BasicsTest < ActiveRecord::TestCase assert_equal 3, scoped_developers.size end + def test_no_limit_offset + assert_nothing_raised do + Developer.find(:all, :offset => 2) + end + end + def test_scoped_find_limit_offset scoped_developers = Developer.send(:with_scope, :find => { :limit => 3, :offset => 2 }) do Developer.find(:all, :order => 'id') @@ -1456,6 +1462,7 @@ class BasicsTest < ActiveRecord::TestCase UnloadablePost.class_eval do default_scope order('posts.comments_count ASC') end + UnloadablePost.scoped_methods # make Thread.current[:UnloadablePost_scoped_methods] not nil UnloadablePost.unloadable assert_not_nil Thread.current[:UnloadablePost_scoped_methods] diff --git a/activerecord/test/cases/named_scope_test.rb b/activerecord/test/cases/named_scope_test.rb index cc4438395e..fb24c65fff 100644 --- a/activerecord/test/cases/named_scope_test.rb +++ b/activerecord/test/cases/named_scope_test.rb @@ -125,6 +125,12 @@ class NamedScopeTest < ActiveRecord::TestCase assert_equal posts_with_authors_at_address_titles, Post.with_authors_at_address(address).find(:all, :select => 'title') end + def test_scope_with_object + objects = Topic.with_object + assert_operator objects.length, :>, 0 + assert objects.all?(&:approved?), 'all objects should be approved' + end + def test_extensions assert_equal 1, Topic.anonymous_extension.one assert_equal 2, Topic.named_extension.two diff --git a/activerecord/test/cases/relation_scoping_test.rb b/activerecord/test/cases/relation_scoping_test.rb index 64365c1d75..a27e2e72cd 100644 --- a/activerecord/test/cases/relation_scoping_test.rb +++ b/activerecord/test/cases/relation_scoping_test.rb @@ -311,6 +311,35 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal expected, received end + def test_default_scope_with_lambda + expected = Post.find_all_by_author_id(2) + PostForAuthor.selected_author = 2 + received = PostForAuthor.all + assert_equal expected, received + expected = Post.find_all_by_author_id(1) + PostForAuthor.selected_author = 1 + received = PostForAuthor.all + assert_equal expected, received + end + + def test_default_scope_with_thing_that_responds_to_call + klass = Class.new(ActiveRecord::Base) do + self.table_name = 'posts' + end + + klass.class_eval do + default_scope Class.new(Struct.new(:klass)) { + def call + klass.where(:author_id => 2) + end + }.new(self) + end + + records = klass.all + assert_equal 1, records.length + assert_equal 2, records.first.author_id + end + def test_default_scope_is_unscoped_on_find assert_equal 1, DeveloperCalledDavid.count assert_equal 11, DeveloperCalledDavid.unscoped.count @@ -364,6 +393,23 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal 100000, klass.first.salary end + def test_default_scope_called_twice_in_different_place_merges_where_clause + Developer.destroy_all + Developer.create!(:name => "David", :salary => 80000) + Developer.create!(:name => "David", :salary => 100000) + Developer.create!(:name => "Brian", :salary => 100000) + + klass = Class.new(Developer) + klass.class_eval do + default_scope where("name = 'David'") + default_scope where("salary = 100000") + end + + assert_equal 1, klass.count + assert_equal "David", klass.first.name + assert_equal 100000, klass.first.salary + end + def test_method_scope expected = Developer.find(:all, :order => 'salary DESC, name DESC').collect { |dev| dev.salary } received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index 222819cbe5..b01a8bbef1 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -25,13 +25,6 @@ class RelationTest < ActiveRecord::TestCase assert_no_queries { car.engines.length } end - def test_apply_relation_as_where_id - posts = Post.arel_table - post_authors = posts.where(posts[:author_id].eq(1)).project(posts[:id]) - assert_equal 5, post_authors.to_a.size - assert_equal 5, Post.where(:id => post_authors).size - end - def test_dynamic_finder x = Post.where('author_id = ?', 1) assert x.klass.respond_to?(:find_by_id), '@klass should handle dynamic finders' diff --git a/activerecord/test/models/eye.rb b/activerecord/test/models/eye.rb new file mode 100644 index 0000000000..77f17b578e --- /dev/null +++ b/activerecord/test/models/eye.rb @@ -0,0 +1,37 @@ +class Eye < ActiveRecord::Base + attr_reader :after_create_callbacks_stack + attr_reader :after_update_callbacks_stack + attr_reader :after_save_callbacks_stack + + # Callbacks configured before the ones has_one sets up. + after_create :trace_after_create + after_update :trace_after_update + after_save :trace_after_save + + has_one :iris + accepts_nested_attributes_for :iris + + # Callbacks configured after the ones has_one sets up. + after_create :trace_after_create2 + after_update :trace_after_update2 + after_save :trace_after_save2 + + def trace_after_create + (@after_create_callbacks_stack ||= []) << iris.new_record? + end + alias trace_after_create2 trace_after_create + + def trace_after_update + (@after_update_callbacks_stack ||= []) << iris.changed? + end + alias trace_after_update2 trace_after_update + + def trace_after_save + (@after_save_callbacks_stack ||= []) << iris.changed? + end + alias trace_after_save2 trace_after_save +end + +class Iris < ActiveRecord::Base + belongs_to :eye +end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index a3cb9c724a..61e782ff14 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -113,3 +113,9 @@ class PostWithComment < ActiveRecord::Base self.table_name = 'posts' default_scope where("posts.comments_count > 0").order("posts.comments_count ASC") end + +class PostForAuthor < ActiveRecord::Base + self.table_name = 'posts' + cattr_accessor :selected_author + default_scope lambda { where(:author_id => PostForAuthor.selected_author) } +end diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index ba2fe1987b..82d4b5997f 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -18,6 +18,13 @@ class Topic < ActiveRecord::Base 1 end end + + scope :with_object, Class.new(Struct.new(:klass)) { + def call + klass.where(:approved => true) + end + }.new(self) + module NamedExtension def two 2 diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index ea62833d81..bb80a1ca17 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -18,8 +18,13 @@ ActiveRecord::Schema.define do end - # Please keep these create table statements in alphabetical order - # unless the ordering matters. In which case, define them below + # ------------------------------------------------------------------- # + # # + # Please keep these create table statements in alphabetical order # + # unless the ordering matters. In which case, define them below. # + # # + # ------------------------------------------------------------------- # + create_table :accounts, :force => true do |t| t.integer :firm_id t.string :firm_name @@ -54,7 +59,6 @@ ActiveRecord::Schema.define do t.column :favorite_author_id, :integer end - create_table :auto_id_tests, :force => true, :id => false do |t| t.primary_key :auto_id t.integer :value @@ -78,6 +82,11 @@ ActiveRecord::Schema.define do t.boolean :value end + create_table :bulbs, :force => true do |t| + t.integer :car_id + t.string :name + end + create_table "CamelCase", :force => true do |t| t.string :name end @@ -194,15 +203,6 @@ ActiveRecord::Schema.define do t.integer :car_id end - create_table :tyres, :force => true do |t| - t.integer :car_id - end - - create_table :bulbs, :force => true do |t| - t.integer :car_id - t.string :name - end - create_table :entrants, :force => true do |t| t.string :name, :null => false t.integer :course_id, :null => false @@ -218,6 +218,9 @@ ActiveRecord::Schema.define do t.string :title, :limit => 5 end + create_table :eyes, :force => true do |t| + end + create_table :funny_jokes, :force => true do |t| t.string :name end @@ -227,13 +230,8 @@ ActiveRecord::Schema.define do t.string :info end - create_table :invoices, :force => true do |t| - t.integer :balance - t.datetime :updated_at - end - - create_table :items, :force => true do |t| - t.column :name, :string + create_table :guids, :force => true do |t| + t.column :key, :string end create_table :inept_wizards, :force => true do |t| @@ -242,6 +240,26 @@ ActiveRecord::Schema.define do t.column :type, :string end + create_table :integer_limits, :force => true do |t| + t.integer :"c_int_without_limit" + (1..8).each do |i| + t.integer :"c_int_#{i}", :limit => i + end + end + + create_table :invoices, :force => true do |t| + t.integer :balance + t.datetime :updated_at + end + + create_table :iris, :force => true do |t| + t.references :eye + t.string :color + end + + create_table :items, :force => true do |t| + t.column :name, :string + end create_table :jobs, :force => true do |t| t.integer :ideal_reference_id @@ -298,13 +316,6 @@ ActiveRecord::Schema.define do t.string :name 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 :minivans, :force => true, :id => false do |t| t.string :minivan_id t.string :name @@ -367,7 +378,6 @@ ActiveRecord::Schema.define do t.column :happy_at, :datetime end - create_table :paint_colors, :force => true do |t| t.integer :non_poly_one_id end @@ -454,6 +464,13 @@ ActiveRecord::Schema.define do t.boolean :skimmer, :default => false 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 :shape_expressions, :force => true do |t| t.string :paint_type t.integer :paint_id @@ -498,6 +515,18 @@ ActiveRecord::Schema.define do t.integer :book_id end + create_table :tags, :force => true do |t| + t.column :name, :string + t.column :taggings_count, :integer, :default => 0 + end + + create_table :taggings, :force => true do |t| + t.column :tag_id, :integer + t.column :super_tag_id, :integer + t.column :taggable_type, :string + t.column :taggable_id, :integer + end + create_table :tasks, :force => true do |t| t.datetime :starting t.datetime :ending @@ -525,18 +554,6 @@ ActiveRecord::Schema.define do t.string :group end - create_table :taggings, :force => true do |t| - t.column :tag_id, :integer - t.column :super_tag_id, :integer - t.column :taggable_type, :string - t.column :taggable_id, :integer - end - - create_table :tags, :force => true do |t| - t.column :name, :string - t.column :taggings_count, :integer, :default => 0 - end - create_table :toys, :primary_key => :toy_id ,:force => true do |t| t.string :name t.integer :pet_id, :integer @@ -556,6 +573,10 @@ ActiveRecord::Schema.define do t.column :looter_type, :string end + create_table :tyres, :force => true do |t| + t.integer :car_id + end + create_table :variants, :force => true do |t| t.references :product t.string :name @@ -573,17 +594,6 @@ ActiveRecord::Schema.define do create_table(t, :force => true) { } end - create_table :guids, :force => true do |t| - t.column :key, :string - end - - create_table :integer_limits, :force => true do |t| - t.integer :"c_int_without_limit" - (1..8).each do |i| - t.integer :"c_int_#{i}", :limit => i - end - end - # NOTE - the following 4 tables are used by models that have :inverse_of options on the associations create_table :men, :force => true do |t| t.string :name @@ -639,7 +649,6 @@ ActiveRecord::Schema.define do t.string :name end - except 'SQLite' do # fk_test_has_fk should be before fk_test_has_pk create_table :fk_test_has_fk, :force => true do |t| |