Ejemplo n.º 1
0
    def test_serializes_aware_datetime(self):
        encoder = AdvancedJSONEncoder()
        dt = datetime(2014, 12, 31, 15, 45, 30, 999, tzinfo=pytz.UTC)

        self.assertEquals(
            '[{'
            '"_advancedjson_value": "2014-12-31T15:45:30.000999+00:00", '
            '"_advancedjson_type": "datetime"'
            '}]',
            encoder.encode([dt]))
Ejemplo n.º 2
0
    def test_serializes_persistent_mapping_as_dict(self):
        data = PersistentMapping(foo=u'bar')
        encoder = AdvancedJSONEncoder()

        self.assertEquals(
            '[{'
            '"_advancedjson_value": {"foo": "bar"}, '
            '"_advancedjson_type": "PersistentMapping"'
            '}]',
            encoder.encode([data]))
Ejemplo n.º 3
0
    def test_serializes_uuid(self):
        uuid = UUID('61df790e-e547-416f-b661-d236a13250de')
        encoder = AdvancedJSONEncoder()

        self.assertEquals(
            '[{'
            '"_advancedjson_value": "61df790e-e547-416f-b661-d236a13250de", '
            '"_advancedjson_type": "UUID"'
            '}]',
            encoder.encode([uuid]))
Ejemplo n.º 4
0
    def test_serializes_set(self):
        encoder = AdvancedJSONEncoder()
        data = set([u'foo'])

        self.assertEquals(
            '[{'
            '"_advancedjson_value": ["foo"], '
            '"_advancedjson_type": "set"'
            '}]',
            encoder.encode([data]))
Ejemplo n.º 5
0
    def test_encoder_decoder_roundtrip(self):
        encoder = AdvancedJSONEncoder()
        decoder = AdvancedJSONDecoder()

        input = {
            u'dt_naive': datetime(2014, 12, 31, 15, 45, 30, 999),
            u'dt_aware': datetime(2099, 1, 3, 15, 45, 30, 12, tzinfo=pytz.UTC),
            u'somethingelse': u'2017-1-1',
            u'justanumber': 49184913,
            u'dict': dict(qux=u'blabla'),
            u'list': [1, 2, u'44'],
            u'uuid': uuid4(),
            u'set': set([1, 2, u'gugus']),
            u'pmapping': PersistentMapping({u'qux': 123, u'meh': u'bar'}),
        }
        output = decoder.decode(encoder.encode(input))
        self.assertEquals(input, output)
Ejemplo n.º 6
0
 def test_raises_value_error_for_non_unicode_namedtuple_items(self):
     encoder = AdvancedJSONEncoder()
     Thing = namedtuple('Thing', ['attr'])
     data = Thing(attr='fail')
     with self.assertRaises(ValueError):
         encoder.encode(data)
Ejemplo n.º 7
0
 def test_raises_value_error_for_non_unicode_tuple_items(self):
     encoder = AdvancedJSONEncoder()
     data = ('nahhh',)
     with self.assertRaises(ValueError):
         encoder.encode(data)
Ejemplo n.º 8
0
 def test_raises_value_error_for_non_unicode_list_items(self):
     encoder = AdvancedJSONEncoder()
     data = ['mehh']
     with self.assertRaises(ValueError):
         encoder.encode(data)
Ejemplo n.º 9
0
 def test_raises_value_error_for_non_unicode_persistent_mapping_values(self):
     encoder = AdvancedJSONEncoder()
     data = PersistentMapping(
         {'uhoh': PersistentMapping({'nested_ouch': 'nope'})})
     with self.assertRaises(ValueError):
         encoder.encode(data)
Ejemplo n.º 10
0
 def test_raises_value_error_for_non_unicode_dict_values(self):
     encoder = AdvancedJSONEncoder()
     data = {'uhoh': {'nested_uhoh': 'mehh'}}
     with self.assertRaises(ValueError):
         encoder.encode(data)
Ejemplo n.º 11
0
 def test_raises_value_error_for_non_unicode_values(self):
     encoder = AdvancedJSONEncoder()
     with self.assertRaises(ValueError):
         encoder.encode('foo')
Ejemplo n.º 12
0
 def test_delegates_unknown_objects_to_default_encoder(self):
     encoder = AdvancedJSONEncoder()
     data = [object()]
     with self.assertRaises(TypeError):
         encoder.encode(data)