finder_test.rb 16.1 KB
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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
require 'helper'
require 'lib/activerecord_test_case'

require 'will_paginate'
WillPaginate.enable_activerecord
WillPaginate.enable_named_scope

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
    assert_queries(1) do
      entries = Topic.paginate :page => nil
      assert_equal 1, entries.current_page
      assert_equal 1, entries.total_pages
      assert_equal 4, entries.size
    end
    
    assert_queries(2) do
      entries = Topic.paginate :page => 2
      assert_equal 1, entries.total_pages
      assert entries.empty?
    end
  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.total_pages

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

    entries = Developer.paginate :page => 1, :per_page => 5
    assert_equal 11, entries.total_entries
    assert_equal 5, entries.size
    assert_equal 3, entries.total_pages
  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.total_pages
  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.total_pages
  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 = nil
    assert_queries(2) do
      entries = Topic.paginate \
        :page     => 1, 
        :include  => :replies,  
        :order    => 'replies.created_at asc, topics.created_at asc', 
        :per_page => 10
    end

    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 a newer version of 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)]

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

    # 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)
    
    assert_queries(2) do
      entries = project.replies.paginate_recent :page => 1
      assert_equal [replies(:brave)], entries
    end
  end
  
  def test_paginate_with_joins
    entries = nil
    
    assert_queries(1) do
      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 &:name
      assert developer_names.include?('David')
      assert developer_names.include?('Jamis')
    end

    assert_queries(1) do
      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
      assert_equal 2, entries.total_entries
    end
  end

  def test_paginate_with_group
    entries = nil
    assert_queries(1) do
      entries = Developer.paginate :page => 1, :per_page => 10,
                                   :group => 'salary', :select => 'salary', :order => 'salary'
    end
    
    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

  ## named_scope ##
  
  def test_paginate_in_named_scope
    entries = Developer.poor.paginate :page => 1, :per_page => 1

    assert_equal 1, entries.size
    assert_equal 2, entries.total_entries
  end
  
  def test_paginate_in_named_scope_on_habtm_association
    project = projects(:active_record)
    assert_queries(2) do
      entries = project.developers.poor.paginate :page => 1, :per_page => 1

      assert_equal 1, entries.size, 'one developer should be found'
      assert_equal 1, entries.total_entries, 'only one developer should be found'
    end
  end

  def test_paginate_in_named_scope_on_hmt_association
    project = projects(:active_record)
    expected = [replies(:brave)]
    
    assert_queries(2) do
      entries = project.replies.recent.paginate :page => 1, :per_page => 1
      assert_equal expected, entries
      assert_equal 1, entries.total_entries, 'only one reply should be found'
    end
  end

  def test_paginate_in_named_scope_on_has_many_association
    project = projects(:active_record)
    expected = [topics(:ar)]
    
    assert_queries(2) do
      entries = project.topics.mentions_activerecord.paginate :page => 1, :per_page => 1
      assert_equal expected, entries
      assert_equal 1, entries.total_entries, 'only one topic should be found'
    end
  end
  
  def test_named_scope_with_include
    project = projects(:active_record)
    entries = project.topics.with_replies_starting_with('AR ').paginate(:page => 1, :per_page => 1)
    assert_equal 1, entries.size
  end

  ## misc ##

  def test_count_and_total_entries_options_are_mutually_exclusive
    e = assert_raise ArgumentError do
      Developer.paginate :page => 1, :count => {}, :total_entries => 1
    end
    assert_match /exclusive/, e.to_s
  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_method?(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_method?(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 ActiveRecord::Base.respond_to? :find_all
    def test_paginate_array_of_ids
      # AR finders also accept arrays of IDs
      # (this was broken in Rails before [6912])
      assert_queries(1) do
        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
  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 => 2
    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_guessing_that_there_are_no_records
      Topic.expects(:find).returns([])
      Topic.expects(:count).never
      
      entries = Topic.paginate :page => 1, :per_page => 4
      assert_equal 0, 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 => 2
    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 => 2
    end

    def test_count_with_scoped_select_when_distinct
      Developer.stubs(:find).returns([])
      Developer.expects(:count).with(:select => 'DISTINCT users.id').returns(0)
      Developer.distinct.paginate :page => 2
    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
      sql = "SELECT * FROM users WHERE type = 'Developer' ORDER BY id"
      entries = Developer.paginate_by_sql(sql, :page => 2, :per_page => 3)
      assert_equal 11, entries.total_entries
      assert_equal [users(:dev_4), users(:dev_5), users(:dev_6)], entries
    end

    def test_paginate_by_sql_respects_total_entries_setting
      sql = "SELECT * FROM users"
      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)
      
      Developer.paginate_by_sql "sql\n ORDER\nby foo, bar, `baz` ASC", :page => 2
    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 => 5, :limit => 5).returns([])
      Topic.expects(:count).with({}).returns(0)
      
      Topic.paginate_tagged_with 'will_paginate', :page => 2, :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([])
      options = { :page => 1, :per_page => 2, :foo => 'bar' }
      options_before = options.dup
      
      Developer.paginate(options)
      assert_equal options_before, options
    end
    
    def test_paginate_by_sql_doesnt_change_original_query
      query = 'SQL QUERY'
      original_query = query.dup
      Developer.expects(:find_by_sql).returns([])
      
      Developer.paginate_by_sql query, :page => 1
      assert_equal original_query, query
    end

    def test_paginated_each
      collection = stub('collection', :size => 5, :empty? => false, :per_page => 5)
      collection.expects(:each).times(2).returns(collection)
      last_collection = stub('collection', :size => 4, :empty? => false, :per_page => 5)
      last_collection.expects(:each).returns(last_collection)
      
      params = { :order => 'id', :total_entries => 0 }
      
      Developer.expects(:paginate).with(params.merge(:page => 2)).returns(collection)
      Developer.expects(:paginate).with(params.merge(:page => 3)).returns(collection)
      Developer.expects(:paginate).with(params.merge(:page => 4)).returns(last_collection)
      
      assert_equal 14, Developer.paginated_each(:page => '2') { }
    end

    def test_paginated_each_with_named_scope
      assert_equal 2, Developer.poor.paginated_each(:per_page => 1) {
        assert_equal 11, Developer.count
      }
    end

    # detect ActiveRecord 2.1
    if ActiveRecord::Base.private_methods.include_method?(:references_eager_loaded_tables?)
      def test_removes_irrelevant_includes_in_count
        Developer.expects(:find).returns([1])
        Developer.expects(:count).with({}).returns(0)

        Developer.paginate :page => 1, :per_page => 1, :include => :projects
      end

      def test_doesnt_remove_referenced_includes_in_count
        Developer.expects(:find).returns([1])
        Developer.expects(:count).with({ :include => :projects, :conditions => 'projects.id > 2' }).returns(0)

        Developer.paginate :page => 1, :per_page => 1,
          :include => :projects, :conditions => 'projects.id > 2'
      end
    end
    
    def test_paginate_from
      result = Developer.paginate(:from => 'users', :page => 1, :per_page => 1)
      assert_equal 1, result.size
    end
    
    def test_hmt_with_include
      # ticket #220
      reply = projects(:active_record).replies.find(:first, :order => 'replies.id')
      assert_equal replies(:decisive), reply
      
      # ticket #223
      Project.find(1, :include => :replies)
      
      # I cannot reproduce any of the failures from those reports :(
    end
  end
end