def test_migrations():
"""Tests whether the migrations are properly generated and executed."""
simulator = MigrationSimulator()
Model = simulator.define_model(
fields={
'id': models.IntegerField(primary_key=True),
'name': models.CharField(max_length=255, null=True),
'other_name': models.CharField(max_length=255)
},
meta_options={
'indexes': [
ConditionalUniqueIndex(
fields=['name', 'other_name'],
condition='"name" IS NOT NULL',
name='index1'
),
ConditionalUniqueIndex(
fields=['other_name'],
condition='"name" IS NULL',
name='index2'
)
]
}
)
migration = simulator.make_migrations()
assert len(migration.operations) == 3
operations = migration.operations
assert isinstance(operations[0], CreateModel)
for operation in operations[1:]:
assert isinstance(operation, AddIndex)
calls = [call[0] for _, call, _ in simulator.migrate('CREATE UNIQUE INDEX')[0]['CREATE UNIQUE INDEX']]
db_table = Model._meta.db_table
assert calls[0] == 'CREATE UNIQUE INDEX "index1" ON "{0}" ("name", "other_name") WHERE "name" IS NOT NULL'.format(
db_table
)
assert calls[1] == 'CREATE UNIQUE INDEX "index2" ON "{0}" ("other_name") WHERE "name" IS NULL'.format(
db_table
)
with transaction.atomic():
Model.objects.create(id=1, name="name", other_name="other_name")
with pytest.raises(IntegrityError):
Model.objects.create(id=2, name="name", other_name="other_name")
with transaction.atomic():
Model.objects.create(id=1, name=None, other_name="other_name")
with pytest.raises(IntegrityError):
Model.objects.create(id=2, name=None, other_name="other_name")
python类AddIndex()的实例源码
test_conditional_unique_index.py 文件源码
项目:django-postgres-extra
作者: SectorLabs
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0