def test_step_two_create_table(app): db = Database(app, auto_migrate=False) db.define_models(StepTwoThing) ops = _make_ops(db) op = ops.ops[0] sql = _make_sql(db, op) assert sql == _step_two_sql
def db(): app = App(__name__) db = Database( app, config=sdict( uri='sqlite:memory', auto_migrate=True, auto_connect=True)) db.define_models(Register) return db
def generator(*models): rv = Database(app) rv.define_models(*models) migration = generate_runtime_migration(rv) with rv.connection(): migration.up() request.addfinalizer(_db_teardown_generator(rv, migration)) return rv
def test_step_one_create_table(app): db = Database(app, auto_migrate=False) db.define_models(StepOneThing) ops = _make_ops(db) diffs = ops.as_diffs() assert len(diffs) == 1 and diffs[0][0] == "add_table" op = ops.ops[0] sql = _make_sql(db, op) assert sql == _step_one_sql
def db(): app = App(__name__) db = Database(app, config=sdict(uri='sqlite://validators.db', auto_connect=True, auto_migrate=True)) db.define_models([ A, AA, AAA, B, Consist, Len, Inside, Num, Eq, Match, Anyone, Proc, Person, Thing, Allowed, Mixed ]) return db
def db(): app = App(__name__) db = Database(app, config=sdict(uri='sqlite://dal.db', auto_connect=True, auto_migrate=True)) db.define_models([ Stuff, Person, Thing, Feature, Price, Doctor, Patient, Appointment, User, Organization, Membership, House, Mouse, NeedSplit, Zoo, Animal, Elephant, Dog, Subscription ]) return db
def db(): app = App(__name__) db = Database(app, config=sdict(uri='sqlite:memory', auto_migrate=True, auto_connect=False)) return db
def _db(app): try: shutil.rmtree(os.path.join(app.root_path, 'databases')) except: pass db = Database(app, auto_migrate=True) app.pipeline.append(db.pipe) return db
def test_step_three_drop_column(app): db = Database(app, auto_migrate=False) db.define_models(StepThreeThingTwo) ops = _make_ops(db) db2 = Database(app, auto_migrate=False) db2.define_models(StepThreeThingThree) ops2 = _make_ops(db2, ops) op = ops2.ops[0] sql = _make_sql(db, op) assert sql == _step_three_sql_drop
def test_step_four_alter_table(app): db = Database(app, auto_migrate=False) db.define_models(StepFourThing) ops = _make_ops(db) db2 = Database(app, auto_migrate=False) db2.define_models(StepFourThingEdit) ops2 = _make_ops(db2, ops) sql = [] for op in ops2.ops: sql.append(_make_sql(db2, op)) assert "\n".join(sql) == _step_four_sql
def test_step_one_drop_table(app): db = Database(app, auto_migrate=False) db.define_models(StepOneThing) ops = _make_ops(db) db2 = Database(app, auto_migrate=False) db2.define_models() ops2 = _make_ops(db2, ops) diffs = ops2.as_diffs() assert len(diffs) == 1 and diffs[0][0] == "remove_table" op = ops2.ops[0] sql = _make_sql(db2, op) assert sql == _step_one_sql_drop
def test_step_five_indexes(app): db = Database(app, auto_migrate=False) db.define_models(StepFiveThing) ops = _make_ops(db) index_ops = ops.ops[1:] for op in index_ops: sql = _make_sql(db, op) assert sql in _step_five_sql_before db2 = Database(app, auto_migrate=False) db2.define_models(StepFiveThingEdit) ops2 = _make_ops(db2, ops) for op in ops2.ops: sql = _make_sql(db, op) assert sql in _step_five_sql_after
fields_rw = {'user': False, 'date': False} class Comment(Model): belongs_to('user', 'post') text = Field.text() date = Field.datetime() default_values = {'user': lambda: session.auth.user.id, 'date': now} validation = {'text': {'presence': True}} fields_rw = {'user': False, 'post': False, 'date': False} #: init db and auth db = Database(app) auth = Auth(app, db, user_model=User) db.define_models(Post, Comment) #: setup helping function def setup_admin(): with db.connection(): # create the user user = User.create(email="*****@*****.**", first_name="Emmett", last_name="Brown", password="******") # create an admin group admins = auth.create_group("admin") # add user to admins group
def raw_db(request, app): rv = Database(app) return rv
default_values = { 'user': lambda: session.auth.user.id, 'date': now } validation = { 'text': {'presence': True} } fields_rw = { 'user': False, 'post': False, 'date': False } #: init db and auth db = Database(app, auto_migrate=True) auth = Auth(app, db, user_model=User) db.define_models(Post, Comment) #: setup helping function def setup_admin(): with db.connection(): # create the user user = User.create( email="*****@*****.**", first_name="Walter", last_name="Bishop", password="******" ) # create an admin group
from emmett_rest import REST app = App(__name__) app.use_extension(REST) app.config.REST.default_pagesize = 200 app.config.REST.min_pagesize = 10 app.config.REST.max_pagesize = 5000 # Config # app.config_from_yaml('app.yml') # app.config_from_yaml('db.yml', 'db') # app.config_from_yaml('mailer.yml', 'mailer') # app.config_from_yaml('auth.yml', 'auth') app.config.db.uri = 'sqlite://database.sqlite' db = Database(app, auto_migrate=False) # mailer = Mailer(app) auth = Auth(app, db, user_model=User) db.define_models([User, Post, Subscription]) app.pipeline = [ SessionManager.cookies('b795-18a878ae9368', expire=4 * 60 * 60), db.pipe, auth.pipe, ] auth_routes = auth.module(__name__) from backend.controllers import *
message = Field.string() @rowmethod('serialize') def _serialize(self, row): return {'id': row.id, 'message': row.message} app.config.handle_static = False app.config.db.adapter = 'postgres:psycopg2' app.config.db.host = 'tfb-database' app.config.db.user = '******' app.config.db.password = '******' app.config.db.database = 'hello_world' app.config.db.pool_size = 10 db = Database(app) db.define_models(World, Fortune) @app.route() @service.json async def json(): return {'message': 'Hello, World!'} @app.route("/db", pipeline=[db.pipe]) @service.json async def get_random_world(): return World.get(randint(1, 10000)).serialize()
def test_step_one_no_diff_in_migration(app): db = Database(app, auto_migrate=False) db.define_models(StepOneThing) ops = _make_ops(db) ops2 = _make_ops(db, ops) assert len(ops2.as_diffs()) == 0