[ruby] Add tests to operation servers in ruby client (#16179)

* add tests to operation servers in ruby client

* update samples

* revert change in spec, remove invalid? check in anyof model

* fix hsot table

* fix circleci config

* fix

* fix host table

* fix host table

* minor fix

* fix

* reorder

* update

* fix
This commit is contained in:
William Cheng 2023-07-25 19:29:06 +08:00 committed by GitHub
parent 24656156ed
commit 9aa15d9f09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 287 additions and 7 deletions

View File

@ -38,9 +38,18 @@ commands: # a reusable command with parameters
- run: - run:
command: 'sudo docker info >/dev/null 2>&1 || sudo service docker start; ' command: 'sudo docker info >/dev/null 2>&1 || sudo service docker start; '
- run: - run:
command: |- command: |
printf '127.0.0.1 petstore.swagger.io sudo tee -a /etc/hosts \<<< "127.0.0.1 path.v1.test.openapi-generator.tech"
' | sudo tee -a /etc/hosts sudo tee -a /etc/hosts \<<< "127.0.0.1 path.v2.test.openapi-generator.tech"
sudo tee -a /etc/hosts \<<< "127.0.0.111 path.v3.test.openapi-generator.tech"
sudo tee -a /etc/hosts \<<< "127.0.0.1 operation.v1.test.openapi-generator.tech"
sudo tee -a /etc/hosts \<<< "127.0.0.1 operation.v2.test.openapi-generator.tech"
sudo tee -a /etc/hosts \<<< "127.0.0.111 operation.v3.test.openapi-generator.tech"
sudo tee -a /etc/hosts \<<< "127.0.0.1 server.v1.test.openapi-generator.tech"
sudo tee -a /etc/hosts \<<< "127.0.0.1 server.v2.test.openapi-generator.tech"
sudo tee -a /etc/hosts \<<< "127.0.0.111 server.v3.test.openapi-generator.tech"
sudo tee -a /etc/hosts \<<< "127.0.0.1 petstore.swagger.io"
cat /etc/hosts
# - run: docker pull openapitools/openapi-petstore # - run: docker pull openapitools/openapi-petstore
# - run: docker run -d -e OPENAPI_BASE_PATH=/v3 -e DISABLE_API_KEY=1 -e DISABLE_OAUTH=1 -p 80:8080 openapitools/openapi-petstore # - run: docker run -d -e OPENAPI_BASE_PATH=/v3 -e DISABLE_API_KEY=1 -e DISABLE_OAUTH=1 -p 80:8080 openapitools/openapi-petstore
- run: docker pull swaggerapi/petstore - run: docker pull swaggerapi/petstore

View File

@ -81,7 +81,7 @@
# raise if data contains keys that are not known to the model # raise if data contains keys that are not known to the model
raise unless (data.keys - const.acceptable_attributes).empty? raise unless (data.keys - const.acceptable_attributes).empty?
model = const.build_from_hash(data) model = const.build_from_hash(data)
return model if model && model.valid? return model if model
end end
end end
end end

View File

@ -48,6 +48,15 @@ paths:
- 80 - 80
- 8080 - 8080
default: 80 default: 80
- url: 'http://path.{version}.test.openapi-generator.tech/v2'
description: test server with variables
variables:
version:
description: target server
enum:
- 'v1'
- 'v2'
default: 'v2'
post: post:
tags: tags:
- pet - pet
@ -178,6 +187,33 @@ paths:
- 'read:pets' - 'read:pets'
deprecated: true deprecated: true
'/pet/{petId}': '/pet/{petId}':
servers:
- url: 'http://petstore.swagger.io/v2'
- url: 'http://path-server-test.petstore.local/v2'
- url: 'http://{server}.swagger.io:{port}/v2'
description: test server with variables
variables:
server:
description: target server
enum:
- 'petstore'
- 'qa-petstore'
- 'dev-petstore'
default: 'petstore'
port:
enum:
- 80
- 8080
default: 80
- url: 'http://path.{version}.test.openapi-generator.tech/v2'
description: test server with variables
variables:
version:
description: target server
enum:
- 'v1'
- 'v2'
default: 'v2'
get: get:
tags: tags:
- pet - pet
@ -1256,6 +1292,15 @@ servers:
default: 'v2' default: 'v2'
- url: https://127.0.0.1/no_varaible - url: https://127.0.0.1/no_varaible
description: The local server without variables description: The local server without variables
- url: http://server.{version}.openapi-generator.tech
description: The openapi-generator test server
variables:
version:
description: target server
enum:
- 'v1'
- 'v2'
default: 'v2'
components: components:
requestBodies: requestBodies:
UserArray: UserArray:

View File

@ -1192,8 +1192,8 @@
</property> </property>
</activation> </activation>
<modules> <modules>
<module>samples/client/petstore/ruby-faraday</module>
<module>samples/client/petstore/ruby</module> <module>samples/client/petstore/ruby</module>
<module>samples/client/petstore/ruby-faraday</module>
<module>samples/client/petstore/ruby-autoload</module> <module>samples/client/petstore/ruby-autoload</module>
</modules> </modules>
</profile> </profile>

View File

@ -316,6 +316,20 @@ module Petstore
{ {
url: "https://127.0.0.1/no_varaible", url: "https://127.0.0.1/no_varaible",
description: "The local server without variables", description: "The local server without variables",
},
{
url: "http://server.{version}.openapi-generator.tech",
description: "The openapi-generator test server",
variables: {
version: {
description: "target server",
default_value: "v2",
enum_values: [
"v1",
"v2"
]
}
}
} }
] ]
end end
@ -353,6 +367,114 @@ module Petstore
] ]
} }
} }
},
{
url: "http://path.{version}.test.openapi-generator.tech/v2",
description: "test server with variables",
variables: {
version: {
description: "target server",
default_value: "v2",
enum_values: [
"v1",
"v2"
]
}
}
}
],
"PetApi.delete_pet": [
{
url: "http://petstore.swagger.io/v2",
description: "No description provided",
},
{
url: "http://path-server-test.petstore.local/v2",
description: "No description provided",
},
{
url: "http://{server}.swagger.io:{port}/v2",
description: "test server with variables",
variables: {
server: {
description: "target server",
default_value: "petstore",
enum_values: [
"petstore",
"qa-petstore",
"dev-petstore"
]
},
port: {
description: "No description provided",
default_value: "80",
enum_values: [
"80",
"8080"
]
}
}
},
{
url: "http://path.{version}.test.openapi-generator.tech/v2",
description: "test server with variables",
variables: {
version: {
description: "target server",
default_value: "v2",
enum_values: [
"v1",
"v2"
]
}
}
}
],
"PetApi.get_pet_by_id": [
{
url: "http://petstore.swagger.io/v2",
description: "No description provided",
},
{
url: "http://path-server-test.petstore.local/v2",
description: "No description provided",
},
{
url: "http://{server}.swagger.io:{port}/v2",
description: "test server with variables",
variables: {
server: {
description: "target server",
default_value: "petstore",
enum_values: [
"petstore",
"qa-petstore",
"dev-petstore"
]
},
port: {
description: "No description provided",
default_value: "80",
enum_values: [
"80",
"8080"
]
}
}
},
{
url: "http://path.{version}.test.openapi-generator.tech/v2",
description: "test server with variables",
variables: {
version: {
description: "target server",
default_value: "v2",
enum_values: [
"v1",
"v2"
]
}
}
} }
], ],
"PetApi.update_pet": [ "PetApi.update_pet": [
@ -386,6 +508,67 @@ module Petstore
] ]
} }
} }
},
{
url: "http://path.{version}.test.openapi-generator.tech/v2",
description: "test server with variables",
variables: {
version: {
description: "target server",
default_value: "v2",
enum_values: [
"v1",
"v2"
]
}
}
}
],
"PetApi.update_pet_with_form": [
{
url: "http://petstore.swagger.io/v2",
description: "No description provided",
},
{
url: "http://path-server-test.petstore.local/v2",
description: "No description provided",
},
{
url: "http://{server}.swagger.io:{port}/v2",
description: "test server with variables",
variables: {
server: {
description: "target server",
default_value: "petstore",
enum_values: [
"petstore",
"qa-petstore",
"dev-petstore"
]
},
port: {
description: "No description provided",
default_value: "80",
enum_values: [
"80",
"8080"
]
}
}
},
{
url: "http://path.{version}.test.openapi-generator.tech/v2",
description: "test server with variables",
variables: {
version: {
description: "target server",
default_value: "v2",
enum_values: [
"v1",
"v2"
]
}
}
} }
], ],
} }

View File

@ -89,7 +89,7 @@ module Petstore
# raise if data contains keys that are not known to the model # raise if data contains keys that are not known to the model
raise unless (data.keys - const.acceptable_attributes).empty? raise unless (data.keys - const.acceptable_attributes).empty?
model = const.build_from_hash(data) model = const.build_from_hash(data)
return model if model && model.valid? return model if model
end end
end end
end end

View File

@ -61,7 +61,7 @@ describe Petstore::Configuration do
} }
expect { expect {
config.base_url(:'PetApi.add_pet') config.base_url(:'PetApi.add_pet')
}.to raise_error(ArgumentError, 'Invalid index 10 when selecting the server. Must not be nil and must be less than 3') }.to raise_error(ArgumentError, 'Invalid index 10 when selecting the server. Must not be nil and must be less than 4')
end end
it 'should remove trailing slashes' do it 'should remove trailing slashes' do

View File

@ -60,6 +60,49 @@ describe "Pet" do
expect(pet.category.name).to eq("category test") expect(pet.category.name).to eq("category test")
end end
it "should fetch a pet object using invalid operation path" do
# backup index
index_backup = @pet_api.api_client.config.server_operation_index
expect(index_backup).to eq({})
# test operation index 1 (invalid path)
@pet_api.api_client.config.server_operation_index = {
:'PetApi.get_pet_by_id' => 1
}
expect(@pet_api.api_client.config.base_url(:'PetApi.get_pet_by_id')).to eq('http://path-server-test.petstore.local/v2')
expect {
pet = @pet_api.get_pet_by_id(@pet_id)
}.to raise_error(Petstore::ApiError) # path-server-test.petstore.local is invalid (not defined in host table)
# restore index
@pet_api.api_client.config.server_operation_index = index_backup
expect(@pet_api.api_client.config.server_operation_index).to eq({})
end
it "should fetch a pet object using operation path" do
# backup index
index_backup = @pet_api.api_client.config.server_operation_index
expect(index_backup).to eq({})
# test operation index 3
@pet_api.api_client.config.server_operation_index = {
:'PetApi.get_pet_by_id' => 3
}
expect(@pet_api.api_client.config.base_url(:'PetApi.get_pet_by_id')).to eq('http://path.v2.test.openapi-generator.tech/v2')
pet = @pet_api.get_pet_by_id(@pet_id)
expect(pet).to be_a(Petstore::Pet)
expect(pet.id).to eq(@pet_id)
expect(pet.name).to eq("RUBY UNIT TESTING")
expect(pet.tags[0].name).to eq("tag test")
expect(pet.category.name).to eq("category test")
# restore index
@pet_api.api_client.config.server_operation_index = index_backup
expect(@pet_api.api_client.config.server_operation_index).to eq({})
expect(@pet_api.api_client.config.base_url(:'PetApi.get_pet_by_id')).to eq('http://petstore.swagger.io/v2')
end
it "should fetch a pet object with http info" do it "should fetch a pet object with http info" do
pet, status_code, headers = @pet_api.get_pet_by_id_with_http_info(@pet_id) pet, status_code, headers = @pet_api.get_pet_by_id_with_http_info(@pet_id)
expect(status_code).to eq(200) expect(status_code).to eq(200)