本文整理汇总了Python中factory.django.DjangoModelFactory方法的典型用法代码示例。如果您正苦于以下问题:Python django.DjangoModelFactory方法的具体用法?Python django.DjangoModelFactory怎么用?Python django.DjangoModelFactory使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类factory.django
的用法示例。
在下文中一共展示了django.DjangoModelFactory方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _test_uniqueness_ignores_deleted
# 需要导入模块: from factory import django [as 别名]
# 或者: from factory.django import DjangoModelFactory [as 别名]
def _test_uniqueness_ignores_deleted(
self, factory: Type[DjangoModelFactory], **kwargs
):
"""Ensure uniqueness doesn't take deleted instances into account.
Parameters
----------
factory: Type[DjangoModelFactory]
The factory to use to create/delete some objects
kwargs:
Arguments to pass to the given `factory`.
"""
obj1 = factory(**kwargs)
# force a soft deletion (useful for through models configured with ``HARD_DELETE``)
obj1.delete(force_policy=SOFT_DELETE_CASCADE)
self.assertIsSoftDeleted(obj1)
# we can create it again
factory(**kwargs)
# when exists non-deleted, cannot create another one
with self.assertRaises(IntegrityError):
factory(**kwargs)
示例2: _test_soft_deletion
# 需要导入模块: from factory import django [as 别名]
# 或者: from factory.django import DjangoModelFactory [as 别名]
def _test_soft_deletion(self, factory: Type[DjangoModelFactory], **kwargs):
"""Ensure that soft deletion works for the model represented by the factory.
Parameters
----------
factory: Type[DjangoModelFactory]
The factory to use to create an object to soft-delete.
kwargs:
Arguments to pass to the given `factory`.
"""
model = factory._meta.model # noqa
obj = factory(**kwargs)
self.assertIsNone(obj.deleted)
obj.delete()
self.assertIsNotNone(obj.deleted)
# object should be invisible to normal queryset
with self.assertRaises(model.DoesNotExist):
model.objects.get(pk=obj.pk)
# but still present in deleted mode
self.assertEqual(model.objects.deleted_only().get(pk=obj.pk), obj)
# we could still force delete it
obj.delete(force_policy=HARD_DELETE)
self.assertIsHardDeleted(obj)
示例3: _test_hard_deletion
# 需要导入模块: from factory import django [as 别名]
# 或者: from factory.django import DjangoModelFactory [as 别名]
def _test_hard_deletion(self, factory: Type[DjangoModelFactory], **kwargs):
"""Ensure directly deleting an instance of the given kind implies hard deletion.
Parameters
----------
factory: Type[DjangoModelFactory]
The factory to use to create an object to delete.
kwargs:
Arguments to pass to the given `factory`.
"""
obj = factory(**kwargs)
obj.delete()
self.assertIsHardDeleted(obj)