from ...models import Administre, StatutPamChoices from django.db.utils import IntegrityError from django.test import TestCase class AdministreTestCase(TestCase): def test_attributes_statut_pam(self): """ vérifie que le comportement reste correct avec l'ajout de nouveaux attributs """ attr_calc = 'calc_enabled' attr_dec = 'dec_enabled' for choice in StatutPamChoices: self.assertIsInstance(choice.label, str, f"{choice} : le libellé n'a pas le bon type") self.assertIsInstance(choice.value, str, f"{choice} : la valeur n'a pas le bon type") self.assertEqual(choice.value, choice, f"{choice} : n'est pas égal à sa valeur") self.assertIsInstance(getattr(choice, attr_calc), bool, f"{choice} : l'attribut {attr_calc} n'a pas le bon type") self.assertIsInstance(getattr(choice, attr_dec), bool, f"{choice} : l'attribut {attr_dec} n'a pas le bon type") def test_constraint_statut_pam(self): """ vérifie qu'il n'est pas possible de stocker n'importe quoi en base """ valides = StatutPamChoices.values invalide = 'autre' def given(): self.assertTrue(valides, 'il devrait exister des choix valides') self.assertNotIn(invalide, valides, 'le test nécessite une valeur invalide') given() id_sap = 1 # valide : création OK for valide in valides: Administre.objects.create(pk=id_sap, a_statut_pam=valide) id_sap = id_sap + 1 # invalide : création KO with self.assertRaises(IntegrityError): Administre.objects.create(pk=id_sap, a_statut_pam=invalide)