aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/test/activerecord/pagination_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/test/activerecord/pagination_test.rb')
-rw-r--r--actionpack/test/activerecord/pagination_test.rb146
1 files changed, 146 insertions, 0 deletions
diff --git a/actionpack/test/activerecord/pagination_test.rb b/actionpack/test/activerecord/pagination_test.rb
new file mode 100644
index 0000000000..00ae78614f
--- /dev/null
+++ b/actionpack/test/activerecord/pagination_test.rb
@@ -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