Permalink
Browse files

* Fix pagination problems when using include

* Introduce Unit Tests for pagination
* Allow count to work with :include by using count distinct.

[Kevin Clark & Jeremy Hopple]


git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3553 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 803b9a4 commit d49a5fcb4cfe90824337dc3756bae7161cea768b @NZKoz NZKoz committed Feb 9, 2006
View
@@ -1,5 +1,11 @@
*SVN*
+* Fix problems with pagination and :include. [Kevin Clark]
+
+* Add ActiveRecordTestCase for testing AR integration. [Kevin Clark]
+
+* Add Unit Tests for pagination [Kevin Clark]
+
* Add :html option for specifying form tag options in form_for. [Sam Stephenson]
* Replace dubious controller parent class in filter docs. #3655, #3722 [info@rhalff.com, eigentone@gmail.com]
View
@@ -31,6 +31,13 @@ Rake::TestTask.new { |t|
t.verbose = true
}
+desc 'ActiveRecord Integration Tests'
+Rake::TestTask.new(:test_active_record_integration) do |t|
+ t.libs << "test"
+ t.test_files = Dir.glob("test/activerecord/*_test.rb")
+ t.verbose = true
+end
+
# Genereate the RDoc documentation
@@ -163,10 +163,11 @@ def create_paginators_and_retrieve_collections #:nodoc:
# Returns the total number of items in the collection to be paginated for
# the +model+ and given +conditions+. Override this method to implement a
# custom counter.
- def count_collection_for_pagination(model, conditions, joins)
- model.count(conditions,joins)
+ def count_collection_for_pagination(model, options)
+ model.count(:conditions => options[:conditions],
+ :joins => options[:join] || options[:joins], :include => options[:include])
end
-
+
# Returns a collection of items for the given +model+ and +options[conditions]+,
# ordered by +options[order]+, for the current page in the given +paginator+.
# Override this method to implement a custom finder.
@@ -185,12 +186,10 @@ def find_collection_for_pagination(model, options, paginator)
def paginator_and_collection_for(collection_id, options) #:nodoc:
klass = options[:class_name].constantize
page = @params[options[:parameter]]
- count = count_collection_for_pagination(klass, options[:conditions],
- options[:join] || options[:joins])
-
+ count = count_collection_for_pagination(klass, options)
paginator = Paginator.new(self, count, options[:per_page], page)
collection = find_collection_for_pagination(klass, options, paginator)
-
+
return paginator, collection
end
@@ -0,0 +1,88 @@
+require File.dirname(__FILE__) + '/abstract_unit'
+
+# Define the essentials
+class ActiveRecordTestConnector
+ cattr_accessor :able_to_connect
+ cattr_accessor :connected
+
+ # Set our defaults
+ self.connected = false
+ self.able_to_connect = true
+end
+
+# Try to grab AR
+begin
+ PATH_TO_AR = File.dirname(__FILE__) + '/../../activerecord'
+ require "#{PATH_TO_AR}/lib/active_record" unless Object.const_defined?(:ActiveRecord)
+ require "#{PATH_TO_AR}/lib/active_record/fixtures" unless Object.const_defined?(:Fixtures)
+rescue Object => e
+ $stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}"
+ ActiveRecordTestConnector.able_to_connect = false
+end
+
+# Define the rest of the connector
+class ActiveRecordTestConnector
+ def self.setup
+ unless self.connected || !self.able_to_connect
+ setup_connection
+ load_schema
+ self.connected = true
+ end
+ rescue Object => e
+ $stderr.puts "\nSkipping ActiveRecord assertion tests: #{e}"
+ #$stderr.puts " #{e.backtrace.join("\n ")}\n"
+ self.able_to_connect = false
+ end
+
+ private
+
+ def self.setup_connection
+ if Object.const_defined?(:ActiveRecord)
+
+ begin
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:')
+ ActiveRecord::Base.connection
+ rescue Object
+ $stderr.puts 'SQLite 3 unavailable; falling to SQLite 2.'
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite', :dbfile => ':memory:')
+ ActiveRecord::Base.connection
+ end
+
+ Object.send(:const_set, :QUOTED_TYPE, ActiveRecord::Base.connection.quote_column_name('type')) unless Object.const_defined?(:QUOTED_TYPE)
+ else
+ raise "Couldn't locate ActiveRecord."
+ end
+ end
+
+ # Load actionpack sqlite tables
+ def self.load_schema
+ File.read(File.dirname(__FILE__) + "/fixtures/db_definitions/sqlite.sql").split(';').each do |sql|
+ ActiveRecord::Base.connection.execute(sql) unless sql.blank?
+ end
+ end
+end
+
+# Test case for inheiritance
+class ActiveRecordTestCase < Test::Unit::TestCase
+ # Set our fixture path
+ self.fixture_path = "#{File.dirname(__FILE__)}/fixtures/"
+
+ def setup
+ abort_tests unless ActiveRecordTestConnector.connected = true
+ end
+
+ # Default so Test::Unit::TestCase doesn't complain
+ def test_truth
+ end
+
+ private
+
+ # If things go wrong, we don't want to run our test cases. We'll just define them to test nothing.
+ def abort_tests
+ self.class.public_instance_methods.grep(/^test./).each do |method|
+ self.class.class_eval { define_method(method.to_sym){} }
+ end
+ end
+end
+
+ActiveRecordTestConnector.setup
@@ -1,45 +1,6 @@
-require "#{File.dirname(__FILE__)}/../abstract_unit"
-
-# Unfurl the safety net.
-path_to_ar = File.dirname(__FILE__) + '/../../../activerecord'
-if Object.const_defined?(:ActiveRecord) || File.exist?(path_to_ar)
- begin
-
-# These tests require Active Record, so you're going to need AR in a
-# sibling directory to AP and have SQLite installed.
-
-unless Object.const_defined?(:ActiveRecord)
- require "#{path_to_ar}/lib/active_record"
-end
-
-begin
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:')
- ActiveRecord::Base.connection
-rescue Object
- $stderr.puts 'SQLite 3 unavailable; falling to SQLite 2.'
- ActiveRecord::Base.establish_connection(:adapter => 'sqlite', :dbfile => ':memory:')
- ActiveRecord::Base.connection
-end
-
-# Set up company fixtures.
-$LOAD_PATH << "#{path_to_ar}/test"
-QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type') unless Object.const_defined?(:QUOTED_TYPE)
+require "#{File.dirname(__FILE__)}/../active_record_unit"
require 'fixtures/company'
-File.read("#{path_to_ar}/test/fixtures/db_definitions/sqlite.sql").split(';').each do |sql|
- ActiveRecord::Base.connection.execute(sql) unless sql.blank?
-end
-
-# Add some validation rules to trip up the assertions.
-class Company
- protected
- def validate
- errors.add_on_empty('name')
- errors.add('rating', 'rating should not be 2') if rating == 2
- errors.add_to_base('oh oh') if rating == 3
- end
-end
-# A controller to host the assertions.
class ActiveRecordAssertionsController < ActionController::Base
self.template_root = "#{File.dirname(__FILE__)}/../fixtures/"
@@ -76,12 +37,15 @@ def bad_company
# the safety dance......
def rescue_action(e) raise; end
end
-
-class ActiveRecordAssertionsControllerTest < Test::Unit::TestCase
+
+class ActiveRecordAssertionsControllerTest < ActiveRecordTestCase
+ fixtures :companies
+
def setup
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
@controller = ActiveRecordAssertionsController.new
+ super
end
# test for 1 bad apple column
@@ -117,11 +81,4 @@ def test_invalid_record
assert_success
assert_invalid_record 'company'
end
-end
-
-# End of safety net.
- rescue Object => e
- $stderr.puts "Skipping Active Record assertion tests: #{e}"
- #$stderr.puts " #{e.backtrace.join("\n ")}"
- end
-end
+end
@@ -0,0 +1,146 @@
+require File.dirname(__FILE__) + '/../active_record_unit'
+
+require 'fixtures/topic'
+require 'fixtures/reply'
+require 'fixtures/developer'
+require 'fixtures/project'
+
+class PaginationTest < ActiveRecordTestCase
+ fixtures :topics, :replies, :developers, :projects, :developers_projects
+
+ class PaginationController < ActionController::Base
+ self.template_root = "#{File.dirname(__FILE__)}/../fixtures/"
+
+ def simple_paginate
+ @topic_pages, @topics = paginate(:topics)
+ render :nothing => true
+ end
+
+ def paginate_with_per_page
+ @topic_pages, @topics = paginate(:topics, :per_page => 1)
+ render :nothing => true
+ end
+
+ def paginate_with_order
+ @topic_pages, @topics = paginate(:topics, :order => 'created_at asc')
+ render :nothing => true
+ end
+
+ def paginate_with_order_by
+ @topic_pages, @topics = paginate(:topics, :order_by => 'created_at asc')
+ render :nothing => true
+ end
+
+ def paginate_with_include_and_order
+ @topic_pages, @topics = paginate(:topics, :include => :replies, :order => 'replies.created_at asc, topics.created_at asc')
+ render :nothing => true
+ end
+
+ def paginate_with_conditions
+ @topic_pages, @topics = paginate(:topics, :conditions => ["created_at > ?", 30.minutes.ago])
+ render :nothing => true
+ end
+
+ def paginate_with_class_name
+ @developer_pages, @developers = paginate(:developers, :class_name => "DeVeLoPeR")
+ render :nothing => true
+ end
+
+ def paginate_with_singular_name
+ @developer_pages, @developers = paginate()
+ render :nothing => true
+ end
+
+ def paginate_with_joins
+ @developer_pages, @developers = paginate(:developers,
+ :joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
+ :conditions => 'project_id=1')
+ render :nothing => true
+ end
+
+ def paginate_with_join
+ @developer_pages, @developers = paginate(:developers,
+ :join => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
+ :conditions => 'project_id=1')
+ render :nothing => true
+ end
+
+ def rescue_errors(e) raise e end
+
+ def rescue_action(e) raise end
+
+ end
+
+ def setup
+ @controller = PaginationController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ super
+ end
+
+ # Single Action Pagination Tests
+
+ def test_simple_paginate
+ get :simple_paginate
+ assert_equal 1, assigns(:topic_pages).page_count
+ assert_equal 3, assigns(:topics).size
+ end
+
+ def test_paginate_with_per_page
+ get :paginate_with_per_page
+ assert_equal 1, assigns(:topics).size
+ assert_equal 3, assigns(:topic_pages).page_count
+ end
+
+ def test_paginate_with_order
+ get :paginate_with_order
+ expected = [topics(:futurama),
+ topics(:harvey_birdman),
+ topics(:rails)]
+ assert_equal expected, assigns(:topics)
+ assert_equal 1, assigns(:topic_pages).page_count
+ end
+
+ def test_paginate_with_order_by
+ get :paginate_with_order
+ expected = assigns(:topics)
+ get :paginate_with_order_by
+ assert_equal expected, assigns(:topics)
+ assert_equal 1, assigns(:topic_pages).page_count
+ end
+
+ def test_paginate_with_conditions
+ get :paginate_with_conditions
+ expected = [topics(:rails)]
+ assert_equal expected, assigns(:topics)
+ assert_equal 1, assigns(:topic_pages).page_count
+ end
+
+ def test_paginate_with_class_name
+ get :paginate_with_class_name
+
+ assert assigns(:developers).size > 0
+ assert_equal DeVeLoPeR, assigns(:developers).first.class
+ end
+
+ def test_paginate_with_joins
+ get :paginate_with_joins
+ assert_equal 2, assigns(:developers).size
+ developer_names = assigns(:developers).map { |d| d.name }
+ assert developer_names.include?('David')
+ assert developer_names.include?('Jamis')
+ end
+
+ def test_paginate_with_join_and_conditions
+ get :paginate_with_joins
+ expected = assigns(:topics)
+ get :paginate_with_join
+ assert_equal expected, assigns(:topics)
+ end
+
+ def test_paginate_with_include_and_order
+ get :paginate_with_include_and_order
+ expected = Topic.find(:all, :include => 'replies', :order => 'replies.created_at asc, topics.created_at asc', :limit => 10)
+ assert_equal expected, assigns(:topics)
+ end
+end
@@ -0,0 +1,24 @@
+thirty_seven_signals:
+ id: 1
+ name: 37Signals
+ rating: 4
+
+TextDrive:
+ id: 2
+ name: TextDrive
+ rating: 4
+
+PlanetArgon:
+ id: 3
+ name: Planet Argon
+ rating: 4
+
+Google:
+ id: 4
+ name: Google
+ rating: 4
+
+Ionist:
+ id: 5
+ name: Ioni.st
+ rating: 4
Oops, something went wrong.

0 comments on commit d49a5fc

Please sign in to comment.