def full_dispatch_request(self):
     """actual dispatch request, validate inputs and outputs
     """
     fn = getattr(self, g.action, None)
     if fn is None:
         abort(404, 'Unimplemented action %r' % g.action)
     inputs = self.__class__.schema_inputs.get(g.action)
     outputs = self.__class__.schema_outputs.get(g.action)
     output_types = self.__class__.output_types
     if inputs is not None:
         (errors, values) = validate(g.request_data, inputs)
         if errors:
             return dict(errors), 400
         else:
             if isinstance(values, dict):
                 rv = fn(**values)
             else:
                 rv = fn(values)
     else:
         rv = fn()
     rv, code, headers = unpack(rv)
     if outputs is not None:
         (errors, values) = validate(rv, outputs, proxy_types=output_types)
         if errors:
             return dict(errors), 500
         else:
             rv = values
     return rv, code, headers
Exemple #2
0
def test_custom_validaters():
    assert "year" in my_validaters
    assert "list" in my_validaters
    assert "abs" in my_validaters

    # parse schema with custom_validaters
    parse([{"year": "year&required"}], my_validaters)

    data = {
        'year': ['2016', '1984'],
        'list': [[], [None], [123, 456]],
        'abs': [1, -1, 0, -100]
    }
    expect = {
        'year': ['2016', '1984'],
        'list': [[], [None], [123, 456]],
        'abs': [1, 1, 0, 100]
    }
    for sche, objs in data.items():
        schema = parse(sche, my_validaters)
        for i, obj in enumerate(objs):
            err, val = validate(obj, schema)
            assert not err
            assert val == expect[sche][i]

    with pytest.raises(Exception):
        sche = parse('abs&required&debug', my_validaters)
        validate(object(), sche)

    remove_validater("year", my_validaters)
    remove_validater("list", my_validaters)
    remove_validater("abs", my_validaters)
    assert "year" not in my_validaters
    assert "list" not in my_validaters
    assert "abs" not in my_validaters
Exemple #3
0
def test_validate_deep_schema():
    sche = parse({"user": {'userid': 'int&required'}})
    err, val = validate({"user": {"userid": "123"}}, sche)
    assert not err
    assert val == {"user": {"userid": 123}}

    err, val = validate({"user": {"userid": None}}, sche)
    assert err and "required" in dict(err)["user.userid"]
    assert val == {"user": {"userid": None}}
Exemple #4
0
def test_date():
    dt = datetime.datetime(2016, 1, 1)
    d = datetime.datetime(2016, 1, 1).date()
    s = "2016-01-01"
    err, val = validate(d, parse("date&output"))
    assert not err
    assert val == s
    err, val = validate(dt, parse("date&output"))
    assert not err
    assert val == s
    err, val = validate(s, parse("date&output"))
    assert not err
    assert val == s

    err, val = validate(d, parse("date&input"))
    assert not err
    assert val == d
    err, val = validate(dt, parse("date&input"))
    assert not err
    assert val == d
    err, val = validate(s, parse("date&input"))
    assert not err
    assert val == d

    err, val = validate(d, parse("date"))
    assert not err
    assert val == s
    err, val = validate(dt, parse("date"))
    assert not err
    assert val == s
    err, val = validate(s, parse("date"))
    assert not err
    assert val == d
Exemple #5
0
def test_validate_single_schema():
    sche = parse('int&required')
    err, val = validate('123', sche)
    assert not err
    assert val == 123
    err, val = validate(None, sche)
    assert err and 'required' in dict(err)['']
    assert val is None
    err, val = validate('abc', sche)
    assert err and 'int' in dict(err)['']
    assert val is None
Exemple #6
0
def test_validate_schema_callable_default():
    import random
    num = random.randint(0, 1)
    sche = parse({'validater': 'int', 'args': (0, 1), 'default': lambda: num})
    err, val = validate(None, sche)
    assert not err
    assert val == num
Exemple #7
0
def test_validate_simple_schema():
    sche = parse({'userid': 'int&required'})
    err, val = validate({'userid': '123'}, sche)
    assert not err
    assert val['userid'] == 123

    err, val = validate({'userid': None}, sche)
    assert err and 'required' in dict(err)['userid']
    assert val == {'userid': None}

    err, val = validate({}, sche)
    assert err and 'required' in dict(err)['userid']
    assert val == {'userid': None}

    err, val = validate(None, sche)
    assert err and 'must be dict' in dict(err)['']
    assert val == {}
Exemple #8
0
def test_validate_list_schema():
    sche = parse({'userid': ['int&required']})
    err, val = validate({'userid': []}, sche)
    assert not err
    assert val == {'userid': []}

    err, val = validate({'userid': ['123', '456']}, sche)
    assert not err
    assert val == {'userid': [123, 456]}

    err, val = validate({'userid': ['x123', 'x456']}, sche)
    assert err and set(['userid[0]', 'userid[1]']) == set(dict(err))
    assert val == {'userid': [None, None]}

    err, val = validate({}, sche)
    assert err and 'must be list' in dict(err)['userid']
    assert val == {'userid': []}

    err, val = validate({'userid': '123'}, sche)
    assert err and 'must be list' in dict(err)['userid']
    assert val == {'userid': []}
 def full_dispatch_request(self, *args, **kwargs):
     """actual dispatch request, validate inputs and outputs
     """
     fn = getattr(self, request.action, None)
     if fn is None:
         abort(404, 'Unimplemented action %r' % fn.name)
     inputs = self.__class__.schema_inputs.get(request.action)
     outputs = self.__class__.schema_outputs.get(request.action)
     output_types = self.__class__.output_types
     method = request.method.lower()
     if inputs is not None:
         if method in ["get", "delete"]:
             data = request.args.copy()
         elif method in ["post", "put"]:
             if request.headers["Content-Type"] == 'application/json':
                 try:
                     data = request.get_json()
                 except:
                     abort(400, "Invalid json content")
             else:
                 data = request.form.copy()
         else:
             data = {}
         (errors, values) = validate(data, inputs)
         if errors:
             abort(400, dict(errors))
         else:
             rv = fn(**values)
     else:
         rv = fn()
     if outputs is not None:
         if output_types and isinstance(rv, tuple(output_types)):
             (errors, values) = validate(ProxyDict(rv, output_types), outputs)
         else:
             (errors, values) = validate(rv, outputs)
         if errors:
             abort(500, dict(errors))
         else:
             rv = values
     return rv
Exemple #10
0
def test_validate_empty_value():
    # empty value is not always None, depends on validater
    # string type's empty value is ""
    err, val = validate(None, parse('str'))
    assert not err
    assert val == str("")

    err, val = validate(None, parse('unicode'))
    assert not err
    assert val == ""

    err, val = validate(None, parse('url'))
    assert not err
    assert val == ""

    # treat empty string as None
    err, val = validate(str(''), parse('str&required'))
    assert err and err == [('', 'required')]
    assert val == str("")

    err, val = validate('', parse('unicode&required'))
    assert err and err == [('', 'required')]
    assert val == ""

    err, val = validate('', parse('url&required'))
    assert err and err == [('', 'required')]
    assert val == ""
Exemple #11
0
def validate(batch, remote, debug, dependency = []):
    params = cache.get("batch/%s/params" % batch, remote)
    numEpisodes = params['episodes']['num']
    
    i_ = range(numEpisodes)
    f = lambda i : validater.validate(batch, params, i, remote, debug)
    
    logging.info("running %s validate instances" % len(i_))
    if (remote):
        k_ = cloud.map(f, i_, _label = "%s/validate" % batch, _depends_on = dependency, _type = 'c1', _max_runtime = 30)
        logging.info("k_ %s" % k_)
        return k_
    else:
        results = map(f, i_)
        return results
Exemple #12
0
def test_validate_custom_types():
    class User(object):

        def __init__(self, userid):
            self.userid = userid

    sche = parse({
        'userid': "int&required",
        'friends': [{'userid': "int&required"}]})
    jack, f1, f2 = User(0), User(1), User(2)
    jack.friends = [f1, f2]
    err, val = validate(jack, sche, proxy_types=[User])
    assert not err
    assert val == {'userid': 0,
                   'friends': [{'userid': 1}, {'userid': 2}]}
Exemple #13
0
def test_custom_validaters():
    api = Api()

    def foo_validater(v):
        return (True, v)
    api.validater.add_validater("foo", foo_validater)
    assert "foo" in api.validater.validaters
    assert "foo" not in validater.default_validaters
    sche = api.validater.parse("foo&required")
    err, val = validater.validate('haha', sche)
    assert not err
    assert val == 'haha'
    err, val = api.validater.validate('haha', sche)
    assert not err
    assert val == 'haha'
    api.validater.remove_validater("foo")
    assert "foo" not in api.validater.validaters
Exemple #14
0
def test_datetime():
    d = datetime.datetime(2016, 1, 1)
    s = "2016-01-01T00:00:00.000000Z"
    err, val = validate(d, parse("datetime&output"))
    assert not err
    assert val == s
    err, val = validate(s, parse("datetime&output"))
    assert not err
    assert val == s

    err, val = validate(d, parse("datetime&input"))
    assert not err
    assert val == d
    err, val = validate(s, parse("datetime&input"))
    assert not err
    assert val == d

    err, val = validate(d, parse("datetime"))
    assert not err
    assert val == s
    err, val = validate(s, parse("datetime"))
    assert not err
    assert val == d
Exemple #15
0
def test_validate_simple_schema_has_default_value():
    sche = parse({'userid': 'int&required&default=0'})
    err, val = validate({'userid': None}, sche)
    assert not err
    assert val == {'userid': 0}
    err, val = validate({}, sche)
    assert not err
    assert val == {'userid': 0}

    sche = parse({'userid': 'int&default=0'})
    err, val = validate({'userid': None}, sche)
    assert not err
    assert val == {'userid': 0}
    err, val = validate({}, sche)
    assert not err
    assert val == {'userid': 0}

    sche = parse({'userid': 'int&default=None'})
    err, val = validate({'userid': None}, sche)
    assert not err
    assert val == {'userid': None}
    err, val = validate({}, sche)
    assert not err
    assert val == {'userid': None}
Exemple #16
0
def test_validate_err():
    for sche, v in data_err.items():
        sche = parse(sche + "&required")
        for x in v:
            err, val = validate(x, sche)
            assert err, "%s: %s" % (sche, x)
Exemple #17
0
def test_validate_deep_list_schema_error():
    sche = parse([{'nums': ['int']}])
    err, val = validate([{'nums': ['x123', 'x456']}, {'nums': 'x'}, 'x'], sche)
    expect = set(['[0].nums[0]', '[0].nums[1]', '[1].nums', '[2]'])
    assert expect == set(dict(err))