File: active_record_spec.rb

package info (click to toggle)
ruby-api-pagination 4.8.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 272 kB
  • sloc: ruby: 1,125; makefile: 3
file content (50 lines) | stat: -rw-r--r-- 1,499 bytes parent folder | download | duplicates (2)
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
require 'spec_helper'
require 'support/active_record/foo'
require 'nulldb_rspec'

ActiveRecord::Base.establish_connection(
  adapter: :nulldb,
  schema: 'spec/support/active_record/schema.rb'
)

NullDB.configure { |ndb| def ndb.project_root; Dir.pwd; end; }

shared_examples 'produces_correct_sql' do 
  it 'produces correct sql for first page' do
    allow(collection).to receive(:count).and_return(collection_size)
    paginated_sql, _ = ApiPagination.paginate(collection, per_page: per_page)
    expect(paginated_sql.to_sql).to eql(Foo.limit(per_page).offset(0).to_sql)
  end
end

describe 'ActiveRecord Support' do 
  let(:collection) { Foo.all }
  let(:collection_size) { 50 }
  let(:per_page) { 5 }
  
  if ApiPagination.config.paginator == :will_paginate
    require 'will_paginate/active_record'
  end

  context "pagination with #{ApiPagination.config.paginator}" do 
    include_examples 'produces_correct_sql'
  end


  if ApiPagination.config.paginator != :pagy
    context 'reflections' do
      it 'invokes the correct methods to determine type' do 
        expect(collection).to receive(:klass).at_least(:once)
                                             .and_call_original
        ApiPagination.paginate(collection)
      end

      it 'does not fail if table name is not snake cased class name' do
        allow(collection).to receive(:table_name).and_return(SecureRandom.uuid)
        expect { ApiPagination.paginate(collection) }.to_not raise_error
      end
    end
  end
end