Skip to content

Commit bfd17e4

Browse files
committed
Merge pull request #1980 from wing328/ruby_add_test
[Ruby] add auto-generated test cases (rspec)
2 parents d779174 + e4a991d commit bfd17e4

File tree

12 files changed

+1007
-0
lines changed

12 files changed

+1007
-0
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/RubyClientCodegen.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class RubyClientCodegen extends DefaultCodegen implements CodegenConfig {
2929
protected String gemName;
3030
protected String moduleName;
3131
protected String gemVersion = "1.0.0";
32+
protected String specFolder = "spec";
3233
protected String libFolder = "lib";
3334
protected String gemLicense = "Apache-2.0";
3435
protected String gemHomepage = "http://swagger.io";
@@ -47,6 +48,9 @@ public RubyClientCodegen() {
4748
apiTemplateFiles.put("api.mustache", ".rb");
4849
embeddedTemplateDir = templateDir = "ruby";
4950

51+
modelTestTemplateFiles.put("model_test.mustache", ".rb");
52+
apiTestTemplateFiles.put("api_test.mustache", ".rb");
53+
5054
typeMapping.clear();
5155
languageSpecificPrimitives.clear();
5256

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

242+
@Override
243+
public String apiTestFileFolder() {
244+
return outputFolder + File.separator + specFolder + File.separator + apiPackage.replace("/", File.separator);
245+
}
246+
247+
@Override
248+
public String modelTestFileFolder() {
249+
return outputFolder + File.separator + specFolder + File.separator + modelPackage.replace("/", File.separator);
250+
}
251+
238252
@Override
239253
public String getTypeDeclaration(Property p) {
240254
if (p instanceof ArrayProperty) {
@@ -367,6 +381,16 @@ public String toApiFilename(String name) {
367381
return underscore(name) + "_api";
368382
}
369383

384+
@Override
385+
public String toApiTestFilename(String name) {
386+
return toApiName(name) + "_spec";
387+
}
388+
389+
@Override
390+
public String toModelTestFilename(String name) {
391+
return toModelName(name) + "_spec";
392+
}
393+
370394
@Override
371395
public String toApiName(String name) {
372396
if (name.length() == 0) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
require 'spec_helper'
2+
require 'json'
3+
4+
# Unit tests for {{moduleName}}::{{classname}}
5+
# Automatically generated by swagger-codegen (github.com/swagger-api/swagger-codegen)
6+
# Please update as you see appropriate
7+
{{#operations}}describe '{{classname}}' do
8+
before do
9+
# run before each test
10+
@instance = {{moduleName}}::{{classname}}.new
11+
end
12+
13+
after do
14+
# run after each test
15+
end
16+
17+
describe 'test an instance of {{classname}}' do
18+
it 'should create an instact of {{classname}}' do
19+
@instance.should be_a({{moduleName}}::{{classname}})
20+
end
21+
end
22+
23+
{{#operation}}
24+
# unit tests for {{operationId}}
25+
# {{summary}}
26+
# {{notes}}
27+
{{#allParams}}{{#required}} # @param {{paramName}} {{description}}
28+
{{/required}}{{/allParams}} # @param [Hash] opts the optional parameters
29+
{{#allParams}}{{^required}} # @option opts [{{{dataType}}}] :{{paramName}} {{description}}
30+
{{/required}}{{/allParams}} # @return [{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}nil{{/returnType}}]
31+
describe '{{operationId}} test' do
32+
it "should work" do
33+
# assertion here
34+
# should be_a()
35+
# should be_nil
36+
# should ==
37+
# should_not ==
38+
end
39+
end
40+
41+
{{/operation}}
42+
end
43+
{{/operations}}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require 'spec_helper'
2+
require 'json'
3+
require 'date'
4+
5+
# Unit tests for {{moduleName}}::{{classname}}
6+
# Automatically generated by swagger-codegen (github.com/swagger-api/swagger-codegen)
7+
# Please update as you see appropriate
8+
{{#models}}{{#model}}describe '{{classname}}' do
9+
before do
10+
# run before each test
11+
@instance = {{moduleName}}::{{classname}}.new
12+
end
13+
14+
after do
15+
# run after each test
16+
end
17+
18+
describe 'test an instance of {{classname}}' do
19+
it 'should create an instact of {{classname}}' do
20+
@instance.should be_a({{moduleName}}::{{classname}})
21+
end
22+
end
23+
{{#vars}}
24+
describe 'test attribute "{{{name}}}"' do
25+
it 'should work' do
26+
# assertion here
27+
# should be_a()
28+
# should be_nil
29+
# should ==
30+
# should_not ==
31+
end
32+
end
33+
34+
{{/vars}}
35+
end
36+
{{/model}}{{/models}}

samples/client/petstore/ruby/lib/petstore/api/pet_api.rb

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,124 @@ def upload_file_with_http_info(pet_id, opts = {})
495495
end
496496
return data, status_code, headers
497497
end
498+
499+
# Fake endpoint to test byte array return by 'Find pet by ID'
500+
# Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions
501+
# @param pet_id ID of pet that needs to be fetched
502+
# @param [Hash] opts the optional parameters
503+
# @return [binary]
504+
def get_pet_by_id_with_byte_array(pet_id, opts = {})
505+
data, status_code, headers = get_pet_by_id_with_byte_array_with_http_info(pet_id, opts)
506+
return data
507+
end
508+
509+
# Fake endpoint to test byte array return by 'Find pet by ID'
510+
# Returns a pet when ID < 10. ID > 10 or nonintegers will simulate API error conditions
511+
# @param pet_id ID of pet that needs to be fetched
512+
# @param [Hash] opts the optional parameters
513+
# @return [Array<(binary, Fixnum, Hash)>] binary data, response status code and response headers
514+
def get_pet_by_id_with_byte_array_with_http_info(pet_id, opts = {})
515+
if @api_client.config.debugging
516+
@api_client.config.logger.debug "Calling API: PetApi#get_pet_by_id_with_byte_array ..."
517+
end
518+
519+
# verify the required parameter 'pet_id' is set
520+
fail "Missing the required parameter 'pet_id' when calling get_pet_by_id_with_byte_array" if pet_id.nil?
521+
522+
# resource path
523+
path = "/pet/{petId}?testing_byte_array=true".sub('{format}','json').sub('{' + 'petId' + '}', pet_id.to_s)
524+
525+
# query parameters
526+
query_params = {}
527+
528+
# header parameters
529+
header_params = {}
530+
531+
# HTTP header 'Accept' (if needed)
532+
_header_accept = ['application/json', 'application/xml']
533+
_header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
534+
535+
# HTTP header 'Content-Type'
536+
_header_content_type = []
537+
header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
538+
539+
# form parameters
540+
form_params = {}
541+
542+
# http body (model)
543+
post_body = nil
544+
545+
546+
auth_names = ['api_key']
547+
data, status_code, headers = @api_client.call_api(:GET, path,
548+
:header_params => header_params,
549+
:query_params => query_params,
550+
:form_params => form_params,
551+
:body => post_body,
552+
:auth_names => auth_names,
553+
:return_type => 'binary')
554+
if @api_client.config.debugging
555+
@api_client.config.logger.debug "API called: PetApi#get_pet_by_id_with_byte_array\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
556+
end
557+
return data, status_code, headers
558+
end
559+
560+
# Fake endpoint to test byte array in body parameter for adding a new pet to the store
561+
#
562+
# @param [Hash] opts the optional parameters
563+
# @option opts [binary] :body Pet object in the form of byte array
564+
# @return [nil]
565+
def add_pet_using_byte_array(opts = {})
566+
add_pet_using_byte_array_with_http_info(opts)
567+
return nil
568+
end
569+
570+
# Fake endpoint to test byte array in body parameter for adding a new pet to the store
571+
#
572+
# @param [Hash] opts the optional parameters
573+
# @option opts [binary] :body Pet object in the form of byte array
574+
# @return [Array<(nil, Fixnum, Hash)>] nil, response status code and response headers
575+
def add_pet_using_byte_array_with_http_info(opts = {})
576+
if @api_client.config.debugging
577+
@api_client.config.logger.debug "Calling API: PetApi#add_pet_using_byte_array ..."
578+
end
579+
580+
# resource path
581+
path = "/pet?testing_byte_array=true".sub('{format}','json')
582+
583+
# query parameters
584+
query_params = {}
585+
586+
# header parameters
587+
header_params = {}
588+
589+
# HTTP header 'Accept' (if needed)
590+
_header_accept = ['application/json', 'application/xml']
591+
_header_accept_result = @api_client.select_header_accept(_header_accept) and header_params['Accept'] = _header_accept_result
592+
593+
# HTTP header 'Content-Type'
594+
_header_content_type = ['application/json', 'application/xml']
595+
header_params['Content-Type'] = @api_client.select_header_content_type(_header_content_type)
596+
597+
# form parameters
598+
form_params = {}
599+
600+
# http body (model)
601+
post_body = @api_client.object_to_http_body(opts[:'body'])
602+
603+
604+
auth_names = ['petstore_auth']
605+
data, status_code, headers = @api_client.call_api(:POST, path,
606+
:header_params => header_params,
607+
:query_params => query_params,
608+
:form_params => form_params,
609+
:body => post_body,
610+
:auth_names => auth_names)
611+
if @api_client.config.debugging
612+
@api_client.config.logger.debug "API called: PetApi#add_pet_using_byte_array\nData: #{data.inspect}\nStatus code: #{status_code}\nHeaders: #{headers}"
613+
end
614+
return data, status_code, headers
615+
end
498616
end
499617
end
500618

0 commit comments

Comments
 (0)