def basic_app(): app, db = _app() class PersonDefault(db.Entity): name = Required(str) age = Optional(int) class Person(db.Entity): name = Required(str) age = Optional(int) class Meta: route_base = 'persons' route_prefix = '/api' _bind_generate_db(db) with db_session: p1 = Person(name="Doctor Aphra", age=20) p2 = Person(name="Faro Argyus", age=21) p3 = PersonDefault(name="Shara Bey", age=34) api = PonyAPI(app, db) yield app.test_client() os.remove("tests/test-database.sqlite")
def test_ponyapi_init_when_exclude_options_not_list(): app, db = _app() class User(db.Entity): name = Required(str) class Meta: exclude = ('secret', ) _bind_generate_db(db) with pytest.raises(InvalidMetaOption): PonyAPI(app, db) os.remove("tests/test-database.sqlite")
def features_app(): app, db = _app() class User(db.Entity): name = Required(str) age = Optional(int) secret = Optional(str) class Meta: route_base = 'users' route_prefix = '/api' exclude = ['secret'] _bind_generate_db(db) with db_session: u1 = User(name="Douglas Quaid", age=32, secret='Hauser') api = PonyAPI(app, db) yield app.test_client() os.remove("tests/test-database.sqlite")
def test_ponyapi_creation_not_list_of_entities(): app, db = _app() db = ('a', 'b') with pytest.raises(NotPonyDatabase): PonyAPI(app, db)
from flask import Flask from pony.orm import Database, Required, Optional, db_session from flask_ponyapi import PonyAPI app = Flask(__name__) db = Database() class Person(db.Entity): name = Required(str) age = Optional(int) class Meta: route_base = 'persons' route_prefix = '/api' api = PonyAPI(app, db) if __name__ == '__main__': db.bind(provider='sqlite', filename='database.sqlite', create_db=True) db.generate_mapping(create_tables=True) with db_session: p1 = Person(name='Almec', age=99) p2 = Person(name="Amee", age=100) app.run(debug=True)