Ejemplo n.º 1
0
async def handle_order_create(request):
    body = await request.json()
    try:
        validate(
            body, {
                'sender_name': field.String(empty=False),
                'amount': field.Integer(positive=True),
                'receiver_name': field.String(empty=False),
                'receiver_phone': field.Integer(),
                'receiver_addr': field.String(empty=False),
            })
    except InvalidInputError as e:
        print('fuckfuck')
        print(e)
        return web.Response(status=400)
    order_number = uuid.uuid4().hex
    new_order = {
        'order_number': order_number,
        'order_type': OrderType.APPLEJUICE,  # Default order type
        'sender_name': body['sender_name'],
        'amount': body['amount'],
        'receiver_name': body['receiver_name'],
        'receiver_phone': body['receiver_phone'],
        'receiver_addr': body['receiver_addr'],
    }
    query = (order.insert().values(new_order))
    result = request.app['db_engine'].execute(query)
    order_id = result.inserted_primary_key
    if order_id is None:
        return web.Response(status=500)
    return web.json_response({'id': order_id[0], 'order_number': order_number})
Ejemplo n.º 2
0
async def handle_article_create(request):
    body = await request.json()
    try:
        validate(
            body, {
                'title': field.String(),
                'board': field.Integer(nonnegative=True),
                'content': field.String(),
                'created_by': field.Integer(nonnegative=True)
            })
    except InvalidInputError as e:
        return web.Response(text=str(e), status=400)
    # TODO: validate inputs.
    new_article = {
        'title': body['title'],
        'board': body['board'],
        'content': body['content'],
        'created_by': body['created_by'],
    }
    query = (article.insert().values(new_article))
    result = request.app['db_engine'].execute(query)
    article_id = result.inserted_primary_key
    if not article_id:
        return web.Response(status=500)
    return web.json_response({'id': article_id[0]})
Ejemplo n.º 3
0
def test_validate_field():
    spec = field.Integer()

    assert validate_field(3, spec)

    with pytest.raises(InvalidInputError):
        validate_field('some-wrong-input', spec)
Ejemplo n.º 4
0
def test_integer_nonnegative():
    spec = field.Integer(nonnegative=True)

    assert spec.validate_field(3)

    assert spec.validate_field(0)
    assert spec.validate_field('0')

    with pytest.raises(AssertionError):
        spec.validate_field(-3)
    with pytest.raises(AssertionError):
        spec.validate_field('-3')
Ejemplo n.º 5
0
def test_integer_max():
    spec = field.Integer(max=3)

    assert spec.validate_field(3)
    assert spec.validate_field('3')

    assert spec.validate_field(2)
    assert spec.validate_field('2')

    with pytest.raises(AssertionError):
        spec.validate_field(6)
    with pytest.raises(AssertionError):
        spec.validate_field('6')

    assert spec.validate_field(0)
    assert spec.validate_field('0')
Ejemplo n.º 6
0
def test_validate():
    validation_spec = {
        'username': field.String(length=32),
        'password': field.String(length=32),
        'age': field.Integer(positive=True),
        'description': field.String(nullable=True),
    }

    body = {
        'username': '******',
        'password': '******',
        'age': 23,
    }
    assert validate(body, validation_spec)

    wrong_body = {
        'password': '******',
        'age': 23,
    }
    with pytest.raises(InvalidInputError):
        validate(wrong_body, validation_spec)
Ejemplo n.º 7
0
def test_integer_type():
    spec = field.Integer()

    assert spec.validate_field(3)

    assert spec.validate_field(3)
    assert spec.validate_field('3')

    assert spec.validate_field(0)
    assert spec.validate_field('0')

    assert spec.validate_field(-3)
    assert spec.validate_field('-3')

    with pytest.raises(AssertionError):
        spec.validate_field('haha')

    with pytest.raises(AssertionError):
        spec.validate_field('3.3')

    with pytest.raises(AssertionError):
        spec.validate_field(None)
Ejemplo n.º 8
0
def test_integer_nullable():
    spec = field.Integer(nullable=True)

    assert spec.validate_field(None)