add back petstore test (#6663)

This commit is contained in:
wing328 2017-10-12 19:31:36 +08:00 committed by GitHub
parent 278bcfe96b
commit e9f49abfee

View File

@ -1,25 +1,3 @@
/*
* Swagger Petstore
*
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.IO;
using System.Collections.Generic;
@ -47,6 +25,45 @@ namespace IO.Swagger.Test
{
private PetApi instance;
private long petId = 11088;
/// <summary>
/// Create a Pet object
/// </summary>
private Pet createPet()
{
// create pet
Pet p = new Pet(Name: "Csharp test", PhotoUrls: new List<string> { "http://petstore.com/csharp_test" });
p.Id = petId;
//p.Name = "Csharp test";
p.Status = Pet.StatusEnum.Available;
// create Category object
Category category = new Category();
category.Id = 56;
category.Name = "sample category name2";
List<String> photoUrls = new List<String>(new String[] {"sample photoUrls"});
// create Tag object
Tag tag = new Tag();
tag.Id = petId;
tag.Name = "csharp sample tag name1";
List<Tag> tags = new List<Tag>(new Tag[] {tag});
p.Tags = tags;
p.Category = category;
p.PhotoUrls = photoUrls;
return p;
}
/// <summary>
/// Convert string to byte array
/// </summary>
private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
/// <summary>
/// Setup before each unit test
/// </summary>
@ -54,6 +71,13 @@ namespace IO.Swagger.Test
public void Init()
{
instance = new PetApi();
// create pet
Pet p = createPet();
// add pet before testing
PetApi petApi = new PetApi("http://petstore.swagger.io/v2/");
petApi.AddPet (p);
}
/// <summary>
@ -62,7 +86,9 @@ namespace IO.Swagger.Test
[TearDown]
public void Cleanup()
{
// remove the pet after testing
PetApi petApi = new PetApi ();
petApi.DeletePet(petId, "test key");
}
/// <summary>
@ -71,8 +97,7 @@ namespace IO.Swagger.Test
[Test]
public void InstanceTest()
{
// TODO uncomment below to test 'IsInstanceOfType' PetApi
//Assert.IsInstanceOfType(typeof(PetApi), instance, "instance is a PetApi");
Assert.IsInstanceOf<PetApi>(instance);
}
@ -82,10 +107,9 @@ namespace IO.Swagger.Test
[Test]
public void AddPetTest()
{
// TODO uncomment below to test the method and replace null with proper value
//Pet body = null;
//instance.AddPet(body);
// create pet
Pet p = createPet();
instance.AddPet(p);
}
/// <summary>
@ -94,11 +118,7 @@ namespace IO.Swagger.Test
[Test]
public void DeletePetTest()
{
// TODO uncomment below to test the method and replace null with proper value
//long? petId = null;
//string apiKey = null;
//instance.DeletePet(petId, apiKey);
// no need to test as it'c covered by Cleanup() already
}
/// <summary>
@ -107,10 +127,15 @@ namespace IO.Swagger.Test
[Test]
public void FindPetsByStatusTest()
{
// TODO uncomment below to test the method and replace null with proper value
//List<string> status = null;
//var response = instance.FindPetsByStatus(status);
//Assert.IsInstanceOf<List<Pet>> (response, "response is List<Pet>");
PetApi petApi = new PetApi ();
List<String> tagsList = new List<String>(new String[] {"available"});
List<Pet> listPet = petApi.FindPetsByTags (tagsList);
foreach (Pet pet in listPet) // Loop through List with foreach.
{
Assert.IsInstanceOf<Pet>(pet);
Assert.AreEqual ("csharp sample tag name1", pet.Tags[0]);
}
}
/// <summary>
@ -119,10 +144,9 @@ namespace IO.Swagger.Test
[Test]
public void FindPetsByTagsTest()
{
// TODO uncomment below to test the method and replace null with proper value
//List<string> tags = null;
//var response = instance.FindPetsByTags(tags);
//Assert.IsInstanceOf<List<Pet>> (response, "response is List<Pet>");
List<string> tags = new List<String>(new String[] {"pet"});
var response = instance.FindPetsByTags(tags);
Assert.IsInstanceOf<List<Pet>>(response);
}
/// <summary>
@ -131,10 +155,85 @@ namespace IO.Swagger.Test
[Test]
public void GetPetByIdTest()
{
// TODO uncomment below to test the method and replace null with proper value
//long? petId = null;
//var response = instance.GetPetById(petId);
//Assert.IsInstanceOf<Pet> (response, "response is Pet");
// set timeout to 10 seconds
Configuration c1 = new Configuration();
c1.Timeout = 10000;
c1.UserAgent = "TEST_USER_AGENT";
PetApi petApi = new PetApi (c1);
Pet response = petApi.GetPetById (petId);
Assert.IsInstanceOf<Pet>(response);
Assert.AreEqual ("Csharp test", response.Name);
Assert.AreEqual (Pet.StatusEnum.Available, response.Status);
Assert.IsInstanceOf<List<Tag>>(response.Tags);
Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);
Assert.IsInstanceOf<List<String>>(response.PhotoUrls);
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);
Assert.IsInstanceOf<Category>(response.Category);
Assert.AreEqual (56, response.Category.Id);
Assert.AreEqual ("sample category name2", response.Category.Name);
}
/// <summary>
/// Test GetPetByIdAsync
/// </summary>
[Test ()]
public void TestGetPetByIdAsync ()
{
PetApi petApi = new PetApi ();
var task = petApi.GetPetByIdAsync (petId);
Pet response = task.Result;
Assert.IsInstanceOf<Pet>(response);
Assert.AreEqual ("Csharp test", response.Name);
Assert.AreEqual (Pet.StatusEnum.Available, response.Status);
Assert.IsInstanceOf<List<Tag>>(response.Tags);
Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);
Assert.IsInstanceOf<List<String>>(response.PhotoUrls);
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);
Assert.IsInstanceOf<Category>(response.Category);
Assert.AreEqual (56, response.Category.Id);
Assert.AreEqual ("sample category name2", response.Category.Name);
}
/// <summary>
/// Test GetPetByIdAsyncWithHttpInfo
/// </summary>
[Test ()]
public void TestGetPetByIdAsyncWithHttpInfo ()
{
PetApi petApi = new PetApi ();
var task = petApi.GetPetByIdAsyncWithHttpInfo (petId);
Assert.AreEqual (200, task.Result.StatusCode);
Assert.IsTrue (task.Result.Headers.ContainsKey("Content-Type"));
Assert.AreEqual (task.Result.Headers["Content-Type"], "application/json");
Pet response = task.Result.Data;
Assert.IsInstanceOf<Pet>(response);
Assert.AreEqual ("Csharp test", response.Name);
Assert.AreEqual (Pet.StatusEnum.Available, response.Status);
Assert.IsInstanceOf<List<Tag>>(response.Tags);
Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual ("csharp sample tag name1", response.Tags [0].Name);
Assert.IsInstanceOf<List<String>>(response.PhotoUrls);
Assert.AreEqual ("sample photoUrls", response.PhotoUrls [0]);
Assert.IsInstanceOf<Category>(response.Category);
Assert.AreEqual (56, response.Category.Id);
Assert.AreEqual ("sample category name2", response.Category.Name);
}
/// <summary>
@ -143,10 +242,9 @@ namespace IO.Swagger.Test
[Test]
public void UpdatePetTest()
{
// TODO uncomment below to test the method and replace null with proper value
//Pet body = null;
//instance.UpdatePet(body);
// create pet
Pet p = createPet();
instance.UpdatePet(p);
}
/// <summary>
@ -155,12 +253,24 @@ namespace IO.Swagger.Test
[Test]
public void UpdatePetWithFormTest()
{
// TODO uncomment below to test the method and replace null with proper value
//long? petId = null;
//string name = null;
//string status = null;
//instance.UpdatePetWithForm(petId, name, status);
PetApi petApi = new PetApi();
petApi.UpdatePetWithForm (petId, "new form name", "pending");
Pet response = petApi.GetPetById (petId);
Assert.IsInstanceOf<Pet>(response);
Assert.IsInstanceOf<Category>(response.Category);
Assert.IsInstanceOf<List<Tag>>(response.Tags);
Assert.AreEqual ("new form name", response.Name);
Assert.AreEqual (Pet.StatusEnum.Pending, response.Status);
Assert.AreEqual (petId, response.Tags [0].Id);
Assert.AreEqual (56, response.Category.Id);
// test optional parameter
petApi.UpdatePetWithForm (petId, "new form name2");
Pet response2 = petApi.GetPetById (petId);
Assert.AreEqual ("new form name2", response2.Name);
}
/// <summary>
@ -169,12 +279,44 @@ namespace IO.Swagger.Test
[Test]
public void UploadFileTest()
{
// TODO uncomment below to test the method and replace null with proper value
//long? petId = null;
//string additionalMetadata = null;
//System.IO.Stream file = null;
//var response = instance.UploadFile(petId, additionalMetadata, file);
//Assert.IsInstanceOf<ApiResponse> (response, "response is ApiResponse");
Assembly _assembly = Assembly.GetExecutingAssembly();
Stream _imageStream = _assembly.GetManifestResourceStream("IO.Swagger.Test.swagger-logo.png");
PetApi petApi = new PetApi ();
// test file upload with form parameters
petApi.UploadFile(petId, "new form name", _imageStream);
// test file upload without any form parameters
// using optional parameter syntax introduced at .net 4.0
petApi.UploadFile(petId: petId, file: _imageStream);
}
/// <summary>
/// Test status code
/// </summary>
[Test ()]
public void TestStatusCodeAndHeader ()
{
PetApi petApi = new PetApi ();
var response = petApi.GetPetByIdWithHttpInfo (petId);
Assert.AreEqual (response.StatusCode, 200);
Assert.IsTrue (response.Headers.ContainsKey("Content-Type"));
Assert.AreEqual (response.Headers["Content-Type"], "application/json");
}
/// <summary>
/// Test default header (should be deprecated
/// </summary>
[Test ()]
public void TestDefaultHeader ()
{
PetApi petApi = new PetApi ();
// commented out the warning test below as it's confirmed the warning is working as expected
// there should be a warning for using AddDefaultHeader (deprecated) below
//petApi.AddDefaultHeader ("header_key", "header_value");
// the following should be used instead as suggested in the doc
petApi.Configuration.AddDefaultHeader ("header_key2", "header_value2");
}
}