示例#1
0
 def call_test(self):
     """
     test simple calls
     """
     cast = Cast({
         AllSubSetsOf(dict): MappingNode,
         AllSubSetsOf(list): IterableNode,
         AllSubSetsOf(object): IdentityNode,
     })
     self.assertEqual(cast({'a': 1, 'b': 2}, dumper=dict, loader=dict), {'a': 1, 'b': 2})
     self.assertEqual(cast({'a': 1, 'b': 2}, loader=list), [1, 2])
     self.assertEqual(cast(['a', 'b', 'c'], loader=dict), {0: 'a', 1: 'b', 2: 'c'})
     self.assertEqual(cast(['a', 'b', 'c'], loader=list), ['a', 'b', 'c'])
     self.assertEqual(cast(1, loader=int), 1)
示例#2
0
    def call_inpt_with_dump_test(self):
        """
        Test call with an input that has itself a __dump__ method.
        """
        class MyDumper(object):
            @staticmethod
            def __dump__(obj):
                yield AttrDict.KeyFinal, str(obj)

        class MyObject(dict):
            def __dump__(self):
                for k, v in self.iteritems():
                    yield k, v
                yield '__count__', len(self)

            def __dschema__(self):
                return {'__count__': MyDumper, AttrDict.KeyAny: NodeInfo()}

        cast = Cast(
            {
                AllSubSetsOf(object): IdentityNode,
                AllSubSetsOf(dict): MappingNode,
            }, {AllSubSetsOf(object): IdentityNode})
        inpt = MyObject({'a': 1, 'b': 2})
        self.assertEqual(cast(inpt, loader=dict), {
            'a': 1,
            'b': 2,
            '__count__': '2'
        })
示例#3
0
    def call_inpt_with_dump_test(self):
        """
        Test call with an input that has itself a __dump__ method.
        """
        class MyDumper(object):
                
            @staticmethod
            def __dump__(obj):
                yield AttrDict.KeyFinal, str(obj)

        class MyObject(dict):

            def __dump__(self):
                for k, v in self.iteritems():
                    yield k, v
                yield '__count__', len(self)

            def __dschema__(self):
                return {'__count__': MyDumper, AttrDict.KeyAny: NodeInfo()}

        cast = Cast({
            AllSubSetsOf(object): IdentityNode,
            AllSubSetsOf(dict): MappingNode,
        }, {
            AllSubSetsOf(object): IdentityNode
        })
        inpt = MyObject({'a': 1, 'b': 2})
        self.assertEqual(cast(inpt, loader=dict), {'a': 1, 'b': 2, '__count__': '2'})
示例#4
0
 def call_test(self):
     """
     test simple calls
     """
     cast = Cast({
         AllSubSetsOf(dict): MappingNode,
         AllSubSetsOf(list): IterableNode,
         AllSubSetsOf(object): IdentityNode,
     })
     self.assertEqual(cast({
         'a': 1,
         'b': 2
     }, dumper=dict, loader=dict), {
         'a': 1,
         'b': 2
     })
     self.assertEqual(cast({'a': 1, 'b': 2}, loader=list), [1, 2])
     self.assertEqual(cast(['a', 'b', 'c'], loader=dict), {
         0: 'a',
         1: 'b',
         2: 'c'
     })
     self.assertEqual(cast(['a', 'b', 'c'], loader=list), ['a', 'b', 'c'])
     self.assertEqual(cast(1, loader=int), 1)
示例#5
0
    def call_inpt_with_load_test(self):
        """
        Test call to a class that has itself a load method.
        """
        class MyDict(dict):
            def __load__(self, items_iter):
                for k, v in items_iter:
                    if k.startswith('key'):
                        self[k] = v
                return self

        my_dict = MyDict()
        cast = Cast(
            {
                AllSubSetsOf(object): IdentityNode,
                AllSubSetsOf(dict): MappingNode,
            }, {AllSubSetsOf(object): IdentityNode})
        inpt = {'a': 1, 'b': 2, 'key_c': 3, 'key_d': 4}
        self.assertEqual(cast(inpt, loader=my_dict), {'key_c': 3, 'key_d': 4})
示例#6
0
    def call_inpt_with_load_test(self):
        """
        Test call to a class that has itself a load method.
        """
        class MyDict(dict):
                
            def __load__(self, items_iter):
                for k, v in items_iter:
                    if k.startswith('key'):
                        self[k] = v
                return self

        my_dict = MyDict()
        cast = Cast({
            AllSubSetsOf(object): IdentityNode,
            AllSubSetsOf(dict): MappingNode,
        }, {
            AllSubSetsOf(object): IdentityNode
        })
        inpt = {'a': 1, 'b': 2, 'key_c': 3, 'key_d': 4}
        self.assertEqual(cast(inpt, loader=my_dict), {'key_c': 3, 'key_d': 4})