def test_json(self): request = RequestFactory().get('/') s = serializers.SerializerMixin() s.request = request self.assertEqual(s.get_serializer('application/json'), s.serialize_to_json)
def test_excel(self): request = RequestFactory().get('/') s = serializers.SerializerMixin() s.request = request self.assertEqual(s.get_serializer('application/vnd.ms-excel'), s.serialize_to_excel)
def setUp(self): request = RequestFactory().get('') s = serializers.SerializerMixin() s.request = request s.template = {'fields': ['name', 'surname']} s.excel_filename = 'file.xls' self.s = s
def test_json(self): request = RequestFactory().get('/', HTTP_ACCEPT='application/json') s = serializers.SerializerMixin() s.request = request self.assertEqual( s.get_serialization_format(), 'application/json', )
def test_no_accept_header(self): request = RequestFactory().get('/') s = serializers.SerializerMixin() s.request = request self.assertEqual( s.get_serialization_format(), s.DEFAULT_SERIALIZATION_FORMAT, )
def test_dict(self): request = RequestFactory().get('/') s = serializers.SerializerMixin() s.request = request data = {'key': 'value'} self.assertEqual(s.serialize_to_json(data), (json.dumps(data, indent=4), { 'Content-Type': 'application/json; charset=utf-8' }))
def test_json_datetime(self): request = RequestFactory().get('/') s = serializers.SerializerMixin() s.request = request data = datetime.now() self.assertEqual(s.serialize_to_json(data), (json.dumps(data, cls=DateTimeAwareJSONEncoder), { 'Content-Type': 'application/json; charset=utf-8' }))
def test_json_string(self): request = RequestFactory().get('/') s = serializers.SerializerMixin() s.request = request data = 'some string' self.assertEqual(s.serialize_to_json(data), (json.dumps(data), { 'Content-Type': 'application/json; charset=utf-8' }))
def test_json_list(self): request = RequestFactory().get('/') s = serializers.SerializerMixin() s.request = request data = [1, 2, 3, 'a', 'b'] self.assertEqual(s.serialize_to_json(data), (json.dumps(data, indent=4), { 'Content-Type': 'application/json; charset=utf-8' }))
def test_get_response(self): request = RequestFactory().get('/') s = serializers.SerializerMixin() s.request = request data = [1, 2, 3, 'a', 'b'] headers = {'Content-Type': 'application/json; charset=utf-8'} self.assertItemsEqual( s.get_response(data), HttpResponse(json.dumps(data, indent=4), content_type='application/json; charset=utf-8'))
def get_response(self, request): s = serializers.SerializerMixin() s.request = request body, headers = s.serialize(self.errors, s.DEFAULT_SERIALIZATION_FORMAT) res = http.HttpResponseBadRequest(body) for key, value in headers.items(): res[key] = value return res
def test_serialize_with_ser_format(self): #accept header requests excel request = RequestFactory().get('/', HTTP_ACCEPT='application/vnd.ms-excel') s = serializers.SerializerMixin() s.request = request # But ``serialize`` is called with ``application/json`` data = [1, 2, 3, 'a', 'b'] self.assertItemsEqual( s.serialize(data, 'application/json'), (json.dumps(data, indent=4), { 'Content-Type': 'application/json; charset=utf-8' }))