From abc895b82829657d34f4902ce0cf04f0682bab63 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 3 Apr 2005 10:52:05 +0000 Subject: Added new Base.find API and deprecated find_all, find_first. Added preliminary support for eager loading of associations git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1077 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/associations_test.rb | 24 +- activerecord/test/deprecated_finder_test.rb | 138 ++++++++++ activerecord/test/finder_test.rb | 283 --------------------- activerecord/test/fixtures/author.rb | 3 + activerecord/test/fixtures/authors.yml | 9 + activerecord/test/fixtures/comment.rb | 3 + activerecord/test/fixtures/comments.yml | 14 + activerecord/test/fixtures/courses.yml | 8 +- .../test/fixtures/db_definitions/sqlite.drop.sql | 4 +- .../test/fixtures/db_definitions/sqlite.sql | 18 ++ activerecord/test/fixtures/post.rb | 4 + activerecord/test/fixtures/posts.yml | 9 + 12 files changed, 228 insertions(+), 289 deletions(-) create mode 100755 activerecord/test/deprecated_finder_test.rb delete mode 100755 activerecord/test/finder_test.rb create mode 100644 activerecord/test/fixtures/author.rb create mode 100644 activerecord/test/fixtures/authors.yml create mode 100644 activerecord/test/fixtures/comment.rb create mode 100644 activerecord/test/fixtures/comments.yml create mode 100644 activerecord/test/fixtures/post.rb create mode 100644 activerecord/test/fixtures/posts.yml (limited to 'activerecord/test') diff --git a/activerecord/test/associations_test.rb b/activerecord/test/associations_test.rb index c4f1022076..68245379ed 100755 --- a/activerecord/test/associations_test.rb +++ b/activerecord/test/associations_test.rb @@ -5,6 +5,9 @@ require 'fixtures/company' require 'fixtures/topic' require 'fixtures/reply' require 'fixtures/computer' +require 'fixtures/post' +require 'fixtures/comment' +require 'fixtures/author' # Can't declare new classes in test case methods, so tests before that bad_collection_keys = false @@ -203,8 +206,9 @@ end class HasManyAssociationsTest < Test::Unit::TestCase + fixtures :accounts, :companies, :developers, :projects, :developers_projects, :topics, :posts, :comments + def setup - create_fixtures "accounts", "companies", "developers", "projects", "developers_projects", "topics" @signals37 = Firm.find(1) end @@ -530,6 +534,24 @@ class HasManyAssociationsTest < Test::Unit::TestCase def test_adding_array_and_collection assert_nothing_raised { Firm.find_first.clients + Firm.find_all.last.clients } end + + def test_eager_association_loading_with_one_association + posts = Post.find(:all, :include => :comments) + assert_equal 2, posts.first.comments.size + assert_equal @greetings.body, posts.first.comments.first.body + end + + def test_eager_association_loading_with_multiple_associations + posts = Post.find(:all, :include => [ :comments, :author ]) + assert_equal 2, posts.first.comments.size + assert_equal @greetings.body, posts.first.comments.first.body + end + + def xtest_eager_association_loading_with_belongs_to + comments = Comment.find(:all, :include => :post) + assert_equal @welcome.title, comments.first.post.title + assert_equal @thinking.title, comments.last.post.title + end end class BelongsToAssociationsTest < Test::Unit::TestCase diff --git a/activerecord/test/deprecated_finder_test.rb b/activerecord/test/deprecated_finder_test.rb new file mode 100755 index 0000000000..c9bf7c7f43 --- /dev/null +++ b/activerecord/test/deprecated_finder_test.rb @@ -0,0 +1,138 @@ +require 'abstract_unit' +require 'fixtures/company' +require 'fixtures/topic' +require 'fixtures/entrant' +require 'fixtures/developer' + +class FinderTest < Test::Unit::TestCase + fixtures :companies, :topics, :entrants, :developers + + def test_find_all_with_limit + entrants = Entrant.find_all nil, "id ASC", 2 + + assert_equal(2, entrants.size) + assert_equal(@entrants["first"]["name"], entrants.first.name) + end + + def test_find_all_with_prepared_limit_and_offset + if ActiveRecord::ConnectionAdapters.const_defined? :OracleAdapter + if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OracleAdapter) + assert_raises(ArgumentError) { Entrant.find_all nil, "id ASC", [2, 1] } + end + else + entrants = Entrant.find_all nil, "id ASC", [2, 1] + + assert_equal(2, entrants.size) + assert_equal(@entrants["second"]["name"], entrants.first.name) + end + end + + def test_find_first + first = Topic.find_first "title = 'The First Topic'" + assert_equal(@topics["first"]["title"], first.title) + end + + def test_find_first_failing + first = Topic.find_first "title = 'The First Topic!'" + assert_nil(first) + end + + def test_deprecated_find_on_conditions + assert Topic.find_on_conditions(1, "approved = 0") + assert_raises(ActiveRecord::RecordNotFound) { Topic.find_on_conditions(1, "approved = 1") } + end + + def test_condition_interpolation + assert_kind_of Firm, Company.find_first(["name = '%s'", "37signals"]) + assert_nil Company.find_first(["name = '%s'", "37signals!"]) + assert_nil Company.find_first(["name = '%s'", "37signals!' OR 1=1"]) + assert_kind_of Time, Topic.find_first(["id = %d", 1]).written_on + end + + def test_bind_variables + assert_kind_of Firm, Company.find_first(["name = ?", "37signals"]) + assert_nil Company.find_first(["name = ?", "37signals!"]) + assert_nil Company.find_first(["name = ?", "37signals!' OR 1=1"]) + assert_kind_of Time, Topic.find_first(["id = ?", 1]).written_on + assert_raises(ActiveRecord::PreparedStatementInvalid) { + Company.find_first(["id=? AND name = ?", 2]) + } + assert_raises(ActiveRecord::PreparedStatementInvalid) { + Company.find_first(["id=?", 2, 3, 4]) + } + end + + def test_bind_variables_with_quotes + Company.create("name" => "37signals' go'es agains") + assert Company.find_first(["name = ?", "37signals' go'es agains"]) + end + + def test_named_bind_variables_with_quotes + Company.create("name" => "37signals' go'es agains") + assert Company.find_first(["name = :name", {:name => "37signals' go'es agains"}]) + end + + def test_named_bind_variables + assert_equal '1', bind(':a', :a => 1) # ' ruby-mode + assert_equal '1 1', bind(':a :a', :a => 1) # ' ruby-mode + + assert_kind_of Firm, Company.find_first(["name = :name", { :name => "37signals" }]) + assert_nil Company.find_first(["name = :name", { :name => "37signals!" }]) + assert_nil Company.find_first(["name = :name", { :name => "37signals!' OR 1=1" }]) + assert_kind_of Time, Topic.find_first(["id = :id", { :id => 1 }]).written_on + assert_raises(ActiveRecord::PreparedStatementInvalid) { + Company.find_first(["id=:id and name=:name", { :id=>3 }]) + } + assert_raises(ActiveRecord::PreparedStatementInvalid) { + Company.find_first(["id=:id", { :id=>3, :name=>"37signals!" }]) + } + end + + def test_count + assert_equal(0, Entrant.count("id > 3")) + assert_equal(1, Entrant.count(["id > ?", 2])) + assert_equal(2, Entrant.count(["id > ?", 1])) + end + + def test_count_by_sql + assert_equal(0, Entrant.count_by_sql("SELECT COUNT(*) FROM entrants WHERE id > 3")) + assert_equal(1, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 2])) + assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1])) + end + + def test_find_all_with_limit + first_five_developers = Developer.find_all nil, 'id ASC', 5 + assert_equal 5, first_five_developers.length + assert_equal 'David', first_five_developers.first.name + assert_equal 'fixture_5', first_five_developers.last.name + + no_developers = Developer.find_all nil, 'id ASC', 0 + assert_equal 0, no_developers.length + + assert_equal first_five_developers, Developer.find_all(nil, 'id ASC', [5]) + assert_equal no_developers, Developer.find_all(nil, 'id ASC', [0]) + end + + def test_find_all_with_limit_and_offset + first_three_developers = Developer.find_all nil, 'id ASC', [3, 0] + second_three_developers = Developer.find_all nil, 'id ASC', [3, 3] + last_two_developers = Developer.find_all nil, 'id ASC', [2, 8] + + assert_equal 3, first_three_developers.length + assert_equal 3, second_three_developers.length + assert_equal 2, last_two_developers.length + + assert_equal 'David', first_three_developers.first.name + assert_equal 'fixture_4', second_three_developers.first.name + assert_equal 'fixture_9', last_two_developers.first.name + end + + protected + def bind(statement, *vars) + if vars.first.is_a?(Hash) + ActiveRecord::Base.send(:replace_named_bind_variables, statement, vars.first) + else + ActiveRecord::Base.send(:replace_bind_variables, statement, vars) + end + end +end diff --git a/activerecord/test/finder_test.rb b/activerecord/test/finder_test.rb deleted file mode 100755 index 6b6b58a9c8..0000000000 --- a/activerecord/test/finder_test.rb +++ /dev/null @@ -1,283 +0,0 @@ -require 'abstract_unit' -require 'fixtures/company' -require 'fixtures/topic' -require 'fixtures/entrant' -require 'fixtures/developer' - -class FinderTest < Test::Unit::TestCase - fixtures :companies, :topics, :entrants, :developers - - def test_find - assert_equal(@topics["first"]["title"], Topic.find(1).title) - end - - def test_exists - assert (Topic.exists?(1)) - assert !(Topic.exists?(45)) - assert !(Topic.exists?("foo")) - assert !(Topic.exists?([1,2])) - end - - def test_find_by_array_of_one_id - assert_kind_of(Array, Topic.find([ 1 ])) - assert_equal(1, Topic.find([ 1 ]).length) - end - - def test_find_by_ids - assert_equal(2, Topic.find(1, 2).length) - assert_equal(@topics["second"]["title"], Topic.find([ 2 ]).first.title) - end - - def test_find_by_ids_missing_one - assert_raises(ActiveRecord::RecordNotFound) { - Topic.find(1, 2, 45) - } - end - - def test_find_all_with_limit - entrants = Entrant.find_all nil, "id ASC", 2 - - assert_equal(2, entrants.size) - assert_equal(@entrants["first"]["name"], entrants.first.name) - end - - def test_find_all_with_prepared_limit_and_offset - if ActiveRecord::ConnectionAdapters.const_defined? :OracleAdapter - if ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters::OracleAdapter) - assert_raises(ArgumentError) { Entrant.find_all nil, "id ASC", [2, 1] } - end - else - entrants = Entrant.find_all nil, "id ASC", [2, 1] - - assert_equal(2, entrants.size) - assert_equal(@entrants["second"]["name"], entrants.first.name) - end - end - - def test_find_with_entire_select_statement - topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'" - - assert_equal(1, topics.size) - assert_equal(@topics["second"]["title"], topics.first.title) - end - - def test_find_with_prepared_select_statement - topics = Topic.find_by_sql ["SELECT * FROM topics WHERE author_name = ?", "Mary"] - - assert_equal(1, topics.size) - assert_equal(@topics["second"]["title"], topics.first.title) - end - - def test_find_first - first = Topic.find_first "title = 'The First Topic'" - assert_equal(@topics["first"]["title"], first.title) - end - - def test_find_first_failing - first = Topic.find_first "title = 'The First Topic!'" - assert_nil(first) - end - - def test_unexisting_record_exception_handling - assert_raises(ActiveRecord::RecordNotFound) { - Topic.find(1).parent - } - - Topic.find(2).parent - end - - def test_find_on_conditions - assert Topic.find(1, :conditions => "approved = 0") - assert_raises(ActiveRecord::RecordNotFound) { Topic.find(1, :conditions => "approved = 1") } - end - - def test_deprecated_find_on_conditions - assert Topic.find_on_conditions(1, "approved = 0") - assert_raises(ActiveRecord::RecordNotFound) { Topic.find_on_conditions(1, "approved = 1") } - end - - def test_condition_interpolation - assert_kind_of Firm, Company.find_first(["name = '%s'", "37signals"]) - assert_nil Company.find_first(["name = '%s'", "37signals!"]) - assert_nil Company.find_first(["name = '%s'", "37signals!' OR 1=1"]) - assert_kind_of Time, Topic.find_first(["id = %d", 1]).written_on - end - - def test_bind_variables - assert_kind_of Firm, Company.find_first(["name = ?", "37signals"]) - assert_nil Company.find_first(["name = ?", "37signals!"]) - assert_nil Company.find_first(["name = ?", "37signals!' OR 1=1"]) - assert_kind_of Time, Topic.find_first(["id = ?", 1]).written_on - assert_raises(ActiveRecord::PreparedStatementInvalid) { - Company.find_first(["id=? AND name = ?", 2]) - } - assert_raises(ActiveRecord::PreparedStatementInvalid) { - Company.find_first(["id=?", 2, 3, 4]) - } - end - - def test_bind_variables_with_quotes - Company.create("name" => "37signals' go'es agains") - assert Company.find_first(["name = ?", "37signals' go'es agains"]) - end - - def test_named_bind_variables_with_quotes - Company.create("name" => "37signals' go'es agains") - assert Company.find_first(["name = :name", {:name => "37signals' go'es agains"}]) - end - - def test_bind_arity - assert_nothing_raised { bind '' } - assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '', 1 } - - assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?' } - assert_nothing_raised { bind '?', 1 } - assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '?', 1, 1 } - end - - def test_named_bind_variables - assert_equal '1', bind(':a', :a => 1) # ' ruby-mode - assert_equal '1 1', bind(':a :a', :a => 1) # ' ruby-mode - - assert_kind_of Firm, Company.find_first(["name = :name", { :name => "37signals" }]) - assert_nil Company.find_first(["name = :name", { :name => "37signals!" }]) - assert_nil Company.find_first(["name = :name", { :name => "37signals!' OR 1=1" }]) - assert_kind_of Time, Topic.find_first(["id = :id", { :id => 1 }]).written_on - assert_raises(ActiveRecord::PreparedStatementInvalid) { - Company.find_first(["id=:id and name=:name", { :id=>3 }]) - } - assert_raises(ActiveRecord::PreparedStatementInvalid) { - Company.find_first(["id=:id", { :id=>3, :name=>"37signals!" }]) - } - end - - def test_named_bind_arity - assert_nothing_raised { bind '', {} } - assert_raises(ActiveRecord::PreparedStatementInvalid) { bind '', :a => 1 } - assert_raises(ActiveRecord::PreparedStatementInvalid) { bind ':a', {} } # ' ruby-mode - assert_nothing_raised { bind ':a', :a => 1 } # ' ruby-mode - assert_raises(ActiveRecord::PreparedStatementInvalid) { bind ':a', :a => 1, :b => 2 } # ' ruby-mode - assert_nothing_raised { bind ':a :a', :a => 1 } # ' ruby-mode - assert_raises(ActiveRecord::PreparedStatementInvalid) { bind ':a :a', :a => 1, :b => 2 } # ' ruby-mode - end - - def test_bind_array - assert_equal '1,2,3', bind('?', [1, 2, 3]) - assert_equal %('a','b','c'), bind('?', %w(a b c)) - - assert_equal '1,2,3', bind(':a', :a => [1, 2, 3]) - assert_equal %('a','b','c'), bind(':a', :a => %w(a b c)) - end - - def test_string_sanitation - assert_not_equal "'something ' 1=1'", ActiveRecord::Base.sanitize("something ' 1=1") - assert_equal "'something; select table'", ActiveRecord::Base.sanitize("something; select table") - end - - def test_count - assert_equal(0, Entrant.count("id > 3")) - assert_equal(1, Entrant.count(["id > ?", 2])) - assert_equal(2, Entrant.count(["id > ?", 1])) - end - - def test_count_by_sql - assert_equal(0, Entrant.count_by_sql("SELECT COUNT(*) FROM entrants WHERE id > 3")) - assert_equal(1, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 2])) - assert_equal(2, Entrant.count_by_sql(["SELECT COUNT(*) FROM entrants WHERE id > ?", 1])) - end - - def test_find_by_one_attribute - assert_equal @topics["first"].find, Topic.find_by_title("The First Topic") - assert_nil Topic.find_by_title("The First Topic!") - end - - def test_find_by_one_missing_attribute - assert_raises(NoMethodError) { Topic.find_by_undertitle("The First Topic!") } - end - - def test_find_by_two_attributes - assert_equal @topics["first"].find, Topic.find_by_title_and_author_name("The First Topic", "David") - assert_nil Topic.find_by_title_and_author_name("The First Topic", "Mary") - end - - def test_find_all_by_one_attribute - topics = Topic.find_all_by_content("Have a nice day") - assert_equal 2, topics.size - assert topics.include?(@topics["first"].find) - - assert_equal [], Topic.find_all_by_title("The First Topic!!") - end - - def test_find_all_by_boolean_attribute - topics = Topic.find_all_by_approved(false) - assert_equal 1, topics.size - assert topics.include?(@topics["first"].find) - - topics = Topic.find_all_by_approved(true) - assert_equal 1, topics.size - assert topics.include?(@topics["second"].find) - end - - def test_find_by_nil_attribute - topic = Topic.find_by_last_read nil - assert_not_nil topic - assert_nil topic.last_read - end - - def test_find_all_by_nil_attribute - topics = Topic.find_all_by_last_read nil - assert_equal 1, topics.size - assert_nil topics[0].last_read - end - - def test_find_by_nil_and_not_nil_attributes - topic = Topic.find_by_last_read_and_author_name nil, "Mary" - assert_equal "Mary", topic.author_name - end - - def test_find_all_by_nil_and_not_nil_attributes - topics = Topic.find_all_by_last_read_and_author_name nil, "Mary" - assert_equal 1, topics.size - assert_equal "Mary", topics[0].author_name - end - - def test_find_with_bad_sql - assert_raises(ActiveRecord::StatementInvalid) { Topic.find_by_sql "select 1 from badtable" } - end - - def test_find_all_with_limit - first_five_developers = Developer.find_all nil, 'id ASC', 5 - assert_equal 5, first_five_developers.length - assert_equal 'David', first_five_developers.first.name - assert_equal 'fixture_5', first_five_developers.last.name - - no_developers = Developer.find_all nil, 'id ASC', 0 - assert_equal 0, no_developers.length - - assert_equal first_five_developers, Developer.find_all(nil, 'id ASC', [5]) - assert_equal no_developers, Developer.find_all(nil, 'id ASC', [0]) - end - - def test_find_all_with_limit_and_offset - first_three_developers = Developer.find_all nil, 'id ASC', [3, 0] - second_three_developers = Developer.find_all nil, 'id ASC', [3, 3] - last_two_developers = Developer.find_all nil, 'id ASC', [2, 8] - - assert_equal 3, first_three_developers.length - assert_equal 3, second_three_developers.length - assert_equal 2, last_two_developers.length - - assert_equal 'David', first_three_developers.first.name - assert_equal 'fixture_4', second_three_developers.first.name - assert_equal 'fixture_9', last_two_developers.first.name - end - - protected - def bind(statement, *vars) - if vars.first.is_a?(Hash) - ActiveRecord::Base.send(:replace_named_bind_variables, statement, vars.first) - else - ActiveRecord::Base.send(:replace_bind_variables, statement, vars) - end - end -end diff --git a/activerecord/test/fixtures/author.rb b/activerecord/test/fixtures/author.rb new file mode 100644 index 0000000000..8d12190086 --- /dev/null +++ b/activerecord/test/fixtures/author.rb @@ -0,0 +1,3 @@ +class Author < ActiveRecord::Base + belongs_to :post +end \ No newline at end of file diff --git a/activerecord/test/fixtures/authors.yml b/activerecord/test/fixtures/authors.yml new file mode 100644 index 0000000000..718c317c37 --- /dev/null +++ b/activerecord/test/fixtures/authors.yml @@ -0,0 +1,9 @@ +david: + id: 1 + post_id: 1 + name: David + +mary: + id: 2 + post_id: 2 + name: Mary diff --git a/activerecord/test/fixtures/comment.rb b/activerecord/test/fixtures/comment.rb new file mode 100644 index 0000000000..662b7db66b --- /dev/null +++ b/activerecord/test/fixtures/comment.rb @@ -0,0 +1,3 @@ +class Comment < ActiveRecord::Base + belongs_to :post +end \ No newline at end of file diff --git a/activerecord/test/fixtures/comments.yml b/activerecord/test/fixtures/comments.yml new file mode 100644 index 0000000000..f970806b0b --- /dev/null +++ b/activerecord/test/fixtures/comments.yml @@ -0,0 +1,14 @@ +greetings: + id: 1 + post_id: 1 + body: Thank you for the welcome + +more_greetings: + id: 2 + post_id: 1 + body: Thank you again for the welcome + +does_it_hurt: + id: 3 + post_id: 2 + body: Don't think too hard \ No newline at end of file diff --git a/activerecord/test/fixtures/courses.yml b/activerecord/test/fixtures/courses.yml index d4c0e3cfb9..5ee1916003 100644 --- a/activerecord/test/fixtures/courses.yml +++ b/activerecord/test/fixtures/courses.yml @@ -1,7 +1,7 @@ -java: - id: 2 - name: Java Development - ruby: id: 1 name: Ruby Development + +java: + id: 2 + name: Java Development diff --git a/activerecord/test/fixtures/db_definitions/sqlite.drop.sql b/activerecord/test/fixtures/db_definitions/sqlite.drop.sql index 1f611c8d5a..3fd60de17b 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.drop.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.drop.sql @@ -15,4 +15,6 @@ DROP TABLE mixins; DROP TABLE people; DROP TABLE binaries; DROP TABLE computers; - +DROP TABLE posts; +DROP TABLE comments; +DROP TABLE authors; diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index d57c3889e0..a6f2efcbfe 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -116,3 +116,21 @@ CREATE TABLE 'computers' ( 'developer' INTEGER NOT NULL ); +CREATE TABLE 'posts' ( + 'id' INTEGER NOT NULL PRIMARY KEY, + 'title' VARCHAR(255) NOT NULL, + 'body' TEXT NOT NULL +); + +CREATE TABLE 'comments' ( + 'id' INTEGER NOT NULL PRIMARY KEY, + 'post_id' INTEGER NOT NULL, + 'body' TEXT NOT NULL +); + +CREATE TABLE 'authors' ( + 'id' INTEGER NOT NULL PRIMARY KEY, + 'post_id' INTEGER NOT NULL, + 'name' VARCHAR(255) NOT NULL +); + diff --git a/activerecord/test/fixtures/post.rb b/activerecord/test/fixtures/post.rb new file mode 100644 index 0000000000..1fd78b8343 --- /dev/null +++ b/activerecord/test/fixtures/post.rb @@ -0,0 +1,4 @@ +class Post < ActiveRecord::Base + has_many :comments + has_one :author +end \ No newline at end of file diff --git a/activerecord/test/fixtures/posts.yml b/activerecord/test/fixtures/posts.yml new file mode 100644 index 0000000000..21a110ef91 --- /dev/null +++ b/activerecord/test/fixtures/posts.yml @@ -0,0 +1,9 @@ +welcome: + id: 1 + title: Welcome to the weblog + body: Such a lovely day + +thinking: + id: 2 + title: So I was thinking + body: Like I hopefully always am -- cgit v1.2.3