From 1f072a697bd4fff95bbe5febc059f6f8372868ce Mon Sep 17 00:00:00 2001 From: xhh Date: Wed, 20 Jan 2016 13:48:46 +0800 Subject: [PATCH] Fix Ruby tests of the Petstore sample --- samples/client/petstore/ruby/spec/pet_spec.rb | 70 +++++++++++-------- .../client/petstore/ruby/spec/spec_helper.rb | 37 +++++----- .../client/petstore/ruby/spec/store_spec.rb | 10 ++- 3 files changed, 64 insertions(+), 53 deletions(-) diff --git a/samples/client/petstore/ruby/spec/pet_spec.rb b/samples/client/petstore/ruby/spec/pet_spec.rb index 9426d2024b9..b9aa9398bc2 100644 --- a/samples/client/petstore/ruby/spec/pet_spec.rb +++ b/samples/client/petstore/ruby/spec/pet_spec.rb @@ -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 diff --git a/samples/client/petstore/ruby/spec/spec_helper.rb b/samples/client/petstore/ruby/spec/spec_helper.rb index 5eff37b134a..5382a3879ef 100644 --- a/samples/client/petstore/ruby/spec/spec_helper.rb +++ b/samples/client/petstore/ruby/spec/spec_helper.rb @@ -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 diff --git a/samples/client/petstore/ruby/spec/store_spec.rb b/samples/client/petstore/ruby/spec/store_spec.rb index 1e0f035d143..30743fd374e 100644 --- a/samples/client/petstore/ruby/spec/store_spec.rb +++ b/samples/client/petstore/ruby/spec/store_spec.rb @@ -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