Adding the classic_pagination to svn as the svn external is no longer working
[rails.git] / vendor / plugins / classic_pagination / test / pagination_test.rb
1 require File.dirname(__FILE__) + '/helper'
2 require File.dirname(__FILE__) + '/../init'
3
4 class PaginationTest < ActiveRecordTestCase
5   fixtures :topics, :replies, :developers, :projects, :developers_projects
6   
7   class PaginationController < ActionController::Base
8     if respond_to? :view_paths=
9       self.view_paths = [ "#{File.dirname(__FILE__)}/../fixtures/" ]
10     else
11       self.template_root = [ "#{File.dirname(__FILE__)}/../fixtures/" ]
12     end
13     
14     def simple_paginate
15       @topic_pages, @topics = paginate(:topics)
16       render :nothing => true
17     end
18     
19     def paginate_with_per_page
20       @topic_pages, @topics = paginate(:topics, :per_page => 1)
21       render :nothing => true
22     end
23     
24     def paginate_with_order
25       @topic_pages, @topics = paginate(:topics, :order => 'created_at asc')
26       render :nothing => true
27     end
28     
29     def paginate_with_order_by
30       @topic_pages, @topics = paginate(:topics, :order_by => 'created_at asc')
31       render :nothing => true
32     end
33     
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
37     end
38     
39     def paginate_with_conditions
40       @topic_pages, @topics = paginate(:topics, :conditions => ["created_at > ?", 30.minutes.ago])
41       render :nothing => true
42     end
43     
44     def paginate_with_class_name
45       @developer_pages, @developers = paginate(:developers, :class_name => "DeVeLoPeR")
46       render :nothing => true
47     end
48     
49     def paginate_with_singular_name
50       @developer_pages, @developers = paginate()
51       render :nothing => true
52     end
53     
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
59     end
60     
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
66     end
67      
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',
72                                              :count => "d.id")        
73       render :nothing => true
74     end
75
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
81     end
82     
83     def rescue_errors(e) raise e end
84
85     def rescue_action(e) raise end
86     
87   end
88   
89   def setup
90     @controller = PaginationController.new
91     @request    = ActionController::TestRequest.new
92     @response   = ActionController::TestResponse.new
93     super
94   end
95
96   # Single Action Pagination Tests
97
98   def test_simple_paginate
99     get :simple_paginate
100     assert_equal 1, assigns(:topic_pages).page_count
101     assert_equal 3, assigns(:topics).size
102   end
103   
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
108   end
109   
110   def test_paginate_with_order
111     get :paginate_with_order
112     expected = [topics(:futurama),
113                topics(:harvey_birdman),
114                topics(:rails)]
115     assert_equal expected, assigns(:topics)
116     assert_equal 1, assigns(:topic_pages).page_count
117   end
118   
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    
125   end
126   
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
132   end
133   
134   def test_paginate_with_class_name
135     get :paginate_with_class_name
136     
137     assert assigns(:developers).size > 0
138     assert_equal DeVeLoPeR, assigns(:developers).first.class
139   end
140       
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')
147   end
148   
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)
154   end
155   
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)
161   end
162   
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)
167   end
168
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')
176   end
177 end