Fix Ruby tests of the Petstore sample

This commit is contained in:
xhh 2016-01-20 13:48:46 +08:00 committed by Maelig Nantel
parent dc32ac18b7
commit 1f072a697b
3 changed files with 64 additions and 53 deletions

View File

@ -4,7 +4,17 @@ require 'json'
describe "Pet" do
before do
@pet_api = Petstore::PetApi.new(API_CLIENT)
prepare_pet @pet_api
@pet_id = prepare_pet(@pet_api)
end
after do
# remove the testing pet
begin
@pet_api.delete_pet(@pet_id)
rescue Petstore::ApiError => e
# ignore ApiError 404 (Not Found)
raise e if e.code != 404
end
end
describe "pet methods" do
@ -14,7 +24,7 @@ describe "Pet" do
category1 = Petstore::Category.new({:id => 1, :name => 'category unknown'})
# initalize using both string and symbol key
pet_hash = {
:id => 10002,
:id => @pet_id,
:name => "RUBY UNIT TESTING",
:status => "pending",
:photo_urls => ["url1", "url2"],
@ -25,7 +35,7 @@ describe "Pet" do
# test new
pet.name.should == "RUBY UNIT TESTING"
pet.status.should == "pending"
pet.id.should == 10002
pet.id.should == @pet_id
pet.tags[0].id.should == 1
pet.tags[1].name.should == 'tag2'
pet.category.name.should == 'category unknown'
@ -42,20 +52,20 @@ describe "Pet" do
end
it "should fetch a pet object" do
pet = @pet_api.get_pet_by_id(10002)
pet = @pet_api.get_pet_by_id(@pet_id)
pet.should be_a(Petstore::Pet)
pet.id.should == 10002
pet.id.should == @pet_id
pet.name.should == "RUBY UNIT TESTING"
pet.tags[0].name.should == "tag test"
pet.category.name.should == "category test"
end
it "should fetch a pet object with http info" do
pet, status_code, headers = @pet_api.get_pet_by_id_with_http_info(10002)
pet, status_code, headers = @pet_api.get_pet_by_id_with_http_info(@pet_id)
status_code.should == 200
headers['Content-Type'].should == 'application/json'
pet.should be_a(Petstore::Pet)
pet.id.should == 10002
pet.id.should == @pet_id
pet.name.should == "RUBY UNIT TESTING"
pet.tags[0].name.should == "tag test"
pet.category.name.should == "category test"
@ -63,7 +73,7 @@ describe "Pet" do
it "should not find a pet that does not exist" do
begin
@pet_api.get_pet_by_id(-10002)
@pet_api.get_pet_by_id(-@pet_id)
fail 'it should raise error'
rescue Petstore::ApiError => e
e.code.should == 404
@ -74,6 +84,20 @@ describe "Pet" do
end
end
it "should update a pet" do
pet = @pet_api.get_pet_by_id(@pet_id)
pet.id.should == @pet_id
pet.name.should == "RUBY UNIT TESTING"
pet.status.should == 'pending'
@pet_api.update_pet_with_form(@pet_id, name: 'new name', status: 'sold')
fetched = @pet_api.get_pet_by_id(@pet_id)
fetched.id.should == @pet_id
fetched.name.should == "new name"
fetched.status.should == 'sold'
end
it "should find pets by status" do
pets = @pet_api.find_pets_by_status(:status => 'available')
pets.length.should >= 3
@ -97,43 +121,27 @@ describe "Pet" do
end
end
it "should update a pet" do
pet = Petstore::Pet.new({'id' => 10002, 'status' => 'sold'})
@pet_api.add_pet(:body => pet)
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")
pet = Petstore::Pet.new('id' => 10003, 'name' => "RUBY UNIT TESTING")
result = @pet_api.add_pet(:body => pet)
# nothing is returned
result.should be_nil
pet = @pet_api.get_pet_by_id(10002)
pet.id.should == 10002
pet = @pet_api.get_pet_by_id(10003)
pet.id.should == 10003
pet.name.should == "RUBY UNIT TESTING"
@pet_api.delete_pet(10003)
end
it "should upload a file to a pet" do
pet = Petstore::Pet.new('id' => 10002, 'name' => "RUBY UNIT TESTING")
result = @pet_api.add_pet(:body => pet)
# nothing is returned
result.should be_nil
result = @pet_api.upload_file(10002, file: File.new('hello.txt'))
result = @pet_api.upload_file(@pet_id, file: File.new('hello.txt'))
# nothing is returned
result.should be_nil
end
it "should upload a file with form parameter to a pet" do
pet = Petstore::Pet.new('id' => 10002, 'name' => 'RUBY UNIT TESTING')
result = @pet_api.add_pet(body: pet)
result.should be_nil
result = @pet_api.upload_file(10002, file: File.new('hello.txt'), additional_metadata: 'metadata')
result = @pet_api.upload_file(@pet_id, file: File.new('hello.txt'), additional_metadata: 'metadata')
result.should be_nil
end

View File

@ -38,33 +38,32 @@ end
API_CLIENT = Petstore::ApiClient.new(Petstore::Configuration.new)
# always delete and then re-create the pet object with 10002
def prepare_pet(pet_api)
begin
# remove the pet
pet_api.delete_pet(10002)
rescue Petstore::ApiError => e
# ignore ApiError 404 (Not Found)
raise e if e.code != 404
end
# 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')
pet_api.add_pet(:'body'=> pet)
def random_id
rand(1000000) + 20000
end
# always delete and then re-create the store order
# create a random pet, return its id
def prepare_pet(pet_api)
pet_id = random_id
category = Petstore::Category.new('id' => 20002, 'name' => 'category test')
tag = Petstore::Tag.new('id' => 30002, 'name' => 'tag test')
pet = Petstore::Pet.new('id' => pet_id, 'name' => "RUBY UNIT TESTING", 'photo_urls' => 'photo url',
'category' => category, 'tags' => [tag], 'status' => 'pending')
pet_api.add_pet(:'body'=> pet)
return pet_id
end
# create a random order, return its id
def prepare_store(store_api)
order = Petstore::Order.new("id" => 10002,
"petId" => 10002,
order_id = random_id
order = Petstore::Order.new("id" => order_id,
"petId" => 123,
"quantity" => 789,
"shipDate" => "2015-04-06T23:42:01.678Z",
"status" => "placed",
"complete" => false)
store_api.place_order(:body => order)
return order_id
end
# A random string to tack onto stuff to ensure we're not seeing

View File

@ -3,12 +3,16 @@ require 'spec_helper'
describe "Store" do
before do
@api = Petstore::StoreApi.new(API_CLIENT)
prepare_store @api
@order_id = prepare_store(@api)
end
after do
@api.delete_order(@order_id)
end
it "should fetch an order" do
item = @api.get_order_by_id(10002)
item.id.should == 10002
item = @api.get_order_by_id(@order_id)
item.id.should == @order_id
end
it "should featch the inventory" do