aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/test')
-rw-r--r--activerecord/test/associations_extensions_test.rb4
-rwxr-xr-xactiverecord/test/associations_test.rb8
-rw-r--r--activerecord/test/conditions_scoping_test.rb13
-rw-r--r--activerecord/test/finder_test.rb14
-rw-r--r--activerecord/test/fixtures/comment.rb12
-rw-r--r--activerecord/test/fixtures/post.rb3
6 files changed, 43 insertions, 11 deletions
diff --git a/activerecord/test/associations_extensions_test.rb b/activerecord/test/associations_extensions_test.rb
index 92bdb86eb5..fa9674a149 100644
--- a/activerecord/test/associations_extensions_test.rb
+++ b/activerecord/test/associations_extensions_test.rb
@@ -1,9 +1,11 @@
require 'abstract_unit'
+require 'fixtures/post'
+require 'fixtures/comment'
require 'fixtures/project'
require 'fixtures/developer'
class AssociationsExtensionsTest < Test::Unit::TestCase
- fixtures :projects, :developers
+ fixtures :projects, :developers, :comments, :posts
def test_extension_on_habtm
assert_equal projects(:action_controller), developers(:david).projects.find_most_recent
diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb
index 6a4beb4135..fc2afb721f 100755
--- a/activerecord/test/associations_test.rb
+++ b/activerecord/test/associations_test.rb
@@ -490,6 +490,14 @@ class HasManyAssociationsTest < Test::Unit::TestCase
assert_equal 3, companies(:first_firm).clients_of_firm(true).size
end
+ def test_find_or_create
+ 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, :refresh).clients.size
+ assert_equal the_client, companies(:first_firm).clients.find_or_create_by_name("Yet another client")
+ assert_equal number_of_clients + 1, companies(:first_firm, :refresh).clients.size
+ end
+
def test_deleting
force_signal37_to_load_all_clients_of_firm
companies(:first_firm).clients_of_firm.delete(companies(:first_firm).clients_of_firm.first)
diff --git a/activerecord/test/conditions_scoping_test.rb b/activerecord/test/conditions_scoping_test.rb
index a997ee9e85..cf8b670c26 100644
--- a/activerecord/test/conditions_scoping_test.rb
+++ b/activerecord/test/conditions_scoping_test.rb
@@ -5,7 +5,7 @@ require 'fixtures/post'
require 'fixtures/category'
class ConditionsScopingTest < Test::Unit::TestCase
- fixtures :developers
+ fixtures :developers, :comments, :posts
def test_set_conditions
Developer.constrain(:conditions => 'just a test...') do
@@ -42,6 +42,17 @@ class ConditionsScopingTest < Test::Unit::TestCase
end
end
+ def test_scoped_create
+ new_comment = nil
+
+ VerySpecialComment.constrain(:creation => { :post_id => 1 }) do
+ assert_equal({ :post_id => 1 }, Thread.current[:constraints][VerySpecialComment][:creation])
+ new_comment = VerySpecialComment.create :body => "Wonderful world"
+ end
+
+ assert Post.find(1).comments.include?(new_comment)
+ end
+
def test_immutable_constraint
options = { :conditions => "name = 'David'" }
Developer.constrain(options) do
diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb
index 7c30308a48..dc3464727e 100644
--- a/activerecord/test/finder_test.rb
+++ b/activerecord/test/finder_test.rb
@@ -278,6 +278,20 @@ class FinderTest < Test::Unit::TestCase
assert_equal "Mary", topics[0].author_name
end
+ def test_find_or_create_from_one_attribute
+ number_of_companies = Company.count
+ sig38 = Company.find_or_create_by_name("38signals")
+ assert_equal number_of_companies + 1, Company.count
+ assert_equal sig38, Company.find_or_create_by_name("38signals")
+ end
+
+ def test_find_or_create_from_two_attributes
+ number_of_companies = Company.count
+ sig38 = Company.find_or_create_by_name("38signals")
+ assert_equal number_of_companies + 1, Company.count
+ assert_equal sig38, Company.find_or_create_by_name("38signals")
+ end
+
def test_find_with_bad_sql
assert_raises(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" }
end
diff --git a/activerecord/test/fixtures/comment.rb b/activerecord/test/fixtures/comment.rb
index 0605fd7046..fd3a43ff5a 100644
--- a/activerecord/test/fixtures/comment.rb
+++ b/activerecord/test/fixtures/comment.rb
@@ -10,18 +10,14 @@ class Comment < ActiveRecord::Base
end
end
-class SpecialComment < Comment;
-
+class SpecialComment < Comment
def self.what_are_you
'a special comment...'
end
-
-end;
+end
-class VerySpecialComment < Comment;
-
+class VerySpecialComment < Comment
def self.what_are_you
'a very special comment...'
end
-
-end;
+end \ No newline at end of file
diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb
index 6163ec90f8..1a34823ca0 100644
--- a/activerecord/test/fixtures/post.rb
+++ b/activerecord/test/fixtures/post.rb
@@ -11,8 +11,9 @@ class Post < ActiveRecord::Base
end
}
+ has_one :very_special_comment
+ has_many :special_comments
- has_many :special_comments, :class_name => "SpecialComment"
has_and_belongs_to_many :categories
has_and_belongs_to_many :special_categories, :join_table => "categories_posts"