flask_restplus.mask.apply

Here are the examples of the python api flask_restplus.mask.apply taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

23 Examples 7

Example 1

Project: flask-restplus Source File: test_fields_mask.py
Function: test_empty
    def test_empty(self):
        data = {
            'integer': 42,
            'string': 'a string',
            'boolean': True,
        }
        result = mask.apply(data, '{}')
        self.assertEqual(result, {})

Example 2

Project: flask-restplus Source File: test_fields_mask.py
Function: test_single_field
    def test_single_field(self):
        data = {
            'integer': 42,
            'string': 'a string',
            'boolean': True,
        }
        result = mask.apply(data, '{integer}')
        self.assertEqual(result, {'integer': 42})

Example 3

Project: flask-restplus Source File: test_fields_mask.py
Function: test_multiple_fields
    def test_multiple_fields(self):
        data = {
            'integer': 42,
            'string': 'a string',
            'boolean': True,
        }
        result = mask.apply(data, '{integer, string}')
        self.assertEqual(result, {'integer': 42, 'string': 'a string'})

Example 4

Project: flask-restplus Source File: test_fields_mask.py
    def test_star_only(self):
        data = {
            'integer': 42,
            'string': 'a string',
            'boolean': True,
        }
        result = mask.apply(data, '*')
        self.assertEqual(result, data)

Example 5

Project: flask-restplus Source File: test_fields_mask.py
    def test_with_objects(self):
        data = DObject({
            'integer': 42,
            'string': 'a string',
            'boolean': True,
        })
        result = mask.apply(data, '{integer, string}')
        self.assertEqual(result, {'integer': 42, 'string': 'a string'})

Example 6

Project: flask-restplus Source File: test_fields_mask.py
    def test_with_ordered_dict(self):
        data = OrderedDict({
            'integer': 42,
            'string': 'a string',
            'boolean': True,
        })
        result = mask.apply(data, '{integer, string}')
        self.assertEqual(result, {'integer': 42, 'string': 'a string'})

Example 7

Project: flask-restplus Source File: test_fields_mask.py
Function: test_nested_field
    def test_nested_field(self):
        data = {
            'integer': 42,
            'string': 'a string',
            'boolean': True,
            'nested': {
                'integer': 42,
                'string': 'a string',
                'boolean': True,
            }
        }
        result = mask.apply(data, '{nested}')
        self.assertEqual(result, {'nested': {
            'integer': 42,
            'string': 'a string',
            'boolean': True,
        }})

Example 8

Project: flask-restplus Source File: test_fields_mask.py
Function: test_nested_fields
    def test_nested_fields(self):
        data = {
            'nested': {
                'integer': 42,
                'string': 'a string',
                'boolean': True,
            }
        }
        result = mask.apply(data, '{nested{integer}}')
        self.assertEqual(result, {'nested': {'integer': 42}})

Example 9

Project: flask-restplus Source File: test_fields_mask.py
    def test_nested_with_start(self):
        data = {
            'nested': {
                'integer': 42,
                'string': 'a string',
                'boolean': True,
            },
            'other': 'value',
        }
        result = mask.apply(data, '{nested{integer},*}')
        self.assertEqual(result, {'nested': {'integer': 42}, 'other': 'value'})

Example 10

Project: flask-restplus Source File: test_fields_mask.py
    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}}

        self.assertDataEqual(marshal(data, result), expected)
        # Should leave th original mask untouched
        self.assertDataEqual(marshal(data, family_fields), data)

Example 11

Project: flask-restplus Source File: test_fields_mask.py
    def test_list_fields_with_simple_field(self):
        family_fields = {
            'name': fields.String,
            'members': fields.List(fields.String)
        }

        result = mask.apply(family_fields, 'members')
        self.assertEqual(set(result.keys()), set(['members']))
        self.assertIsInstance(result['members'], fields.List)
        self.assertIsInstance(result['members'].container, fields.String)

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

        self.assertDataEqual(marshal(data, result), expected)
        # Should leave th original mask untouched
        self.assertDataEqual(marshal(data, family_fields), data)

Example 12

Project: flask-restplus Source File: test_fields_mask.py
    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'}]}

        self.assertDataEqual(marshal(data, result), expected)
        # Should leave th original mask untouched
        self.assertDataEqual(marshal(data, family_fields), data)

Example 13

Project: flask-restplus Source File: test_fields_mask.py
Function: test_list
    def test_list(self):
        data = [{
            'integer': 42,
            'string': 'a string',
            'boolean': True,
        }, {
            'integer': 404,
            'string': 'another string',
            'boolean': False,
        }]
        result = mask.apply(data, '{integer, string}')
        self.assertEqual(result, [
            {'integer': 42, 'string': 'a string'},
            {'integer': 404, 'string': 'another string'}
        ])

Example 14

Project: flask-restplus Source File: test_fields_mask.py
Function: test_nested_list
    def test_nested_list(self):
        data = {
            'integer': 42,
            'list': [{
                'integer': 42,
                'string': 'a string',
            }, {
                'integer': 404,
                'string': 'another string',
            }]
        }
        result = mask.apply(data, '{list}')
        self.assertEqual(result, {'list': [{
            'integer': 42,
            'string': 'a string',
        }, {
            'integer': 404,
            'string': 'another string',
        }]})

Example 15

Project: flask-restplus Source File: test_fields_mask.py
    def test_nested_list_fields(self):
        data = {
            'list': [{
                'integer': 42,
                'string': 'a string',
            }, {
                'integer': 404,
                'string': 'another string',
            }]
        }
        result = mask.apply(data, '{list{integer}}')
        self.assertEqual(result, {'list': [{'integer': 42}, {'integer': 404}]})

Example 16

Project: flask-restplus Source File: test_fields_mask.py
    def test_nested_fields_when_none(self):
        data = {'nested': None}
        result = mask.apply(data, '{nested{integer}}')
        self.assertEqual(result, {'nested': None})

Example 17

Project: flask-restplus Source File: test_fields_mask.py
    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}')
        self.assertEqual(set(result.keys()), set(['father', 'mother']))
        self.assertIsInstance(result['father'], fields.Nested)
        self.assertEqual(set(result['father'].nested.keys()), set(['name']))
        self.assertIsInstance(result['mother'], fields.Nested)
        self.assertEqual(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}}

        self.assertDataEqual(marshal(data, result), expected)
        # Should leave th original mask untouched
        self.assertDataEqual(marshal(data, family_fields), data)

Example 18

Project: flask-restplus Source File: test_fields_mask.py
    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}}}')
        self.assertEqual(set(result.keys()), set(['nested']))
        self.assertIsInstance(result['nested'], fields.Nested)
        self.assertEqual(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'}
                }
            }
        }

        self.assertDataEqual(marshal(data, result), expected)
        # Should leave th original mask untouched
        self.assertDataEqual(marshal(data, root), data)

Example 19

Project: flask-restplus Source File: test_fields_mask.py
    def test_list_fields_with_nested(self):
        family_fields = {
            'members': fields.List(fields.Nested(person_fields))
        }

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

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

        self.assertDataEqual(marshal(data, result), expected)
        # Should leave th original mask untouched
        self.assertDataEqual(marshal(data, family_fields), data)

Example 20

Project: flask-restplus Source File: test_fields_mask.py
    def test_list_fields_with_nested_inherited(self):
        api = Api(self.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'},
        ]}

        self.assertDataEqual(marshal(data, result), expected)
        # Should leave th original mask untouched
        self.assertDataEqual(marshal(data, family), data)

Example 21

Project: flask-restplus Source File: test_fields_mask.py
    def test_missing_field_none_by_default(self):
        result = mask.apply({}, '{integer}')
        self.assertEqual(result, {'integer': None})

Example 22

Project: flask-restplus Source File: test_fields_mask.py
    def test_missing_field_skipped(self):
        result = mask.apply({}, '{integer}', skip=True)
        self.assertEqual(result, {})

Example 23

Project: flask-restplus Source File: test_fields_mask.py
    def test_missing_nested_field_skipped(self):
        result = mask.apply({}, 'nested{integer}', skip=True)
        self.assertEqual(result, {})