RSpec 3 syntax
[arvados.git] / spec / google / api_client / service_spec.rb
index 464ffda21f059729e88c3e95f6d0f4384690fcd7..e5184372c2cc63ab25b548139fe95d0678852162 100644 (file)
@@ -21,27 +21,27 @@ require 'google/api_client/service'
 
 fixtures_path = File.expand_path('../../../fixtures', __FILE__)
 
-describe Google::APIClient::Service do
+RSpec.describe Google::APIClient::Service do
   include ConnectionHelpers
 
   APPLICATION_NAME = 'API Client Tests'
 
   it 'should error out when called without an API name or version' do
-    (lambda do
+    expect(lambda do
       Google::APIClient::Service.new
-    end).should raise_error(ArgumentError)
+    end).to raise_error(ArgumentError)
   end
 
   it 'should error out when called without an API version' do
-    (lambda do
+    expect(lambda do
       Google::APIClient::Service.new('foo')
-    end).should raise_error(ArgumentError)
+    end).to raise_error(ArgumentError)
   end
 
   it 'should error out when the options hash is not a hash' do
-    (lambda do
+    expect(lambda do
       Google::APIClient::Service.new('foo', 'v1', 42)
-    end).should raise_error(ArgumentError)
+    end).to raise_error(ArgumentError)
   end
 
   describe 'with the AdSense Management API' do
@@ -49,6 +49,7 @@ describe Google::APIClient::Service do
     it 'should make a valid call for a method with no parameters' do
       conn = stub_connection do |stub|
         stub.get('/adsense/v1.3/adclients') do |env|
+          [200, {}, '{}']
         end
       end
       adsense = Google::APIClient::Service.new(
@@ -57,7 +58,8 @@ describe Google::APIClient::Service do
         {
           :application_name => APPLICATION_NAME,
           :authenticated => false,
-          :connection => conn
+          :connection => conn,
+          :cache_store => nil
         }
       )
 
@@ -68,6 +70,7 @@ describe Google::APIClient::Service do
     it 'should make a valid call for a method with parameters' do
       conn = stub_connection do |stub|
         stub.get('/adsense/v1.3/adclients/1/adunits') do |env|
+          [200, {}, '{}']
         end
       end
       adsense = Google::APIClient::Service.new(
@@ -76,7 +79,8 @@ describe Google::APIClient::Service do
         {
           :application_name => APPLICATION_NAME,
           :authenticated => false,
-          :connection => conn
+          :connection => conn,
+          :cache_store => nil
         }
       )
       req = adsense.adunits.list(:adClientId => '1').execute()
@@ -85,6 +89,7 @@ describe Google::APIClient::Service do
     it 'should make a valid call for a deep method' do
       conn = stub_connection do |stub|
         stub.get('/adsense/v1.3/accounts/1/adclients') do |env|
+          [200, {}, '{}']
         end
       end
       adsense = Google::APIClient::Service.new(
@@ -93,7 +98,8 @@ describe Google::APIClient::Service do
         {
           :application_name => APPLICATION_NAME,
           :authenticated => false,
-          :connection => conn
+          :connection => conn,
+          :cache_store => nil
         }
       )
       req = adsense.accounts.adclients.list(:accountId => '1').execute()
@@ -102,38 +108,38 @@ describe Google::APIClient::Service do
     describe 'with no connection' do
       before do
         @adsense = Google::APIClient::Service.new('adsense', 'v1.3',
-          {:application_name => APPLICATION_NAME})
+          {:application_name => APPLICATION_NAME, :cache_store => nil})
       end
 
       it 'should return a resource when using a valid resource name' do
-        @adsense.accounts.should be_a(Google::APIClient::Service::Resource)
+        expect(@adsense.accounts).to be_a(Google::APIClient::Service::Resource)
       end
 
       it 'should throw an error when using an invalid resource name' do
-        (lambda do
+        expect(lambda do
            @adsense.invalid_resource
-        end).should raise_error
+        end).to raise_error
       end
 
       it 'should return a request when using a valid method name' do
         req = @adsense.adclients.list
-        req.should be_a(Google::APIClient::Service::Request)
-        req.method.id.should == 'adsense.adclients.list'
-        req.parameters.should be_nil
+        expect(req).to be_a(Google::APIClient::Service::Request)
+        expect(req.method.id).to eq('adsense.adclients.list')
+        expect(req.parameters).to be_nil
       end
 
       it 'should throw an error when using an invalid method name' do
-        (lambda do
+        expect(lambda do
            @adsense.adclients.invalid_method
-        end).should raise_error
+        end).to raise_error
       end
 
       it 'should return a valid request with parameters' do
         req = @adsense.adunits.list(:adClientId => '1')
-        req.should be_a(Google::APIClient::Service::Request)
-        req.method.id.should == 'adsense.adunits.list'
-        req.parameters.should_not be_nil
-        req.parameters[:adClientId].should == '1'
+        expect(req).to be_a(Google::APIClient::Service::Request)
+        expect(req.method.id).to eq('adsense.adunits.list')
+        expect(req.parameters).not_to be_nil
+        expect(req.parameters[:adClientId]).to eq('1')
       end
     end
   end
@@ -143,7 +149,8 @@ describe Google::APIClient::Service do
     it 'should make a valid call with an object body' do
       conn = stub_connection do |stub|
         stub.post('/prediction/v1.5/trainedmodels?project=1') do |env|
-          env.body.should == '{"id":"1"}'
+          expect(env.body).to eq('{"id":"1"}')
+          [200, {}, '{}']
         end
       end
       prediction = Google::APIClient::Service.new(
@@ -152,7 +159,8 @@ describe Google::APIClient::Service do
         {
           :application_name => APPLICATION_NAME,
           :authenticated => false,
-          :connection => conn
+          :connection => conn,
+          :cache_store => nil
         }
       )
       req = prediction.trainedmodels.insert(:project => '1').body({'id' => '1'}).execute()
@@ -162,7 +170,8 @@ describe Google::APIClient::Service do
     it 'should make a valid call with a text body' do
       conn = stub_connection do |stub|
         stub.post('/prediction/v1.5/trainedmodels?project=1') do |env|
-          env.body.should == '{"id":"1"}'
+          expect(env.body).to eq('{"id":"1"}')
+          [200, {}, '{}']
         end
       end
       prediction = Google::APIClient::Service.new(
@@ -171,7 +180,8 @@ describe Google::APIClient::Service do
         {
           :application_name => APPLICATION_NAME,
           :authenticated => false,
-          :connection => conn
+          :connection => conn,
+          :cache_store => nil
         }
       )
       req = prediction.trainedmodels.insert(:project => '1').body('{"id":"1"}').execute()
@@ -181,25 +191,25 @@ describe Google::APIClient::Service do
     describe 'with no connection' do
       before do
         @prediction = Google::APIClient::Service.new('prediction', 'v1.5',
-          {:application_name => APPLICATION_NAME})
+          {:application_name => APPLICATION_NAME, :cache_store => nil})
       end
 
       it 'should return a valid request with a body' do
         req = @prediction.trainedmodels.insert(:project => '1').body({'id' => '1'})
-        req.should be_a(Google::APIClient::Service::Request)
-        req.method.id.should == 'prediction.trainedmodels.insert'
-        req.body.should == {'id' => '1'}
-        req.parameters.should_not be_nil
-        req.parameters[:project].should == '1'
+        expect(req).to be_a(Google::APIClient::Service::Request)
+        expect(req.method.id).to eq('prediction.trainedmodels.insert')
+        expect(req.body).to eq({'id' => '1'})
+        expect(req.parameters).not_to be_nil
+        expect(req.parameters[:project]).to eq('1')
       end
 
       it 'should return a valid request with a body when using resource name' do
         req = @prediction.trainedmodels.insert(:project => '1').training({'id' => '1'})
-        req.should be_a(Google::APIClient::Service::Request)
-        req.method.id.should == 'prediction.trainedmodels.insert'
-        req.training.should == {'id' => '1'}
-        req.parameters.should_not be_nil
-        req.parameters[:project].should == '1'
+        expect(req).to be_a(Google::APIClient::Service::Request)
+        expect(req.method.id).to eq('prediction.trainedmodels.insert')
+        expect(req.training).to eq({'id' => '1'})
+        expect(req.parameters).not_to be_nil
+        expect(req.parameters[:project]).to eq('1')
       end
     end
   end
@@ -218,7 +228,8 @@ describe Google::APIClient::Service do
     it 'should make a valid call with an object body and media upload' do
       conn = stub_connection do |stub|
         stub.post('/upload/drive/v1/files?uploadType=multipart') do |env|
-          env.body.should be_a Faraday::CompositeReadIO
+          expect(env.body).to be_a Faraday::CompositeReadIO
+          [200, {}, '{}']
         end
       end
       drive = Google::APIClient::Service.new(
@@ -227,7 +238,8 @@ describe Google::APIClient::Service do
         {
           :application_name => APPLICATION_NAME,
           :authenticated => false,
-          :connection => conn
+          :connection => conn,
+          :cache_store => nil
         }
       )
       req = drive.files.insert(:uploadType => 'multipart').body(@metadata).media(@media).execute()
@@ -237,27 +249,27 @@ describe Google::APIClient::Service do
     describe 'with no connection' do
       before do
         @drive = Google::APIClient::Service.new('drive', 'v1',
-          {:application_name => APPLICATION_NAME})
+          {:application_name => APPLICATION_NAME, :cache_store => nil})
       end
 
       it 'should return a valid request with a body and media upload' do
         req = @drive.files.insert(:uploadType => 'multipart').body(@metadata).media(@media)
-        req.should be_a(Google::APIClient::Service::Request)
-        req.method.id.should == 'drive.files.insert'
-        req.body.should == @metadata
-        req.media.should == @media
-        req.parameters.should_not be_nil
-        req.parameters[:uploadType].should == 'multipart'
+        expect(req).to be_a(Google::APIClient::Service::Request)
+        expect(req.method.id).to eq('drive.files.insert')
+        expect(req.body).to eq(@metadata)
+        expect(req.media).to eq(@media)
+        expect(req.parameters).not_to be_nil
+        expect(req.parameters[:uploadType]).to eq('multipart')
       end
 
       it 'should return a valid request with a body and media upload when using resource name' do
         req = @drive.files.insert(:uploadType => 'multipart').file(@metadata).media(@media)
-        req.should be_a(Google::APIClient::Service::Request)
-        req.method.id.should == 'drive.files.insert'
-        req.file.should == @metadata
-        req.media.should == @media
-        req.parameters.should_not be_nil
-        req.parameters[:uploadType].should == 'multipart'
+        expect(req).to be_a(Google::APIClient::Service::Request)
+        expect(req.method.id).to eq('drive.files.insert')
+        expect(req.file).to eq(@metadata)
+        expect(req.media).to eq(@media)
+        expect(req.parameters).not_to be_nil
+        expect(req.parameters[:uploadType]).to eq('multipart')
       end
     end
   end
@@ -265,22 +277,23 @@ describe Google::APIClient::Service do
   describe 'with the Discovery API' do
     it 'should make a valid end-to-end request' do
       discovery = Google::APIClient::Service.new('discovery', 'v1',
-          {:application_name => APPLICATION_NAME, :authenticated => false})
+          {:application_name => APPLICATION_NAME, :authenticated => false,
+           :cache_store => nil})
       result = discovery.apis.get_rest(:api => 'discovery', :version => 'v1').execute
-      result.should_not be_nil
-      result.data.name.should == 'discovery'
-      result.data.version.should == 'v1'
+      expect(result).not_to be_nil
+      expect(result.data.name).to eq('discovery')
+      expect(result.data.version).to eq('v1')
     end
   end
 end
 
 
-describe Google::APIClient::Service::Result do
+RSpec.describe Google::APIClient::Service::Result do
 
   describe 'with the plus API' do
     before do
       @plus = Google::APIClient::Service.new('plus', 'v1',
-          {:application_name => APPLICATION_NAME})
+          {:application_name => APPLICATION_NAME, :cache_store => nil})
       @reference = Google::APIClient::Reference.new({
         :api_method => @plus.activities.list.method,
         :parameters => {
@@ -294,8 +307,8 @@ describe Google::APIClient::Service::Result do
 
       # Response double
       @response = double("response")
-      @response.stub(:status).and_return(200)
-      @response.stub(:headers).and_return({
+      allow(@response).to receive(:status).and_return(200)
+      allow(@response).to receive(:headers).and_return({
         'etag' => '12345',
         'x-google-apiary-auth-scopes' =>
           'https://www.googleapis.com/auth/plus.me',
@@ -322,51 +335,54 @@ describe Google::APIClient::Service::Result do
             "items": []
           }
           END_OF_STRING
-        @response.stub(:body).and_return(@body)
+        allow(@response).to receive(:body).and_return(@body)
         base_result = Google::APIClient::Result.new(@reference, @response)
         @result = Google::APIClient::Service::Result.new(@request, base_result)
       end
 
       it 'should indicate a successful response' do
-        @result.error?.should be_false
+        expect(@result.error?).to be_falsey
       end
 
       it 'should return the correct next page token' do
-        @result.next_page_token.should == 'NEXT+PAGE+TOKEN'
+        expect(@result.next_page_token).to eq('NEXT+PAGE+TOKEN')
       end
 
       it 'generate a correct request when calling next_page' do
         next_page_request = @result.next_page
-        next_page_request.parameters.should include('pageToken')
-        next_page_request.parameters['pageToken'].should == 'NEXT+PAGE+TOKEN'
+        expect(next_page_request.parameters).to include('pageToken')
+        expect(next_page_request.parameters['pageToken']).to eq('NEXT+PAGE+TOKEN')
         @request.parameters.each_pair do |param, value|
-          next_page_request.parameters[param].should == value
+          expect(next_page_request.parameters[param]).to eq(value)
         end
       end
 
       it 'should return content type correctly' do
-        @result.media_type.should == 'application/json'
+        expect(@result.media_type).to eq('application/json')
       end
 
       it 'should return the body correctly' do
-        @result.body.should == @body
+        expect(@result.body).to eq(@body)
       end
 
       it 'should return the result data correctly' do
-        @result.data?.should be_true
-        @result.data.class.to_s.should ==
+        expect(@result.data?).to be_truthy
+        expect(@result.data.class.to_s).to eq(
             'Google::APIClient::Schema::Plus::V1::ActivityFeed'
-        @result.data.kind.should == 'plus#activityFeed'
-        @result.data.etag.should == 'FOO'
-        @result.data.nextPageToken.should == 'NEXT+PAGE+TOKEN'
-        @result.data.selfLink.should ==
+        )
+        expect(@result.data.kind).to eq('plus#activityFeed')
+        expect(@result.data.etag).to eq('FOO')
+        expect(@result.data.nextPageToken).to eq('NEXT+PAGE+TOKEN')
+        expect(@result.data.selfLink).to eq(
             'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
-        @result.data.nextLink.should ==
+        )
+        expect(@result.data.nextLink).to eq(
             'https://www.googleapis.com/plus/v1/people/foo/activities/public?' +
             'maxResults=20&pageToken=NEXT%2BPAGE%2BTOKEN'
-        @result.data.title.should == 'Plus Public Activity Feed for '
-        @result.data.id.should == "123456790"
-        @result.data.items.should be_empty
+        )
+        expect(@result.data.title).to eq('Plus Public Activity Feed for ')
+        expect(@result.data.id).to eq("123456790")
+        expect(@result.data.items).to be_empty
       end
     end
 
@@ -383,34 +399,36 @@ describe Google::APIClient::Service::Result do
             "items": []
           }
           END_OF_STRING
-        @response.stub(:body).and_return(@body)
+        allow(@response).to receive(:body).and_return(@body)
         base_result = Google::APIClient::Result.new(@reference, @response)
         @result = Google::APIClient::Service::Result.new(@request, base_result)
       end
 
       it 'should not return a next page token' do
-        @result.next_page_token.should == nil
+        expect(@result.next_page_token).to eq(nil)
       end
 
       it 'should return content type correctly' do
-        @result.media_type.should == 'application/json'
+        expect(@result.media_type).to eq('application/json')
       end
 
       it 'should return the body correctly' do
-        @result.body.should == @body
+        expect(@result.body).to eq(@body)
       end
 
       it 'should return the result data correctly' do
-        @result.data?.should be_true
-        @result.data.class.to_s.should ==
+        expect(@result.data?).to be_truthy
+        expect(@result.data.class.to_s).to eq(
             'Google::APIClient::Schema::Plus::V1::ActivityFeed'
-        @result.data.kind.should == 'plus#activityFeed'
-        @result.data.etag.should == 'FOO'
-        @result.data.selfLink.should ==
+        )
+        expect(@result.data.kind).to eq('plus#activityFeed')
+        expect(@result.data.etag).to eq('FOO')
+        expect(@result.data.selfLink).to eq(
             'https://www.googleapis.com/plus/v1/people/foo/activities/public?'
-        @result.data.title.should == 'Plus Public Activity Feed for '
-        @result.data.id.should == "123456790"
-        @result.data.items.should be_empty
+        )
+        expect(@result.data.title).to eq('Plus Public Activity Feed for ')
+        expect(@result.data.id).to eq("123456790")
+        expect(@result.data.items).to be_empty
       end
     end
 
@@ -431,45 +449,143 @@ describe Google::APIClient::Service::Result do
           }
          }
          END_OF_STRING
-        @response.stub(:body).and_return(@body)
-        @response.stub(:status).and_return(400)
+        allow(@response).to receive(:body).and_return(@body)
+        allow(@response).to receive(:status).and_return(400)
         base_result = Google::APIClient::Result.new(@reference, @response)
         @result = Google::APIClient::Service::Result.new(@request, base_result)
       end
 
       it 'should return error status correctly' do
-        @result.error?.should be_true
+        expect(@result.error?).to be_truthy
       end
 
       it 'should return the correct error message' do
-        @result.error_message.should == 'Parse Error'
+        expect(@result.error_message).to eq('Parse Error')
       end
 
       it 'should return the body correctly' do
-        @result.body.should == @body
+        expect(@result.body).to eq(@body)
       end
     end
 
     describe 'with 204 No Content response' do
       before do
-        @response.stub(:body).and_return('')
-        @response.stub(:status).and_return(204)
-        @response.stub(:headers).and_return({})
+        allow(@response).to receive(:body).and_return('')
+        allow(@response).to receive(:status).and_return(204)
+        allow(@response).to receive(:headers).and_return({})
         base_result = Google::APIClient::Result.new(@reference, @response)
         @result = Google::APIClient::Service::Result.new(@request, base_result)
       end
 
       it 'should indicate no data is available' do
-        @result.data?.should be_false
+        expect(@result.data?).to be_falsey
       end
 
       it 'should return nil for data' do
-        @result.data.should == nil
+        expect(@result.data).to eq(nil)
       end
 
       it 'should return nil for media_type' do
-        @result.media_type.should == nil
+        expect(@result.media_type).to eq(nil)
       end
     end
   end
 end
+
+RSpec.describe Google::APIClient::Service::BatchRequest do
+  describe 'with the discovery API' do
+    before do
+      @discovery = Google::APIClient::Service.new('discovery', 'v1',
+          {:application_name => APPLICATION_NAME, :authorization => nil,
+           :cache_store => nil})
+    end
+
+    describe 'with two valid requests' do
+      before do
+        @calls = [
+          @discovery.apis.get_rest(:api => 'plus', :version => 'v1'),
+          @discovery.apis.get_rest(:api => 'discovery', :version => 'v1')
+        ]
+      end
+
+      it 'should execute both when using a global callback' do
+        block_called = 0
+        batch = @discovery.batch(@calls) do |result|
+          block_called += 1
+          expect(result.status).to eq(200)
+        end
+
+        batch.execute
+        expect(block_called).to eq(2)
+      end
+
+      it 'should execute both when using individual callbacks' do
+        call1_returned, call2_returned = false, false
+        batch = @discovery.batch
+
+        batch.add(@calls[0]) do |result|
+          call1_returned = true
+          expect(result.status).to eq(200)
+          expect(result.call_index).to eq(0)
+        end
+
+        batch.add(@calls[1]) do |result|
+          call2_returned = true
+          expect(result.status).to eq(200)
+          expect(result.call_index).to eq(1)
+        end
+
+        batch.execute
+        expect(call1_returned).to eq(true)
+        expect(call2_returned).to eq(true)
+      end
+    end
+
+    describe 'with a valid request and an invalid one' do
+      before do
+        @calls = [
+          @discovery.apis.get_rest(:api => 'plus', :version => 'v1'),
+          @discovery.apis.get_rest(:api => 'invalid', :version => 'invalid')
+        ]
+      end
+
+      it 'should execute both when using a global callback' do
+        block_called = 0
+        batch = @discovery.batch(@calls) do |result|
+          block_called += 1
+          if result.call_index == 0
+            expect(result.status).to eq(200)
+          else
+            expect(result.status).to be >= 400
+            expect(result.status).to be < 500
+          end
+        end
+
+        batch.execute
+        expect(block_called).to eq(2)
+      end
+
+      it 'should execute both when using individual callbacks' do
+        call1_returned, call2_returned = false, false
+        batch = @discovery.batch
+
+        batch.add(@calls[0]) do |result|
+          call1_returned = true
+          expect(result.status).to eq(200)
+          expect(result.call_index).to eq(0)
+        end
+
+        batch.add(@calls[1]) do |result|
+          call2_returned = true
+          expect(result.status).to be >= 400
+          expect(result.status).to be < 500
+          expect(result.call_index).to eq(1)
+        end
+
+        batch.execute
+        expect(call1_returned).to eq(true)
+        expect(call2_returned).to eq(true)
+      end
+    end
+  end
+end
\ No newline at end of file