Add test cases for sanitize_for_serialization method in python client.

This commit is contained in:
geekerzp 2015-08-25 15:04:46 +08:00
parent 392e5172b5
commit 568b7a4fcd

View File

@ -10,6 +10,7 @@ $ nosetests -v
import os import os
import time import time
import unittest import unittest
from dateutil.parser import parse
import swagger_client import swagger_client
import swagger_client.configuration import swagger_client.configuration
@ -51,7 +52,7 @@ class ApiClientTests(unittest.TestCase):
accepts = ['APPLICATION/JSON', 'APPLICATION/XML'] accepts = ['APPLICATION/JSON', 'APPLICATION/XML']
accept = self.api_client.select_header_accept(accepts) accept = self.api_client.select_header_accept(accepts)
self.assertEqual(accept, 'application/json') self.assertEqual(accept, 'application/json')
accepts = ['application/json', 'application/xml'] accepts = ['application/json', 'application/xml']
accept = self.api_client.select_header_accept(accepts) accept = self.api_client.select_header_accept(accepts)
self.assertEqual(accept, 'application/json') self.assertEqual(accept, 'application/json')
@ -72,19 +73,95 @@ class ApiClientTests(unittest.TestCase):
content_types = ['APPLICATION/JSON', 'APPLICATION/XML'] content_types = ['APPLICATION/JSON', 'APPLICATION/XML']
content_type = self.api_client.select_header_content_type(content_types) content_type = self.api_client.select_header_content_type(content_types)
self.assertEqual(content_type, 'application/json') self.assertEqual(content_type, 'application/json')
content_types = ['application/json', 'application/xml'] content_types = ['application/json', 'application/xml']
content_type = self.api_client.select_header_content_type(content_types) content_type = self.api_client.select_header_content_type(content_types)
self.assertEqual(content_type, 'application/json') self.assertEqual(content_type, 'application/json')
content_types = ['application/xml', 'application/json'] content_types = ['application/xml', 'application/json']
content_type = self.api_client.select_header_content_type(content_types) content_type = self.api_client.select_header_content_type(content_types)
self.assertEqual(content_type, 'application/json') self.assertEqual(content_type, 'application/json')
content_types = ['text/plain', 'application/xml'] content_types = ['text/plain', 'application/xml']
content_type = self.api_client.select_header_content_type(content_types) content_type = self.api_client.select_header_content_type(content_types)
self.assertEqual(content_type, 'text/plain') self.assertEqual(content_type, 'text/plain')
content_types = [] content_types = []
content_type = self.api_client.select_header_content_type(content_types) content_type = self.api_client.select_header_content_type(content_types)
self.assertEqual(content_type, 'application/json') self.assertEqual(content_type, 'application/json')
def test_sanitize_for_serialization(self):
# None
data = None
result = self.api_client.sanitize_for_serialization(None)
self.assertEqual(result, data)
# str
data = "test string"
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, data)
# int
data = 1
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, data)
# bool
data = True
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, data)
# date
data = parse("1997-07-16").date() # date
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, "1997-07-16")
# datetime
data = parse("1997-07-16T19:20:30.45+01:00") # datetime
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, "1997-07-16T19:20:30.450000+01:00")
# list
data = [1]
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, data)
# dict
data = {"test key": "test value"}
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, data)
# model
pet_dict = {"id": 1, "name": "monkey",
"category": {"id": 1, "name": "test category"},
"tags": [{"id": 1, "name": "test tag1"},
{"id": 2, "name": "test tag2"}],
"status": "available",
"photoUrls": ["http://foo.bar.com/3",
"http://foo.bar.com/4"]}
pet = swagger_client.Pet()
pet.id = pet_dict["id"]
pet.name = pet_dict["name"]
cate = swagger_client.Category()
cate.id = pet_dict["category"]["id"]
cate.name = pet_dict["category"]["name"]
pet.category = cate
tag1 = swagger_client.Tag()
tag1.id = pet_dict["tags"][0]["id"]
tag1.name = pet_dict["tags"][0]["name"]
tag2 = swagger_client.Tag()
tag2.id = pet_dict["tags"][1]["id"]
tag2.name = pet_dict["tags"][1]["name"]
pet.tags = [tag1, tag2]
pet.status = pet_dict["status"]
pet.photo_urls = pet_dict["photoUrls"]
data = pet
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, pet_dict)
# list of models
list_of_pet_dict = [pet_dict]
data = [pet]
result = self.api_client.sanitize_for_serialization(data)
self.assertEqual(result, list_of_pet_dict)