def test_fields(self): hHead = Household() fields = [str(item.attname) for item in hHead._meta.fields] self.assertEqual(len(fields), 9) for field in ['id', 'investigator_id', 'created', 'modified', 'uid', 'ea_id', 'random_sample_number', 'survey_id', 'household_code']: self.assertIn(field, fields)
def test_should_know_how_to_set_household_location_given_a_set_of_households(self): district = LocationType.objects.create(name="District", slug=slugify("district")) county = LocationType.objects.create(name="County", slug=slugify("county")) sub_county = LocationType.objects.create(name="Subcounty", slug=slugify("sub-county")) parish = LocationType.objects.create(name="Parish", slug=slugify("parish")) village = LocationType.objects.create(name="Village", slug=slugify("village")) uganda = Location.objects.create(name="Uganda", type=self.country) kampala_district = Location.objects.create(name="Kampala", type=district, tree_parent=uganda) bukoto_county = Location.objects.create(name="Bukoto", type=county, tree_parent=kampala_district) some_sub_county = Location.objects.create(name="Some sub county", type=sub_county, tree_parent=bukoto_county) some_parish = Location.objects.create(name="Some parish", type=parish, tree_parent=some_sub_county) some_village = Location.objects.create(name="Some village", type=village, tree_parent=some_parish) survey = Survey.objects.create(name="huhu") ea = EnumerationArea.objects.create(name="EA2", survey=survey) ea.locations.add(some_village) investigator1 = Interviewer.objects.create(name="Investigator", mobile_number="987654321", ea=ea, backend=Backend.objects.create(name='something1')) household1 = Household.objects.create(investigator=investigator1, ea=investigator1.ea, uid=0) household_location = {'District': 'Kampala', 'County': 'Bukoto', 'Subcounty': 'Some sub county', 'Parish': 'Some parish', 'Village': 'Some village'} households = Household.set_related_locations([household1]) self.assertEqual(household_location, households[0].related_locations)
def test_should_know_how_to_set_household_location_given_a_set_of_households(self): district = LocationType.objects.create(name="District", slug=slugify("district")) county = LocationType.objects.create(name="County", slug=slugify("county")) sub_county = LocationType.objects.create(name="Subcounty", slug=slugify("sub-county")) parish = LocationType.objects.create(name="Parish", slug=slugify("parish")) village = LocationType.objects.create(name="Village", slug=slugify("village")) uganda = Location.objects.create(name="Uganda", type=self.country) kampala_district = Location.objects.create(name="Kampala", type=district, tree_parent=uganda) bukoto_county = Location.objects.create(name="Bukoto", type=county, tree_parent=kampala_district) some_sub_county = Location.objects.create(name="Some sub county", type=sub_county, tree_parent=bukoto_county) some_parish = Location.objects.create(name="Some parish", type=parish, tree_parent=some_sub_county) some_village = Location.objects.create(name="Some village", type=village, tree_parent=some_parish) survey = Survey.objects.create(name="huhu") ea = EnumerationArea.objects.create(name="EA2", survey=survey) ea.locations.add(some_village) investigator1 = Investigator.objects.create(name="Investigator", mobile_number="987654321", ea=ea, backend=Backend.objects.create(name='something1')) household1 = Household.objects.create(investigator=investigator1, ea=investigator1.ea, uid=0) household_location = {'District': 'Kampala', 'County': 'Bukoto', 'Subcounty': 'Some sub county', 'Parish': 'Some parish', 'Village': 'Some village'} households = Household.set_related_locations([household1]) self.assertEqual(household_location, households[0].related_locations)
def _format_answer(self, locations, answers, survey): question_options = self.options.all() data = SortedDict() for location in locations: households = Household.all_households_in(location, survey) data[location] = {option.text: answers.filter(answer=option, household__in=households).count() for option in question_options} return data
def _format_answer(self, locations, answers, survey): question_options = self.options.all() data = OrderedDict() for location in locations: households = Household.all_households_in(location, survey) data[location] = {option.text: answers.filter(value=option, interview__householdmember__household__in=households).count() for option in question_options} return data
def test_should_know_total_households_in_location(self): open_survey = Survey.objects.create(name="open survey", description="open survey", has_sampling=True) Household.objects.all().delete() kampala_city = Location.objects.create(name='Kampala City', tree_parent=self.kampala, type=self.city) ea = EnumerationArea.objects.create(name="EA2", survey=self.survey) ea.locations.add(kampala_city) investigator_1 = Interviewer.objects.create(name='some_inv', mobile_number='123456783', male=True, ea=self.ea) investigator_2 = Interviewer.objects.create(name='some_inv', mobile_number='123456781', male=True, ea=ea) household_1 = Household.objects.create(investigator=investigator_1, ea=investigator_1.ea, survey=open_survey) household_2 = Household.objects.create(investigator=investigator_2, ea=investigator_2.ea, survey=open_survey) self.assertEqual(2, Household.all_households_in(self.uganda, open_survey).count()) self.assertIn(household_1, Household.all_households_in(self.uganda, open_survey)) self.assertIn(household_2, Household.all_households_in(self.uganda, open_survey))
def __init__(self, is_edit=False, uid=None, survey=None, *args, **kwargs): super(HouseholdForm, self).__init__(*args, **kwargs) self.is_editing = is_edit if not self.is_editing: self.fields["uid"].initial = Household.next_uid(survey) else: self.fields["uid"].initial = self.instance.uid self.fields["uid"].widget.attrs["disabled"] = "disabled"
def test_should_know_total_households_in_location(self): open_survey = Survey.objects.create(name="open survey", description="open survey", has_sampling=True) Household.objects.all().delete() kampala_city = Location.objects.create(name='Kampala City', tree_parent=self.kampala, type=self.city) ea = EnumerationArea.objects.create(name="EA2", survey=self.survey) ea.locations.add(kampala_city) investigator_1 = Investigator.objects.create(name='some_inv', mobile_number='123456783', male=True, ea=self.ea) investigator_2 = Investigator.objects.create(name='some_inv', mobile_number='123456781', male=True, ea=ea) household_1 = Household.objects.create(investigator=investigator_1, ea=investigator_1.ea, survey=open_survey) household_2 = Household.objects.create(investigator=investigator_2, ea=investigator_2.ea, survey=open_survey) self.assertEqual(2, Household.all_households_in(self.uganda, open_survey).count()) self.assertIn(household_1, Household.all_households_in(self.uganda, open_survey)) self.assertIn(household_2, Household.all_households_in(self.uganda, open_survey))
def test_knows_next_uid_for_households_if_survey_is_open_is_survey_dependent(self): open_survey = Survey.objects.create(name="open survey", description="open survey", has_sampling=True) with patch.object(Survey, "currently_open_survey", return_value=open_survey): investigator = Investigator.objects.create(name="Investigator", mobile_number="987654321", backend=Backend.objects.create(name='something1')) Household.objects.create(investigator=investigator, survey=open_survey, uid=101) Household.objects.create(investigator=investigator, survey=open_survey, uid=102) Household.objects.create(investigator=investigator, uid=103) self.assertEqual(103, Household.next_uid(open_survey))
def test_knows_next_uid_for_households_if_survey_is_open_is_survey_dependent(self): open_survey = Survey.objects.create(name="open survey", description="open survey", has_sampling=True) with patch.object(Survey, "currently_open_survey", return_value=open_survey): investigator = Interviewer.objects.create(name="Investigator", mobile_number="987654321", backend=Backend.objects.create(name='something1')) Household.objects.create(investigator=investigator, survey=open_survey, uid=101) Household.objects.create(investigator=investigator, survey=open_survey, uid=102) Household.objects.create(investigator=investigator, uid=103) self.assertEqual(103, Household.next_uid(open_survey))
def test_fields(self): hHead = Household() fields = [str(item.attname) for item in hHead._meta.fields] print fields self.assertEqual(len(fields), 10) for field in [ 'id', 'created', 'modified', 'house_number', 'listing_id', 'physical_address', 'last_registrar_id', 'registration_channel', 'head_desc', 'head_sex' ]: self.assertIn(field, fields)
def _format_answer(self, locations, answers, survey): question_options = self.options.all() data = OrderedDict() for location in locations: households = Household.all_households_in(location, survey) data[location] = { option.text: answers.filter( value=option, interview__householdmember__household__in=households). count() for option in question_options } return data
def test_create_households(self): country = LocationType.objects.create(name='country', slug='country') uganda = Location.objects.create(name="Uganda", type=country) burundi = Location.objects.create(name="Burundi", type=country) ea = EnumerationArea.objects.create(name="EA2") ea.locations.add(uganda) investigator = Interviewer.objects.create(name="inv", mobile_number='987654321', ea=ea, backend=Backend.objects.create(name='something')) form_data = { 'ea': investigator.ea.id, 'investigator': investigator.id, 'surname': 'Rajini', 'first_name': 'Kant', 'male': 'False', 'date_of_birth': date(1980, 9, 01), 'occupation': 'Student', 'level_of_education': 'Nursery', 'resident_since_year': '2013', 'resident_since_month': '5', 'time_measure': 'Years', 'uid': '2', } household_code = LocationCode.get_household_code(investigator) + str(Household.next_uid()) hHead = HouseholdHead.objects.filter(surname=form_data['surname']) household = Household.objects.filter(uid=form_data['uid']) self.failIf(hHead) self.failIf(household) response = self.client.post('/households/new/', data=form_data) self.failUnlessEqual(response.status_code, 302) # ensure redirection to list investigator page hHead = HouseholdHead.objects.get(surname=form_data['surname']) household = Household.objects.get(uid=form_data['uid']) self.failUnless(hHead.id) self.failUnless(household.id) for key in ['surname', 'first_name', 'date_of_birth', 'occupation', 'level_of_education', 'resident_since_year', 'resident_since_month']: value = getattr(hHead, key) self.assertEqual(str(form_data[key]), str(value)) self.assertEqual(hHead.male, False) self.assertEqual(household.investigator, investigator) self.assertEqual(household.location, investigator.location) self.assertEqual(household.household_code, household_code) self.assertEqual(hHead.household, household) investigator.ea = ea investigator.save() self.assertEqual(household.location, uganda)
def test_knows_number_of_households_in_an_ea(self): kisasi = Location.objects.create(name="Kisasi", type=self.city, tree_parent=self.uganda) ea = EnumerationArea.objects.create(name="EA2", survey=self.survey) ea.locations.add(kisasi) investigator_1 = Investigator.objects.create(name='some_inv', mobile_number='123456783', male=True, ea=self.ea) household_1 = Household.objects.create(uid='123', investigator=investigator_1, ea=investigator_1.ea, survey=self.survey) household_3 = Household.objects.create(uid='123', investigator=investigator_1, survey=self.survey, ea=ea) households_in_ea = Household.all_households_in(self.uganda, self.survey, ea=self.ea) self.assertEqual(households_in_ea.count(), 2) self.assertIn(household_1, households_in_ea) self.assertIn(self.household, households_in_ea) self.assertNotIn(household_3, households_in_ea)
def test_knows_number_of_households_in_an_ea(self): kisasi = Location.objects.create(name="Kisasi", type=self.city, tree_parent=self.uganda) ea = EnumerationArea.objects.create(name="EA2", survey=self.survey) ea.locations.add(kisasi) investigator_1 = Interviewer.objects.create(name='some_inv', mobile_number='123456783', male=True, ea=self.ea) household_1 = Household.objects.create(uid='123', investigator=investigator_1, ea=investigator_1.ea, survey=self.survey) household_3 = Household.objects.create(uid='123', investigator=investigator_1, survey=self.survey, ea=ea) households_in_ea = Household.all_households_in(self.uganda, self.survey, ea=self.ea) self.assertEqual(households_in_ea.count(), 2) self.assertIn(household_1, households_in_ea) self.assertIn(self.household, households_in_ea) self.assertNotIn(household_3, households_in_ea)
def list_households(request): selected_location = None selected_ea = None all_households = Household.objects.order_by('household_member__householdhead__surname') params = request.GET if params.has_key('location') and params['location'].isdigit(): selected_location = Location.objects.get(id=int(params['location'])) corresponding_locations = selected_location.get_descendants(include_self=True) all_households = all_households.filter(ea__locations__in=corresponding_locations) if params.has_key('ea') and params['ea'].isdigit(): selected_ea = EnumerationArea.objects.get(id=int(params['ea'])) all_households = all_households.filter(ea=selected_ea) households = _remove_duplicates(all_households) households = Household.set_related_locations(households) if not households: location_type = selected_location.type.name.lower() if selected_location and selected_location.type else 'location' messages.error(request, "There are no households currently registered for this %s." % location_type) return render(request, 'households/index.html', {'households': households, 'location_data': LocationWidget(selected_location, ea=selected_ea), 'request': request})
def test_knows_next_uid_for_households(self): investigator = Investigator.objects.create(name="Investigator", mobile_number="987654321", backend=Backend.objects.create(name='something1')) Household.objects.create(investigator=investigator, uid=101) self.assertEqual(102, Household.next_uid())
def create_household(householdform, investigator, valid, uid): is_valid_household = householdform['household'].is_valid() if investigator and is_valid_household: household = householdform['household'].save(commit=False) household.investigator = investigator household.ea = investigator.ea open_survey = Survey.currently_open_survey(investigator.location) household.household_code = LocationCode.get_household_code(investigator) + str(Household.next_uid(open_survey)) if uid: household.uid = uid household.household_code = LocationCode.get_household_code(investigator) + str(uid) household.survey = open_survey household.save() valid['household'] = True return valid
'ea': investigator.ea.id, 'investigator': investigator.id, 'surname': 'Rajini', 'first_name': 'Kant', 'male': 'False', 'date_of_birth': date(1980, 9, 01), 'occupation': 'Student', 'level_of_education': 'Nursery', 'resident_since_year': '2013', 'resident_since_month': '5', 'time_measure': 'Years', 'uid': '2', } household_code = LocationCode.get_household_code(investigator) + str( Household.next_uid()) hHead = HouseholdHead.objects.filter(surname=form_data['surname']) household = Household.objects.filter(uid=form_data['uid']) self.failIf(hHead) self.failIf(household) response = self.client.post('/households/new/', data=form_data) self.failUnlessEqual( response.status_code, 302) # ensure redirection to list investigator page hHead = HouseholdHead.objects.get(surname=form_data['surname']) household = Household.objects.get(uid=form_data['uid']) self.failUnless(hHead.id) self.failUnless(household.id) for key in [
def test_knows_next_uid_for_households(self): investigator = Interviewer.objects.create(name="Investigator", mobile_number="987654321", backend=Backend.objects.create(name='something1')) Household.objects.create(investigator=investigator, uid=101) self.assertEqual(102, Household.next_uid())