本文整理汇总了Python中tastypie.serializers.Serializer.from_json方法的典型用法代码示例。如果您正苦于以下问题:Python Serializer.from_json方法的具体用法?Python Serializer.from_json怎么用?Python Serializer.from_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tastypie.serializers.Serializer
的用法示例。
在下文中一共展示了Serializer.from_json方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_from_json
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_json [as 别名]
def test_from_json(self):
serializer = Serializer()
sample_1 = serializer.from_json('{"age": 27, "date_joined": "2010-03-27", "name": "Daniel"}')
self.assertEqual(len(sample_1), 3)
self.assertEqual(sample_1['name'], 'Daniel')
self.assertEqual(sample_1['age'], 27)
self.assertEqual(sample_1['date_joined'], u'2010-03-27')
示例2: ProjectResourceTest
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_json [as 别名]
class ProjectResourceTest(TestCase):
fixtures = ['data.json']
def setUp(self):
super(ProjectResourceTest, self).setUp()
self.username = 'marcos'
self.password = 'test'
#self.user = User.objects.create_user(self.username, '[email protected]', self.password)
#self.api = slumber.API("http://localhost:5000/api/v1/", auth=(self.username, self.password))
self.post_data = {
'title': 'testTitle',
'description': 'testDescription',
'user' : '/api/v1/user/4ea9c4fdbb69337f8e000002/',
'tasks': []
}
self.serializer = Serializer()
def test_post_project(self):
format = self.serializer.content_types.get('json')
serialized_data = self.serializer.serialize(self.post_data, format='application/json')
self.assertEqual(Project.objects.count(), 1)
resp = self.client.post('/api/v1/project/', data = serialized_data, content_type='application/json')
self.assertEqual(resp.status_code, 201)
self.assertEqual(Project.objects.count(), 2)
def test_put_project(self):
format = self.serializer.content_types.get('json')
project_data = self.post_data
project_data['title'] = 'changedTitle'
serialized_data = self.serializer.serialize(project_data, format='application/json')
resp = self.client.put('/api/v1/project/4ea9c4fdbb69337f8e000001/', data = serialized_data, content_type='application/json')
self.assertEqual(resp.status_code, 204)
resp = self.client.get('/api/v1/project/')
self.assertEqual("changedTitle", self.serializer.deserialize(resp.content)['objects'][0]['title'])
def test_get_projects(self):
resp = self.client.get('/api/v1/project/')
self.assertEqual(resp.status_code, 200)
self.assertTrue(resp['Content-Type'].startswith('application/json'))
self.serializer.from_json(resp.content)
self.assertEqual(len(self.serializer.deserialize(resp.content)['objects']), 1)
assert True
"""def test_get_tasks(self):
示例3: test_round_trip_json
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_json [as 别名]
def test_round_trip_json(self):
serializer = Serializer()
sample_data = self.get_sample2()
serialized = serializer.to_json(sample_data)
unserialized = serializer.from_json(serialized)
self.assertEqual(sample_data, unserialized)
示例4: ResourceTestCase
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_json [as 别名]
#.........这里部分代码省略.........
"""
return self.assertEqual(resp.status_code, 409)
def assertHttpGone(self, resp):
"""
Ensures the response is returning a HTTP 410.
"""
return self.assertEqual(resp.status_code, 410)
def assertHttpTooManyRequests(self, resp):
"""
Ensures the response is returning a HTTP 429.
"""
return self.assertEqual(resp.status_code, 429)
def assertHttpApplicationError(self, resp):
"""
Ensures the response is returning a HTTP 500.
"""
return self.assertEqual(resp.status_code, 500)
def assertHttpNotImplemented(self, resp):
"""
Ensures the response is returning a HTTP 501.
"""
return self.assertEqual(resp.status_code, 501)
def assertValidJSON(self, data):
"""
Given the provided ``data`` as a string, ensures that it is valid JSON &
can be loaded properly.
"""
# Just try the load. If it throws an exception, the test case will fail.
self.serializer.from_json(data)
def assertValidXML(self, data):
"""
Given the provided ``data`` as a string, ensures that it is valid XML &
can be loaded properly.
"""
# Just try the load. If it throws an exception, the test case will fail.
self.serializer.from_xml(data)
def assertValidYAML(self, data):
"""
Given the provided ``data`` as a string, ensures that it is valid YAML &
can be loaded properly.
"""
# Just try the load. If it throws an exception, the test case will fail.
self.serializer.from_yaml(data)
def assertValidPlist(self, data):
"""
Given the provided ``data`` as a string, ensures that it is valid
binary plist & can be loaded properly.
"""
# Just try the load. If it throws an exception, the test case will fail.
self.serializer.from_plist(data)
def assertValidJSONResponse(self, resp):
"""
Given a ``HttpResponse`` coming back from using the ``client``, assert that
you get back:
* An HTTP 200
* The correct content-type (``application/json``)
示例5: ApiTestCase
# 需要导入模块: from tastypie.serializers import Serializer [as 别名]
# 或者: from tastypie.serializers.Serializer import from_json [as 别名]
class ApiTestCase(DirectSEOTestCase):
fixtures = ['seo_views_testdata.json']
def setUp(self):
super(ApiTestCase, self).setUp()
# Create a test user and an API key for that user.
self.user, created = User.objects.create_user(email='[email protected]',
password='password')
self.username = self.user.email
self.user.save()
self.key = ApiKey(user=self.user)
self.key.save()
self.api_key = self.key.key
self.auth_qs = '?&username=%s&api_key=%s' % (self.username,
self.api_key)
self.entry_1 = SeoSite.objects.get(group=1)
self.detail_url = '/api/v1/seosite/{0}/'.format(self.entry_1.pk)
self.serializer = Serializer()
def deserialize(self, resp):
return self.serializer.deserialize(resp.content, format=resp['Content-Type'])
def test_not_authorized(self):
"""
Test if a user can gain access without an API key
"""
user, created = User.objects.create_user(email='[email protected]',
password='password')
self.username = self.user.email
user.save()
resp = self.client.get("api/v1/jobsearch/?format=xml")
self.assertEqual(resp.status_code, 404)
resp = self.client.get("api/v1/seosite/?format=xml")
self.assertEqual(resp.status_code, 404)
key = ApiKey(user=self.user)
def test_list_xml(self):
resp = self.client.get("/api/v1/seosite/?%s&format=xml" % (self.auth_qs))
self.assertEqual(resp.status_code, 200)
self.serializer.from_xml(resp.content)
def test_list_json(self):
resp = self.client.get("/api/v1/seosite/?%s&format=json" % (self.auth_qs))
self.assertEqual(len(self.deserialize(resp)['objects']), 1)
self.assertEqual(resp.status_code, 200)
self.serializer.from_json(resp.content)
def test_get_detail_xml(self):
resp = self.client.get("/api/v1/seosite/1/%s&format=xml" % (self.auth_qs))
self.assertEqual(resp.status_code, 200)
self.serializer.from_xml(resp.content)
def test_nopost(self):
"""
Ensure that POST requests are rejected. This test can be removed
if/when we allow other methods besides GET to our resources.
"""
jobjson = ("""{"buid": 13543, "city": "Chester",\
"company": "Edward Jones", "country": "United States",\
"date_new": "2012-05-31T11:49:23",\
"mocs": "[\'021\', \'0193\', \'2820\', \'2G000\', \'2G091\',\
\'2R000\', \'2R071\', \'2R090\', \'2R171\', \'2S000\',\
\'2S071\', \'2S091\', \'2T000\', \'2T071\', \'2T091\',\
\'3A000\', \'3A071\', \'3A091\', \'3C000\', \'3C071\',\
\'3C090\', \'3C171\', \'3C191\', \'4A000\', \'4A091\',\
\'4A100\', \'4A191\', \'6F000\', \'6F091\', \'8M000\']",\
"onet": "43101100",\
"resource_uri": "/seo/v1/jobposting/29068157/",\
"state": "Virginia", "title": "Branch Office Administrator -\
Chester, VA - Branch 48113", "uid": "29068157"}""")
resp = self.client.post("/api/v1/jobsearch/%s" % self.auth_qs,
data=jobjson, content_type="application/json")
# HTTP 405 == 'Method Not Allowed'
self.assertEqual(resp.status_code, 405)