|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +describe JSONAPI::Utils::Support::Pagination do |
| 4 | + subject do |
| 5 | + OpenStruct.new(params: {}).extend(JSONAPI::Utils::Support::Pagination) |
| 6 | + end |
| 7 | + |
| 8 | + before(:all) do |
| 9 | + FactoryGirl.create_list(:user, 2) |
| 10 | + end |
| 11 | + |
| 12 | + let(:options) { {} } |
| 13 | + |
| 14 | + ## |
| 15 | + # Public API |
| 16 | + ## |
| 17 | + |
| 18 | + describe '#record_count_for' do |
| 19 | + context 'with array' do |
| 20 | + let(:records) { User.all.to_a } |
| 21 | + |
| 22 | + it 'applies memoization on the record count' do |
| 23 | + expect(records).to receive(:length).and_return(records.length).once |
| 24 | + 2.times { subject.record_count_for(records, options) } |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + context 'with ActiveRecord object' do |
| 29 | + let(:records) { User.all } |
| 30 | + |
| 31 | + it 'applies memoization on the record count' do |
| 32 | + expect(records).to receive(:except).and_return(records).once |
| 33 | + 2.times { subject.record_count_for(records, options) } |
| 34 | + end |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + ## |
| 39 | + # Private API |
| 40 | + ## |
| 41 | + |
| 42 | + describe '#count_records' do |
| 43 | + shared_examples_for 'counting records' do |
| 44 | + it 'counts records' do |
| 45 | + expect(subject.send(:count_records, records, options)).to eq(count) |
| 46 | + end |
| 47 | + end |
| 48 | + |
| 49 | + context 'with count present within the options' do |
| 50 | + let(:records) { User.all } |
| 51 | + let(:options) { { count: 999 } } |
| 52 | + let(:count) { 999 } |
| 53 | + it_behaves_like 'counting records' |
| 54 | + end |
| 55 | + |
| 56 | + context 'with array' do |
| 57 | + let(:records) { User.all.to_a } |
| 58 | + let(:count) { records.length } |
| 59 | + it_behaves_like 'counting records' |
| 60 | + end |
| 61 | + |
| 62 | + context 'with ActiveRecord object' do |
| 63 | + let(:records) { User.all } |
| 64 | + let(:count) { records.count } |
| 65 | + it_behaves_like 'counting records' |
| 66 | + end |
| 67 | + |
| 68 | + context 'when no strategy can be applied' do |
| 69 | + let(:records) { Object.new } |
| 70 | + let(:count) { } |
| 71 | + |
| 72 | + it 'raises an error' do |
| 73 | + expect { |
| 74 | + subject.send(:count_records, records, options) |
| 75 | + }.to raise_error(JSONAPI::Utils::Support::Pagination::RecordCountError) |
| 76 | + end |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + describe '#count_records_from_database' do |
| 81 | + shared_examples_for 'skipping eager load SQL when counting records' do |
| 82 | + it 'skips any eager load for the SQL count query (default)' do |
| 83 | + expect(records).to receive(:except) |
| 84 | + .with(:includes, :group, :order) |
| 85 | + .and_return(User.all) |
| 86 | + .once |
| 87 | + expect(records).to receive(:except) |
| 88 | + .with(:group, :order) |
| 89 | + .and_return(User.all) |
| 90 | + .exactly(0) |
| 91 | + .times |
| 92 | + subject.send(:count_records_from_database, records, options) |
| 93 | + end |
| 94 | + end |
| 95 | + |
| 96 | + context 'when not eager loading records' do |
| 97 | + let(:records) { User.all } |
| 98 | + it_behaves_like 'skipping eager load SQL when counting records' |
| 99 | + end |
| 100 | + |
| 101 | + context 'when eager loading records' do |
| 102 | + let(:records) { User.includes(:posts) } |
| 103 | + it_behaves_like 'skipping eager load SQL when counting records' |
| 104 | + end |
| 105 | + |
| 106 | + context 'when eager loading records and using where clause on associations' do |
| 107 | + let(:records) { User.includes(:posts).where(posts: { id: 1 }) } |
| 108 | + |
| 109 | + it 'fallbacks to the SQL count query with eager load' do |
| 110 | + expect(records).to receive(:except) |
| 111 | + .with(:includes, :group, :order) |
| 112 | + .and_raise(ActiveRecord::StatementInvalid) |
| 113 | + .once |
| 114 | + expect(records).to receive(:except) |
| 115 | + .with(:group, :order) |
| 116 | + .and_return(User.all) |
| 117 | + .once |
| 118 | + subject.send(:count_records_from_database, records, options) |
| 119 | + end |
| 120 | + end |
| 121 | + end |
| 122 | + |
| 123 | + describe '#distinct_count_sql' do |
| 124 | + let(:records) { OpenStruct.new(table_name: 'foos', primary_key: 'id') } |
| 125 | + |
| 126 | + it 'builds the distinct count SQL query' do |
| 127 | + expect(subject.send(:distinct_count_sql, records)).to eq('DISTINCT foos.id') |
| 128 | + end |
| 129 | + end |
| 130 | +end |
0 commit comments