CETiendaVirtual is an open source project powered by Assembla

Assembla offers free public and private SVN/Git repositories and project hosting with bug/issue tracking and collaboration tools.

cetiendavirtual

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
require File.dirname(__FILE__) + '/helper'
require File.dirname(__FILE__) + '/lib/activerecord_test_case'

require 'will_paginate'
WillPaginate.enable_activerecord

class FinderTest < ActiveRecordTestCase
  fixtures :topics, :replies, :users, :projects, :developers_projects

  def test_new_methods_presence
    assert_respond_to_all Topic, %w(per_page paginate paginate_by_sql)
  end

  def test_simple_paginate
    entries = Topic.paginate :page => nil
    assert_equal 1, entries.current_page
    assert_nil entries.previous_page
    assert_nil entries.next_page
    assert_equal 1, entries.page_count
    assert_equal 4, entries.size

    entries = Topic.paginate :page => 2
    assert_equal 2, entries.current_page
    assert_equal 1, entries.previous_page
    assert_equal 1, entries.page_count
    assert entries.empty?
  end

  def test_parameter_api
    # :page parameter in options is required!
    assert_raise(ArgumentError){ Topic.paginate }
    assert_raise(ArgumentError){ Topic.paginate({}) }

    # explicit :all should not break anything
    assert_equal Topic.paginate(:page => nil), Topic.paginate(:all, :page => 1)

    # :count could be nil and we should still not cry
    assert_nothing_raised { Topic.paginate :page => 1, :count => nil }
  end

  def test_paginate_with_per_page
    entries = Topic.paginate :page => 1, :per_page => 1
    assert_equal 1, entries.size
    assert_equal 4, entries.page_count

    # Developer class has explicit per_page at 10
    entries = Developer.paginate :page => 1
    assert_equal 10, entries.size
    assert_equal 2, entries.page_count

    entries = Developer.paginate :page => 1, :per_page => 5
    assert_equal 11, entries.total_entries
    assert_equal 5, entries.size
    assert_equal 3, entries.page_count
  end

  def test_paginate_with_order
    entries = Topic.paginate :page => 1, :order => 'created_at desc'
    expected = [topics(:futurama), topics(:harvey_birdman), topics(:rails), topics(:ar)].reverse
    assert_equal expected, entries.to_a
    assert_equal 1, entries.page_count
  end

  def test_paginate_with_conditions
    entries = Topic.paginate :page => 1, :conditions => ["created_at > ?", 30.minutes.ago]
    expected = [topics(:rails), topics(:ar)]
    assert_equal expected, entries.to_a
    assert_equal 1, entries.page_count
  end

  def test_paginate_with_include_and_conditions
    entries = Topic.paginate \
      :page     => 1,
      :include  => :replies,
      :conditions => "replies.content LIKE 'Bird%' ",
      :per_page => 10

    expected = Topic.find :all,
      :include => 'replies',
      :conditions => "replies.content LIKE 'Bird%' ",
      :limit   => 10

    assert_equal expected, entries.to_a
    assert_equal 1, entries.total_entries
  end

  def test_paginate_with_include_and_order
    entries = Topic.paginate \
      :page     => 1,
      :include  => :replies,
      :order    => 'replies.created_at asc, topics.created_at asc',
      :per_page => 10

    expected = Topic.find :all,
      :include => 'replies',
      :order   => 'replies.created_at asc, topics.created_at asc',
      :limit   => 10

    assert_equal expected, entries.to_a
    assert_equal 4, entries.total_entries
  end

  def test_paginate_associations_with_include
    entries, project = nil, projects(:active_record)

    assert_nothing_raised "THIS IS A BUG in Rails 1.2.3 that was fixed in [7326]. " +
        "Please upgrade to the 1-2-stable branch or edge Rails." do
      entries = project.topics.paginate \
        :page     => 1,
        :include  => :replies,
        :conditions => "replies.content LIKE 'Nice%' ",
        :per_page => 10
    end

    expected = Topic.find :all,
      :include => 'replies',
      :conditions => "project_id = #{project.id} AND replies.content LIKE 'Nice%' ",
      :limit   => 10

    assert_equal expected, entries.to_a
  end

  def test_paginate_associations
    dhh = users :david
    expected_name_ordered = [projects(:action_controller), projects(:active_record)]
    expected_id_ordered   = [projects(:active_record), projects(:action_controller)]

    # with association-specified order
    entries = dhh.projects.paginate(:page => 1)
    assert_equal expected_name_ordered, entries
    assert_equal 2, entries.total_entries

    # with explicit order
    entries = dhh.projects.paginate(:page => 1, :order => 'projects.id')
    assert_equal expected_id_ordered, entries
    assert_equal 2, entries.total_entries

    assert_nothing_raised { dhh.projects.find(:all, :order => 'projects.id', :limit => 4) }
    entries = dhh.projects.paginate(:page => 1, :order => 'projects.id', :per_page => 4)
    assert_equal expected_id_ordered, entries

    # has_many with implicit order
    topic = Topic.find(1)
    expected = [replies(:spam), replies(:witty_retort)]
    assert_equal expected.map(&:id).sort, topic.replies.paginate(:page => 1).map(&:id).sort
    assert_equal expected.reverse, topic.replies.paginate(:page => 1, :order => 'replies.id ASC')
  end

  def test_paginate_association_extension
    project = Project.find(:first)
    entries = project.replies.paginate_recent :page => 1
    assert_equal [replies(:brave)], entries
  end

  def test_paginate_with_joins
    entries = Developer.paginate :page => 1,
                        :joins => 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id',
                        :conditions => 'project_id = 1'
    assert_equal 2, entries.size
    developer_names = entries.map { |d| d.name }
    assert developer_names.include?('David')
    assert developer_names.include?('Jamis')

    expected = entries.to_a
    entries = Developer.paginate :page => 1,
                        :joins => 'LEFT JOIN developers_projects ON users.id = developers_projects.developer_id',
                        :conditions => 'project_id = 1', :count => { :select => "users.id" }
    assert_equal expected, entries.to_a
  end

  def test_paginate_with_group
    entries = Developer.paginate :page => 1, :per_page => 10,
                                 :group => 'salary', :select => 'salary', :order => 'salary'
    expected = [ users(:david), users(:jamis), users(:dev_10), users(:poor_jamis) ].map(&:salary).sort
    assert_equal expected, entries.map(&:salary)
  end

  def test_paginate_with_dynamic_finder
    expected = [replies(:witty_retort), replies(:spam)]
    assert_equal expected, Reply.paginate_by_topic_id(1, :page => 1)

    entries = Developer.paginate :conditions => { :salary => 100000 }, :page => 1, :per_page => 5
    assert_equal 8, entries.total_entries
    assert_equal entries, Developer.paginate_by_salary(100000, :page => 1, :per_page => 5)

    # dynamic finder + conditions
    entries = Developer.paginate_by_salary(100000, :page => 1,
                                           :conditions => ['id > ?', 6])
    assert_equal 4, entries.total_entries
    assert_equal (7..10).to_a, entries.map(&:id)

    assert_raises NoMethodError do
      Developer.paginate_by_inexistent_attribute 100000, :page => 1
    end
  end

  def test_scoped_paginate
    entries = Developer.with_poor_ones { Developer.paginate :page => 1 }

    assert_equal 2, entries.size
    assert_equal 2, entries.total_entries
  end

  def test_readonly
    assert_nothing_raised { Developer.paginate :readonly => true, :page => 1 }
  end

  # this functionality is temporarily removed
  def xtest_pagination_defines_method
    pager = "paginate_by_created_at"
    assert !User.methods.include?(pager), "User methods should not include `#{pager}` method"
    # paginate!
    assert 0, User.send(pager, nil, :page => 1).total_entries
    # the paging finder should now be defined
    assert User.methods.include?(pager), "`#{pager}` method should be defined on User"
  end

  # Is this Rails 2.0? Find out by testing find_all which was removed in [6998]
  unless Developer.respond_to? :find_all
    def test_paginate_array_of_ids
      # AR finders also accept arrays of IDs
      # (this was broken in Rails before [6912])
      entries = Developer.paginate((1..8).to_a, :per_page => 3, :page => 2, :order => 'id')
      assert_equal (4..6).to_a, entries.map(&:id)
      assert_equal 8, entries.total_entries
    end
  end

  uses_mocha 'internals' do
    def test_implicit_all_with_dynamic_finders
      Topic.expects(:find_all_by_foo).returns([])
      Topic.expects(:count).returns(0)
      Topic.paginate_by_foo :page => 1
    end

    def test_guessing_the_total_count
      Topic.expects(:find).returns(Array.new(2))
      Topic.expects(:count).never

      entries = Topic.paginate :page => 2, :per_page => 4
      assert_equal 6, entries.total_entries
    end

    def test_extra_parameters_stay_untouched
      Topic.expects(:find).with(:all, {:foo => 'bar', :limit => 4, :offset => 0 }).returns(Array.new(5))
      Topic.expects(:count).with({:foo => 'bar'}).returns(1)

      Topic.paginate :foo => 'bar', :page => 1, :per_page => 4
    end

    def test_count_skips_select
      Developer.stubs(:find).returns([])
      Developer.expects(:count).with({}).returns(0)
      Developer.paginate :select => 'salary', :page => 1
    end

    def test_count_select_when_distinct
      Developer.stubs(:find).returns([])
      Developer.expects(:count).with(:select => 'DISTINCT salary').returns(0)
      Developer.paginate :select => 'DISTINCT salary', :page => 1
    end

    def test_should_use_scoped_finders_if_present
      # scope-out compatibility
      Topic.expects(:find_best).returns(Array.new(5))
      Topic.expects(:with_best).returns(1)

      Topic.paginate_best :page => 1, :per_page => 4
    end

    def test_paginate_by_sql
      assert_respond_to Developer, :paginate_by_sql
      Developer.expects(:find_by_sql).with(regexp_matches(/sql LIMIT 3(,| OFFSET) 3/)).returns([])
      Developer.expects(:count_by_sql).with('SELECT COUNT(*) FROM (sql) AS count_table').returns(0)

      entries = Developer.paginate_by_sql 'sql', :page => 2, :per_page => 3
    end

    def test_paginate_by_sql_respects_total_entries_setting
      Developer.expects(:find_by_sql).returns([])
      Developer.expects(:count_by_sql).never

      entries = Developer.paginate_by_sql 'sql', :page => 1, :total_entries => 999
      assert_equal 999, entries.total_entries
    end

    def test_paginate_by_sql_strips_order_by_when_counting
      Developer.expects(:find_by_sql).returns([])
      Developer.expects(:count_by_sql).with("SELECT COUNT(*) FROM (sql\n ) AS count_table").returns(0)

      entries = Developer.paginate_by_sql "sql\n ORDER\nby foo, bar, `baz` ASC", :page => 1
    end

    # TODO: counts are still wrong
    def test_ability_to_use_with_custom_finders
      # acts_as_taggable defines find_tagged_with(tag, options)
      Topic.expects(:find_tagged_with).with('will_paginate', :offset => 0, :limit => 5).returns([])
      Topic.expects(:count).with({}).returns(0)

      Topic.paginate_tagged_with 'will_paginate', :page => 1, :per_page => 5
    end

    def test_array_argument_doesnt_eliminate_count
      ids = (1..8).to_a
      Developer.expects(:find_all_by_id).returns([])
      Developer.expects(:count).returns(0)

      Developer.paginate_by_id(ids, :per_page => 3, :page => 2, :order => 'id')
    end

    def test_paginating_finder_doesnt_mangle_options
      Developer.expects(:find).returns([])
      Developer.expects(:count).returns(0)
      options = { :page => 1 }
      options.expects(:delete).never
      options_before = options.dup

      Developer.paginate(options)
      assert_equal options, options_before
    end
  end
end
Ajax-loader Loading, please wait...