|
| 1 | +require 'spec_helper' |
| 2 | +require 'support/active_record/foo' |
| 3 | +require 'nulldb_rspec' |
| 4 | + |
| 5 | +ActiveRecord::Base.establish_connection( |
| 6 | + adapter: :nulldb, |
| 7 | + schema: 'spec/support/active_record/schema.rb' |
| 8 | +) |
| 9 | + |
| 10 | +NullDB.configure { |ndb| def ndb.project_root; Dir.pwd; end; } |
| 11 | + |
| 12 | +shared_examples 'produces_correct_sql' do |
| 13 | + it 'produces correct sql for first page' do |
| 14 | + allow(collection).to receive(:count).and_return(collection_size) |
| 15 | + paginated_sql, _ = ApiPagination.paginate(collection, per_page: per_page) |
| 16 | + expect(paginated_sql.to_sql).to eql(Foo.limit(per_page).offset(0).to_sql) |
| 17 | + end |
| 18 | +end |
| 19 | + |
| 20 | +describe 'ActiveRecord Support' do |
| 21 | + let(:collection) { Foo.all } |
| 22 | + let(:collection_size) { 50 } |
| 23 | + let(:per_page) { 5 } |
| 24 | + |
| 25 | + if ApiPagination.config.paginator == :will_paginate |
| 26 | + require 'will_paginate/active_record' |
| 27 | + end |
| 28 | + |
| 29 | + context "pagination with #{ApiPagination.config.paginator}" do |
| 30 | + include_examples 'produces_correct_sql' |
| 31 | + end |
| 32 | + |
| 33 | + |
| 34 | + if ApiPagination.config.paginator != :pagy |
| 35 | + context 'reflections' do |
| 36 | + it 'invokes the correct methods to determine type' do |
| 37 | + expect(collection).to receive(:klass).at_least(:once) |
| 38 | + .and_call_original |
| 39 | + ApiPagination.paginate(collection) |
| 40 | + end |
| 41 | + |
| 42 | + it 'does not fail if table name is not snake cased class name' do |
| 43 | + allow(collection).to receive(:table_name).and_return(SecureRandom.uuid) |
| 44 | + expect { ApiPagination.paginate(collection) }.to_not raise_error |
| 45 | + end |
| 46 | + end |
| 47 | + end |
| 48 | +end |
| 49 | + |
| 50 | + |
0 commit comments