예제 #1
0
    def test_nested_api_fields(self):
        family_fields = {
            'father': fields.Nested(person_fields),
            'mother': fields.Nested(person_fields),
        }

        result = mask.apply(family_fields, 'father{name},mother{age}')
        assert set(result.keys()) == set(['father', 'mother'])
        assert isinstance(result['father'], fields.Nested)
        assert set(result['father'].nested.keys()) == set(['name'])
        assert isinstance(result['mother'], fields.Nested)
        assert set(result['mother'].nested.keys()) == set(['age'])

        data = {
            'father': {
                'name': 'John',
                'age': 42
            },
            'mother': {
                'name': 'Jane',
                'age': 42
            },
        }
        expected = {'father': {'name': 'John'}, 'mother': {'age': 42}}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, family_fields), data)
예제 #2
0
    def test_multiple_nested_api_fields(self):
        level_2 = {'nested_2': fields.Nested(person_fields)}
        level_1 = {'nested_1': fields.Nested(level_2)}
        root = {'nested': fields.Nested(level_1)}

        result = mask.apply(root, 'nested{nested_1{nested_2{name}}}')
        assert set(result.keys()) == set(['nested'])
        assert isinstance(result['nested'], fields.Nested)
        assert set(result['nested'].nested.keys()) == set(['nested_1'])

        data = {
            'nested': {
                'nested_1': {
                    'nested_2': {
                        'name': 'John',
                        'age': 42
                    }
                }
            }
        }
        expected = {'nested': {'nested_1': {'nested_2': {'name': 'John'}}}}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, root), data)
예제 #3
0
    def test_list_fields_with_nested_inherited(self, app):
        api = Api(app)

        person = api.model('Person', {
            'name': fields.String,
            'age': fields.Integer
        })
        child = api.inherit('Child', person, {
            'attr': fields.String
        })

        family = api.model('Family', {
            'children': fields.List(fields.Nested(child))
        })

        result = mask.apply(family.resolved, 'children{name,attr}')

        data = {'children': [
            {'name': 'John', 'age': 5, 'attr': 'value-john'},
            {'name': 'Jane', 'age': 42, 'attr': 'value-jane'},
        ]}
        expected = {'children': [
            {'name': 'John', 'attr': 'value-john'},
            {'name': 'Jane', 'attr': 'value-jane'},
        ]}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, family), data)
예제 #4
0
    def test_list_fields_with_nested(self):
        family_fields = {'members': fields.List(fields.Nested(person_fields))}

        result = mask.apply(family_fields, 'members{name}')
        assert set(result.keys()) == set(['members'])
        assert isinstance(result['members'], fields.List)
        assert isinstance(result['members'].container, fields.Nested)
        assert set(result['members'].container.nested.keys()) == set(['name'])

        data = {
            'members': [
                {
                    'name': 'John',
                    'age': 42
                },
                {
                    'name': 'Jane',
                    'age': 42
                },
            ]
        }
        expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, family_fields), data)
예제 #5
0
    def test_marshal_handle_inheritance(self, app):
        api = Api(app)

        person = api.model('Person', {
            'name': fields.String,
            'age': fields.Integer,
        })

        child = api.inherit('Child', person, {
            'extra': fields.String,
        })

        data = {'name': 'John Doe', 'age': 42, 'extra': 'extra'}

        values = (
            ('name', {
                'name': 'John Doe'
            }),
            ('name,extra', {
                'name': 'John Doe',
                'extra': 'extra'
            }),
            ('extra', {
                'extra': 'extra'
            }),
        )

        for value, expected in values:
            result = marshal(data, child, mask=value)
            assert result == expected
예제 #6
0
 def test_marshal(self):
     model = OrderedDict([('foo', fields.Raw)])
     marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = marshal(marshal_dict, model)
     assert isinstance(output, dict)
     assert not isinstance(output, OrderedDict)
     assert output == {'foo': 'bar'}
예제 #7
0
    def test_list_fields_with_raw(self):
        family_fields = {
            'members': fields.List(fields.Raw)
        }

        result = mask.apply(family_fields, 'members{name}')

        data = {'members': [
            {'name': 'John', 'age': 42},
            {'name': 'Jane', 'age': 42},
        ]}
        expected = {'members': [{'name': 'John'}, {'name': 'Jane'}]}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, family_fields), data)
예제 #8
0
 def test_marshal_wildcard_with_skip_none(self):
     wild = fields.Wildcard(fields.String)
     model = OrderedDict([('foo', fields.Raw), ('*', wild)])
     marshal_dict = OrderedDict([('foo', None), ('bat', None),
                                 ('baz', 'biz'), ('bar', None)])
     output = marshal(marshal_dict, model, skip_none=True)
     assert output == {'baz': 'biz'}
예제 #9
0
    def test_list_fields_with_simple_field(self):
        family_fields = {
            'name': fields.String,
            'members': fields.List(fields.String)
        }

        result = mask.apply(family_fields, 'members')
        assert set(result.keys()) == set(['members'])
        assert isinstance(result['members'], fields.List)
        assert isinstance(result['members'].container, fields.String)

        data = {'name': 'Doe', 'members': ['John', 'Jane']}
        expected = {'members': ['John', 'Jane']}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, family_fields), data)
예제 #10
0
    def test_raw_api_fields(self):
        family_fields = {
            'father': fields.Raw,
            'mother': fields.Raw,
        }

        result = mask.apply(family_fields, 'father{name},mother{age}')

        data = {
            'father': {'name': 'John', 'age': 42},
            'mother': {'name': 'Jane', 'age': 42},
        }
        expected = {'father': {'name': 'John'}, 'mother': {'age': 42}}

        assert_data(marshal(data, result), expected)
        # Should leave th original mask untouched
        assert_data(marshal(data, family_fields), data)
예제 #11
0
 def test_marshal_list_of_lists(self):
     model = OrderedDict([('foo', fields.Raw),
                          ('fee', fields.List(fields.List(fields.String)))])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', [['fye'], ['fum']])])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'bar'), ('fee', [['fye'], ['fum']])])
     assert output == expected
예제 #12
0
 def test_marshal_nested_with_skip_none(self):
     model = OrderedDict([('foo', fields.Raw),
                          ('fee',
                           fields.Nested(OrderedDict([('fye', fields.String)
                                                      ]),
                                         skip_none=True))])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', None)])
     output = marshal(marshal_fields, model, skip_none=True)
     expected = OrderedDict([('foo', 'bar')])
     assert output == expected
예제 #13
0
 def test_marshal_list_of_nesteds(self):
     model = OrderedDict([('foo', fields.Raw),
                          ('fee',
                           fields.List(fields.Nested({'fye':
                                                      fields.String})))])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', {
                                       'fye': 'fum'
                                   })])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'bar'),
                             ('fee', [OrderedDict([('fye', 'fum')])])])
     assert output == expected
예제 #14
0
 def test_marshal_nested_with_null(self):
     model = OrderedDict([
         ('foo', fields.Raw),
         ('fee',
          fields.Nested(OrderedDict([('fye', fields.String),
                                     ('blah', fields.String)]),
                        allow_null=True))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', None)])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'bar'), ('fee', None)])
     assert output == expected
예제 #15
0
 def test_marshal_nested_dict(self):
     model = OrderedDict([
         ('foo', fields.Raw),
         ('bar', OrderedDict([
             ('a', fields.Raw),
             ('b', fields.Raw),
         ])),
     ])
     marshal_fields = OrderedDict([('foo', 'foo-val'), ('bar', 'bar-val'),
                                   ('bat', 'bat-val'), ('a', 1), ('b', 2),
                                   ('c', 3)])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'foo-val'),
                             ('bar', OrderedDict([('a', 1), ('b', 2)]))])
     assert output == expected
예제 #16
0
 def test_marshal_wildcard_with_envelope(self):
     wild = fields.Wildcard(fields.String)
     model = OrderedDict([('foo', fields.Raw), ('*', wild)])
     marshal_dict = OrderedDict([('foo', {
         'bat': 'baz'
     }), ('a', 'toto'), ('b', 'tata')])
     output = marshal(marshal_dict, model, envelope='hey')
     assert output == {
         'hey': {
             'a': 'toto',
             'b': 'tata',
             'foo': {
                 'bat': 'baz'
             }
         }
     }
예제 #17
0
 def test_skip_none_presents_data(self):
     model = OrderedDict([
         ('foo', fields.Raw),
         ('fee',
          fields.Nested(OrderedDict([('fye', fields.String),
                                     ('blah', fields.String),
                                     ('foe', fields.String)]),
                        skip_none=True))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz'),
                                   ('fee', {
                                       'blah': 'cool',
                                       'foe': None
                                   })])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'bar'),
                             ('fee', OrderedDict([('blah', 'cool')]))])
     assert output == expected
예제 #18
0
    def test_marshal_nested_ordered(self):
        model = OrderedDict([('foo', fields.Raw),
                             ('fee', fields.Nested({
                                 'fye': fields.String,
                             }))])

        marshal_fields = {
            'foo': 'bar',
            'bat': 'baz',
            'fee': {
                'fye': 'fum'
            },
        }
        expected = OrderedDict([('foo', 'bar'),
                                ('fee', OrderedDict([('fye', 'fum')]))])

        output = marshal(marshal_fields, model, ordered=True)

        assert isinstance(output, OrderedDict)
        assert output == expected
        assert isinstance(output['fee'], OrderedDict)
예제 #19
0
    def test_marshal_nested(self):
        model = {
            'foo': fields.Raw,
            'fee': fields.Nested({'fye': fields.String}),
        }

        marshal_fields = {
            'foo': 'bar',
            'bat': 'baz',
            'fee': {
                'fye': 'fum'
            },
        }
        expected = {
            'foo': 'bar',
            'fee': {
                'fye': 'fum'
            },
        }

        output = marshal(marshal_fields, model)

        assert output == expected
예제 #20
0
    def test_marshal_nested_property_with_skip_none(self):
        class TestObject(object):
            @property
            def fee(self):
                return {'blah': 'cool', 'foe': None}

        model = OrderedDict([
            ('foo', fields.Raw),
            ('fee',
             fields.Nested(OrderedDict([('fye', fields.String),
                                        ('blah', fields.String),
                                        ('foe', fields.String)]),
                           skip_none=True))
        ])
        obj = TestObject()
        obj.foo = 'bar'
        obj.bat = 'baz'
        output = marshal([obj], model)
        expected = [
            OrderedDict([('foo', 'bar'),
                         ('fee', OrderedDict([('blah', 'cool')]))])
        ]
        assert output == expected
예제 #21
0
 async def get(self, request):
     return marshal({"foo": 3.0}, self.fields)
예제 #22
0
 def test_marshal_field(self):
     model = OrderedDict({'foo': fields.Raw()})
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = marshal(marshal_fields, model)
     assert output == {'foo': 'bar'}
예제 #23
0
 def test_marshal_tuple(self):
     model = OrderedDict({'foo': fields.Raw})
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = marshal((marshal_fields, ), model)
     assert output == [{'foo': 'bar'}]
예제 #24
0
 def test_marshal_tuple_with_envelope(self):
     model = OrderedDict({'foo': fields.Raw})
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = marshal((marshal_fields, ), model, envelope='hey')
     assert output == {'hey': [{'foo': 'bar'}]}
예제 #25
0
def marshal_simple_with_mask(app):
    with app.test_request_context('/', headers={'X-Fields': 'name'}):
        return marshal(person(), person_fields)
예제 #26
0
def marshal_nested():
    return marshal(family(), family_fields)
예제 #27
0
def marshal_nested_with_mask(app):
    with app.test_request_context(
            '/', headers={'X-Fields': 'father,children{name}'}):
        return marshal(family(), family_fields)
예제 #28
0
 def test_marshal_with_skip_none(self):
     model = OrderedDict([('foo', fields.Raw), ('bat', fields.Raw),
                          ('qux', fields.Raw)])
     marshal_dict = OrderedDict([('foo', 'bar'), ('bat', None)])
     output = marshal(marshal_dict, model, skip_none=True)
     assert output == {'foo': 'bar'}
예제 #29
0
 def test_marshal_with_envelope(self):
     model = OrderedDict([('foo', fields.Raw)])
     marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = marshal(marshal_dict, model, envelope='hey')
     assert output == {'hey': {'foo': 'bar'}}
예제 #30
0
 def get(self):
     return marshal({"foo": 3.0}, self.fields)