Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
class StepFiveThing(Model):
name = Field()
value = Field.int()
created_at = Field.datetime()
indexes = {
'name': True,
('name', 'value'): True
}
class StepFiveThingEdit(StepFiveThing):
tablename = "step_five_things"
indexes = {
'name': False,
'name_created': {
'fields': 'name',
'expressions': lambda m: m.created_at.coalesce(None)}
ops2 = _make_ops(db2, ops)
op = ops2.ops[0]
sql = _make_sql(db, op)
assert sql == _step_three_sql_drop
class StepFourThing(Model):
name = Field(notnull=True)
value = Field.float(default=8.8)
available = Field.bool(default=True)
asd = Field()
class StepFourThingEdit(Model):
tablename = "step_four_things"
name = Field()
value = Field.float()
available = Field.bool(default=True)
asd = Field.int()
_step_four_sql = """ALTER TABLE "step_four_things" ALTER COLUMN "name" DROP NOT NULL;
ALTER TABLE "step_four_things" ALTER COLUMN "value" DROP DEFAULT;
ALTER TABLE "step_four_things" ALTER COLUMN "asd" TYPE INTEGER;"""
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)
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
class StepFourThing(Model):
name = Field(notnull=True)
value = Field.float(default=8.8)
available = Field.bool(default=True)
asd = Field()
class StepFourThingEdit(Model):
tablename = "step_four_things"
name = Field()
value = Field.float()
available = Field.bool(default=True)
asd = Field.int()
_step_four_sql = """ALTER TABLE "step_four_things" ALTER COLUMN "name" DROP NOT NULL;
ALTER TABLE "step_four_things" ALTER COLUMN "value" DROP DEFAULT;
ALTER TABLE "step_four_things" ALTER COLUMN "asd" TYPE INTEGER;"""
def test_step_four_alter_table(app):
db = Database(app, auto_migrate=False)
assert sql == _step_two_sql
class StepThreeThingOne(Model):
a = Field()
class StepThreeThingTwo(Model):
tablename = "step_three_thing_ones"
a = Field()
b = Field()
class StepThreeThingThree(Model):
tablename = "step_three_thing_ones"
b = Field()
_step_three_sql = 'ALTER TABLE "step_three_thing_ones" ADD "b" CHAR(512);'
_step_three_sql_drop = 'ALTER TABLE "step_three_thing_ones" DROP COLUMN "a";'
def test_step_three_create_column(app):
db = Database(app, auto_migrate=False)
db.define_models(StepThreeThingOne)
ops = _make_ops(db)
db2 = Database(app, auto_migrate=False)
db2.define_models(StepThreeThingTwo)
ops2 = _make_ops(db2, ops)
op = ops2.ops[0]
sql = _make_sql(db, op)
assert sql == _step_three_sql
----------------------
Test pyDAL transactions implementation over Emmett.
:copyright: (c) 2014-2019 by Giovanni Barillari
:license: BSD, see LICENSE for more details.
"""
import pytest
from emmett import App, sdict
from emmett.orm import Database, Field, Model
class Register(Model):
value = Field.int()
@pytest.fixture(scope='module')
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
@pytest.fixture(scope='function')
def cleanup(request, db):
def teardown():
Register.all().delete()
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
class StepThreeThingOne(Model):
a = Field()
class StepThreeThingTwo(Model):
tablename = "step_three_thing_ones"
a = Field()
b = Field()
class StepThreeThingThree(Model):
tablename = "step_three_thing_ones"
b = Field()
_step_three_sql = 'ALTER TABLE "step_three_thing_ones" ADD "b" CHAR(512);'
_step_three_sql_drop = 'ALTER TABLE "step_three_thing_ones" DROP COLUMN "a";'
def test_step_three_create_column(app):
db = Database(app, auto_migrate=False)
db.define_models(StepThreeThingOne)
ops = _make_ops(db)
db2 = Database(app, auto_migrate=False)
def login_fields(auth):
model = auth.models['user']
rv = {
'email': Field(
validation={'is': 'email', 'presence': True},
label=model.email.label),
'password': Field(
'password', validation=model.password._requires,
label=model.password.label)
}
if auth.ext.config.remember_option:
rv['remember'] = Field(
'bool', default=True,
label=auth.ext.config.messages['remember_button'])
return rv
def login_fields(auth):
model = auth.models['user']
rv = {
'email': Field(
validation={'is': 'email', 'presence': True},
label=model.email.label),
'password': Field(
'password', validation=model.password._requires,
label=model.password.label)
}
if auth.ext.config.remember_option:
rv['remember'] = Field(
'bool', default=True,
label=auth.ext.config.messages['remember_button'])
return rv
def password_change_fields(auth):
password_validation = auth.ext.config.models['user'].password._requires
rv = {
'old_password': Field(
'password', validation=password_validation,
label=auth.ext.config.messages['old_password']),
'new_password': Field(
'password', validation=password_validation,
label=auth.ext.config.messages['new_password']),
'new_password2': Field(
'password', label=auth.ext.config.messages['verify_password'])
}
return rv