Django - 单元测试对象选择不按预期工作
Django - Unit test object's delection does not work as expacted
对于我的两个模型,用户和组,我有一个删除对象的视图。他们每个人的代码几乎相同,它在应用程序中工作,但单元测试有不同的结果:它适用于用户,而不是组。
预先感谢您的建议。
单元测试如下:
class TestAdmUsers(TestCase):
def setUp(self):
self.company = create_dummy_company("Société de test")
self.user_staff = create_dummy_user(self.company, "staff", admin=True)
self.usr11 = create_dummy_user(self.company, "user11")
self.usr12 = create_dummy_user(self.company, "user12", admin=True)
self.usr13 = create_dummy_user(self.company, "user13")
self.client.force_login(self.user_staff.user)
def test_adm_delete_user(self):
test_usercomp_id = self.usr13.id
usrcomp = UserComp.objects.get(user__username="user13")
self.assertEqual(usrcomp.id, test_usercomp_id)
url = reverse("polls:adm_delete_user", args=[self.company.comp_slug, test_usercomp_id])
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
with self.assertRaises(User.DoesNotExist):
User.objects.get(id=test_usercomp_id)
with self.assertRaises(UserComp.DoesNotExist):
UserComp.objects.get(id=test_usercomp_id)
class TestAdmGroups(TestCase):
def setUp(self):
self.company = create_dummy_company("Société de test")
self.user_staff = create_dummy_user(self.company, "staff", admin=True)
self.usr11 = create_dummy_user(self.company, "user11")
self.usr12 = create_dummy_user(self.company, "user12", admin=True)
self.usr13 = create_dummy_user(self.company, "user13")
self.usr14 = create_dummy_user(self.company, "user14")
user_list = [self.usr11.id, self.usr12.id, self.usr13.id, self.usr14.id]
users = UserComp.objects.filter(id__in=user_list)
self.group1 = UserGroup.create_group({
"company": self.company,
"group_name": "Groupe 1",
"weight": 40,
},
user_list=users)
def test_adm_delete_group(self):
test_group_id = self.group1.id
grp = UserGroup.objects.get(group_name="Groupe 1")
self.assertEqual(grp.id, test_group_id)
url = reverse("polls:adm_delete_group", args=[self.company.comp_slug, test_group_id])
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
with self.assertRaises(UserGroup.DoesNotExist):
UserGroup.objects.get(id=test_group_id)
测试对用户运行良好,但我对组有这个问题:
Traceback (most recent call last): File "D:\Mes
documents\Informatique\Developpement\Votes
AG\projet_votes\polls\tests_admin.py", line 354, in
test_adm_delete_group
UserGroup.objects.get(id=test_group_id) AssertionError: DoesNotExist not raised
相关型号如下:
class UserComp(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="Utilisateur")
company = models.ForeignKey(Company, on_delete=models.CASCADE, verbose_name="Société")
phone_regex = RegexValidator(regex=r'^0[0-9]([ .-]?[0-9]{2}){4}$', message=("Format de numéro de téléphone invalide"))
phone_num = models.CharField("numéro de téléphone", validators=[phone_regex], max_length=14, null=True, blank=True)
is_admin = models.BooleanField("administrateur", default=False)
class UserGroup(models.Model):
company = models.ForeignKey(
Company, on_delete=models.CASCADE, verbose_name="société"
)
users = models.ManyToManyField(UserComp, verbose_name="utilisateurs", blank=True)
group_name = models.CharField("nom", max_length=100)
weight = models.IntegerField("poids", default=0)
hidden = models.BooleanField(default=False)
以及浏览量:
@user_passes_test(lambda u: u.is_superuser or (u.id is not None and u.usercomp.is_admin))
def adm_delete_user(request, comp_slug, usr_id):
del_usr = User.objects.get(pk=usr_id)
msg = "Utilisateur {0} {1} supprimé.".\
format(del_usr.last_name, del_usr.first_name)
User.objects.get(pk=usr_id).delete()
messages.success(request, msg)
return redirect("polls:adm_users", comp_slug=comp_slug)
@user_passes_test(lambda u: u.is_superuser or (u.id is not None and u.usercomp.is_admin))
def adm_delete_group(request, comp_slug, grp_id):
del_grp = UserGroup.objects.get(pk=grp_id)
msg = "Groupe {0} supprimé.".format(del_grp.group_name)
UserGroup.objects.get(pk=grp_id).delete()
messages.success(request, msg)
return redirect("polls:adm_groups", comp_slug=comp_slug)
您的 TestAdmGroups
测试 class 上没有登录人员,因此它很可能不会 运行 删除,但 user_passes_test
失败(returns一个 302).
所以要修复,只需在 TestAdmGroups
中登录员工用户:
class TestAdmGroups(TestCase):
...
self.client.force_login(self.user_staff.user)
对于我的两个模型,用户和组,我有一个删除对象的视图。他们每个人的代码几乎相同,它在应用程序中工作,但单元测试有不同的结果:它适用于用户,而不是组。
预先感谢您的建议。
单元测试如下:
class TestAdmUsers(TestCase):
def setUp(self):
self.company = create_dummy_company("Société de test")
self.user_staff = create_dummy_user(self.company, "staff", admin=True)
self.usr11 = create_dummy_user(self.company, "user11")
self.usr12 = create_dummy_user(self.company, "user12", admin=True)
self.usr13 = create_dummy_user(self.company, "user13")
self.client.force_login(self.user_staff.user)
def test_adm_delete_user(self):
test_usercomp_id = self.usr13.id
usrcomp = UserComp.objects.get(user__username="user13")
self.assertEqual(usrcomp.id, test_usercomp_id)
url = reverse("polls:adm_delete_user", args=[self.company.comp_slug, test_usercomp_id])
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
with self.assertRaises(User.DoesNotExist):
User.objects.get(id=test_usercomp_id)
with self.assertRaises(UserComp.DoesNotExist):
UserComp.objects.get(id=test_usercomp_id)
class TestAdmGroups(TestCase):
def setUp(self):
self.company = create_dummy_company("Société de test")
self.user_staff = create_dummy_user(self.company, "staff", admin=True)
self.usr11 = create_dummy_user(self.company, "user11")
self.usr12 = create_dummy_user(self.company, "user12", admin=True)
self.usr13 = create_dummy_user(self.company, "user13")
self.usr14 = create_dummy_user(self.company, "user14")
user_list = [self.usr11.id, self.usr12.id, self.usr13.id, self.usr14.id]
users = UserComp.objects.filter(id__in=user_list)
self.group1 = UserGroup.create_group({
"company": self.company,
"group_name": "Groupe 1",
"weight": 40,
},
user_list=users)
def test_adm_delete_group(self):
test_group_id = self.group1.id
grp = UserGroup.objects.get(group_name="Groupe 1")
self.assertEqual(grp.id, test_group_id)
url = reverse("polls:adm_delete_group", args=[self.company.comp_slug, test_group_id])
response = self.client.get(url)
self.assertEqual(response.status_code, 302)
with self.assertRaises(UserGroup.DoesNotExist):
UserGroup.objects.get(id=test_group_id)
测试对用户运行良好,但我对组有这个问题:
Traceback (most recent call last): File "D:\Mes documents\Informatique\Developpement\Votes AG\projet_votes\polls\tests_admin.py", line 354, in test_adm_delete_group UserGroup.objects.get(id=test_group_id) AssertionError: DoesNotExist not raised
相关型号如下:
class UserComp(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name="Utilisateur")
company = models.ForeignKey(Company, on_delete=models.CASCADE, verbose_name="Société")
phone_regex = RegexValidator(regex=r'^0[0-9]([ .-]?[0-9]{2}){4}$', message=("Format de numéro de téléphone invalide"))
phone_num = models.CharField("numéro de téléphone", validators=[phone_regex], max_length=14, null=True, blank=True)
is_admin = models.BooleanField("administrateur", default=False)
class UserGroup(models.Model):
company = models.ForeignKey(
Company, on_delete=models.CASCADE, verbose_name="société"
)
users = models.ManyToManyField(UserComp, verbose_name="utilisateurs", blank=True)
group_name = models.CharField("nom", max_length=100)
weight = models.IntegerField("poids", default=0)
hidden = models.BooleanField(default=False)
以及浏览量:
@user_passes_test(lambda u: u.is_superuser or (u.id is not None and u.usercomp.is_admin))
def adm_delete_user(request, comp_slug, usr_id):
del_usr = User.objects.get(pk=usr_id)
msg = "Utilisateur {0} {1} supprimé.".\
format(del_usr.last_name, del_usr.first_name)
User.objects.get(pk=usr_id).delete()
messages.success(request, msg)
return redirect("polls:adm_users", comp_slug=comp_slug)
@user_passes_test(lambda u: u.is_superuser or (u.id is not None and u.usercomp.is_admin))
def adm_delete_group(request, comp_slug, grp_id):
del_grp = UserGroup.objects.get(pk=grp_id)
msg = "Groupe {0} supprimé.".format(del_grp.group_name)
UserGroup.objects.get(pk=grp_id).delete()
messages.success(request, msg)
return redirect("polls:adm_groups", comp_slug=comp_slug)
您的 TestAdmGroups
测试 class 上没有登录人员,因此它很可能不会 运行 删除,但 user_passes_test
失败(returns一个 302).
所以要修复,只需在 TestAdmGroups
中登录员工用户:
class TestAdmGroups(TestCase):
...
self.client.force_login(self.user_staff.user)