Improving testing and coverage.

git-svn-id: https://google-api-ruby-client.googlecode.com/svn/trunk@37 c1d61fac-ed7f-fcc1-18f7-ff78120a04ef
This commit is contained in:
Bob Aman 2010-09-30 21:33:23 +00:00
parent bbcc946f33
commit 7e48c8913c
2 changed files with 190 additions and 12 deletions

View File

@ -33,6 +33,11 @@ module Google #:nodoc:
@options = { @options = {
# TODO: What configuration options need to go here? # TODO: What configuration options need to go here?
}.merge(options) }.merge(options)
if !self.authorization.respond_to?(:generate_authenticated_request)
raise TypeError,
'Expected authorization mechanism to respond to ' +
'#generate_authenticated_request.'
end
end end
## ##
@ -135,6 +140,11 @@ module Google #:nodoc:
end end
def discovered_service(service_name, service_version='v1') def discovered_service(service_name, service_version='v1')
if !service_name.kind_of?(String) && !service_name.kind_of?(Symbol)
raise TypeError,
"Expected String or Symbol, got #{service_name.class}."
end
service_name = service_name.to_s
for service in self.discovered_services for service in self.discovered_services
if service.name == service_name && if service.name == service_name &&
service.version.to_s == service_version.to_s service.version.to_s == service_version.to_s
@ -145,6 +155,11 @@ module Google #:nodoc:
end end
def discovered_method(rpc_name, service_version='v1') def discovered_method(rpc_name, service_version='v1')
if !rpc_name.kind_of?(String) && !rpc_name.kind_of?(Symbol)
raise TypeError,
"Expected String or Symbol, got #{rpc_name.class}."
end
rpc_name = rpc_name.to_s
for service in self.discovered_services for service in self.discovered_services
# This looks kinda weird, but is not a real problem because there's # This looks kinda weird, but is not a real problem because there's
# almost always only one service, and this is memoized anyhow. # almost always only one service, and this is memoized anyhow.
@ -156,6 +171,11 @@ module Google #:nodoc:
end end
def latest_service(service_name) def latest_service(service_name)
if !service_name.kind_of?(String) && !service_name.kind_of?(Symbol)
raise TypeError,
"Expected String or Symbol, got #{service_name.class}."
end
service_name = service_name.to_s
versions = {} versions = {}
for service in self.discovered_services for service in self.discovered_services
next if service.name != service_name next if service.name != service_name
@ -174,15 +194,18 @@ module Google #:nodoc:
:parser => self.parser, :parser => self.parser,
:service_version => 'v1' :service_version => 'v1'
}.merge(options) }.merge(options)
if api_method.kind_of?(String) if api_method.kind_of?(String) || api_method.kind_of?(Symbol)
api_method = self.discovered_method( api_method = self.discovered_method(
api_method, options[:service_version] api_method.to_s, options[:service_version]
) )
elsif !api_method.kind_of?(::Google::APIClient::Service) elsif !api_method.kind_of?(::Google::APIClient::Service)
raise TypeError, raise TypeError,
"Expected String or Google::APIClient::Service, " + "Expected String, Symbol, or Google::APIClient::Service, " +
"got #{api_method.class}." "got #{api_method.class}."
end end
unless api_method
raise ArgumentError, "API method does not exist."
end
request = api_method.generate_request(parameters, body, headers) request = api_method.generate_request(parameters, body, headers)
if options[:signed] if options[:signed]
request = self.sign_request(request) request = self.sign_request(request)
@ -202,15 +225,9 @@ module Google #:nodoc:
end end
def sign_request(request) def sign_request(request)
if self.authorization.respond_to?(:generate_authenticated_request)
return self.authorization.generate_authenticated_request( return self.authorization.generate_authenticated_request(
:request => request :request => request
) )
else
raise TypeError,
'Expected authorization mechanism to respond to ' +
'#generate_authenticated_request.'
end
end end
end end
end end

View File

@ -45,6 +45,14 @@ describe Google::APIClient, 'configured for a bogus API' do
end end
end end
describe Google::APIClient, 'configured for bogus authorization' do
it 'should raise a type error' do
(lambda do
Google::APIClient.new(:service => 'prediction', :authorization => 42)
end).should raise_error(TypeError)
end
end
describe Google::APIClient, 'configured for the prediction API' do describe Google::APIClient, 'configured for the prediction API' do
before do before do
@client = Google::APIClient.new(:service => 'prediction') @client = Google::APIClient.new(:service => 'prediction')
@ -59,8 +67,60 @@ describe Google::APIClient, 'configured for the prediction API' do
@client.discovered_services.size.should > 1 @client.discovered_services.size.should > 1
end end
it 'should find APIs that are in the discovery document' do
@client.discovered_service('prediction').name.should == 'prediction'
@client.discovered_service('prediction').version.should == 'v1'
@client.discovered_service(:prediction).name.should == 'prediction'
@client.discovered_service(:prediction).version.should == 'v1'
end
it 'should find API versions that are in the discovery document' do
@client.discovered_service('prediction', 'v1.1').version.should == 'v1.1'
end
it 'should not find APIs that are not in the discovery document' do
@client.discovered_service('bogus').should == nil
end
it 'should raise an error for bogus services' do
(lambda do
@client.discovered_service(42)
end).should raise_error(TypeError)
end
it 'should find methods that are in the discovery document' do
@client.discovered_method('prediction.training.insert').name.should ==
'insert'
@client.discovered_method(:'prediction.training.insert').name.should ==
'insert'
end
it 'should find methods for versions that are in the discovery document' do
@client.discovered_method(
'prediction.training.delete', 'v1.1'
).should_not == nil
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('prediction.training.delete', 'v1').should == nil
@client.discovered_method('prediction.bogus').should == nil
end
it 'should raise an error for bogus methods' do
(lambda do
@client.discovered_method(42)
end).should raise_error(TypeError)
end
it 'should correctly determine the latest version' do it 'should correctly determine the latest version' do
@client.latest_service('prediction').version.should_not == 'v1' @client.latest_service('prediction').version.should_not == 'v1'
@client.latest_service(:prediction).version.should_not == 'v1'
end
it 'should raise an error for bogus services' do
(lambda do
@client.latest_service(42)
end).should raise_error(TypeError)
end end
it 'should correctly determine the latest version' do it 'should correctly determine the latest version' do
@ -111,6 +171,19 @@ describe Google::APIClient, 'configured for the prediction API' do
'https://www.googleapis.com/prediction/v1/training?query=12345' 'https://www.googleapis.com/prediction/v1/training?query=12345'
end end
it 'should generate requests against the correct URIs' do
request = @client.generate_request(
:'prediction.training.insert',
{'query' => '12345'},
'',
[],
{:signed => false}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/prediction/v1/training?query=12345'
end
it 'should generate signed requests' do it 'should generate signed requests' do
@client.authorization.token_credential_key = '12345' @client.authorization.token_credential_key = '12345'
@client.authorization.token_credential_secret = '12345' @client.authorization.token_credential_secret = '12345'
@ -140,4 +213,92 @@ describe Google::APIClient, 'configured for the prediction API' do
status, headers, body = response status, headers, body = response
status.should == 401 status.should == 401
end end
it 'should raise an error for bogus methods' do
(lambda do
@client.generate_request(42)
end).should raise_error(TypeError)
end
end
describe Google::APIClient, 'configured for the buzz API' do
before do
@client = Google::APIClient.new(:service => 'buzz')
end
it 'should correctly determine the discovery URI' do
@client.discovery_uri.should ===
'http://www.googleapis.com/discovery/0.1/describe?api=buzz'
end
it 'should find APIs that are in the discovery document' do
@client.discovered_service('buzz').name.should == 'buzz'
@client.discovered_service('buzz').version.should == 'v1'
end
it 'should not find APIs that are not in the discovery document' do
@client.discovered_service('bogus').should == nil
end
it 'should find methods that are in the discovery document' do
# TODO(bobaman) Fix this when the RPC names are correct
@client.discovered_method('chili.activities.list').name.should == 'list'
end
it 'should not find methods that are not in the discovery document' do
@client.discovered_method('buzz.bogus').should == nil
end
it 'should generate requests against the correct URIs' do
# TODO(bobaman) Fix this when the RPC names are correct
request = @client.generate_request(
'chili.activities.list',
{'userId' => 'hikingfan', 'scope' => '@public'},
'',
[],
{:signed => false}
)
method, uri, headers, body = request
uri.should ==
'https://www.googleapis.com/buzz/v1/activities/hikingfan/@public'
end
it 'should correctly validate parameters' do
# TODO(bobaman) Fix this when the RPC names are correct
(lambda do
@client.generate_request(
'chili.activities.list',
{'alt' => 'json'},
'',
[],
{:signed => false}
)
end).should raise_error(ArgumentError)
end
it 'should correctly validate parameters' do
# TODO(bobaman) Fix this when the RPC names are correct
(lambda do
@client.generate_request(
'chili.activities.list',
{'userId' => 'hikingfan', 'scope' => '@bogus'},
'',
[],
{:signed => false}
)
end).should raise_error(ArgumentError)
end
it 'should be able to execute requests without authorization' do
# TODO(bobaman) Fix this when the RPC names are correct
response = @client.execute(
'chili.activities.list',
{'alt' => 'json', 'userId' => 'hikingfan', 'scope' => '@public'},
'',
[],
{:signed => false}
)
status, headers, body = response
status.should == 200
end
end end