示例#1
0
    def to_dict(self):
        output = {}
        for key in ['id', 'name', 'category', 'description', 'template']:
            output[key] = getattr(self, key)

        output['params'] = self.params
        output['handlers'] = Converter.encode(self.handlers)

        return output
示例#2
0
    def test_encoding_objects(self):
        """Test encoding of objects, nested objects, list and dict
        of nested objects"""
        class A():
            def __init__(self, a):
                self.a = a

        class B():
            def __init__(self, b):
                self.b = b

        value = A(3)
        encoded_value = Converter.encode(value)
        self.assertEqual(encoded_value, {'__A__': {'a': 3}})

        value = A(B(2))
        encoded_value = Converter.encode(value)
        self.assertEqual(encoded_value, {'__A__': {'a': {'__B__': {'b': 2}}}})

        value = [A(3), A(5)]
        encoded_value = Converter.encode(value)
        self.assertEqual(
            encoded_value, [{'__A__': {'a': 3}}, {'__A__': {'a': 5}}]
        )

        value = [A(B(2)), A(B(3))]
        encoded_value = Converter.encode(value)
        self.assertEqual(
            encoded_value,
            [{'__A__': {'a': {'__B__': {'b': 2}}}},
             {'__A__': {'a': {'__B__': {'b': 3}}}}]
        )

        value = {1: [A(B(2)), A(B(3))], 2: [A(B(4)), A(B(5))]}
        encoded_value = Converter.encode(value)
        self.assertEqual(
            encoded_value,
            {
                1: [{'__A__': {'a': {'__B__': {'b': 2}}}},
                    {'__A__': {'a': {'__B__': {'b': 3}}}}],
                2: [{'__A__': {'a': {'__B__': {'b': 4}}}},
                    {'__A__': {'a': {'__B__': {'b': 5}}}}]
            })
示例#3
0
    def test_encoding_builtin_types(self):
        """Test encoding of built-in datatypes."""
        value = 'test string'
        encoded_value = Converter.encode(value)
        self.assertEqual(encoded_value, u'test string')

        value = [1, 'a string', 3.5, [2, 3]]
        encoded_value = Converter.encode(value)
        self.assertEqual(encoded_value, [1, u'a string', 3.5, [2, 3]])

        value = (1, 2, 3)
        with self.assertRaises(NotImplementedError):
            encoded_value = Converter.encode(value)

        value = {1, 2, 3}
        with self.assertRaises(NotImplementedError):
            encoded_value = Converter.encode(value)

        value = {'mango': 'fruit', '1': 'number'}
        encoded_value = Converter.encode(value)
        self.assertEqual(encoded_value, {'mango': 'fruit', '1': 'number'})