def test_add_not_null_constraint_with_records_and_default(self): class SomeModel(pw.Model): some_field = pw.CharField(null=True) class Meta: database = self.db self.evolve_and_check_noop() x = SomeModel.create(some_field=None) y = SomeModel.create(some_field='not_null') peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.CharField(null=False, default='woot') class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual( SomeModel.select().where(SomeModel.id == x.id).first().some_field, 'woot') self.assertEqual( SomeModel.select().where(SomeModel.id == y.id).first().some_field, 'not_null')
def test_drop_unique(self): 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)]) 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_reorder_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() class SomeModel(pw.Model): some_field = pw.CharField(null=True) class Meta: database = self.db indexes = ((('some_field', 'id'), 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'some_field', u'id'), False)])
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 setUp(self): os.system( "createdb peeweedbevolve_test && psql peeweedbevolve_test -c 'create extension IF NOT EXISTS hstore;' > /dev/null 2> /dev/null" ) self.db = pwe.PostgresqlExtDatabase('peeweedbevolve_test') self.db.connect() peeweedbevolve.clear()
def test_drop_column(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): class Meta: database = self.db self.evolve_and_check_noop() self.assertFalse(hasattr(SomeModel.select().first(), 'some_field'))
def test_rename_column_aka_list(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): some_other_field = pw.CharField(null=True, aka=['some_field']) class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().some_other_field, 'woot')
def test_ignore_existing_table_evolve_command(self): class SomeModel(pw.Model): some_field = pw.TimeField(null=True) class Meta: database = self.db self.evolve_and_check_noop() peeweedbevolve.clear() self.assertEqual( peeweedbevolve.calc_changes(self.db, ignore_tables=['somemodel']), [])
def test_remove_not_null_constraint(self): self.test_add_not_null_constraint() peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.CharField(null=True) class Meta: database = self.db self.evolve_and_check_noop() SomeModel.create()
def test_rename_table_aka_string(self): self.test_create_table() peeweedbevolve.clear() class SomeOtherModel(pw.Model): some_field = pw.CharField(null=True) class Meta: database = self.db aka = 'somemodel' self.evolve_and_check_noop() self.assertEqual(SomeOtherModel.select().first().some_field, 'woot')
def test_add_column(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.CharField(null=True) another_field = pw.CharField(null=True) class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().another_field, None)
def test_drop_table(self): class SomeModel(pw.Model): some_field = pw.CharField(null=True) class Meta: database = self.db self.evolve_and_check_noop() SomeModel.create(some_field='woot') peeweedbevolve.clear() self.evolve_and_check_noop() with self.assertRaises(pw.ProgrammingError): SomeModel.create( some_field='woot2') # fails because table isn't there
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_rename_column_add_not_null_constraint(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): some_other_field = pw.CharField(null=False, aka='some_field') class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().some_other_field, 'woot') with self.db.atomic() as txn: self.assertRaises(Exception, lambda: SomeModel.create(some_other_field=None))
def test_add_index_column_rename(self): self.test_create_table() peeweedbevolve.clear() class SomeModel(pw.Model): some_field2 = pw.CharField(index=True, null=True, aka='some_field') class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual( sorted( peeweedbevolve.normalize_indexes( self.db.get_indexes('somemodel'))), [(u'somemodel', (u'id', ), True), (u'somemodel', (u'some_field2', ), False)])
def test_change_datetime_timezone(self): class SomeModel(pw.Model): some_field = pw.DateTimeField() class Meta: database = self.db self.evolve_and_check_noop() peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pwe.DateTimeTZField() class Meta: database = self.db self.evolve_and_check_noop()
def test_add_columns_with_false_defaults(self): class SomeModel(pw.Model): class Meta: database = self.db self.evolve_and_check_noop() SomeModel.create(some_field=None) peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.BooleanField(null=False, default=False) created_at = pw.DateTimeField(default=datetime.datetime.now) class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().some_field, False)
def test_add_not_null_constraint_with_records_and_false_default(self): class SomeModel(pw.Model): some_field = pw.BooleanField(null=True) class Meta: database = self.db self.evolve_and_check_noop() SomeModel.create(some_field=None) peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.BooleanField(null=False, default=False) class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().some_field, False)
def test_change_fixed_column_max_length(self): class SomeModel(pw.Model): some_field = pw.FixedCharField(default='w', max_length=1) class Meta: database = self.db self.evolve_and_check_noop() peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.FixedCharField(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_change_column_type_char_text(self): class SomeModel(pw.Model): some_field = pw.CharField(default='woot') class Meta: database = self.db self.evolve_and_check_noop() model = SomeModel.create() self.assertEqual(model.some_field, 'woot') peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.TextField(default='woot') class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().some_field, 'woot')
def test_change_column_type_float_decimal(self): class SomeModel(pw.Model): some_field = pw.FloatField(default=8) class Meta: database = self.db self.evolve_and_check_noop() model = SomeModel.create() self.assertEqual(model.some_field, 8) peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.DecimalField(default=8) class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().some_field, 8)
def test_dont_add_column(self): class SomeModel(pw.Model): some_field = pw.CharField(null=True) class Meta: database = self.db self.evolve_and_check_noop() peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.CharField(null=True) some_other_field = pw.CharField(null=False) class Meta: database = self.db evolve = False self.evolve_and_check_noop() # should not fail because the not-null column wasn't added SomeModel.create(some_field='woot')
def test_dont_drop_table(self): class SomeModel(pw.Model): some_field = pw.CharField(null=True) class Meta: database = self.db self.evolve_and_check_noop() SomeModel.create(some_field='woot') peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.CharField(null=True) class Meta: database = self.db evolve = False self.evolve_and_check_noop() # doesn't fail because table is still there SomeModel.create(some_field='woot2')
def test_add_not_null_constraint_with_records_and_default_which_is_function( self): class SomeModel(pw.Model): some_field = pw.CharField(null=True) class Meta: database = self.db self.evolve_and_check_noop() SomeModel.create(some_field=None) peeweedbevolve.clear() def woot(): return 'woot' class SomeModel(pw.Model): some_field = pw.CharField(null=False, default=woot) class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().some_field, 'woot')
def test_change_decimal_precision(self): class SomeModel(pw.Model): some_field = pw.DecimalField(max_digits=4, decimal_places=0) class Meta: database = self.db self.evolve_and_check_noop() model = SomeModel.create(some_field=1234) self.assertEqual(model.some_field, 1234) peeweedbevolve.clear() class SomeModel(pw.Model): some_field = pw.DecimalField(max_digits=8, decimal_places=2) class Meta: database = self.db self.evolve_and_check_noop() self.assertEqual(SomeModel.select().first().some_field, 1234) model.some_field = 123456.78 model.save() self.assertEqual(SomeModel.select().first().some_field, decimal.Decimal('123456.78'))
def setUp(self): os.system('echo "create database peeweedbevolve_test;" | mysql') self.db = pw.MySQLDatabase('peeweedbevolve_test') self.db.connect() peeweedbevolve.clear()
def setUp(self): os.system('createdb peeweedbevolve_test') self.db = pw.PostgresqlDatabase('peeweedbevolve_test') self.db.connect() peeweedbevolve.clear()
def test_delete_table(self): self.test_create_table() peeweedbevolve.clear() self.assertEqual( peeweedbevolve.calc_changes(self.db)[0][0].split()[:2], [u'DROP', u'TABLE'])