def test_add_index_table_and_column_rename(self):
self.test_create_table()
peeweedbevolve.clear()
class SomeModel2(pw.Model):
some_field2 = pw.CharField(index=True, null=True, aka='some_field')
class Meta:
database = self.db
aka = 'somemodel'
self.evolve_and_check_noop()
self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel2'))), [(u'somemodel2', (u'id',), True), (u'somemodel2', (u'some_field2',), False)])
python类CharField()的实例源码
def test_drop_index_table_rename(self):
class SomeModel2(pw.Model):
some_field = pw.CharField(index=True, null=True)
class Meta:
database = self.db
self.evolve_and_check_noop()
self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel2'))), [(u'somemodel2', (u'id',), True), (u'somemodel2', (u'some_field',), False)])
peeweedbevolve.clear()
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
aka = 'somemodel2'
self.evolve_and_check_noop()
self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True),])
def test_add_unique(self):
self.test_create_table()
peeweedbevolve.clear()
class SomeModel(pw.Model):
some_field = pw.CharField(unique=True, null=True)
class Meta:
database = self.db
self.evolve_and_check_noop()
self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True), (u'somemodel', (u'some_field',), True)])
def test_add_multi_index(self):
self.test_create_table()
peeweedbevolve.clear()
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
indexes = (
(('id', 'some_field'), False),
)
self.evolve_and_check_noop()
self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True), (u'somemodel', (u'id',u'some_field'), False)])
def test_drop_multi_index(self):
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
indexes = (
(('id', 'some_field'), False),
)
self.evolve_and_check_noop()
self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True), (u'somemodel', (u'id',u'some_field'), False)])
peeweedbevolve.clear()
self.test_create_table()
self.assertEqual(sorted(peeweedbevolve.normalize_indexes(peeweedbevolve.get_indexes_by_table(self.db,'somemodel'))), [(u'somemodel', (u'id',), True),])
def test_add_column_default(self):
self.test_create_table()
peeweedbevolve.clear()
class SomeModel(pw.Model):
some_field = pw.CharField(null=True, default='woot2')
class Meta:
database = self.db
self.evolve_and_check_noop()
model = SomeModel.create()
self.assertEqual(model.some_field, 'woot2')
self.assertEqual(SomeModel.get(SomeModel.id==model.id).some_field, 'woot2')
def test_change_column_max_length(self):
class SomeModel(pw.Model):
some_field = pw.CharField(default='w', max_length=1)
class Meta:
database = self.db
self.evolve_and_check_noop()
peeweedbevolve.clear()
class SomeModel(pw.Model):
some_field = pw.CharField(default='woot', max_length=4)
class Meta:
database = self.db
self.evolve_and_check_noop()
model = SomeModel.create()
self.assertEqual(model.some_field, 'woot')
self.assertEqual(SomeModel.select().first().some_field, 'woot')
def test_ignore_new_model(self):
class SomeModel(pw.Model):
some_field = pw.CharField(null=True)
class Meta:
database = self.db
evolve = False
self.evolve_and_check_noop()
with self.assertRaises(pw.ProgrammingError):
# should fail because table does not exist
SomeModel.create(some_field='woot')
def test_related_name_collision(flushdb):
class Foo(TestModel):
f1 = CharField()
with pytest.raises(AttributeError):
class FooRel(TestModel):
foo = ForeignKeyField(Foo, related_name='f1')
def test_meta_remove_field():
class _Model(Model):
title = CharField(max_length=25)
content = TextField(default='')
_Model._meta.remove_field('content')
assert 'content' not in _Model._meta.fields
assert 'content' not in _Model._meta.sorted_field_names
assert [f.name for f in _Model._meta.sorted_fields] == ['id', 'title']
def main(config):
config = load_config(config)
database.init(**config['processing_database'])
database.connect()
migrator = MySQLMigrator(database)
run_type_key = IntegerField(null=True)
run_type_name = CharField(null=True)
migrate(
migrator.add_column('raw_data_files', 'run_type_key', run_type_key),
migrator.add_column('raw_data_files', 'run_type_name', run_type_name)
)
def folder_model(database):
class Folder(FieldSignatureMixin, ArchivedMixin):
# This class represents a Folder in a file system. Two Folders with
# the same name cannot exist in the same Folder. If the Folder has
# no Parent Folder, it exists in the top level of the file system.
name = peewee.CharField(max_length=255, null=False)
parent_folder = peewee.ForeignKeyField('self', null=True)
class Meta:
signature_fields = ('name', 'parent_folder')
Folder._meta.database = database.database
Folder.create_table(True)
return Folder
def post_model(database):
"""Fixture that provides a fixture for the Post model."""
class Post(SearchMixin, database.Model):
title = peewee.CharField(max_length=255, null=False)
body = peewee.TextField(null=False, default='')
Post._meta.database = database.database
Post.create_table(True)
return Post
def user_model(database):
"""Fixture that provides a simple User model."""
class User(Model):
email = peewee.CharField(max_length=100, null=False, unique=True)
password = PasswordField(null=False, iterations=4)
active = peewee.BooleanField(null=False, default=True)
@classmethod
def base_query(cls):
# Only query for active Users
return super(User, cls).base_query().where(cls.active == True)
try:
@pre_save(sender=User)
def validate_email(sender, instance, created):
# Ensure that the email is valid.
assert '@' in instance.email
@post_save(sender=User)
def send_welcome_email(sender, instance, created):
# Send the user an email when they are created initially.
if created:
print('Sending welcome email to {}'.format(instance.email))
except ValueError:
# This gets hit because you can't connect an event listener more than
# once and for some reason this fixture is sticking around.
pass
User.create_table(True)
yield User
def test_column_aliases():
tc = TableCreator('awesome')
tc.bare('col_bare')
tc.biginteger('col_biginteger')
tc.binary('col_binary')
tc.blob('col_blob')
tc.bool('col_bool')
tc.char('col_char')
tc.date('col_date')
tc.datetime('col_datetime')
tc.decimal('col_decimal')
tc.double('col_double')
tc.fixed('col_fixed')
tc.float('col_float')
tc.int('col_int')
tc.integer('col_integer')
tc.smallint('col_smallint')
tc.smallinteger('col_smallinteger')
tc.text('col_text')
tc.time('col_time')
tc.uuid('col_uuid')
assert isinstance(tc.model.col_bare, peewee.BareField)
assert isinstance(tc.model.col_biginteger, peewee.BigIntegerField)
assert isinstance(tc.model.col_binary, peewee.BlobField)
assert isinstance(tc.model.col_blob, peewee.BlobField)
assert isinstance(tc.model.col_bool, peewee.BooleanField)
assert isinstance(tc.model.col_char, peewee.CharField)
assert isinstance(tc.model.col_date, peewee.DateField)
assert isinstance(tc.model.col_datetime, peewee.DateTimeField)
assert isinstance(tc.model.col_decimal, peewee.DecimalField)
assert isinstance(tc.model.col_double, peewee.DoubleField)
assert isinstance(tc.model.col_fixed, peewee.CharField)
assert isinstance(tc.model.col_float, peewee.FloatField)
assert isinstance(tc.model.col_int, peewee.IntegerField)
assert isinstance(tc.model.col_integer, peewee.IntegerField)
assert isinstance(tc.model.col_smallint, peewee.SmallIntegerField)
assert isinstance(tc.model.col_smallinteger, peewee.SmallIntegerField)
assert isinstance(tc.model.col_text, peewee.TextField)
assert isinstance(tc.model.col_time, peewee.TimeField)
assert isinstance(tc.model.col_uuid, peewee.UUIDField)
def test_column():
tc = TableCreator('awesome')
tc.primary_key('id')
tc.column('bare', 'col_bare')
tc.column('biginteger', 'col_biginteger')
tc.column('binary', 'col_binary')
tc.column('blob', 'col_blob')
tc.column('bool', 'col_bool')
tc.column('char', 'col_char')
tc.column('date', 'col_date')
tc.column('datetime', 'col_datetime')
tc.column('decimal', 'col_decimal')
tc.column('double', 'col_double')
tc.column('fixed', 'col_fixed')
tc.column('float', 'col_float')
tc.column('int', 'col_int')
tc.column('integer', 'col_integer')
tc.column('smallint', 'col_smallint')
tc.column('smallinteger', 'col_smallinteger')
tc.column('text', 'col_text')
tc.column('time', 'col_time')
tc.column('uuid', 'col_uuid')
assert isinstance(tc.model.id, peewee.PrimaryKeyField)
assert isinstance(tc.model.col_bare, peewee.BareField)
assert isinstance(tc.model.col_biginteger, peewee.BigIntegerField)
assert isinstance(tc.model.col_binary, peewee.BlobField)
assert isinstance(tc.model.col_blob, peewee.BlobField)
assert isinstance(tc.model.col_bool, peewee.BooleanField)
assert isinstance(tc.model.col_char, peewee.CharField)
assert isinstance(tc.model.col_date, peewee.DateField)
assert isinstance(tc.model.col_datetime, peewee.DateTimeField)
assert isinstance(tc.model.col_decimal, peewee.DecimalField)
assert isinstance(tc.model.col_double, peewee.DoubleField)
assert isinstance(tc.model.col_fixed, peewee.CharField)
assert isinstance(tc.model.col_float, peewee.FloatField)
assert isinstance(tc.model.col_int, peewee.IntegerField)
assert isinstance(tc.model.col_integer, peewee.IntegerField)
assert isinstance(tc.model.col_smallint, peewee.SmallIntegerField)
assert isinstance(tc.model.col_smallinteger, peewee.SmallIntegerField)
assert isinstance(tc.model.col_text, peewee.TextField)
assert isinstance(tc.model.col_time, peewee.TimeField)
assert isinstance(tc.model.col_uuid, peewee.UUIDField)