Example #1
0
    def test_model_deepcopy(self):
        parent = Model('Person', {
            'name': fields.String,
            'age': fields.Integer(description="foo"),
        })

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

        parent_copy = copy.deepcopy(parent)

        assert parent_copy["age"].description == "foo"

        parent_copy["age"].description = "bar"

        assert parent["age"].description == "foo"
        assert parent_copy["age"].description == "bar"

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

        child_copy = copy.deepcopy(child)
        assert child_copy.__parents__[0] == parent
Example #2
0
 def test_with_attribute(self):
     data = [{'a': 1, 'b': 1}, {'a': 2, 'b': 1}, {'a': 3, 'b': 1}]
     field = fields.List(fields.Integer(attribute='a'))
     self.assert_field(field, data, [1, 2, 3])
Example #3
0
from quart_restplus import Api, Resource, fields

app = Quart(__name__)
api = Api(
    app,
    version='1.0',
    title='TodoMVC API',
    description='A simple TodoMVC API',
)

ns = api.namespace('todos', description='TODO operations')

todo = api.model(
    'Todo', {
        'id':
        fields.Integer(readonly=True,
                       description='The task unique identifier'),
        'task':
        fields.String(required=True, description='The task details')
    })


class TodoDAO(object):
    def __init__(self):
        self.counter = 0
        self.todos = []

    def get(self, id):
        for todo in self.todos:
            if todo['id'] == id:
                return todo
        api.abort(404, "Todo {} doesn't exist".format(id))
Example #4
0
 def test_decode_error(self):
     field = fields.Integer()
     self.assert_field_raises(field, 'an int')
Example #5
0
 def test_value(self, value, expected):
     self.assert_field(fields.Integer(), value, expected)
Example #6
0
 def test_with_default(self):
     field = fields.Integer(default=42)
     assert not field.required
     assert field.__schema__ == {'type': 'integer', 'default': 42}
     self.assert_field(field, None, 42)
Example #7
0
 def test_defaults(self):
     field = fields.Integer()
     assert not field.required
     assert field.__schema__ == {'type': 'integer'}
Example #8
0
    def test_wildcard(self, api):
        wild1 = fields.Wildcard(fields.String)
        wild2 = fields.Wildcard(fields.Integer)
        wild3 = fields.Wildcard(fields.String)
        wild4 = fields.Wildcard(fields.String, default='x')
        wild5 = fields.Wildcard(fields.String)
        wild6 = fields.Wildcard(fields.Integer)
        wild7 = fields.Wildcard(fields.String)
        wild8 = fields.Wildcard(fields.String)

        mod5 = OrderedDict()
        mod5['toto'] = fields.Integer
        mod5['bob'] = fields.Integer
        mod5['*'] = wild5

        wild_fields1 = api.model('WildcardModel1', {'*': wild1})
        wild_fields2 = api.model('WildcardModel2', {'j*': wild2})
        wild_fields3 = api.model('WildcardModel3', {'*': wild3})
        wild_fields4 = api.model('WildcardModel4', {'*': wild4})
        wild_fields5 = api.model('WildcardModel5', mod5)
        wild_fields6 = api.model(
            'WildcardModel6', {
                'nested': {
                    'f1': fields.String(default='12'),
                    'f2': fields.Integer(default=13)
                },
                'a*': wild6
            })
        wild_fields7 = api.model('WildcardModel7', {'*': wild7})
        wild_fields8 = api.model('WildcardModel8', {'*': wild8})

        class Dummy(object):
            john = 12
            bob = '42'
            alice = None

        class Dummy2(object):
            pass

        class Dummy3(object):
            a = None
            b = None

        data = {'John': 12, 'bob': 42, 'Jane': '68'}
        data3 = Dummy()
        data4 = Dummy2()
        data5 = {'John': 12, 'bob': 42, 'Jane': '68', 'toto': '72'}
        data6 = {'nested': {'f1': 12, 'f2': 13}, 'alice': '14'}
        data7 = Dummy3()
        data8 = None
        expected1 = {'John': '12', 'bob': '42', 'Jane': '68'}
        expected2 = {'John': 12, 'Jane': 68}
        expected3 = {'john': '12', 'bob': '42'}
        expected4 = {'*': 'x'}
        expected5 = {'John': '12', 'bob': 42, 'Jane': '68', 'toto': 72}
        expected6 = {'nested': {'f1': '12', 'f2': 13}, 'alice': 14}
        expected7 = {}
        expected8 = {}

        result1 = api.marshal(data, wild_fields1)
        result2 = api.marshal(data, wild_fields2)
        result3 = api.marshal(data3, wild_fields3, skip_none=True)
        result4 = api.marshal(data4, wild_fields4)
        result5 = api.marshal(data5, wild_fields5)
        result6 = api.marshal(data6, wild_fields6)
        result7 = api.marshal(data7, wild_fields7, skip_none=True)
        result8 = api.marshal(data8, wild_fields8, skip_none=True)

        assert expected1 == result1
        assert expected2 == result2
        assert expected3 == result3
        assert expected4 == result4
        assert expected5 == result5
        assert expected6 == result6
        assert expected7 == result7
        assert expected8 == result8