def test_gathers_only_error_free_messages(self): self.record_response("8 boys", 8, has_errors=False) self.record_response("My phone number is 0794443337", 794443337, has_errors=True) self.assertEqual(1, NumericResponsesFor([self.poll]).query.count()) self.assertEqual(8, NumericResponsesFor([self.poll]).total())
def get_numeric_enrollment_data(polls, locations, time_range): results = NumericResponsesFor(polls).forDateRange(time_range) \ .forLocations(locations) \ .excludeZeros() \ .groupBySchools() return sum(results.values()), results.keys()
def test_gathers_only_incoming_messages(self): self.record_response("8 boys", 8, direction='I') self.record_response("Do you have more than 1 latrine?", None, direction='O') self.assertEqual(1, NumericResponsesFor([self.poll]).query.count()) self.assertEqual(8, NumericResponsesFor([self.poll]).total())
def test_groups_by_location(self): self.record_response_for(self.reporter_for(self.kampala), "6 boys", 6) self.record_response_for(self.reporter_for(self.kampala), "7 boys", 7) self.record_response_for(self.reporter_for(self.gulu), "9 boys", 9) results = NumericResponsesFor([self.poll]).groupByLocation() self.assertEqual(13, results[self.kampala.id]) self.assertEqual(9, results[self.gulu.id])
def test_filters_by_value(self): self.record_response_for(self.reporter_for(self.kampala), "6 boys", 6) self.record_response_for(self.reporter_for(self.kampala), "6 boys", 6) self.record_response_for(self.reporter_for(self.kampala), "7 boys", 7) self.record_response_for(self.reporter_for(self.gulu), "9 boys", 9) self.assertEqual( 19, NumericResponsesFor([self.poll]).forValues([6, 7]).total())
def test_calculates_mode(self): self.record_response("8 boys", 8) self.record_response("9 boys", 9) self.record_response("9 boys", 9) self.record_response("10 boys", 10) self.record_response("10 boys", 10) self.record_response("10 boys", 10) self.assertEqual(10, NumericResponsesFor([self.poll]).mode())
def test_groups_by_location(self): gulu = Location.objects.create(name="Gulu") kampala = Location.objects.create(name="Kampala") self.record_response_for(self.reporter_for(kampala), "6 boys", 6) self.record_response_for(self.reporter_for(kampala), "7 boys", 7) self.record_response_for(self.reporter_for(gulu), "9 boys", 9) results = NumericResponsesFor([self.poll]).groupByLocation() self.assertEqual(13, results[kampala.id]) self.assertEqual(9, results[gulu.id])
def test_filters_by_school(self): (mary, st_marys) = self.create_teacher_with_school() (mark, st_marks) = self.create_teacher_with_school() self.record_response_for(mary, "3 girls", 3) self.record_response_for(mark, "2 boys", 2) self.assertEqual( 3, NumericResponsesFor([self.poll]).forSchools([st_marys]).total())
def test_filters_by_date_range(self): today = now yesterday = today - timedelta(days=1) tomorrow = today + timedelta(days=1) next_week = today + timedelta(days=7) self.record_response("6 boys", 6, date=yesterday) self.record_response("10 boys", 10, date=tomorrow) self.assertEqual(10, NumericResponsesFor([self.poll]).forDateRange((today,next_week)).total())
def test_groups_by_school(self): (mary, st_marys) = self.create_teacher_with_school() (mark, st_marks) = self.create_teacher_with_school() self.record_response_for(mary, "3 girls", 3) self.record_response_for(mary, "4 girls", 4) self.record_response_for(mark, "2 boys", 2) results = NumericResponsesFor([self.poll]).groupBySchools() self.assertEqual(7, results[st_marys.id]) self.assertEqual(2, results[st_marks.id])
def test_filters_by_school(self): gulu = Location.objects.create(name="Gulu") mary = EmisReporter.objects.create() st_marys = School.objects.create(name="St Marys", location=gulu) mary.schools = [st_marys] mary.save() mark = EmisReporter.objects.create() st_marks = School.objects.create(name="St Marks", location=gulu) mark.schools = [st_marks] mark.save() self.record_response_for(mary, "3 girls", 3) self.record_response_for(mark, "2 boys", 2) self.assertEqual(3, NumericResponsesFor([self.poll]).forSchools([st_marys]).total())
def test_groups_by_school(self): gulu = Location.objects.create(name="Gulu") mary = EmisReporter.objects.create() st_marys = School.objects.create(name="St Marys", location=gulu) mary.schools = [st_marys] mary.save() mark = EmisReporter.objects.create() st_marks = School.objects.create(name="St Marks", location=gulu) mark.schools = [st_marks] mark.save() self.record_response_for(mary, "3 girls", 3) self.record_response_for(mary, "4 girls", 4) self.record_response_for(mark, "2 boys", 2) results = NumericResponsesFor([self.poll]).groupBySchools() self.assertEqual(7, results[st_marys.id]) self.assertEqual(2, results[st_marks.id])
def get_numeric_data_all_schools_for_locations(polls, locations, time_range): return NumericResponsesFor(polls).forDateRange(time_range) \ .forLocations(locations) \ .groupBySchools()
def test_excludes_zeros(self): self.record_response("0 boys", 0) self.record_response("10 boys", 10) self.assertEqual(1, NumericResponsesFor([self.poll]).excludeZeros().query.count())
def get_numeric_data(polls, locations, time_range): return NumericResponsesFor(polls).forDateRange(time_range).forLocations( locations).total()
def test_mode_defaults_to_zero(self): self.assertEqual(0, NumericResponsesFor([self.poll]).mode())
def test_calculates_mean(self): self.record_response("6 boys", 6) self.record_response("10 boys", 10) self.assertEqual(8, NumericResponsesFor([self.poll]).mean())
def get_numeric_data_for_location(polls, locations, time_range): return NumericResponsesFor(polls).forDateRange(time_range).forLocations( locations).groupByLocation()
def test_excludes_greater_than(self): self.record_response("6 boys", 6) self.record_response("10 boys", 10) self.assertEqual(6, NumericResponsesFor([self.poll]).excludeGreaterThan(6).total())
def get_numeric_data_by_school(polls, schools, time_range): return NumericResponsesFor(polls).forDateRange(time_range) \ .forSchools(schools) \ .groupBySchools() \ .values()
def test_filters_by_location(self): gulu = Location.objects.create(name="Gulu") kampala = Location.objects.create(name="Kampala") self.record_response_for(self.reporter_for(kampala), "6 boys", 6) self.record_response_for(self.reporter_for(gulu), "9 boys", 9) self.assertEqual(9, NumericResponsesFor([self.poll]).forLocations([gulu]).total())
def test_filters_by_location(self): self.record_response_for(self.reporter_for(self.kampala), "6 boys", 6) self.record_response_for(self.reporter_for(self.gulu), "9 boys", 9) self.assertEqual( 9, NumericResponsesFor([self.poll]).forLocations([self.gulu]).total())