def test_invalid_code(self): # Empty Code l_form = LocalisationForm( data={ 'code': "", 'insee': self.insee, 'is_enable': self.is_enable, 'parent': self.parent, 'order': self.order }) self.assertFalse(l_form.is_valid(), "Form is not valid !") self.assertEqual(len(l_form.errors), 1, f"Expected only 1 error ! {l_form.errors}") self.assertEqual(len(l_form['code'].errors), 1, "Expected only 1 error for this field !") self.assertEqual(l_form['code'].errors[0], "This field is required.", "Error message not expected !") # Code too long l_form = LocalisationForm( data={ 'code': "1" * (MAX_CODE_LENGTH + 1), 'insee': self.insee, 'is_enable': self.is_enable, 'parent': self.parent, 'order': self.order }) self.assertFalse(l_form.is_valid(), "Form is not valid !") self.assertEqual(len(l_form.errors), 1, "Expected only 1 error !") self.assertEqual(len(l_form['code'].errors), 1, "Expected only 1 error for this field !") self.assertEqual( l_form['code'].errors[0], f"Ensure this value has at most {MAX_CODE_LENGTH} characters (it has {MAX_CODE_LENGTH+1}).", "Error message not expected !")
def test_unique_order(self): # Create first localisation l_form = LocalisationForm(data={ 'order': MINIMUM_ORDER, 'code': self.code, 'insee': self.insee }) self.assertTrue(l_form.is_valid(), f"Form is not valid ! {l_form.errors}") self.assertEqual(Localisation.objects.all().count(), 0, "An object already exist !") l_form.save(user=self.user) # Create second localisation with same order value (error) l_form = LocalisationForm(data={ 'order': MINIMUM_ORDER, 'code': f"{self.code}A", 'insee': self.insee }) self.assertFalse(l_form.is_valid(), f"Form is valid !") self.assertEqual(len(l_form.errors), 1, "Unexpected errors quantity !") self.assertEqual(len(l_form['order'].errors), 1, "Unexpected errors quantity for this field !") self.assertEqual( l_form['order'].errors[0], "A localisation with this order and parent already exist.", "Error message not expected !")
def test_parent_deletion_without_parent_to_replace(self): """ Test if Childrens will inherit of parent of their parent which is None """ # Create Top Parent l_form = LocalisationForm(data={ 'order': MINIMUM_ORDER, 'code': self.code, 'insee': self.insee }) self.assertTrue(l_form.is_valid(), f"Form is not valid ! {l_form.errors}") self.assertEqual(Localisation.objects.all().count(), 0) l_localisation_parent = l_form.save(user=self.user) self.assertEqual(Localisation.objects.all().count(), 1) # Create Children 1 l_form = LocalisationForm( data={ 'order': MINIMUM_ORDER, 'code': f"{self.code}A", 'insee': self.insee, 'parent': l_localisation_parent.pk }) self.assertTrue(l_form.is_valid(), f"Form is not valid ! {l_form.errors}") l_localisation_children_1 = l_form.save(user=self.user) self.assertEqual(Localisation.objects.all().count(), 2) # Create Children 2 l_form = LocalisationForm( data={ 'order': MINIMUM_ORDER + 1, 'code': f"{self.code}B", 'insee': self.insee, 'parent': l_localisation_parent.pk }) self.assertTrue(l_form.is_valid(), f"Form is not valid ! {l_form.errors}") l_localisation_children_2 = l_form.save(user=self.user) self.assertEqual(Localisation.objects.all().count(), 3) # Delete Parent l_localisation_parent.delete() self.assertEqual(Localisation.objects.all().count(), 2) # Update children local queryset l_localisation_children_1 = Localisation.objects.get( pk=l_localisation_children_1.pk) l_localisation_children_2 = Localisation.objects.get( pk=l_localisation_children_2.pk) # Test if parent is now Parent self.assertIsNone(l_localisation_children_1.parent, None) self.assertIsNone(l_localisation_children_2.parent, None)
def test_user_valid(self): l_localisation = LocalisationForm( data={ 'code': self.code, 'insee': self.insee, 'is_enable': self.is_enable, 'parent': self.parent, 'order': self.order }) self.assertTrue(l_localisation.is_valid(), "Form is not valid !") self.assertEqual(Localisation.objects.all().count(), 0, "An object already exist !") l_localisation.save(user=self.user) # Check Localisation self.assertEqual(Localisation.objects.all().count(), 1, "Object has not been save !") l_localisation = Localisation.objects.filter()[0] self.assertEqual(l_localisation.code, self.code, "Unexpected value !") self.assertEqual(l_localisation.insee, self.insee, "Unexpected value !") self.assertEqual(l_localisation.is_enable, self.is_enable, "Unexpected value !") self.assertIsNone(l_localisation.parent, "Unexpected value !") self.assertEqual(l_localisation.order, self.order, "Unexpected value !") # Check LocalisationStats l_localisation_stat = l_localisation.get_statistics() self.assertIsNotNone(l_localisation_stat, "Stats does not exist !") self.assertIsNotNone(l_localisation_stat.date_creation) self.assertEqual(l_localisation_stat.user_creation, self.user) self.assertIsNotNone(l_localisation_stat.date_validation) self.assertEqual(l_localisation_stat.user_validation, self.user)
def test_parent(self): # Create Parent localisation l_form = LocalisationForm(data={ 'order': MINIMUM_ORDER, 'code': self.code, 'insee': self.insee }) self.assertTrue(l_form.is_valid(), f"Form is not valid ! {l_form.errors}") self.assertEqual(Localisation.objects.all().count(), 0) l_localisation_parent = l_form.save(user=self.user) self.assertEqual(Localisation.objects.all().count(), 1) # Create first children l_form = LocalisationForm( data={ 'order': MINIMUM_ORDER, 'code': f"{self.code}A", 'insee': self.insee, 'parent': l_localisation_parent.pk }) self.assertTrue(l_form.is_valid(), f"Form is not valid ! {l_form.errors}") l_form.save(user=self.user) self.assertEqual(Localisation.objects.all().count(), 2) # Create second children l_form = LocalisationForm( data={ 'order': MINIMUM_ORDER + 1, 'code': f"{self.code}B", 'insee': self.insee, 'parent': l_localisation_parent.pk }) self.assertTrue(l_form.is_valid(), f"Form is not valid ! {l_form.errors}") l_form.save(user=self.user) self.assertEqual(Localisation.objects.all().count(), 3)
def test_invalid_order(self): l_form = LocalisationForm( data={ 'code': self.code, 'insee': self.insee, 'is_enable': self.is_enable, 'parent': self.parent, 'order': MINIMUM_ORDER - 1 }) self.assertFalse(l_form.is_valid(), "Form is not valid !") self.assertEqual(len(l_form.errors), 1, "Unexpected errors quantity !") self.assertEqual(len(l_form['order'].errors), 1, "Unexpected errors quantity for this field !") self.assertEqual( l_form['order'].errors[0], f"Ensure this value is greater than or equal to {MINIMUM_ORDER}.", "Error message not expected !")
def __init__(self, fromCvsFile, mongo_list, functionnalUser: User, parent_list, order: int): # Check input variable if not parent_list: self.logging.critical( "No parent list impossible to continue !") self.logging = create_logger("DepartmentData") self.dep = fromCvsFile[0] self.reg = to_integer(fromCvsFile[1]) self.cheflieu = str(fromCvsFile[2]) self.tncc = fromCvsFile[3] self.ncc = fromCvsFile[4] self.nccenr = fromCvsFile[5] self.libelle = fromCvsFile[6] # Check variable # - dep if not self.dep: self.logging.critical( f"Value '{self.dep}' is not a number !") # - reg if not self.reg: self.logging.critical( f"Value '{self.reg}' is not a number !") # - nccenr if not self.nccenr: self.logging.critical(f"Value '{self.nccenr}' is empty !") # - parent l_parent = None for parent in parent_list: if parent.insee == self.reg: l_parent = parent if not parent: self.logging.critical( f"Parent not find with insee value '{self.reg}' !") # Create Localisation l_localisationForm = LocalisationForm( data={ 'code': self.dep, 'insee': int( re.compile("(\d+)").match(self.dep).group( 1)), # required for 2A and 2B 'is_enable': True, 'is_linkeable': True, 'parent': l_parent, 'order': order }) if l_localisationForm.is_valid() is False: self.logging.critical( f"Error on Localisation with code={self.codeiso3}: {l_localisationForm.errors}" ) l_localisation = l_localisationForm.save(user=functionnalUser) # Create LocalisationData (FR) l_localisationDataForm = LocalisationDataForm( data={ 'name': self.nccenr, 'resume': 'Département Français', 'language': LanguageAvailable.FR.value }) if l_localisationDataForm.is_valid( localisation=l_localisation) is False: self.logging.critical( f"Error on LocalisationData with name={self.libcog}: {l_localisationDataForm.errors}" ) l_localisationDataForm.save() # Create LocalisationData (EN) l_localisationDataForm = LocalisationDataForm( data={ 'name': self.nccenr, 'resume': 'French Department', 'language': LanguageAvailable.EN.value }) if l_localisationDataForm.is_valid( localisation=l_localisation) is False: self.logging.critical( f"Error on LocalisationData with name={self.libcog_en}: {l_localisationDataForm.errors}" ) l_localisationDataForm.save() # Register localisation mongo_list.append(l_localisation)
def __init__(self, fromCvsFile, mongo_list, functionnalUser: User, france, order: int): if not france: self.critical( f"France contains nothing impossible to continue !") self.logging = create_logger("RegionData") self.reg = to_integer(fromCvsFile[0]) self.cheflieu = fromCvsFile[1] self.tncc = to_lowerCase(fromCvsFile[2]) self.ncc = to_lowerCase(fromCvsFile[3]) self.nccenr = to_lowerCase(fromCvsFile[4]) self.libelle = fromCvsFile[5] # Check Variables # - Reg if not self.reg: self.logging.error( f"Value '{self.cog}' is not a number ! Ignore it...") return # Create Localisation l_localisationForm = LocalisationForm( data={ 'code': self.cheflieu, 'insee': self.reg, 'is_enable': True, 'is_linkeable': True, 'parent': france, 'order': order }) if l_localisationForm.is_valid() is False: self.logging.critical( f"Error on Localisation with code={self.cheflieu}: {l_localisationForm.errors}" ) l_localisation = l_localisationForm.save(user=functionnalUser) # Create LocalisationData (FR) l_localisationDataForm = LocalisationDataForm( data={ 'name': self.nccenr, 'resume': 'Région Française', 'localisation': l_localisation, 'language': LanguageAvailable.FR.value }) if l_localisationDataForm.is_valid( localisation=l_localisation) is False: self.logging.critical( f"Error on LocalisationData with name={self.cheflieu}: {l_localisationDataForm.errors}" ) l_localisationDataForm.save() # Create LocalisationData (EN) l_localisationDataForm = LocalisationDataForm( data={ 'name': self.nccenr, 'resume': 'French Region', 'localisation': l_localisation, 'language': LanguageAvailable.EN.value }) if l_localisationDataForm.is_valid( localisation=l_localisation) is False: self.logging.critical( f"Error on LocalisationData with name={self.cheflieu}: {l_localisationDataForm.errors}" ) l_localisationDataForm.save() # Register localisation mongo_list.append(l_localisation)
def __init__(self, fromCvsFile, mongo_list, functionnalUser: User, order: int, parent_list): # Check input variable if not parent_list: self.logging.critical( "No parent list impossible to continue !") self.logging = create_logger("CountryData") self.cog = to_integer(fromCvsFile[0]) self.actual = to_integer(fromCvsFile[1]) self.capay = fromCvsFile[2] self.crpay = fromCvsFile[3] self.ani = fromCvsFile[4] self.libcog = to_lowerCase(fromCvsFile[5]) self.libenr = fromCvsFile[6] self.ancnom = fromCvsFile[7] self.codeiso2 = fromCvsFile[8] self.codeiso3 = fromCvsFile[9] self.codenum3 = fromCvsFile[10] # To Ignore: if self.actual != 1: return # Check value if self.cog is None: if self.libcog != "France": self.logging.error( f"Value '{self.cog}' is not a number ! Ignore it..." ) return self.cog = 99100 if self.cog < self.cog_minimum_value: self.logging.warning( f"cog value \'{self.cog}\' is less than {self.cog_minimum_value} ! Ignore it..." ) return if not self.libcog: self.logging.error( f"Value '{self.libcog}' is empty after regexp ! Ignore it..." ) return # - parent l_parent = None l_parent_insee = int(str(self.cog)[:3]) * 100 for parent in parent_list: if parent.insee == l_parent_insee: l_parent = parent if not l_parent: self.logging.warning( f"Parent not find with insee value \'{l_parent_insee}\' ! Ignore it..." ) return # Create Localisation l_localisationForm = LocalisationForm( data={ 'code': self.codeiso3, 'insee': self.cog, 'is_enable': True, 'is_linkeable': True, 'parent': l_parent, 'order': order }) if l_localisationForm.is_valid() is False: self.logging.critical( f"Error on Localisation with code={self.libcog}: {l_localisationForm.errors}" ) l_localisation = l_localisationForm.save(user=functionnalUser) # Create LocalisationData (FR) l_localisationDataForm = LocalisationDataForm( data={ 'name': self.libcog, 'resume': 'Pays', 'localisation': l_localisation, 'language': LanguageAvailable.FR.value }) if l_localisationDataForm.is_valid( localisation=l_localisation) is False: self.logging.critical( f"Error on LocalisationData with name={self.libcog}: {l_localisationDataForm.errors}" ) l_localisationDataForm.save() # Create LocalisationData (EN) l_localisationDataForm = LocalisationDataForm( data={ 'name': self.libcog, 'resume': 'Country', 'localisation': l_localisation, 'language': LanguageAvailable.EN.value }) if l_localisationDataForm.is_valid( localisation=l_localisation) is False: self.logging.critical( f"Error on LocalisationData with name={self.libcog}: {l_localisationDataForm.errors}" ) l_localisationDataForm.save() # Register localisation mongo_list.append(l_localisation)
def __init__(self, fromCvsFile, mongo_list, functionnalUser: User, order: int): self.logging = create_logger("ContinentData") self.cog = to_integer(fromCvsFile[0]) self.libcog = to_lowerCase(fromCvsFile[1]) self.libcog_en = to_lowerCase(fromCvsFile[2]) self.codeiso2 = fromCvsFile[3] self.codeiso3 = fromCvsFile[4] # Check Variable if not self.cog: self.logging.error( f"Value '{self.cog}' is not a number ! Ignore it...") return if not self.libcog: self.logging.error( f"Value '{self.libcog}' is empty after regexp ! Ignore it..." ) return if not self.libcog_en: self.logging.error( f"Value '{self.libcog_en}' is empty after regexp ! Ignore it..." ) return # Create Localisation l_localisationForm = LocalisationForm( data={ 'code': self.codeiso3, 'insee': self.cog, 'is_enable': True, 'is_linkeable': True, 'parent': None, 'order': order }) if l_localisationForm.is_valid() is False: self.logging.critical( f"Error on Localisation with code={self.codeiso3}: {l_localisationForm.errors}" ) l_localisation = l_localisationForm.save(user=functionnalUser) # Create LocalisationData (FR) l_localisationDataForm = LocalisationDataForm( data={ 'name': self.libcog, 'resume': 'Continent', 'language': LanguageAvailable.FR.value }) if l_localisationDataForm.is_valid( localisation=l_localisation) is False: self.logging.critical( f"Error on LocalisationData with name={self.libcog}: {l_localisationDataForm.errors}" ) l_localisationDataForm.save() # Create LocalisationData (EN) l_localisationDataForm = LocalisationDataForm( data={ 'name': self.libcog_en, 'resume': 'Continent', 'language': LanguageAvailable.EN.value }) if l_localisationDataForm.is_valid( localisation=l_localisation) is False: self.logging.critical( f"Error on LocalisationData with name={self.libcog_en}: {l_localisationDataForm.errors}" ) l_localisationDataForm.save() # Register localisation mongo_list.append(l_localisation)
def test_parent_list(self): """ This test verify that the field Parent choices is human friendly and keep order right even for random creation """ # Create Parent 1 l_form = LocalisationForm( data={ 'code': self.code, 'insee': self.insee, 'is_enable': False, 'parent': None, 'order': MINIMUM_ORDER }) self.assertTrue(l_form.is_valid(), "Form is not valid !") l_localisation_1 = l_form.save(user=self.user) # Create Parent 2 l_form = LocalisationForm( data={ 'code': f"{self.code}A", 'insee': self.insee, 'is_enable': False, 'parent': None, 'order': MINIMUM_ORDER + 1 }) self.assertTrue(l_form.is_valid(), "Form is not valid !") l_localisation_2 = l_form.save(user=self.user) # Create Children 13 l_form = LocalisationForm( data={ 'code': f"{self.code}B", 'insee': self.insee, 'is_enable': False, 'parent': l_localisation_1.pk, 'order': MINIMUM_ORDER + 2 }) self.assertTrue(l_form.is_valid(), "Form is not valid !") l_localisation_13 = l_form.save(user=self.user) # Create Children 11 l_form = LocalisationForm( data={ 'code': f"{self.code}C", 'insee': self.insee, 'is_enable': False, 'parent': l_localisation_1.pk, 'order': MINIMUM_ORDER }) self.assertTrue(l_form.is_valid(), "Form is not valid !") l_localisation_11 = l_form.save(user=self.user) # Create Children 22 l_form = LocalisationForm( data={ 'code': f"{self.code}D", 'insee': self.insee, 'is_enable': False, 'parent': l_localisation_2.pk, 'order': MINIMUM_ORDER + 1 }) self.assertTrue(l_form.is_valid(), "Form is not valid !") l_localisation_22 = l_form.save(user=self.user) # Create Children 12 l_form = LocalisationForm( data={ 'code': f"{self.code}E", 'insee': self.insee, 'is_enable': False, 'parent': l_localisation_1.pk, 'order': MINIMUM_ORDER + 1 }) self.assertTrue(l_form.is_valid(), "Form is not valid !") l_localisation_12 = l_form.save(user=self.user) # Create Children 21 l_form = LocalisationForm( data={ 'code': f"{self.code}F", 'insee': self.insee, 'is_enable': False, 'parent': l_localisation_2.pk, 'order': MINIMUM_ORDER }) self.assertTrue(l_form.is_valid(), "Form is not valid !") l_localisation_21 = l_form.save(user=self.user) # Create Children 122 l_form = LocalisationForm( data={ 'code': f"{self.code}G", 'insee': self.insee, 'is_enable': False, 'parent': l_localisation_12.pk, 'order': MINIMUM_ORDER + 1 }) self.assertTrue(l_form.is_valid(), "Form is not valid !") l_localisation_122 = l_form.save(user=self.user) # Create Children 121 l_form = LocalisationForm( data={ 'code': f"{self.code}H", 'insee': self.insee, 'is_enable': False, 'parent': l_localisation_12.pk, 'order': MINIMUM_ORDER }) self.assertTrue(l_form.is_valid(), "Form is not valid !") l_localisation_121 = l_form.save(user=self.user) # Check if all required Localisation has been created self.assertEqual(len(Localisation.objects.all()), 9, "Form has not been save !") # Check display order in form for i, pk in enumerate(LocalisationForm().fields['parent'].choices): if i == 0: self.assertEqual(pk, l_localisation_1.pk, f"Unexpected object ! {pk}") elif i == 1: self.assertEqual(pk, l_localisation_11.pk, f"Unexpected object ! {pk}") elif i == 2: self.assertEqual(pk, l_localisation_12.pk, f"Unexpected object ! {pk}") elif i == 3: self.assertEqual(pk, l_localisation_121.pk, f"Unexpected object ! {pk}") elif i == 4: self.assertEqual(pk, l_localisation_122.pk, f"Unexpected object ! {pk}") elif i == 5: self.assertEqual(pk, l_localisation_13.pk, f"Unexpected object ! {pk}") elif i == 6: self.assertEqual(pk, l_localisation_2.pk, f"Unexpected object ! {pk}") elif i == 7: self.assertEqual(pk, l_localisation_21.pk, f"Unexpected object ! {pk}") elif i == 8: self.assertEqual(pk, l_localisation_22.pk, f"Unexpected object ! {pk}")