def test_reqparse_string_length():
    no_validation = reqparse.string_length()
    assert no_validation('') == ''
    assert no_validation('a' * 25) == 'a' * 25

    yes_validation = reqparse.string_length(min_len=1, max_len=10)

    assert yes_validation('c3po') == 'c3po'

    with pytest.raises(ValueError) as excinfo:
        yes_validation('')

    assert 'must be at least 1 character long' == str(excinfo.value)

    with pytest.raises(ValueError) as excinfo:
        yes_validation('a' * 11)

    assert 'must be no more than 10 characters long' == str(excinfo.value)
def test_reqparse_string_length():
    no_validation = reqparse.string_length()
    assert no_validation('') == ''
    assert no_validation('a' * 25) == 'a' * 25

    yes_validation = reqparse.string_length(min_len=1, max_len=10)

    assert yes_validation('c3po') == 'c3po'

    with pytest.raises(ValueError) as excinfo:
        yes_validation('')

    assert 'must be at least 1 character long' == str(excinfo.value)

    with pytest.raises(ValueError) as excinfo:
        yes_validation('a' * 11)

    assert 'must be no more than 10 characters long' == str(excinfo.value)
from flask.ext.restful import \
    Resource, fields, marshal_with, reqparse
from google.appengine.ext import ndb
from app.models import name
from app.utils.reqparse import string_length
from app.utils.func import compose
# from app.utils.werkzeug_debugger import werkzeug_debugger

name_validation = compose(name.Name.ensure_name_not_in_datastore,
                          string_length(min_len=1, max_len=10))

request_parser = reqparse.RequestParser(trim=True,
                                        bundle_errors=True,
                                        namespace_class=dict)
request_parser.add_argument(
    'name',
    type=name_validation,
    # location='json',
    required=True)

response_body_schema = {'name': fields.String}


class Name(Resource):
    decorators = [marshal_with(response_body_schema)]

    def get(self):
        "return random Name"
        random_name = name.Name.random_key().get()
        return random_name
from flask.ext.restful import \
    Resource, fields, marshal_with, reqparse
from google.appengine.ext import ndb
from app.models import name
from app.utils.reqparse import string_length
from app.utils.func import compose
# from app.utils.werkzeug_debugger import werkzeug_debugger


name_validation = compose(
    name.Name.ensure_name_not_in_datastore,
    string_length(min_len=1, max_len=10)
)


request_parser = reqparse.RequestParser(
    trim=True,
    bundle_errors=True,
    namespace_class=dict
)
request_parser.add_argument(
    'name',
    type=name_validation,
    # location='json',
    required=True
)


response_body_schema = {
    'name': fields.String
}