def test_get_ancestor_including_self(self): country = LocationType.objects.create(name='Country', slug='country') region = LocationType.objects.create(name='Region', slug='region') city = LocationType.objects.create(name='City', slug='city') parish = LocationType.objects.create(name='Parish', slug='parish') village = LocationType.objects.create(name='Village', slug='village') subcounty = LocationType.objects.create( name='Subcounty', slug='subcounty') africa = Location.objects.create(name='Africa', type=country) uganda = Location.objects.create( name='Uganda', type=region, parent=africa) abim = Location.objects.create(name='ABIM', parent=uganda, type=city) abim_son = Location.objects.create( name='LABWOR', parent=abim, type=parish) abim_son_son = Location.objects.create( name='KALAKALA', parent=abim_son, type=village) abim_son_daughter = Location.objects.create( name='OYARO', parent=abim_son, type=village) abim_son_daughter_daughter = Location.objects.create( name='WIAWER', parent=abim_son_daughter, type=subcounty) abim_son_son_daughter = Location.objects.create( name='ATUNGA', parent=abim_son_son, type=subcounty) abim_son_son_son = Location.objects.create( name='WICERE', parent=abim_son_son, type=subcounty) expected_location_ancestors = [ abim_son_son_son, abim_son_son, abim_son, abim, uganda, africa] ancestors = get_ancestors(abim_son_son_son, include_self=True) self.assertEqual(expected_location_ancestors, ancestors)
def _get_ancestors_names(self, household_location, exclude_type='country'): location_ancestors = get_ancestors(household_location, include_self=True) if exclude_type: exclude_location = Location.objects.filter(type__name__iexact=exclude_type.lower()) for location in exclude_location: location_ancestors.remove(location) result= [ancestor.name for ancestor in location_ancestors] result.reverse() return result
def test_get_ancestor_including_self(self): country = LocationType.objects.create(name='Country', slug='country') region = LocationType.objects.create(name='Region', slug='region') city = LocationType.objects.create(name='City', slug='city') parish = LocationType.objects.create(name='Parish', slug='parish') village = LocationType.objects.create(name='Village', slug='village') subcounty = LocationType.objects.create(name='Subcounty', slug='subcounty') africa = Location.objects.create(name='Africa', type=country) LocationTypeDetails.objects.create(country=africa, location_type=country) LocationTypeDetails.objects.create(country=africa, location_type=region) LocationTypeDetails.objects.create(country=africa, location_type=city) LocationTypeDetails.objects.create(country=africa, location_type=parish) LocationTypeDetails.objects.create(country=africa, location_type=village) LocationTypeDetails.objects.create(country=africa, location_type=subcounty) uganda = Location.objects.create(name='Uganda', type=region, tree_parent=africa) abim = Location.objects.create(name='ABIM', tree_parent=uganda, type=city) abim_son = Location.objects.create(name='LABWOR', tree_parent=abim, type=parish) abim_son_son = Location.objects.create(name='KALAKALA', tree_parent=abim_son, type=village) abim_son_daughter = Location.objects.create(name='OYARO', tree_parent=abim_son, type=village) abim_son_daughter_daughter = Location.objects.create( name='WIAWER', tree_parent=abim_son_daughter, type=subcounty) abim_son_son_daughter = Location.objects.create( name='ATUNGA', tree_parent=abim_son_son, type=subcounty) abim_son_son_son = Location.objects.create(name='WICERE', tree_parent=abim_son_son, type=subcounty) expected_location_ancestors = [ abim_son_son_son, abim_son_son, abim_son, abim, uganda, africa ] ancestors = get_ancestors(abim_son_son_son, include_self=True) self.assertEqual(expected_location_ancestors, ancestors)
def _get_ancestors_names(self, household_location, exclude_type='country'): location_ancestors = get_ancestors(household_location, include_self=True) if exclude_type: exclude_location = Location.objects.filter( type__name__iexact=exclude_type.lower()) for location in exclude_location: location_ancestors.remove(location) result = [ancestor.name for ancestor in location_ancestors] result.reverse() return result
def test_get_ancestor(self): country = LocationType.objects.create(name='Country', slug='country') region = LocationType.objects.create(name='Region', slug='region') city = LocationType.objects.create(name='City', slug='city') parish = LocationType.objects.create(name='Parish', slug='parish') village = LocationType.objects.create(name='Village', slug='village') subcounty = LocationType.objects.create(name='Subcounty', slug='subcounty') africa = Location.objects.create(name='Africa', type=country) LocationTypeDetails.objects.create(country=africa, location_type=country) LocationTypeDetails.objects.create(country=africa, location_type=region) LocationTypeDetails.objects.create(country=africa, location_type=city) LocationTypeDetails.objects.create(country=africa, location_type=parish) LocationTypeDetails.objects.create(country=africa, location_type=village) LocationTypeDetails.objects.create(country=africa, location_type=subcounty) uganda = Location.objects.create(name='Uganda', type=region, tree_parent=africa) abim = Location.objects.create(name='ABIM', tree_parent=uganda, type=city) abim_son = Location.objects.create(name='LABWOR', tree_parent=abim, type=parish) abim_son_son = Location.objects.create(name='KALAKALA', tree_parent=abim_son, type=village) abim_son_daughter = Location.objects.create(name='OYARO', tree_parent=abim_son, type=village) abim_son_daughter_daughter = Location.objects.create(name='WIAWER', tree_parent=abim_son_daughter, type=subcounty) abim_son_son_daughter = Location.objects.create(name='ATUNGA', tree_parent=abim_son_son, type=subcounty) abim_son_son_son = Location.objects.create(name='WICERE', tree_parent=abim_son_son, type=subcounty) self.assertEqual([], get_ancestors(africa)) self.assertEqual([africa], get_ancestors(uganda)) self.assertEqual([uganda, africa], get_ancestors(abim)) self.assertEqual([abim, uganda, africa], get_ancestors(abim_son)) self.assertEqual([abim_son, abim, uganda, africa], get_ancestors(abim_son_son)) self.assertEqual([abim_son_son, abim_son, abim, uganda, africa], get_ancestors(abim_son_son_son))
def ancestors_reversed(location): ancestors = get_ancestors(location) ancestors.reverse() return ancestors