Exemplo n.º 1
0
from apistar import app, routing, schema, test


def get_boolean(value: schema.Boolean):
    return {'value': value}


app = app.App(routes=[
    routing.Route('/boolean/', 'GET', get_boolean),
])


client = test.TestClient(app)


def test_boolean():
    assert schema.Boolean(1) is True
    assert schema.Boolean(0) is False
    assert schema.Boolean('1') is True
    assert schema.Boolean('0') is False
    assert schema.Boolean(True) is True
    assert schema.Boolean(False) is False
    assert schema.Boolean('TRUE') is True
    assert schema.Boolean('FALSE') is False


def test_valid_boolean():
    response = client.get('/boolean/?value=true')
    assert response.status_code == 200
    assert response.json() == {'value': True}
Exemplo n.º 2
0
def validate_length(value: MinMaxLength):
    return {'value': value}


def validate_not_blank(value: NotBlank):
    return {'value': value}


def validate_pattern(value: ValidPattern):
    return {'value': value}


app = app.App(routes=[
    routing.Route('/length/', 'GET', validate_length),
    routing.Route('/not_blank/', 'GET', validate_not_blank),
    routing.Route('/pattern/', 'GET', validate_pattern),
])

client = test.TestClient(app)


def test_valid_length():
    response = client.get('/length/?value=abcde')
    assert response.status_code == 200
    assert response.json() == {'value': 'abcde'}

    response = client.get('/length/?value=abcdefghij')
    assert response.status_code == 200
    assert response.json() == {'value': 'abcdefghij'}
Exemplo n.º 3
0

def basic_untyped_list(things: UntypedList):
    return things


def basic_typed_list(things: TypedList):
    return things


routes = [
    routing.Route('/basic_typed_list/', 'POST', basic_typed_list),
    routing.Route('/basic_untyped_list/', 'POST', basic_untyped_list),
]

app = app.App(routes=routes)
client = test.TestClient(app)


def test_valid_untyped_list():
    payload = [
        'foo',
        'bar',
        1,
        3.1415927,
        'baz',
        True,
        None,
        'qux',
        ['tribbles'] * 23,
        {
Exemplo n.º 4
0
    errors = {
        'exact': 'You must agree to the terms and conditions to proceed.'
    }
    enum = ['yes']


def validate_color(value: Color):
    return {'value': value}


def validate_terms(value: TermsAndConditions):
    return {'value': value}


app = app.App(routes=[
    routing.Route('/color/', 'GET', validate_color),
    routing.Route('/terms/', 'GET', validate_terms),
])

client = test.TestClient(app)


def test_valid_enum():
    response = client.get('/color/?value=red')
    assert response.status_code == 200
    assert response.json() == {'value': 'red'}


def test_valid_literal():
    response = client.get('/terms/?value=yes')
    assert response.status_code == 200
    assert response.json() == {'value': 'yes'}
Exemplo n.º 5
0

class MultipleNumber(schema.Number):
    multiple_of = 0.1


def get_min_max(value: MinMaxNumber):
    return {'value': value}


def get_multiple(value: MultipleNumber):
    return {'value': value}


app = app.App(routes=[
    routing.Route('/min_max/', 'GET', get_min_max),
    routing.Route('/multiple/', 'GET', get_multiple),
])

client = test.TestClient(app)


def test_invalid_numberr():
    response = client.get('/min_max/?value=a')
    assert response.status_code == 400
    assert response.json() == {'value': 'Must be a valid number.'}


def test_valid_min_max():
    response = client.get('/min_max/?value=2.9')
    assert response.status_code == 200
    assert response.json() == {'value': 2.9}