from django.db.utils import IntegrityError
from django.test import TestCase
from django.utils import timezone

from ...models.calcul import Calcul, SousVivier
from ...models.calcul import StatutCalculChoices as StatutCalcul


class CalculTestCase(TestCase):

    @classmethod
    def setUpTestData(cls):
        for i in range(1, 15):
            SousVivier.objects.create(pk=str(i))

    def test_constraint_statut(self):
        valides = StatutCalcul.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()

        i = 1

        # valide : création OK
        for valide in valides:
            Calcul.objects.create(pk=str(i), ca_date_debut=timezone.now(), ca_statut=valide, ca_statut_pourcentage=0)
            i = i + 1

        # invalide : création KO
        with self.assertRaises(IntegrityError):
            Calcul.objects.create(pk=str(i), ca_date_debut=timezone.now(), ca_statut=invalide, ca_statut_pourcentage=0)
