Update tests with latest code

This commit is contained in:
xhh 2015-06-30 14:32:55 +08:00
parent 609d915541
commit a75b05b952
7 changed files with 90 additions and 102 deletions

View File

@ -0,0 +1,53 @@
# require 'spec_helper'
require File.dirname(__FILE__) + '/spec_helper'
describe Petstore::ApiClient do
context 'initialization' do
context 'URL stuff' do
context 'host' do
it 'removes http from host' do
c = Petstore::ApiClient.new
c.configure {|c| c.host = 'http://example.com' }
c.host.should == 'example.com'
end
it 'removes https from host' do
c = Petstore::ApiClient.new {|c| c.host = 'https://wookiee.com' }
c.host.should == 'wookiee.com'
end
it 'removes trailing path from host' do
c = Petstore::ApiClient.new
c.configure {|c| c.host = 'hobo.com/v4' }
c.host.should == 'hobo.com'
end
end
context 'base_path' do
it "prepends a slash to base_path" do
c = Petstore::ApiClient.new
c.configure {|c| c.base_path = 'v4/dog' }
c.base_path.should == '/v4/dog'
end
it "doesn't prepend a slash if one is already there" do
c = Petstore::ApiClient.new
c.configure {|c| c.base_path = '/v4/dog' }
c.base_path.should == '/v4/dog'
end
it "ends up as a blank string if nil" do
c = Petstore::ApiClient.new
c.configure {|c| c.base_path = nil }
c.base_path.should == ''
end
end
end
end
end

View File

@ -3,8 +3,8 @@ require 'json'
describe "Pet" do
before do
configure_swagger
prepare_pet
@pet_api = Petstore::PetApi.new(API_CLIENT)
prepare_pet @pet_api
end
describe "pet methods" do
@ -42,7 +42,7 @@ describe "Pet" do
end
it "should fetch a pet object" do
pet = Petstore::PetApi.get_pet_by_id(10002)
pet = @pet_api.get_pet_by_id(10002)
pet.should be_a(Petstore::Pet)
pet.id.should == 10002
pet.name.should == "RUBY UNIT TESTING"
@ -52,9 +52,9 @@ describe "Pet" do
it "should not find a pet that does not exist" do
begin
Petstore::PetApi.get_pet_by_id(-10002)
@pet_api.get_pet_by_id(-10002)
fail 'it should raise error'
rescue Petstore::Swagger::ApiError => e
rescue Petstore::ApiError => e
e.code.should == 404
e.message.should == 'Not Found'
e.response_body.should == '{"code":1,"type":"error","message":"Pet not found"}'
@ -64,7 +64,7 @@ describe "Pet" do
end
it "should find pets by status" do
pets = Petstore::PetApi.find_pets_by_status(:status => 'available')
pets = @pet_api.find_pets_by_status(:status => 'available')
pets.length.should >= 3
pets.each do |pet|
pet.should be_a(Petstore::Pet)
@ -73,12 +73,12 @@ describe "Pet" do
end
it "should not find a pet with invalid status" do
pets = Petstore::PetApi.find_pets_by_status(:status => 'invalid-status')
pets = @pet_api.find_pets_by_status(:status => 'invalid-status')
pets.length.should == 0
end
it "should find a pet by status" do
pets = Petstore::PetApi.find_pets_by_status(:status => "available,sold")
pets = @pet_api.find_pets_by_status(:status => "available,sold")
pets.each do |pet|
if pet.status != 'available' && pet.status != 'sold'
raise "pet status wasn't right"
@ -88,20 +88,20 @@ describe "Pet" do
it "should update a pet" do
pet = Petstore::Pet.new({'id' => 10002, 'status' => 'sold'})
Petstore::PetApi.add_pet(:body => pet)
@pet_api.add_pet(:body => pet)
fetched = Petstore::PetApi.get_pet_by_id(10002)
fetched = @pet_api.get_pet_by_id(10002)
fetched.id.should == 10002
fetched.status.should == 'sold'
end
it "should create a pet" do
pet = Petstore::Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING")
result = Petstore::PetApi.add_pet(:body => pet)
result = @pet_api.add_pet(:body => pet)
# nothing is returned
result.should be_nil
pet = Petstore::PetApi.get_pet_by_id(10002)
pet = @pet_api.get_pet_by_id(10002)
pet.id.should == 10002
pet.name.should == "RUBY UNIT TESTING"
end

View File

@ -1,9 +1,9 @@
require 'spec_helper'
describe Petstore::Swagger::Request do
describe Petstore::Request do
before(:each) do
Petstore::Swagger.configure do |config|
@api_client = Petstore::ApiClient.new do |config|
inject_format = true
config.api_key['api_key'] = 'special-key'
config.host = 'petstore.swagger.io'
@ -15,7 +15,7 @@ describe Petstore::Swagger::Request do
@default_params = {
:params => {:foo => "1", :bar => "2"}
}
@request = Petstore::Swagger::Request.new(@default_http_method, @default_path, @default_params)
@request = Petstore::Request.new(@api_client, @default_http_method, @default_path, @default_params)
end
describe "initialization" do
@ -29,7 +29,7 @@ describe Petstore::Swagger::Request do
end
it "allows params to be nil" do
@request = Petstore::Swagger::Request.new(@default_http_method, @default_path, :params => nil)
@request = Petstore::Request.new(@api_client, @default_http_method, @default_path, :params => nil)
@request.params.should == {}
end
@ -60,7 +60,7 @@ describe Petstore::Swagger::Request do
describe "path" do
it "accounts for a total absence of format in the path string" do
@request = Petstore::Swagger::Request.new(:get, "/word.{format}/cat/entries", @default_params.merge({
@request = Petstore::Request.new(@api_client, :get, "/word.{format}/cat/entries", @default_params.merge({
:format => "xml",
:params => {
}
@ -69,7 +69,7 @@ describe Petstore::Swagger::Request do
end
it "does string substitution (format) on path params" do
@request = Petstore::Swagger::Request.new(:get, "/word.{format}/cat/entries", @default_params.merge({
@request = Petstore::Request.new(@api_client, :get, "/word.{format}/cat/entries", @default_params.merge({
:format => "xml",
:params => {
}
@ -78,7 +78,7 @@ describe Petstore::Swagger::Request do
end
it "URI encodes the path" do
@request = Petstore::Swagger::Request.new(:get, "word.{format}/bill gates/definitions", @default_params.merge({
@request = Petstore::Request.new(@api_client, :get, "word.{format}/bill gates/definitions", @default_params.merge({
:params => {
:word => "bill gates"
}
@ -90,7 +90,7 @@ describe Petstore::Swagger::Request do
describe "#update_params_for_auth!" do
it "sets header api-key parameter with prefix" do
Petstore::Swagger.configure do |config|
@api_client.configure do |config|
inject_format = true
config.api_key_prefix['api_key'] = 'PREFIX'
end
@ -100,7 +100,7 @@ describe Petstore::Swagger::Request do
end
it "sets header api-key parameter without prefix" do
Petstore::Swagger.configure do |config|
@api_client.configure do |config|
inject_format = true
config.api_key_prefix['api_key'] = nil
end

View File

@ -1,18 +1,13 @@
require 'spec_helper'
describe Petstore::Swagger::Response do
before do
configure_swagger
prepare_pet
end
describe Petstore::Response do
before(:each) do
VCR.use_cassette('pet_resource', :record => :new_episodes) do
@raw = Typhoeus::Request.get("http://petstore.swagger.io/v2/pet/10002")
end
@response = Petstore::Swagger::Response.new(@raw)
@response = Petstore::Response.new(API_CLIENT, @raw)
end
describe "initialization" do
@ -43,7 +38,7 @@ describe Petstore::Swagger::Response do
@raw = Typhoeus::Request.get("http://petstore.swagger.io/v2/pet/10002",
:headers => {'Accept'=> "application/xml"})
end
@response = Petstore::Swagger::Response.new(@raw)
@response = Petstore::Response.new(API_CLIENT, @raw)
@response.format.should == 'xml'
@response.xml?.should == true
end

View File

@ -36,40 +36,36 @@ end
# help
#end
def configure_swagger
Petstore::Swagger.configure do |config|
config.api_key['api_key'] = 'special-key'
config.host = 'petstore.swagger.io'
config.base_path = '/v2'
end
API_CLIENT = Petstore::ApiClient.new do |config|
config.api_key['api_key'] = 'special-key'
config.host = 'petstore.swagger.io'
config.base_path = '/v2'
end
# always delete and then re-create the pet object with 10002
def prepare_pet
def prepare_pet(pet_api)
# remove the pet
Petstore::PetApi.delete_pet(10002)
pet_api.delete_pet(10002)
# recreate the pet
category = Petstore::Category.new('id' => 20002, 'name' => 'category test')
tag = Petstore::Tag.new('id' => 30002, 'name' => 'tag test')
pet = Petstore::Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING", 'photo_urls' => 'photo url',
'category' => category, 'tags' => [tag], 'status' => 'pending')
Petstore::PetApi.add_pet(:'body'=> pet)
pet_api.add_pet(:'body'=> pet)
end
# always delete and then re-create the store order
def prepare_store
def prepare_store(store_api)
order = Petstore::Order.new("id" => 10002,
"petId" => 10002,
"quantity" => 789,
"shipDate" => "2015-04-06T23:42:01.678Z",
"status" => "placed",
"complete" => false)
Petstore::StoreApi.place_order(:body => order)
store_api.place_order(:body => order)
end
configure_swagger
# A random string to tack onto stuff to ensure we're not seeing
# data from a previous test run
RAND = ("a".."z").to_a.sample(8).join

View File

@ -2,17 +2,17 @@ require 'spec_helper'
describe "Store" do
before do
configure_swagger
prepare_store
@api = Petstore::StoreApi.new(API_CLIENT)
prepare_store @api
end
it "should fetch an order" do
item = Petstore::StoreApi.get_order_by_id(10002)
item = @api.get_order_by_id(10002)
item.id.should == 10002
end
it "should featch the inventory" do
result = Petstore::StoreApi.get_inventory
result = @api.get_inventory
result.should be_a(Hash)
result.should_not be_empty
result.each do |k, v|

View File

@ -1,56 +0,0 @@
# require 'spec_helper'
require File.dirname(__FILE__) + '/spec_helper'
describe Petstore::Swagger do
before(:each) do
configure_swagger
end
after(:each) do
end
context 'initialization' do
context 'URL stuff' do
context 'host' do
it 'removes http from host' do
Petstore::Swagger.configure {|c| c.host = 'http://example.com' }
Petstore::Swagger.configuration.host.should == 'example.com'
end
it 'removes https from host' do
Petstore::Swagger.configure {|c| c.host = 'https://wookiee.com' }
Petstore::Swagger.configuration.host.should == 'wookiee.com'
end
it 'removes trailing path from host' do
Petstore::Swagger.configure {|c| c.host = 'hobo.com/v4' }
Petstore::Swagger.configuration.host.should == 'hobo.com'
end
end
context 'base_path' do
it "prepends a slash to base_path" do
Petstore::Swagger.configure {|c| c.base_path = 'v4/dog' }
Petstore::Swagger.configuration.base_path.should == '/v4/dog'
end
it "doesn't prepend a slash if one is already there" do
Petstore::Swagger.configure {|c| c.base_path = '/v4/dog' }
Petstore::Swagger.configuration.base_path.should == '/v4/dog'
end
it "ends up as a blank string if nil" do
Petstore::Swagger.configure {|c| c.base_path = nil }
Petstore::Swagger.configuration.base_path.should == ''
end
end
end
end
end