Skip to content

Commit

Permalink
Merge pull request #1980 from wing328/ruby_add_test
Browse files Browse the repository at this point in the history
[Ruby] add auto-generated test cases (rspec)
  • Loading branch information
wing328 committed Jan 27, 2016
2 parents d779174 + e4a991d commit bfd17e4
Show file tree
Hide file tree
Showing 12 changed files with 1,007 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
protected String gemName;
protected String moduleName;
protected String gemVersion = "1.0.0";
protected String specFolder = "spec";
protected String libFolder = "lib";
protected String gemLicense = "Apache-2.0";
protected String gemHomepage = "http://swagger.io";
Expand All @@ -47,6 +48,9 @@ public RubyClientCodegen() {
apiTemplateFiles.put("api.mustache", ".rb");
embeddedTemplateDir = templateDir = "ruby";

modelTestTemplateFiles.put("model_test.mustache", ".rb");
apiTestTemplateFiles.put("api_test.mustache", ".rb");

typeMapping.clear();
languageSpecificPrimitives.clear();

Expand Down Expand Up @@ -235,6 +239,16 @@ public String modelFileFolder() {
return outputFolder + File.separator + libFolder + File.separator + gemName + File.separator + modelPackage.replace("/", File.separator);
}

@Override
public String apiTestFileFolder() {
return outputFolder + File.separator + specFolder + File.separator + apiPackage.replace("/", File.separator);
}

@Override
public String modelTestFileFolder() {
return outputFolder + File.separator + specFolder + File.separator + modelPackage.replace("/", File.separator);
}

@Override
public String getTypeDeclaration(Property p) {
if (p instanceof ArrayProperty) {
Expand Down Expand Up @@ -367,6 +381,16 @@ public String toApiFilename(String name) {
return underscore(name) + "_api";
}

@Override
public String toApiTestFilename(String name) {
return toApiName(name) + "_spec";
}

@Override
public String toModelTestFilename(String name) {
return toModelName(name) + "_spec";
}

@Override
public String toApiName(String name) {
if (name.length() == 0) {
Expand Down
43 changes: 43 additions & 0 deletions modules/swagger-codegen/src/main/resources/ruby/api_test.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'spec_helper'
require 'json'

# Unit tests for {{moduleName}}::{{classname}}
# Automatically generated by swagger-codegen (github.com/swagger-api/swagger-codegen)
# Please update as you see appropriate
{{#operations}}describe '{{classname}}' do
before do
# run before each test
@instance = {{moduleName}}::{{classname}}.new
end

after do
# run after each test
end

describe 'test an instance of {{classname}}' do
it 'should create an instact of {{classname}}' do
@instance.should be_a({{moduleName}}::{{classname}})
end
end

{{#operation}}
# unit tests for {{operationId}}
# {{summary}}
# {{notes}}
{{#allParams}}{{#required}} # @param {{paramName}} {{description}}
{{/required}}{{/allParams}} # @param [Hash] opts the optional parameters
{{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
{{/required}}{{/allParams}} # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}]
describe '{{operationId}} test' do
it "should work" do
# assertion here
# should be_a()
# should be_nil
# should ==
# should_not ==
end
end

{{/operation}}
end
{{/operations}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require 'spec_helper'
require 'json'
require 'date'

# Unit tests for {{moduleName}}::{{classname}}
# Automatically generated by swagger-codegen (github.com/swagger-api/swagger-codegen)
# Please update as you see appropriate
{{#models}}{{#model}}describe '{{classname}}' do
before do
# run before each test
@instance = {{moduleName}}::{{classname}}.new
end

after do
# run after each test
end

describe 'test an instance of {{classname}}' do
it 'should create an instact of {{classname}}' do
@instance.should be_a({{moduleName}}::{{classname}})
end
end
{{#vars}}
describe 'test attribute "{{{name}}}"' do
it 'should work' do
# assertion here
# should be_a()
# should be_nil
# should ==
# should_not ==
end
end

{{/vars}}
end
{{/model}}{{/models}}
118 changes: 118 additions & 0 deletions samples/client/petstore/ruby/lib/petstore/api/pet_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,124 @@ def upload_file_with_http_info(pet_id, opts = {})
end
return data, status_code, headers
end

# Fake endpoint to test byte array return by 'Find pet by ID'
# Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions
# @param pet_id ID of pet that needs to be fetched
# @param [Hash] opts the optional parameters
# @return [binary]
def get_pet_by_id_with_byte_array(pet_id, opts = {})
data, status_code, headers = get_pet_by_id_with_byte_array_with_http_info(pet_id, opts)
return data
end

# Fake endpoint to test byte array return by 'Find pet by ID'
# Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions
# @param pet_id ID of pet that needs to be fetched
# @param [Hash] opts the optional parameters
# @return [Array<(binary, Fixnum, Hash)>] binary data, response status code and response headers
def get_pet_by_id_with_byte_array_with_http_info(pet_id, opts = {})
if @api_client.config.debugging
@api_client.config.logger.debug "Calling API: PetApi#get_pet_by_id_with_byte_array ..."
end

# verify the required parameter 'pet_id' is set
fail "Missing the required parameter 'pet_id' when calling get_pet_by_id_with_byte_array" if pet_id.nil?

# resource path
path = "/pet/{petId}?testing_byte_array=true".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s)

# query parameters
query_params = {}

# header parameters
header_params = {}

# HTTP header 'Accept' (if needed)
_header_accept = ['application/json', 'application/xml']
_header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result

# HTTP header 'Content-Type'
_header_content_type = []
header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)

# form parameters
form_params = {}

# http body (model)
post_body = nil


auth_names = ['api_key']
data, status_code, headers = @api_client.call_api(:GET, path,
:header_params => header_params,
:query_params => query_params,
:form_params => form_params,
:body => post_body,
:auth_names => auth_names,
:return_type => 'binary')
if @api_client.config.debugging
@api_client.config.logger.debug "API called: PetApi#get_pet_by_id_with_byte_array\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end
return data, status_code, headers
end

# Fake endpoint to test byte array in body parameter for adding a new pet to the store
#
# @param [Hash] opts the optional parameters
# @option opts [binary] :body Pet object in the form of byte array
# @return [nil]
def add_pet_using_byte_array(opts = {})
add_pet_using_byte_array_with_http_info(opts)
return nil
end

# Fake endpoint to test byte array in body parameter for adding a new pet to the store
#
# @param [Hash] opts the optional parameters
# @option opts [binary] :body Pet object in the form of byte array
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
def add_pet_using_byte_array_with_http_info(opts = {})
if @api_client.config.debugging
@api_client.config.logger.debug "Calling API: PetApi#add_pet_using_byte_array ..."
end

# resource path
path = "/pet?testing_byte_array=true".sub('{format}','json')

# query parameters
query_params = {}

# header parameters
header_params = {}

# HTTP header 'Accept' (if needed)
_header_accept = ['application/json', 'application/xml']
_header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result

# HTTP header 'Content-Type'
_header_content_type = ['application/json', 'application/xml']
header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)

# form parameters
form_params = {}

# http body (model)
post_body = @api_client.object_to_http_body(opts[:'body'])


auth_names = ['petstore_auth']
data, status_code, headers = @api_client.call_api(:POST, path,
:header_params => header_params,
:query_params => query_params,
:form_params => form_params,
:body => post_body,
:auth_names => auth_names)
if @api_client.config.debugging
@api_client.config.logger.debug "API called: PetApi#add_pet_using_byte_array\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
end
return data, status_code, headers
end
end
end

Expand Down
Loading

0 comments on commit bfd17e4

Please sign in to comment.