Exemple #1
0
 def test_serializer_default_content_type(self):
     serializer = Serializer(
         default_content_type='application/json'
     )
     content, content_type = serializer.serialize({'key': 'value'})
     self.assertEquals(content, '{"key": "value"}')
     self.assertEquals(content_type, 'application/json')
Exemple #2
0
class SerializerSerializeTest(TestCaseNoDB):
    def setUp(self):
        self.dt = SerializerConvertTest.dt
        self.dt_expect = SerializerConvertTest.dt_expect
        self.serializer = Serializer()

        self.data = {'key': self.dt}

    def test_application_json(self):
        content, content_type = \
            self.serializer.serialize(self.data, 'application/json')
        self.assertEquals(content_type, 'application/json')
        self.assertEquals(
            content,
            '{"key": "%s"}' % self.dt_expect
        )

    def test_application_json_indent(self):
        content, content_type = \
            self.serializer.serialize(self.data, 'application/json; indent=4')
        self.assertEquals(content_type, 'application/json')
        self.assertEquals(
            content,
            '{\n    "key": "%s"\n}' % self.dt_expect
        )

    def test_application_json_indent_invalid(self):
        content, content_type = \
            self.serializer.serialize(self.data, 'application/json; indent=a')
        self.assertEquals(content_type, 'application/json')
        self.assertEquals(
            content,
            '{"key": "%s"}' % self.dt_expect
        )

    def test_application_json_q(self):
        content, content_type = \
            self.serializer.serialize(self.data, 'application/json; q=0.5')
        self.assertEquals(content_type, 'application/json')
        self.assertEquals(
            content,
            '{"key": "%s"}' % self.dt_expect
        )

    def test_default(self):
        content, content_type = \
            self.serializer.serialize(self.data)
        self.assertEquals(content_type, self.serializer.default_content_type)
        self.assertEquals(
            content,
            '{"key": "%s"}' % self.dt_expect
        )

    def test_unsupported(self):
        self.assertRaises(
            self.serializer.Unsupported,
            self.serializer.get_format,
            'unsupported/content-type'
        )
Exemple #3
0
class SerializerFormatTest(TestCaseNoDB):
    def setUp(self):
        self.serializer = Serializer()

    def test_get_content_type_json(self):
        self.assertEquals(
            self.serializer.get_content_type('json'),
            'application/json'
        )
Exemple #4
0
class SerializerConvertTest(TestCaseNoDB):
    dt = datetime(2013, 6, 8, 14, 34, 28, 121251)
    dt_expect = '2013-06-08T14:34:28.121251'
    td = timedelta(1, 2, 3)
    tz = pytz.timezone('America/Chicago')

    def do(self, obj, expect):
        self.assertEquals(expect, self.serializer.convert(obj))

    def setUp(self):
        self.serializer = Serializer()

    def test_date(self):
        self.do(self.dt.date(), self.value(self.dt_expect.split('T')[0]))

    def test_datetime(self):
        self.do(self.dt, self.value(self.dt_expect))

    def test_datetime_with_timezone(self):
        self.do(
            self.tz.normalize(self.tz.localize(self.dt)),
            self.value(self.dt_expect + '-05:00')
        )

    def test_decimal(self):
        self.do(Decimal('1.7921579150'), self.value('1.7921579150'))

    def test_dict(self):
        self.do(
            {'key': self.dt},
            {'key': self.dt_expect}
        )

    def test_list(self):
        self.do(
            [self.dt],
            [self.dt_expect]
        )

    def test_primitive(self):
        for value in self.serializer.primitive_values + (1, 1L, 1.1, 'a', u'a'):
            self.assertEquals(value, self.serializer.convert(value))
Exemple #5
0
 def setUp(self):
     self.serializer = Serializer()
 def setUp(self):
     self.serializer = Serializer()
Exemple #7
0
 def setUp(self):
     self.data = {'key': 'value'}
     self.serializer = Serializer()
Exemple #8
0
class SerializerDeserializeTest(TestCaseNoDB):
    def setUp(self):
        self.data = {'key': 'value'}
        self.serializer = Serializer()

    def test_application_json(self):
        self.assertEquals(
            self.data,
            self.serializer.deserialize('{"key": "value"}', 'application/json')
        )

    def test_application_json_indent(self):
        self.assertEquals(
            self.data,
            self.serializer \
                .deserialize('{"key": "value"}', 'application/json; indent=4')
        )

    def test_invalid_data(self):
        for content_type in self.serializer.content_types.keys():
            if content_type.startswith('multipart/'):
                continue
            self.assertRaises(
                ValueError,
                self.serializer.deserialize,
                'invalid body data',
                content_type
            )

    def test_invalid_multipart_boundary(self):
        data = 'data'
        boundary = '\xffinvalid boundary'
        parsed = urlparse('/path/')
        environ = self.client._base_environ(**{
            'CONTENT_TYPE': 'multipart/form-data; boundary=%s' % boundary,
            'CONTENT_LENGTH': len(data),
            'PATH_INFO': self.client._get_path(parsed),
            'QUERY_STRING': parsed[4],
            'REQUEST_METHOD': 'POST',
            'wsgi.input': FakePayload(data),
        })
        request = WSGIRequest(environ)

        for content_type in self.serializer.content_types.keys():
            if not content_type.startswith('multipart/'):
                continue
            self.assertRaises(
                ValueError,
                self.serializer.deserialize_request,
                request,
                request.META['CONTENT_TYPE']
            )

    def test_invalid_multipart_content_length(self):
        data = 'data'
        boundary = 'boundary'
        parsed = urlparse('/path/')
        environ = self.client._base_environ(**{
            'CONTENT_TYPE': 'multipart/form-data; boundary=%s' % boundary,
            'CONTENT_LENGTH': -1,
            'PATH_INFO': self.client._get_path(parsed),
            'QUERY_STRING': parsed[4],
            'REQUEST_METHOD': 'POST',
            'wsgi.input': FakePayload(data),
        })
        request = WSGIRequest(environ)

        for content_type in self.serializer.content_types.keys():
            if not content_type.startswith('multipart/'):
                continue
            self.assertRaises(
                ValueError,
                self.serializer.deserialize_request,
                request,
                request.META['CONTENT_TYPE']
            )
Exemple #9
0
    def setUp(self):
        self.dt = SerializerConvertTest.dt
        self.dt_expect = SerializerConvertTest.dt_expect
        self.serializer = Serializer()

        self.data = {'key': self.dt}