mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-05 16:30:52 +00:00
Update tests with latest code
This commit is contained in:
parent
609d915541
commit
a75b05b952
53
samples/client/petstore/ruby/spec/api_client_spec.rb
Normal file
53
samples/client/petstore/ruby/spec/api_client_spec.rb
Normal 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
|
@ -3,8 +3,8 @@ require 'json'
|
|||||||
|
|
||||||
describe "Pet" do
|
describe "Pet" do
|
||||||
before do
|
before do
|
||||||
configure_swagger
|
@pet_api = Petstore::PetApi.new(API_CLIENT)
|
||||||
prepare_pet
|
prepare_pet @pet_api
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "pet methods" do
|
describe "pet methods" do
|
||||||
@ -42,7 +42,7 @@ describe "Pet" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "should fetch a pet object" do
|
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.should be_a(Petstore::Pet)
|
||||||
pet.id.should == 10002
|
pet.id.should == 10002
|
||||||
pet.name.should == "RUBY UNIT TESTING"
|
pet.name.should == "RUBY UNIT TESTING"
|
||||||
@ -52,9 +52,9 @@ describe "Pet" do
|
|||||||
|
|
||||||
it "should not find a pet that does not exist" do
|
it "should not find a pet that does not exist" do
|
||||||
begin
|
begin
|
||||||
Petstore::PetApi.get_pet_by_id(-10002)
|
@pet_api.get_pet_by_id(-10002)
|
||||||
fail 'it should raise error'
|
fail 'it should raise error'
|
||||||
rescue Petstore::Swagger::ApiError => e
|
rescue Petstore::ApiError => e
|
||||||
e.code.should == 404
|
e.code.should == 404
|
||||||
e.message.should == 'Not Found'
|
e.message.should == 'Not Found'
|
||||||
e.response_body.should == '{"code":1,"type":"error","message":"Pet not found"}'
|
e.response_body.should == '{"code":1,"type":"error","message":"Pet not found"}'
|
||||||
@ -64,7 +64,7 @@ describe "Pet" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "should find pets by status" do
|
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.length.should >= 3
|
||||||
pets.each do |pet|
|
pets.each do |pet|
|
||||||
pet.should be_a(Petstore::Pet)
|
pet.should be_a(Petstore::Pet)
|
||||||
@ -73,12 +73,12 @@ describe "Pet" do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "should not find a pet with invalid status" do
|
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
|
pets.length.should == 0
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should find a pet by status" do
|
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|
|
pets.each do |pet|
|
||||||
if pet.status != 'available' && pet.status != 'sold'
|
if pet.status != 'available' && pet.status != 'sold'
|
||||||
raise "pet status wasn't right"
|
raise "pet status wasn't right"
|
||||||
@ -88,20 +88,20 @@ describe "Pet" do
|
|||||||
|
|
||||||
it "should update a pet" do
|
it "should update a pet" do
|
||||||
pet = Petstore::Pet.new({'id' => 10002, 'status' => 'sold'})
|
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.id.should == 10002
|
||||||
fetched.status.should == 'sold'
|
fetched.status.should == 'sold'
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should create a pet" do
|
it "should create a pet" do
|
||||||
pet = Petstore::Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING")
|
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
|
# nothing is returned
|
||||||
result.should be_nil
|
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.id.should == 10002
|
||||||
pet.name.should == "RUBY UNIT TESTING"
|
pet.name.should == "RUBY UNIT TESTING"
|
||||||
end
|
end
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Petstore::Swagger::Request do
|
describe Petstore::Request do
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
Petstore::Swagger.configure do |config|
|
@api_client = Petstore::ApiClient.new do |config|
|
||||||
inject_format = true
|
inject_format = true
|
||||||
config.api_key['api_key'] = 'special-key'
|
config.api_key['api_key'] = 'special-key'
|
||||||
config.host = 'petstore.swagger.io'
|
config.host = 'petstore.swagger.io'
|
||||||
@ -15,7 +15,7 @@ describe Petstore::Swagger::Request do
|
|||||||
@default_params = {
|
@default_params = {
|
||||||
:params => {:foo => "1", :bar => "2"}
|
: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
|
end
|
||||||
|
|
||||||
describe "initialization" do
|
describe "initialization" do
|
||||||
@ -29,7 +29,7 @@ describe Petstore::Swagger::Request do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "allows params to be nil" do
|
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 == {}
|
@request.params.should == {}
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -60,7 +60,7 @@ describe Petstore::Swagger::Request do
|
|||||||
describe "path" do
|
describe "path" do
|
||||||
|
|
||||||
it "accounts for a total absence of format in the path string" 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",
|
:format => "xml",
|
||||||
:params => {
|
:params => {
|
||||||
}
|
}
|
||||||
@ -69,7 +69,7 @@ describe Petstore::Swagger::Request do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "does string substitution (format) on path params" do
|
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",
|
:format => "xml",
|
||||||
:params => {
|
:params => {
|
||||||
}
|
}
|
||||||
@ -78,7 +78,7 @@ describe Petstore::Swagger::Request do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "URI encodes the path" do
|
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 => {
|
:params => {
|
||||||
:word => "bill gates"
|
:word => "bill gates"
|
||||||
}
|
}
|
||||||
@ -90,7 +90,7 @@ describe Petstore::Swagger::Request do
|
|||||||
|
|
||||||
describe "#update_params_for_auth!" do
|
describe "#update_params_for_auth!" do
|
||||||
it "sets header api-key parameter with prefix" do
|
it "sets header api-key parameter with prefix" do
|
||||||
Petstore::Swagger.configure do |config|
|
@api_client.configure do |config|
|
||||||
inject_format = true
|
inject_format = true
|
||||||
config.api_key_prefix['api_key'] = 'PREFIX'
|
config.api_key_prefix['api_key'] = 'PREFIX'
|
||||||
end
|
end
|
||||||
@ -100,7 +100,7 @@ describe Petstore::Swagger::Request do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "sets header api-key parameter without prefix" do
|
it "sets header api-key parameter without prefix" do
|
||||||
Petstore::Swagger.configure do |config|
|
@api_client.configure do |config|
|
||||||
inject_format = true
|
inject_format = true
|
||||||
config.api_key_prefix['api_key'] = nil
|
config.api_key_prefix['api_key'] = nil
|
||||||
end
|
end
|
||||||
|
@ -1,18 +1,13 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe Petstore::Swagger::Response do
|
describe Petstore::Response do
|
||||||
|
|
||||||
before do
|
|
||||||
configure_swagger
|
|
||||||
prepare_pet
|
|
||||||
end
|
|
||||||
|
|
||||||
before(:each) do
|
before(:each) do
|
||||||
VCR.use_cassette('pet_resource', :record => :new_episodes) do
|
VCR.use_cassette('pet_resource', :record => :new_episodes) do
|
||||||
@raw = Typhoeus::Request.get("http://petstore.swagger.io/v2/pet/10002")
|
@raw = Typhoeus::Request.get("http://petstore.swagger.io/v2/pet/10002")
|
||||||
end
|
end
|
||||||
|
|
||||||
@response = Petstore::Swagger::Response.new(@raw)
|
@response = Petstore::Response.new(API_CLIENT, @raw)
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "initialization" do
|
describe "initialization" do
|
||||||
@ -43,7 +38,7 @@ describe Petstore::Swagger::Response do
|
|||||||
@raw = Typhoeus::Request.get("http://petstore.swagger.io/v2/pet/10002",
|
@raw = Typhoeus::Request.get("http://petstore.swagger.io/v2/pet/10002",
|
||||||
:headers => {'Accept'=> "application/xml"})
|
:headers => {'Accept'=> "application/xml"})
|
||||||
end
|
end
|
||||||
@response = Petstore::Swagger::Response.new(@raw)
|
@response = Petstore::Response.new(API_CLIENT, @raw)
|
||||||
@response.format.should == 'xml'
|
@response.format.should == 'xml'
|
||||||
@response.xml?.should == true
|
@response.xml?.should == true
|
||||||
end
|
end
|
||||||
|
@ -36,40 +36,36 @@ end
|
|||||||
# help
|
# help
|
||||||
#end
|
#end
|
||||||
|
|
||||||
def configure_swagger
|
API_CLIENT = Petstore::ApiClient.new do |config|
|
||||||
Petstore::Swagger.configure do |config|
|
config.api_key['api_key'] = 'special-key'
|
||||||
config.api_key['api_key'] = 'special-key'
|
config.host = 'petstore.swagger.io'
|
||||||
config.host = 'petstore.swagger.io'
|
config.base_path = '/v2'
|
||||||
config.base_path = '/v2'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# always delete and then re-create the pet object with 10002
|
# always delete and then re-create the pet object with 10002
|
||||||
def prepare_pet
|
def prepare_pet(pet_api)
|
||||||
# remove the pet
|
# remove the pet
|
||||||
Petstore::PetApi.delete_pet(10002)
|
pet_api.delete_pet(10002)
|
||||||
# recreate the pet
|
# recreate the pet
|
||||||
category = Petstore::Category.new('id' => 20002, 'name' => 'category test')
|
category = Petstore::Category.new('id' => 20002, 'name' => 'category test')
|
||||||
tag = Petstore::Tag.new('id' => 30002, 'name' => 'tag test')
|
tag = Petstore::Tag.new('id' => 30002, 'name' => 'tag test')
|
||||||
pet = Petstore::Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING", 'photo_urls' => 'photo url',
|
pet = Petstore::Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING", 'photo_urls' => 'photo url',
|
||||||
'category' => category, 'tags' => [tag], 'status' => 'pending')
|
'category' => category, 'tags' => [tag], 'status' => 'pending')
|
||||||
|
|
||||||
Petstore::PetApi.add_pet(:'body'=> pet)
|
pet_api.add_pet(:'body'=> pet)
|
||||||
end
|
end
|
||||||
|
|
||||||
# always delete and then re-create the store order
|
# always delete and then re-create the store order
|
||||||
def prepare_store
|
def prepare_store(store_api)
|
||||||
order = Petstore::Order.new("id" => 10002,
|
order = Petstore::Order.new("id" => 10002,
|
||||||
"petId" => 10002,
|
"petId" => 10002,
|
||||||
"quantity" => 789,
|
"quantity" => 789,
|
||||||
"shipDate" => "2015-04-06T23:42:01.678Z",
|
"shipDate" => "2015-04-06T23:42:01.678Z",
|
||||||
"status" => "placed",
|
"status" => "placed",
|
||||||
"complete" => false)
|
"complete" => false)
|
||||||
Petstore::StoreApi.place_order(:body => order)
|
store_api.place_order(:body => order)
|
||||||
end
|
end
|
||||||
|
|
||||||
configure_swagger
|
|
||||||
|
|
||||||
# A random string to tack onto stuff to ensure we're not seeing
|
# A random string to tack onto stuff to ensure we're not seeing
|
||||||
# data from a previous test run
|
# data from a previous test run
|
||||||
RAND = ("a".."z").to_a.sample(8).join
|
RAND = ("a".."z").to_a.sample(8).join
|
||||||
|
@ -2,17 +2,17 @@ require 'spec_helper'
|
|||||||
|
|
||||||
describe "Store" do
|
describe "Store" do
|
||||||
before do
|
before do
|
||||||
configure_swagger
|
@api = Petstore::StoreApi.new(API_CLIENT)
|
||||||
prepare_store
|
prepare_store @api
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should fetch an order" do
|
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
|
item.id.should == 10002
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should featch the inventory" do
|
it "should featch the inventory" do
|
||||||
result = Petstore::StoreApi.get_inventory
|
result = @api.get_inventory
|
||||||
result.should be_a(Hash)
|
result.should be_a(Hash)
|
||||||
result.should_not be_empty
|
result.should_not be_empty
|
||||||
result.each do |k, v|
|
result.each do |k, v|
|
||||||
|
@ -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
|
|
Loading…
x
Reference in New Issue
Block a user