def test_to_json(self): serializer = Serializer() sample_1 = self.get_sample1() self.assertEqual( serializer.to_json(sample_1), '{"age": 27, "date_joined": "2010-03-27", "name": "Daniel"}')
def test_to_xml2(self): serializer = Serializer() sample_2 = self.get_sample2() self.assertEqual( serializer.to_xml(sample_2), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><somelist type="list"><value>hello</value><value type="integer">1</value><value type="null"/></somelist><somehash type="hash"><pi type="float">3.14</pi><foo>bar</foo></somehash><false type="boolean">False</false><true type="boolean">True</true><somestring>hello</somestring></response>' )
def test_to_xml_single(self): serializer = Serializer() resource = self.obj_list[0] self.assertEqual( serializer.to_xml(resource), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object>' )
def test_to_json_single(self): serializer = Serializer() resource = self.obj_list[0] self.assertEqual( serializer.to_json(resource), '{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": "1", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}' )
def test_to_xml(self): serializer = Serializer() sample_1 = self.get_sample1() self.assertEqual( serializer.to_xml(sample_1), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><age type="integer">27</age><name>Daniel</name><date_joined>2010-03-27</date_joined></response>' )
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')
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')
def test_round_trip_xml(self): serializer = Serializer() sample_data = self.get_sample2() serialized = serializer.to_xml(sample_data) # "response" tags need to be changed to "request" to deserialize properly. # A string substitution works here. serialized = serialized.replace('response', 'request') unserialized = serializer.from_xml(serialized) self.assertEqual(sample_data, unserialized)
def test_to_jsonp(self): serializer = Serializer() sample_1 = self.get_sample1() options = {'callback': 'myCallback'} self.assertEqual( serializer.to_jsonp(sample_1, options), 'myCallback({"age": 27, "date_joined": "2010-03-27", "name": "Daniel"})' )
def test_to_json_nested(self): serializer = Serializer() resource = self.obj_list[0] data = { 'stuff': { 'foo': 'bar', 'object': resource, } } self.assertEqual(serializer.to_json(data), '{"stuff": {"foo": "bar", "object": {"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": "1", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}}}')
def test_from_xml(self): serializer = Serializer() data = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<request><age type="integer">27</age><name>Daniel</name><date_joined>2010-03-27</date_joined><rocksdahouse type="boolean">True</rocksdahouse></request>' self.assertEqual( serializer.from_xml(data), { 'rocksdahouse': True, 'age': 27, 'name': 'Daniel', 'date_joined': '2010-03-27' })
def test_to_xml_nested(self): serializer = Serializer() resource = self.obj_list[0] data = { 'stuff': { 'foo': 'bar', 'object': resource, } } self.assertEqual(serializer.to_xml(data), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><stuff type="hash"><foo>bar</foo><object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object></stuff></response>')
def test_to_xml_nested(self): serializer = Serializer() resource = self.obj_list[0] data = { 'stuff': { 'foo': 'bar', 'object': resource, } } self.assertEqual( serializer.to_xml(data), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><stuff type="hash"><foo>bar</foo><object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object></stuff></response>' )
def test_to_json_nested(self): serializer = Serializer() resource = self.obj_list[0] data = { 'stuff': { 'foo': 'bar', 'object': resource, } } self.assertEqual( serializer.to_json(data), '{"stuff": {"foo": "bar", "object": {"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": "1", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}}}' )
def test_init(self): serializer_1 = Serializer() self.assertEqual(serializer_1.formats, ['json', 'jsonp', 'xml', 'yaml', 'html']) self.assertEqual( serializer_1.content_types, { 'xml': 'application/xml', 'yaml': 'text/yaml', 'json': 'application/json', 'jsonp': 'text/javascript', 'html': 'text/html' }) self.assertEqual(serializer_1.supported_formats, [ 'application/json', 'text/javascript', 'application/xml', 'text/yaml', 'text/html' ]) serializer_2 = Serializer(formats=['json', 'xml']) self.assertEqual(serializer_2.formats, ['json', 'xml']) self.assertEqual( serializer_2.content_types, { 'xml': 'application/xml', 'yaml': 'text/yaml', 'json': 'application/json', 'jsonp': 'text/javascript', 'html': 'text/html' }) self.assertEqual(serializer_2.supported_formats, ['application/json', 'application/xml']) serializer_3 = Serializer(formats=['json', 'xml'], content_types={ 'json': 'text/json', 'xml': 'application/xml' }) self.assertEqual(serializer_3.formats, ['json', 'xml']) self.assertEqual(serializer_3.content_types, { 'xml': 'application/xml', 'json': 'text/json' }) self.assertEqual(serializer_3.supported_formats, ['text/json', 'application/xml']) self.assertRaises(ImproperlyConfigured, Serializer, formats=['json', 'xml'], content_types={'json': 'text/json'})
class ResourceOptions(object): """ A configuration class for ``Resource``. Provides sane defaults and the logic needed to augment these settings with the internal ``class Meta`` used on ``Resource`` subclasses. """ serializer = Serializer() authentication = Authentication() authorization = ReadOnlyAuthorization() cache = NoCache() throttle = BaseThrottle() validation = Validation() limit = getattr(settings, 'API_LIMIT_PER_PAGE', 20) route = None default_format = 'application/json' filtering = {} ordering = [] object_class = None queryset = None fields = [] excludes = [] include_resource_uri = True include_absolute_url = False # only applies to TOC resource resources = [] # only here for compatibility / deprecated api_name = None resource_name = '' def __new__(cls, meta=None): overrides = {} # Handle overrides. if meta: for override_name in dir(meta): # No internals please. if not override_name.startswith('_'): overrides[override_name] = getattr(meta, override_name) return object.__new__(type('ResourceOptions', (cls,), overrides))
def test_from_xml(self): serializer = Serializer() data = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<request><age type="integer">27</age><name>Daniel</name><date_joined>2010-03-27</date_joined><rocksdahouse type="boolean">True</rocksdahouse></request>' self.assertEqual(serializer.from_xml(data), {'rocksdahouse': True, 'age': 27, 'name': 'Daniel', 'date_joined': '2010-03-27'})
def test_to_xml(self): serializer = Serializer() sample_1 = self.get_sample1() self.assertEqual(serializer.to_xml(sample_1), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<response><age type="integer">27</age><name>Daniel</name><date_joined>2010-03-27</date_joined></response>')
def test_from_xml2(self): serializer = Serializer() data = '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<request><somelist type="list"><value>hello</value><value type="integer">1</value><value type="null"/></somelist><somehash type="hash"><pi type="float">3.14</pi><foo>bar</foo></somehash><false type="boolean">False</false><true type="boolean">True</true><somestring>hello</somestring></request>' self.assertEqual(serializer.from_xml(data), self.get_sample2())
def test_to_json_multirepr(self): serializer = Serializer() self.assertEqual( serializer.to_json(self.obj_list), '[{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": "1", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}, {"content": "The dog ate my cat today. He looks seriously uncomfortable.", "created": "2010-03-31T20:05:00", "id": "2", "is_active": true, "resource_uri": "", "slug": "another-post", "title": "Another Post", "updated": "2010-03-31T20:05:00"}, {"content": "My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.", "created": "2010-04-01T20:05:00", "id": "4", "is_active": true, "resource_uri": "", "slug": "recent-volcanic-activity", "title": "Recent Volcanic Activity.", "updated": "2010-04-01T20:05:00"}, {"content": "Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!", "created": "2010-04-02T10:05:00", "id": "6", "is_active": true, "resource_uri": "", "slug": "grannys-gone", "title": "Granny\'s Gone", "updated": "2010-04-02T10:05:00"}]' )
def test_to_xml_multirepr(self): serializer = Serializer() self.assertEqual( serializer.to_xml(self.obj_list), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<objects><object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object><object><updated>2010-03-31T20:05:00</updated><created>2010-03-31T20:05:00</created><title>Another Post</title><is_active type="boolean">True</is_active><slug>another-post</slug><content>The dog ate my cat today. He looks seriously uncomfortable.</content><id>2</id><resource_uri></resource_uri></object><object><updated>2010-04-01T20:05:00</updated><created>2010-04-01T20:05:00</created><title>Recent Volcanic Activity.</title><is_active type="boolean">True</is_active><slug>recent-volcanic-activity</slug><content>My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.</content><id>4</id><resource_uri></resource_uri></object><object><updated>2010-04-02T10:05:00</updated><created>2010-04-02T10:05:00</created><title>Granny\'s Gone</title><is_active type="boolean">True</is_active><slug>grannys-gone</slug><content>Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!</content><id>6</id><resource_uri></resource_uri></object></objects>' )
def test_to_json(self): serializer = Serializer() sample_1 = self.get_sample1() self.assertEqual(serializer.to_json(sample_1), '{"age": 27, "date_joined": "2010-03-27", "name": "Daniel"}')
def test_round_trip_yaml(self): serializer = Serializer() sample_data = self.get_sample2() serialized = serializer.to_yaml(sample_data) unserialized = serializer.from_yaml(serialized) self.assertEqual(sample_data, unserialized)
def test_format_date(self): serializer = Serializer() self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)), '2010-12-16') serializer = Serializer(datetime_formatting='iso-8601') self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)), '2010-12-16') serializer = Serializer(datetime_formatting='rfc-2822') self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)), u'16 Dec 2010') serializer = Serializer(datetime_formatting='random-garbage') self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)), '2010-12-16') # Stow. old_format = getattr(settings, 'apiserver_DATETIME_FORMATTING', 'iso-8601') settings.apiserver_DATETIME_FORMATTING = 'iso-8601' serializer = Serializer() self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)), '2010-12-16') settings.apiserver_DATETIME_FORMATTING = 'rfc-2822' serializer = Serializer() self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)), u'16 Dec 2010') settings.apiserver_DATETIME_FORMATTING = 'random-garbage' serializer = Serializer() self.assertEqual(serializer.format_date(datetime.date(2010, 12, 16)), '2010-12-16') # Restore. settings.apiserver_DATETIME_FORMATTING = old_format
def test_to_xml_multirepr(self): serializer = Serializer() self.assertEqual(serializer.to_xml(self.obj_list), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<objects><object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object><object><updated>2010-03-31T20:05:00</updated><created>2010-03-31T20:05:00</created><title>Another Post</title><is_active type="boolean">True</is_active><slug>another-post</slug><content>The dog ate my cat today. He looks seriously uncomfortable.</content><id>2</id><resource_uri></resource_uri></object><object><updated>2010-04-01T20:05:00</updated><created>2010-04-01T20:05:00</created><title>Recent Volcanic Activity.</title><is_active type="boolean">True</is_active><slug>recent-volcanic-activity</slug><content>My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.</content><id>4</id><resource_uri></resource_uri></object><object><updated>2010-04-02T10:05:00</updated><created>2010-04-02T10:05:00</created><title>Granny\'s Gone</title><is_active type="boolean">True</is_active><slug>grannys-gone</slug><content>Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!</content><id>6</id><resource_uri></resource_uri></object></objects>')
def test_to_jsonp(self): serializer = Serializer() sample_1 = self.get_sample1() options = {'callback': 'myCallback'} self.assertEqual(serializer.to_jsonp(sample_1, options), 'myCallback({"age": 27, "date_joined": "2010-03-27", "name": "Daniel"})')
def test_to_xml_single(self): serializer = Serializer() resource = self.obj_list[0] self.assertEqual(serializer.to_xml(resource), '<?xml version=\'1.0\' encoding=\'utf-8\'?>\n<object><updated>2010-03-30T20:05:00</updated><created>2010-03-30T20:05:00</created><title>First Post!</title><is_active type="boolean">True</is_active><slug>first-post</slug><content>This is my very first post using my shiny new API. Pretty sweet, huh?</content><id>1</id><resource_uri></resource_uri></object>')
def test_to_json_multirepr(self): serializer = Serializer() self.assertEqual(serializer.to_json(self.obj_list), '[{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": "1", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}, {"content": "The dog ate my cat today. He looks seriously uncomfortable.", "created": "2010-03-31T20:05:00", "id": "2", "is_active": true, "resource_uri": "", "slug": "another-post", "title": "Another Post", "updated": "2010-03-31T20:05:00"}, {"content": "My neighborhood\'s been kinda weird lately, especially after the lava flow took out the corner store. Granny can hardly outrun the magma with her walker.", "created": "2010-04-01T20:05:00", "id": "4", "is_active": true, "resource_uri": "", "slug": "recent-volcanic-activity", "title": "Recent Volcanic Activity.", "updated": "2010-04-01T20:05:00"}, {"content": "Man, the second eruption came on fast. Granny didn\'t have a chance. On the upshot, I was able to save her walker and I got a cool shawl out of the deal!", "created": "2010-04-02T10:05:00", "id": "6", "is_active": true, "resource_uri": "", "slug": "grannys-gone", "title": "Granny\'s Gone", "updated": "2010-04-02T10:05:00"}]')
def test_to_json_single(self): serializer = Serializer() resource = self.obj_list[0] self.assertEqual(serializer.to_json(resource), '{"content": "This is my very first post using my shiny new API. Pretty sweet, huh?", "created": "2010-03-30T20:05:00", "id": "1", "is_active": true, "resource_uri": "", "slug": "first-post", "title": "First Post!", "updated": "2010-03-30T20:05:00"}')
def test_format_datetime(self): serializer = Serializer() self.assertEqual( serializer.format_datetime( datetime.datetime(2010, 12, 16, 2, 31, 33)), '2010-12-16T02:31:33') serializer = Serializer(datetime_formatting='iso-8601') self.assertEqual( serializer.format_datetime( datetime.datetime(2010, 12, 16, 2, 31, 33)), '2010-12-16T02:31:33') serializer = Serializer(datetime_formatting='rfc-2822') self.assertEqual( serializer.format_datetime( datetime.datetime(2010, 12, 16, 2, 31, 33)), u'Thu, 16 Dec 2010 02:31:33 -0600') serializer = Serializer(datetime_formatting='random-garbage') self.assertEqual( serializer.format_datetime( datetime.datetime(2010, 12, 16, 2, 31, 33)), '2010-12-16T02:31:33') # Stow. old_format = getattr(settings, 'apiserver_DATETIME_FORMATTING', 'iso-8601') settings.apiserver_DATETIME_FORMATTING = 'iso-8601' serializer = Serializer() self.assertEqual( serializer.format_datetime( datetime.datetime(2010, 12, 16, 2, 31, 33)), '2010-12-16T02:31:33') settings.apiserver_DATETIME_FORMATTING = 'rfc-2822' serializer = Serializer() self.assertEqual( serializer.format_datetime( datetime.datetime(2010, 12, 16, 2, 31, 33)), u'Thu, 16 Dec 2010 02:31:33 -0600') settings.apiserver_DATETIME_FORMATTING = 'random-garbage' serializer = Serializer() self.assertEqual( serializer.format_datetime( datetime.datetime(2010, 12, 16, 2, 31, 33)), '2010-12-16T02:31:33') # Restore. settings.apiserver_DATETIME_FORMATTING = old_format
def test_determine_format(self): serializer = Serializer() request = HttpRequest() # Default. self.assertEqual(determine_format(request, serializer), 'application/json') # Test forcing the ``format`` parameter. request.GET = {'format': 'json'} self.assertEqual(determine_format(request, serializer), 'application/json') request.GET = {'format': 'jsonp'} self.assertEqual(determine_format(request, serializer), 'text/javascript') request.GET = {'format': 'xml'} self.assertEqual(determine_format(request, serializer), 'application/xml') request.GET = {'format': 'yaml'} self.assertEqual(determine_format(request, serializer), 'text/yaml') request.GET = {'format': 'foo'} self.assertEqual(determine_format(request, serializer), 'application/json') # Test the ``Accept`` header. request.META = {'HTTP_ACCEPT': 'application/json'} self.assertEqual(determine_format(request, serializer), 'application/json') request.META = {'HTTP_ACCEPT': 'text/javascript'} self.assertEqual(determine_format(request, serializer), 'text/javascript') request.META = {'HTTP_ACCEPT': 'application/xml'} self.assertEqual(determine_format(request, serializer), 'application/xml') request.META = {'HTTP_ACCEPT': 'text/yaml'} self.assertEqual(determine_format(request, serializer), 'text/yaml') request.META = {'HTTP_ACCEPT': 'text/html'} self.assertEqual(determine_format(request, serializer), 'text/html') request.META = {'HTTP_ACCEPT': '*/*'} self.assertEqual(determine_format(request, serializer), 'application/json') request.META = { 'HTTP_ACCEPT': 'application/json,application/xml;q=0.9,*/*;q=0.8' } self.assertEqual(determine_format(request, serializer), 'application/json') request.META = { 'HTTP_ACCEPT': 'text/plain,application/xml,application/json;q=0.9,*/*;q=0.8' } self.assertEqual(determine_format(request, serializer), 'application/xml') request.META = {'HTTP_ACCEPT': 'application/json; charset=UTF-8'} self.assertEqual(determine_format(request, serializer), 'application/json') request.META = {'HTTP_ACCEPT': 'text/javascript,application/json'} self.assertEqual(determine_format(request, serializer), 'application/json')
def test_format_time(self): serializer = Serializer() self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)), '02:31:33') serializer = Serializer(datetime_formatting='iso-8601') self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)), '02:31:33') serializer = Serializer(datetime_formatting='rfc-2822') self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)), u'02:31:33 -0600') serializer = Serializer(datetime_formatting='random-garbage') self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)), '02:31:33') # Stow. old_format = getattr(settings, 'apiserver_DATETIME_FORMATTING', 'iso-8601') settings.apiserver_DATETIME_FORMATTING = 'iso-8601' serializer = Serializer() self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)), '02:31:33') settings.apiserver_DATETIME_FORMATTING = 'rfc-2822' serializer = Serializer() self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)), u'02:31:33 -0600') settings.apiserver_DATETIME_FORMATTING = 'random-garbage' serializer = Serializer() self.assertEqual(serializer.format_time(datetime.time(2, 31, 33)), '02:31:33') # Restore. settings.apiserver_DATETIME_FORMATTING = old_format