Beispiel #1
0
def test_openapi_other_type(client):
    rest = UnRest(client.app, client.session, framework=client.__framework__)
    rest(
        Tree,
        properties=[
            rest.Property('fake_prop_1', type=Boolean()),
            rest.Property('fake_prop_2', type=INET()),
        ],
    )
    code, json = client.fetch('/api/openapi.json')
    assert code == 200
    assert json['paths']['/tree']['get']['responses']['200']['content'][
        'application/json']['schema']['properties']['objects']['items'][
            'properties'] == {
                'fake_prop_1': {
                    'type': 'boolean'
                },
                'fake_prop_2': {
                    'type': 'string'
                },
                'id': {
                    'format': 'int64',
                    'type': 'integer'
                },
                'name': {
                    'type': 'string'
                },
            }
Beispiel #2
0
def test_endpoint_options_other_type(client):
    rest = UnRest(client.app, client.session, framework=client.__framework__)
    rest(
        Tree,
        properties=[
            rest.Property('fake_prop_1', type=Boolean()),
            rest.Property('fake_prop_2', type=INET()),
        ],
    )
    code, json = client.fetch('/api/tree', method="OPTIONS")
    assert code == 200
    assert {
        'model': 'Tree',
        'description': "Where money doesn't grow",
        'parameters': ['id'],
        'columns': {
            'id': 'int',
            'name': 'str'
        },
        'properties': {
            'fake_prop_1': 'bool',
            'fake_prop_2': 'INET'
        },
        'relationships': {},
        'methods': ['GET', 'OPTIONS'],
        'batch': False,
    }
Beispiel #3
0
def test_sub_fixed(client):
    rest = UnRest(client.app, client.session, framework=client.__framework__)

    fruit = rest(
        Fruit,
        defaults={'size': 1.0},
        fixed={'color': 'blue'},
        methods=rest.all,
        properties=[rest.Property('square_size', Float())],
    )
    subfruit = fruit.sub(lambda q: q.filter(Fruit.age == 2.0))
    for key in ['defaults', 'fixed']:
        assert getattr(subfruit, key) == getattr(fruit, key)
Beispiel #4
0
def test_sub(client):
    rest = UnRest(client.app, client.session, framework=client.__framework__)

    fruit = rest(
        Fruit,
        methods=rest.all,
        properties=[rest.Property('square_size', Float())],
    )
    tree = rest(
        Tree,
        methods=rest.all,
        relationships={'fruits': fruit},
        properties=['fruit_colors'],
        query=lambda q: q.filter(Tree.name != 'pine'),
        allow_batch=True,
    )
    subtree = tree.sub(lambda q: q.filter(Tree.name != 'oak'))
    for key in [
            'unrest',
            'Model',
            'methods',
            'only',
            'exclude',
            'properties',
            'relationships',
            'allow_batch',
            'auth',
            'read_auth',
            'write_auth',
            'validators',
            '_primary_keys',
            'SerializeClass',
            'DeserializeClass',
    ]:
        assert getattr(subtree, key) == getattr(tree, key)
    assert subtree.name == 'subtree'

    code, json = client.fetch('/api/tree')
    assert code == 200
    assert json['occurences'] == 2

    code, json = client.fetch('/api/subtree')
    assert code == 200
    assert json['occurences'] == 1

    subtree = tree.sub(lambda q: q.filter(Tree.name != 'maple'),
                       name='nomaple')
    code, json = client.fetch('/api/nomaple')
    assert code == 200
    assert json['occurences'] == 1
Beispiel #5
0
def test_openapi(client):
    rest = UnRest(
        client.app,
        client.session,
        info={
            'description': '''# Unrest demo
This is the demo of unrest api.
This api expose the `Tree` and `Fruit` entity Rest methods.
''',
            'contact': {
                'name': __about__.__author__,
                'url': __about__.__uri__,
                'email': __about__.__email__,
            },
            'license': {
                'name': __about__.__license__
            },
        },
        framework=client.__framework__,
    )
    fruit = rest(
        Fruit,
        methods=rest.all,
        properties=[rest.Property('square_size', Float())],
    )
    rest(
        Tree,
        methods=rest.all,
        relationships={'fruits': fruit},
        properties=['fruit_colors'],
        allow_batch=True,
    )
    code, json = client.fetch('/api/openapi.json')
    # Only flask supports absolute url
    if 'Flask' not in client.__framework__.__name__:
        res = {**openapi, 'servers': [{'url': '/api'}]}
    else:
        res = openapi
    assert code == 200
    assert json == res
Beispiel #6
0
if not os.path.exists(sqlite_db):
    Base.metadata.create_all(bind=engine)
    fill_data(session)
    session.remove()


@app.route("/")
async def home(request):
    return response.text("A normal sanic route!")


@app.middleware('response')
async def after_request(request, response):
    session.remove()


rest = UnRest(app, session, framework=SanicFramework)
fruit = rest(
    Fruit, methods=rest.all, properties=[rest.Property('square_size', Float())]
)
rest(
    Tree,
    methods=rest.all,
    relationships={'fruits': fruit},
    properties=['fruit_colors'],
    allow_batch=True,
)

app.run()
Beispiel #7
0
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{sqlite_db}'
app.debug = True

db = SQLAlchemy(app)

if not os.path.exists(sqlite_db):
    Base.metadata.create_all(bind=db.engine)
    fill_data(db.session)
    db.session.remove()


@app.route("/")
def home():
    return "A normal flask route!"


rest = UnRest(app, db.session, idiom=JsonServerIdiom)
fruit = rest(Fruit,
             methods=rest.all,
             properties=[rest.Property('square_size', Float())])
rest(
    Tree,
    methods=rest.all,
    relationships={'fruits': fruit},
    properties=['fruit_colors'],
    allow_batch=True,
)

app.run()