spec/unit/models/paginated_resource_spec.rb in ably-0.7.5 vs spec/unit/models/paginated_resource_spec.rb in ably-0.7.6

- old
+ new

@@ -3,11 +3,11 @@ describe Ably::Models::PaginatedResource do let(:paginated_resource_class) { Ably::Models::PaginatedResource } let(:headers) { Hash.new } let(:client) do - instance_double('Ably::Rest::Client', logger: true).tap do |client| + instance_double('Ably::Rest::Client', logger: Ably::Models::NilLogger.new).tap do |client| allow(client).to receive(:get).and_return(http_response) end end let(:body) do [ @@ -25,61 +25,58 @@ let(:full_url) { "#{base_url}/whatever?param=exists" } let(:paginated_resource_options) { Hash.new } let(:first_paged_request) { paginated_resource_class.new(http_response, full_url, client, paginated_resource_options) } subject { first_paged_request } - it 'returns correct length from body' do - expect(subject.length).to eql(body.length) - end + context '#items' do + it 'returns correct length from body' do + expect(subject.items.length).to eql(body.length) + end - it 'supports alias methods for length' do - expect(subject.count).to eql(subject.length) - expect(subject.size).to eql(subject.length) - end + it 'is Enumerable' do + expect(subject.items).to be_kind_of(Enumerable) + end - it 'is Enumerable' do - expect(subject).to be_kind_of(Enumerable) - end + it 'is iterable' do + expect(subject.items.map { |d| d }).to eql(body) + end - it 'is iterable' do - expect(subject.map { |d| d }).to eql(body) - end + context '#each' do + it 'returns an enumerator' do + expect(subject.items.each).to be_a(Enumerator) + end - context '#each' do - it 'returns an enumerator' do - expect(subject.each).to be_a(Enumerator) + it 'yields each item' do + items = [] + subject.items.each do |item| + items << item + end + expect(items).to eq(body) + end end - it 'yields each item' do - items = [] - subject.each do |item| - items << item - end - expect(items).to eq(body) + it 'provides [] accessor method' do + expect(subject.items[0][:id]).to eql(body[0][:id]) + expect(subject.items[1][:id]).to eql(body[1][:id]) + expect(subject.items[2]).to be_nil end - end - it 'provides [] accessor method' do - expect(subject[0][:id]).to eql(body[0][:id]) - expect(subject[1][:id]).to eql(body[1][:id]) - expect(subject[2]).to be_nil - end + specify '#first gets the first item in page' do + expect(subject.items.first[:id]).to eql(body[0][:id]) + end - specify '#first gets the first item in page' do - expect(subject.first[:id]).to eql(body[0][:id]) - end + specify '#last gets the last item in page' do + expect(subject.items.last[:id]).to eql(body[1][:id]) + end - specify '#last gets the last item in page' do - expect(subject.last[:id]).to eql(body[1][:id]) - end + context 'with coercion', :api_private do + let(:paginated_resource_options) { { coerce_into: 'OpenStruct' } } - context 'with coercion', :api_private do - let(:paginated_resource_options) { { coerce_into: 'OpenStruct' } } - - it 'returns coerced objects' do - expect(subject.first).to be_a(OpenStruct) - expect(subject.first.id).to eql(body.first[:id]) + it 'returns coerced objects' do + expect(subject.items.first).to be_a(OpenStruct) + expect(subject.items.first.id).to eql(body.first[:id]) + end end end context 'paged transformations', :api_private do let(:headers) do @@ -88,11 +85,11 @@ '<./history?index=1>; rel="next"' ].join(', ') } end let(:paged_client) do - instance_double('Ably::Rest::Client', logger: true).tap do |client| + instance_double('Ably::Rest::Client', logger: Ably::Models::NilLogger.new).tap do |client| allow(client).to receive(:get).and_return(http_response_page2) end end let(:body_page2) do [ @@ -114,19 +111,19 @@ resource end end it 'calls the block for each resource after retrieving the resources' do - expect(subject[0][:added_attribute_from_block]).to eql("id:#{body[0][:id]}") + expect(subject.items[0][:added_attribute_from_block]).to eql("id:#{body[0][:id]}") end it 'calls the block for each resource on second page after retrieving the resources' do - page_1_first_id = subject[0][:id] - next_page = subject.next_page + page_1_first_id = subject.items[0][:id] + next_page = subject.next - expect(next_page[0][:added_attribute_from_block]).to eql("id:#{body_page2[0][:id]}") - expect(next_page[0][:id]).to_not eql(page_1_first_id) + expect(next_page.items[0][:added_attribute_from_block]).to eql("id:#{body_page2[0][:id]}") + expect(next_page.items[0][:id]).to_not eql(page_1_first_id) end end if defined?(Ably::Realtime) context 'with option async_blocking_operations: true' do @@ -134,33 +131,33 @@ subject do paginated_resource_class.new(http_response, full_url, paged_client, async_blocking_operations: true) end - context '#next_page' do + context '#next' do it 'returns a SafeDeferrable that catches exceptions in callbacks and logs them' do run_reactor do - expect(subject.next_page).to be_a(Ably::Util::SafeDeferrable) + expect(subject.next).to be_a(Ably::Util::SafeDeferrable) stop_reactor end end it 'allows a success callback block to be added' do run_reactor do - subject.next_page do |paginated_resource| + subject.next do |paginated_resource| expect(paginated_resource).to be_a(Ably::Models::PaginatedResource) stop_reactor end end end end - context '#first_page' do + context '#first' do it 'calls the errback callback when first page headers are missing' do run_reactor do - subject.next_page do |paginated_resource| - deferrable = subject.first_page + subject.next do |paginated_resource| + deferrable = subject.first deferrable.errback do |error| expect(error).to be_a(Ably::Exceptions::InvalidPageError) stop_reactor end end @@ -171,27 +168,31 @@ end end context 'with non paged http response' do it 'is the first page' do - expect(subject).to be_first_page + expect(subject).to be_first end it 'is the last page' do - expect(subject).to be_last_page + expect(subject).to be_last end + it 'does not have next page' do + expect(subject).to_not have_next + end + it 'does not support pagination' do expect(subject.supports_pagination?).to_not eql(true) end - it 'raises an exception when accessing next page' do - expect { subject.next_page }.to raise_error Ably::Exceptions::InvalidPageError, /Paging header link next/ + it 'returns nil when accessing next page' do + expect(subject.next).to be_nil end - it 'raises an exception when accessing first page' do - expect { subject.first_page }.to raise_error Ably::Exceptions::InvalidPageError, /Paging header link first/ + it 'returns nil when accessing first page' do + expect(subject.first).to be_nil end end context 'with paged http response' do let(:base_url) { 'http://rest.ably.io/channels/channel_name' } @@ -205,15 +206,19 @@ ].join(', ') } end it 'is the first page' do - expect(subject).to be_first_page + expect(subject).to be_first end + it 'has next page' do + expect(subject).to have_next + end + it 'is not the last page' do - expect(subject).to_not be_last_page + expect(subject).to_not be_last end it 'supports pagination' do expect(subject.supports_pagination?).to eql(true) end @@ -234,53 +239,57 @@ double('http_response', { body: next_body, headers: next_headers }) end - let(:subject) { first_paged_request.next_page } + let(:subject) { first_paged_request.next } before do expect(client).to receive(:get).with("#{base_url}/history?index=1").and_return(next_http_response).once end it 'returns another PaginatedResource' do expect(subject).to be_a(paginated_resource_class) end it 'retrieves the next page of results' do - expect(subject.length).to eql(next_body.length) - expect(subject[0][:id]).to eql(next_body[0][:id]) + expect(subject.items.length).to eql(next_body.length) + expect(subject.items[0][:id]).to eql(next_body[0][:id]) end it 'is not the first page' do - expect(subject).to_not be_first_page + expect(subject).to_not be_first end + it 'does not have a next page' do + expect(subject).to_not have_next + end + it 'is the last page' do - expect(subject).to be_last_page + expect(subject).to be_last end - it 'raises an exception if trying to access the last page when it is the last page' do - expect(subject).to be_last_page - expect { subject.next_page }.to raise_error Ably::Exceptions::InvalidPageError, /There are no more pages/ + it 'returns nil when trying to access the last page when it is the last page' do + expect(subject).to be_last + expect(subject.next).to be_nil end context 'and then first page' do before do expect(client).to receive(:get).with("#{base_url}/history?index=0").and_return(http_response).once end - subject { first_paged_request.next_page.first_page } + subject { first_paged_request.next.first } it 'returns a PaginatedResource' do expect(subject).to be_a(paginated_resource_class) end it 'retrieves the first page of results' do - expect(subject.length).to eql(body.length) + expect(subject.items.length).to eql(body.length) end it 'is the first page' do - expect(subject).to be_first_page + expect(subject).to be_first end end end end end