1 require File.dirname(__FILE__) + '/helper'
2 require File.dirname(__FILE__) + '/../init'
4 class PaginationTest < ActiveRecordTestCase
5 fixtures :topics, :replies, :developers, :projects, :developers_projects
7 class PaginationController < ActionController::Base
8 if respond_to? :view_paths=
9 self.view_paths = [ "#{File.dirname(__FILE__)}/../fixtures/" ]
11 self.template_root = [ "#{File.dirname(__FILE__)}/../fixtures/" ]
15 @topic_pages, @topics = paginate(:topics)
16 render :nothing => true
19 def paginate_with_per_page
20 @topic_pages, @topics = paginate(:topics, :per_page => 1)
21 render :nothing => true
24 def paginate_with_order
25 @topic_pages, @topics = paginate(:topics, :order => 'created_at asc')
26 render :nothing => true
29 def paginate_with_order_by
30 @topic_pages, @topics = paginate(:topics, :order_by => 'created_at asc')
31 render :nothing => true
34 def paginate_with_include_and_order
35 @topic_pages, @topics = paginate(:topics, :include => :replies, :order => 'replies.created_at asc, topics.created_at asc')
36 render :nothing => true
39 def paginate_with_conditions
40 @topic_pages, @topics = paginate(:topics, :conditions => ["created_at > ?", 30.minutes.ago])
41 render :nothing => true
44 def paginate_with_class_name
45 @developer_pages, @developers = paginate(:developers, :class_name => "DeVeLoPeR")
46 render :nothing => true
49 def paginate_with_singular_name
50 @developer_pages, @developers = paginate()
51 render :nothing => true
54 def paginate_with_joins
55 @developer_pages, @developers = paginate(:developers,
56 :joins => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
57 :conditions => 'project_id=1')
58 render :nothing => true
61 def paginate_with_join
62 @developer_pages, @developers = paginate(:developers,
63 :join => 'LEFT JOIN developers_projects ON developers.id = developers_projects.developer_id',
64 :conditions => 'project_id=1')
65 render :nothing => true
68 def paginate_with_join_and_count
69 @developer_pages, @developers = paginate(:developers,
70 :join => 'd LEFT JOIN developers_projects ON d.id = developers_projects.developer_id',
71 :conditions => 'project_id=1',
73 render :nothing => true
76 def paginate_with_join_and_group
77 @developer_pages, @developers = paginate(:developers,
78 :join => 'INNER JOIN developers_projects ON developers.id = developers_projects.developer_id',
79 :group => 'developers.id')
80 render :nothing => true
83 def rescue_errors(e) raise e end
85 def rescue_action(e) raise end
90 @controller = PaginationController.new
91 @request = ActionController::TestRequest.new
92 @response = ActionController::TestResponse.new
96 # Single Action Pagination Tests
98 def test_simple_paginate
100 assert_equal 1, assigns(:topic_pages).page_count
101 assert_equal 3, assigns(:topics).size
104 def test_paginate_with_per_page
105 get :paginate_with_per_page
106 assert_equal 1, assigns(:topics).size
107 assert_equal 3, assigns(:topic_pages).page_count
110 def test_paginate_with_order
111 get :paginate_with_order
112 expected = [topics(:futurama),
113 topics(:harvey_birdman),
115 assert_equal expected, assigns(:topics)
116 assert_equal 1, assigns(:topic_pages).page_count
119 def test_paginate_with_order_by
120 get :paginate_with_order
121 expected = assigns(:topics)
122 get :paginate_with_order_by
123 assert_equal expected, assigns(:topics)
124 assert_equal 1, assigns(:topic_pages).page_count
127 def test_paginate_with_conditions
128 get :paginate_with_conditions
129 expected = [topics(:rails)]
130 assert_equal expected, assigns(:topics)
131 assert_equal 1, assigns(:topic_pages).page_count
134 def test_paginate_with_class_name
135 get :paginate_with_class_name
137 assert assigns(:developers).size > 0
138 assert_equal DeVeLoPeR, assigns(:developers).first.class
141 def test_paginate_with_joins
142 get :paginate_with_joins
143 assert_equal 2, assigns(:developers).size
144 developer_names = assigns(:developers).map { |d| d.name }
145 assert developer_names.include?('David')
146 assert developer_names.include?('Jamis')
149 def test_paginate_with_join_and_conditions
150 get :paginate_with_joins
151 expected = assigns(:developers)
152 get :paginate_with_join
153 assert_equal expected, assigns(:developers)
156 def test_paginate_with_join_and_count
157 get :paginate_with_joins
158 expected = assigns(:developers)
159 get :paginate_with_join_and_count
160 assert_equal expected, assigns(:developers)
163 def test_paginate_with_include_and_order
164 get :paginate_with_include_and_order
165 expected = Topic.find(:all, :include => 'replies', :order => 'replies.created_at asc, topics.created_at asc', :limit => 10)
166 assert_equal expected, assigns(:topics)
169 def test_paginate_with_join_and_group
170 get :paginate_with_join_and_group
171 assert_equal 2, assigns(:developers).size
172 assert_equal 2, assigns(:developer_pages).item_count
173 developer_names = assigns(:developers).map { |d| d.name }
174 assert developer_names.include?('David')
175 assert developer_names.include?('Jamis')