require 'spec_helper' describe Mashery::Service do let(:url) { Mashery.rpc.url } let(:items) { [{"name" => "Name"}] * 10 } let(:params) { { 'method' => "object.query", 'params' => [query], 'id' => 1 } } let(:headers) { { "Content-Type" => "application/json", "Accept" => "text/plain", "Content-Length" => params.size } } let(:json) { { "result" => { "items" => items } }.to_json } context "with standard query" do let(:query) { "SELECT * FROM services ITEMS 100" } it "should grab all the services" do ::RestClient.should_receive(:post).with(url, params.to_json, headers).and_return(json) objects = Mashery::Service.all objects.length.should == 10 end end context "with pagination query" do let(:query) { "SELECT * FROM services PAGE 3 ITEMS 100"} it "should grab the 3rd page of services" do ::RestClient.should_receive(:post).with(url, params.to_json, headers).and_return(json) objects = Mashery::Service.page(3).all objects.length.should == 10 end end context "with auto-pagination via find_each" do let(:total_pages) { 5 } def query(page) if page == 1 "SELECT * FROM services ITEMS 100" else "SELECT * FROM services PAGE #{page} ITEMS 100" end end def params(page) { 'method' => "object.query", 'params' => [query(page)], 'id' => 1 } end def json(page) { "result" => { "total_pages" => total_pages, "current_page" => page, "items" => items } }.to_json end def headers(page) { "Content-Type" => "application/json", "Accept" => "text/plain", "Content-Length" => params(page).size } end it "should invoke a new query for each page" do (1..total_pages).each do |n| ::RestClient.should_receive(:post).with(url, params(n).to_json, headers(n)).and_return(json(n)) end Mashery::Service.find_each do |entry| entry.should be_a(Mashery::Service) end end end end