forked from swagger-api/swagger-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pet_spec.rb
217 lines (190 loc) · 7.41 KB
/
pet_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
require 'spec_helper'
require 'json'
describe "Pet" do
before do
@pet_api = Petstore::PetApi.new(API_CLIENT)
@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
it "should construct a new pet object" do
tag1 = Petstore::Tag.new({'id' => 1, 'name'=> 'tag1'})
tag2 = Petstore::Tag.new({'id' => 2, 'name'=> 'tag2'})
category1 = Petstore::Category.new({:id => 1, :name => 'category unknown'})
# initalize using both string and symbol key
pet_hash = {
:id => @pet_id,
:name => "RUBY UNIT TESTING",
:status => "pending",
:photo_urls => ["url1", "url2"],
:category => category1,
:tags => [tag1, tag2]
}
pet = Petstore::Pet.new(pet_hash)
# test new
expect(pet.name).to eq("RUBY UNIT TESTING")
expect(pet.status).to eq("pending")
expect(pet.id).to eq(@pet_id)
expect(pet.tags[0].id).to eq(1)
expect(pet.tags[1].name).to eq('tag2')
expect(pet.category.name).to eq('category unknown')
# test build_from_hash
pet2 = Petstore::Pet.new
pet2.build_from_hash(pet.to_hash)
expect(pet.to_hash).to eq(pet2.to_hash)
# make sure sub-object has different object id
expect(pet.tags[0].object_id).not_to eq(pet2.tags[0].object_id)
expect(pet.tags[1].object_id).not_to eq(pet2.tags[1].object_id)
expect(pet.category.object_id).not_to eq(pet2.category.object_id)
end
it "should fetch a pet object" do
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")
end
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)
expect(status_code).to eq(200)
expect(headers['Content-Type']).to eq('application/json')
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")
end
it "should not find a pet that does not exist" do
begin
@pet_api.get_pet_by_id(-@pet_id)
fail 'it should raise error'
rescue Petstore::ApiError => e
expect(e.code).to eq(404)
expect(e.message).to eq('Not Found')
expect(e.response_body).to eq('{"code":1,"type":"error","message":"Pet not found"}')
expect(e.response_headers).to be_a(Hash)
expect(e.response_headers['Content-Type']).to eq('application/json')
end
end
# skip the following as original petstore spec does not have endpoints for testing byte array
# we will re-enable this after updating the petstore server
xit "should create and get pet with byte array (binary, string)" do
pet = @pet_api.get_pet_by_id(@pet_id)
pet.id = @pet_id + 1
str = serialize_json(pet)
@pet_api.add_pet_using_byte_array(body: str)
fetched_str = @pet_api.pet_pet_idtesting_byte_arraytrue_get(pet.id)
expect(fetched_str).to be_a(String)
fetched = deserialize_json(fetched_str, 'Pet')
expect(fetched).to be_a(Petstore::Pet)
expect(fetched.id).to eq(pet.id)
expect(fetched.category).to be_a(Petstore::Category)
expect(fetched.category.name).to eq(pet.category.name)
@pet_api.delete_pet(pet.id)
end
# skip the following as original petstore spec does not have endpoints for testing byte array
# we will re-enable this after updating the petstore server
xit "should get pet in object" do
pet = @pet_api.get_pet_by_id_in_object(@pet_id)
expect(pet).to be_a(Petstore::InlineResponse200)
expect(pet.id).to eq(@pet_id)
expect(pet.name).to eq("RUBY UNIT TESTING")
expect(pet.category).to be_a(Hash)
expect(pet.category[:id]).to eq(20002)
expect(pet.category[:name]).to eq('category test')
end
it "should update a pet" do
pet = @pet_api.get_pet_by_id(@pet_id)
expect(pet.id).to eq(@pet_id)
expect(pet.name).to eq("RUBY UNIT TESTING")
expect(pet.status).to eq('pending')
@pet_api.update_pet_with_form(@pet_id, name: 'new name', status: 'sold')
fetched = @pet_api.get_pet_by_id(@pet_id)
expect(fetched.id).to eq(@pet_id)
expect(fetched.name).to eq("new name")
expect(fetched.status).to eq('sold')
end
it "should find pets by status" do
pets = @pet_api.find_pets_by_status(['available'])
expect(pets.length).to be >= 3
pets.each do |pet|
expect(pet).to be_a(Petstore::Pet)
expect(pet.status).to eq('available')
end
end
it "should not find a pet with invalid status" do
pets = @pet_api.find_pets_by_status(['invalid-status'])
expect(pets.length).to eq(0)
end
it "should find a pet by status" do
pets = @pet_api.find_pets_by_status(["available", "sold"])
pets.each do |pet|
if pet.status != 'available' && pet.status != 'sold'
raise "pet status wasn't right"
end
end
end
it "should create a pet" do
id = @pet_id + 1
pet = Petstore::Pet.new('id' => id, 'name' => "RUBY UNIT TESTING")
result = @pet_api.add_pet(pet)
# nothing is returned
expect(result).to be_nil
pet = @pet_api.get_pet_by_id(id)
expect(pet.id).to eq(id)
expect(pet.name).to eq("RUBY UNIT TESTING")
@pet_api.delete_pet(id)
end
it "should upload a file to a pet" do
result = @pet_api.upload_file(@pet_id, file: File.new('hello.txt'))
# ApiResponse is returned
expect(result).to be_a(Petstore::ApiResponse)
end
it "should upload a file with form parameter to a pet" do
result = @pet_api.upload_file(@pet_id, file: File.new('hello.txt'), additional_metadata: 'metadata')
# ApiResponse is returned
expect(result).to be_a(Petstore::ApiResponse)
end
it "should implement eql? and hash" do
pet1 = Petstore::Pet.new
pet2 = Petstore::Pet.new
expect(pet1).to eq(pet2)
expect(pet2).to eq(pet1)
expect(pet1.eql?(pet2)).to eq(true)
expect(pet2.eql?(pet1)).to eq(true)
expect(pet1.hash).to eq(pet2.hash)
expect(pet1).to eq(pet1)
expect(pet1.eql?(pet1)).to eq(true)
expect(pet1.hash).to eq(pet1.hash)
pet1.name = 'really-happy'
pet1.photo_urls = ['http://foo.bar.com/1', 'http://foo.bar.com/2']
expect(pet1).not_to eq(pet2)
expect(pet2).not_to eq(pet1)
expect(pet1.eql?(pet2)).to eq(false)
expect(pet2.eql?(pet1)).to eq(false)
expect(pet1.hash).not_to eq(pet2.hash)
expect(pet1).to eq(pet1)
expect(pet1.eql?(pet1)).to eq(true)
expect(pet1.hash).to eq(pet1.hash)
pet2.name = 'really-happy'
pet2.photo_urls = ['http://foo.bar.com/1', 'http://foo.bar.com/2']
expect(pet1).to eq(pet2)
expect(pet2).to eq(pet1)
expect(pet1.eql?(pet2)).to eq(true)
expect(pet2.eql?(pet1)).to eq(true)
expect(pet1.hash).to eq(pet2.hash)
expect(pet2).to eq(pet2)
expect(pet2.eql?(pet2)).to eq(true)
expect(pet2.hash).to eq(pet2.hash)
end
end
end