def test_filter_disaster_by_date_and_date(self):
        disaster_attr = self.disaster.copy()
        date_1_jan = "2014-01-01"
        disaster_attr['date'] = date_1_jan

        disaster_attr2 = self.disaster.copy()
        date_3_jan = "2014-01-03"
        disaster_attr2['status'] = "Closed"
        disaster_attr2['date'] = date_3_jan

        Disaster(**disaster_attr).save()
        Disaster(**disaster_attr2).save()

        response = self.client.get(self.API_ENDPOINT+'?from=%s&to=%s&status=Closed&format=json' % (date_1_jan, date_3_jan))

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertEqual(datetime.datetime.strptime(date_3_jan, "%Y-%m-%d"), response.data[0]['date'])

        response = self.client.get(self.API_ENDPOINT+'?from=%s&status=closed&format=json' % date_3_jan)

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertEqual(datetime.datetime.strptime(date_3_jan, "%Y-%m-%d"), response.data[0]['date'])

        response = self.client.get(self.API_ENDPOINT+'?to=%s&status=assessment&format=json' % date_1_jan)

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertEqual(datetime.datetime.strptime(date_1_jan, "%Y-%m-%d"), response.data[0]['date'])
Example #2
0
    def test_should_retrieve_message_stats_in_subcounties_when_district_name_supplied(
            self):
        RapidProMessage(**self.message).save()

        bugolobi_name = 'Bugolobi'
        bugolobi = Location(**dict(
            name=bugolobi_name, parent=self.kampala, type='subcounty')).save()
        text = "NECOC.%s. flood" % bugolobi_name
        message_bugolobi = dict(phone_no='123444',
                                text=text,
                                received_at=self.date_time,
                                relayer_id=234,
                                run_id=23243)
        RapidProMessage(**message_bugolobi).save()

        Disaster(**self.disaster_attr).save()
        disaster_attr_bugolobi = self.disaster_attr.copy()
        disaster_attr_bugolobi["locations"] = [bugolobi]
        Disaster(**disaster_attr_bugolobi).save()

        multi_location_stats_service = MultiLocationStatsService(
            self.kampala.name)
        stats = multi_location_stats_service.stats()
        self.assertEqual(1, len(stats))

        self.assertEqual(1, stats['Bugolobi'].messages.count)
        self.assertEqual(50, stats['Bugolobi'].messages.percentage)
        self.assertEqual(1, stats['Bugolobi'].disasters.count)
        self.assertEqual(50, stats['Bugolobi'].disasters.percentage)
Example #3
0
    def test_filter_stats_in_subcounty_by_disaster_type(self):

        bugolobi_name = 'Bugolobi'
        bugolobi = Location(**dict(
            name=bugolobi_name, parent=self.kampala, type='subcounty')).save()

        Disaster(**self.disaster_attr).save()
        disaster_attr_bugolobi = self.disaster_attr.copy()
        disaster_attr_bugolobi["locations"] = [bugolobi]
        disaster_bugolobi = Disaster(**disaster_attr_bugolobi).save()

        expected_serialized_data = {
            'disasters': {
                'count': 1,
                'affected': 1,
                'types': {
                    'Flood': 1
                }
            }
        }

        url = self.API_ENDPOINT + '?location=%s&format=json&disaster_type=%s' % (
            'Bugolobi', str(self.disaster_type.id))
        response = self.client.get(url, format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)
Example #4
0
    def test_should_retrieve_message_stats_in_district(self):
        bugolobi_name = 'Bugolobi'
        bugolobi = Location(**dict(
            name=bugolobi_name, parent=self.kampala, type='subcounty')).save()
        Disaster(**self.disaster_attr).save()
        disaster_attr_bugolobi = self.disaster_attr.copy()
        disaster_attr_bugolobi["locations"] = [bugolobi]
        Disaster(**disaster_attr_bugolobi).save()

        expected_serialized_data = {
            'disasters': {
                'count': 2,
                'affected': 2,
                'types': {
                    'Flood': 2
                }
            }
        }

        url = '%s?location=%s' % (self.API_ENDPOINT,
                                  str(self.kampala.name.lower()))
        response = self.client.get(url, format='json')
        self.assertEqual(200, response.status_code)

        self.assertEqual(expected_serialized_data, response.data)
 def test_changing_disaster_status_sends_email_to_stakeholders(self, mock_send_email):
     disaster = Disaster(**self.disaster).save()
     disaster.status = 'Closed'
     disaster.save()
     mock_send_email.assert_called_with('Status of Disaster Risk has changed',
                                        mock.ANY,
                                        settings.DEFAULT_FROM_EMAIL,
                                        settings.DISASTER_NOTIFY_STATUS)
    def test_should_post_a_single_disaster(self):
        disaster = Disaster(**self.disaster_to_post).save()
        self.disaster_to_post['description'] = "Giant Flood"
        response = self.client.post(self.API_ENDPOINT + str(disaster.id) + '/',
                                    data=json.dumps(self.disaster_to_post),
                                    content_type="application/json")
        self.assertEqual(200, response.status_code)

        retrieved_disaster = Disaster.objects(description="Giant Flood")
        self.assertEqual(1, retrieved_disaster.count())
    def test_should_return_filtered_csv_when_csv_endpoint_is_called(self):
        Disaster(**self.disaster).save()
        disaster_attr2 = self.disaster.copy()
        disaster_attr2['status'] = "Closed"
        Disaster(**disaster_attr2).save()
        disaster2 = self.disaster.copy()
        disaster2['name'] = self.disaster_type2
        sdisaster = Disaster(**disaster2).save()

        response = self.client.get(self.CSV_ENDPOINT, format='csv')

        expected_response = "name,description,location,status,date\r\n" \
                            "Fire,Big Flood,%s,Assessment,2014-12-01 00:00:00" % (self.district.name,)
        expected_response = expected_response + "\r\nFire,Big Flood,%s,Closed,2014-12-01 00:00:00" % (self.district.name)
        expected_response = expected_response + "\r\nFlood,Big Flood,%s,Assessment,2014-12-01 00:00:00" % (self.district.name)

        self.assertEqual(200, response.status_code)
        self.assertEqual(3, len(response.data))
        self.assertTrue(isinstance(response.accepted_renderer, CSVRenderer))
        self.assertEqual(collections.Counter(split_text(expected_response)), collections.Counter(split_text(response.content)))

        response = self.client.get(self.CSV_ENDPOINT + '?status=Assessment&from=undefined', format='csv')

        expected_response = "name,description,location,status,date\r\n" \
                            "Fire,Big Flood,%s,Assessment,2014-12-01 00:00:00" % (self.district.name,)
        expected_response = expected_response + "\r\nFlood,Big Flood,%s,Assessment,2014-12-01 00:00:00" % (self.district.name)

        self.assertEqual(200, response.status_code)
        self.assertEqual(2, len(response.data))
        self.assertTrue(isinstance(response.accepted_renderer, CSVRenderer))
        self.assertEqual(collections.Counter(split_text(expected_response)), collections.Counter(split_text(response.content)))

        sdisaster.date = "2014-12-02 00:00:00"
        sdisaster.save()

        response = self.client.get(self.CSV_ENDPOINT + '?status=Assessment&from=2014-12-02 00:00:00', format='csv')

        expected_response = "name,description,location,status,date\r\n" \
                            "Flood,Big Flood,%s,Assessment,2014-12-02 00:00:00" % (self.district.name)

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertTrue(isinstance(response.accepted_renderer, CSVRenderer))
        self.assertEqual(collections.Counter(split_text(expected_response)), collections.Counter(split_text(response.content)))

        response = self.client.get(self.CSV_ENDPOINT + '?to=2014-12-01 00:00:00', format='csv')

        expected_response = "name,description,location,status,date\r\n" \
                            "Fire,Big Flood,%s,Assessment,2014-12-01 00:00:00" % (self.district.name,)
        expected_response = expected_response + "\r\nFire,Big Flood,%s,Closed,2014-12-01 00:00:00" % (self.district.name)

        self.assertEqual(200, response.status_code)
        self.assertEqual(2, len(response.data))
        self.assertTrue(isinstance(response.accepted_renderer, CSVRenderer))
        self.assertEqual(collections.Counter(split_text(expected_response)), collections.Counter(split_text(response.content)))
Example #8
0
    def test_should_filter_stats_in_country_by_date_and_disaster_type(self):
        kampala_disaster = Disaster(**self.disaster_attr).save()
        bukoto_disaster = Disaster(**self.disaster_attr_bukoto).save()

        expected_serialized_data = {
            'disasters': {
                'count': 2,
                'affected': 2,
                'types': {
                    'Flood': 2
                }
            }
        }

        from_ = self.date_time - datetime.timedelta(days=1)
        from_ = str(from_.date())
        to_ = self.date_time + datetime.timedelta(days=1)
        to_ = str(to_.date())

        response = self.client.get(
            self.API_ENDPOINT + '?format=json&from=%s&to=%s&disaster_type=%s' %
            (from_, to_, str(self.disaster_type.id)))
        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)

        other_type = DisasterType(
            **dict(name='Fire', description="Some flood")).save()

        expected_serialized_data = {
            'disasters': {
                'count': 0,
                'affected': 0,
                'types': {}
            }
        }

        url = self.API_ENDPOINT + '?format=json&from=%s&to=%s&disaster_type=%s' % (
            from_, to_, str(other_type.id))
        response = self.client.get(url, format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)

        url = self.API_ENDPOINT + '?format=json&from=%s&disaster_type=%s' % (
            to_, str(self.disaster_type.id))
        response = self.client.get(url, format='json')
        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)

        url = self.API_ENDPOINT + '?format=json&to=%s&disaster_type=%s' % (
            from_, str(self.disaster_type.id))
        response = self.client.get(url, format='json')
        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)
Example #9
0
    def test_subcounty_empty_GET_parameter_query(self):
        bugolobi_name = 'Bugolobi'
        bugolobi = Location(**dict(
            name=bugolobi_name, parent=self.kampala, type='subcounty')).save()

        Disaster(**self.disaster_attr).save()
        disaster_attr_bugolobi = self.disaster_attr.copy()
        disaster_attr_bugolobi["locations"] = [bugolobi]
        Disaster(**disaster_attr_bugolobi).save()

        expected_serialized_data = {
            'disasters': {
                'count': 1,
                'affected': 1,
                'types': {
                    'Flood': 1
                }
            }
        }

        from_ = self.date_time - datetime.timedelta(days=1)
        from_ = str(from_.date())
        to_ = self.date_time + datetime.timedelta(days=1)
        to_ = str(to_.date())

        url = self.API_ENDPOINT + '?location=%s&format=json&from=%s&to=%s&disaster_type=' % (
            'Bugolobi', from_, to_)
        response = self.client.get(url, format='json')
        self.assertEqual(200, response.status_code)

        self.assertEqual(expected_serialized_data, response.data)

        expected_serialized_data = {
            'disasters': {
                'count': 0,
                'affected': 0,
                'types': {}
            }
        }

        url = self.API_ENDPOINT + '?location=%s&format=json&from=%s&disaster_type=&to=' % (
            'Bugolobi', to_)
        response = self.client.get(url, format='json')
        self.assertEqual(200, response.status_code)

        self.assertEqual(expected_serialized_data, response.data)

        url = self.API_ENDPOINT + '?location=%s&format=json&to=%s' % (
            'Bugolobi', from_)
        response = self.client.get(url, format='json')
        self.assertEqual(200, response.status_code)

        self.assertEqual(expected_serialized_data, response.data)
Example #10
0
    def test_should_retrieve_message_count_in_a_location(self):
        Disaster(**self.disaster_attr).save()
        attr2 = self.disaster_attr.copy()
        attr2["status"] = "Closed"
        Disaster(**attr2).save()

        location_stats_service = LocationStatsService(location=self.district)
        stats = location_stats_service.aggregate_stats()
        disasters_stats = stats.disasters

        self.assertEqual(2, disasters_stats.count)
        self.assertEqual(100, disasters_stats.percentage)
    def test_filter_disaster_by_status(self):
        Disaster(**self.disaster).save()
        disaster_attr2 = self.disaster.copy()
        disaster_attr2['status'] = "Closed"
        Disaster(**disaster_attr2).save()

        response = self.client.get(self.API_ENDPOINT+'?status=Closed&format=json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertEqual('Closed', response.data[0]['status'])
        self.assertEqual(self.disaster_to_post['date'], str(response.data[0]['date']))
        self.assertEqual(self.disaster_to_post['description'], response.data[0]['description'])
Example #12
0
    def test_should_retrieve_message_count_affected_types_of_a_district(self):
        Disaster(**self.disaster_attr).save()
        attr2 = self.disaster_attr.copy()
        attr2["status"] = "Closed"
        Disaster(**attr2).save()

        location_stats_service = StatsSummaryService(location=self.kampala)
        stats = location_stats_service.aggregate_stats()
        disasters_stats = stats.disasters

        self.assertEqual(2, disasters_stats.count)
        self.assertEqual(1, disasters_stats.affected)
        self.assertEqual({'Flood': 2}, disasters_stats.types)
Example #13
0
    def test_types_of_a_subcounty(self):
        Disaster(**self.disaster_attr).save()
        fire_type = DisasterType(**dict(name='Fire', description="whatever")).save()
        attr2 = self.disaster_attr.copy()
        attr2["name"] = fire_type
        Disaster(**attr2).save()

        location_stats_service = StatsSummaryService(location=self.bukoto)
        stats = location_stats_service.aggregate_stats()
        disasters_stats = stats.disasters

        self.assertEqual(2, disasters_stats.count)

        self.assertEqual(1, disasters_stats.affected)
        self.assertEqual({'Flood': 1, 'Fire': 1}, disasters_stats.types)
Example #14
0
    def test_should_retrieve_disasters_percentage_in_a_location(self):
        Disaster(**self.disaster_attr).save()
        attr2 = self.disaster_attr.copy()
        attr2["locations"] = [
            Location(**dict(name='Location that is not Kampala',
                            type='district')).save()
        ]
        Disaster(**attr2).save()

        location_stats_service = LocationStatsService(location=self.district)
        stats = location_stats_service.aggregate_stats()
        disasters_stats = stats.disasters

        self.assertEqual(1, disasters_stats.count)
        self.assertEqual(50, disasters_stats.percentage)
    def test_should_serialize_summary_stats_service_integration(self):
        disaster_type = DisasterType(
            **dict(name='Flood', description="Some flood"))
        disaster_type.save()

        disaster_attr = dict(name=disaster_type,
                             locations=[self.district],
                             description="Big Flood",
                             date="2014-12-01",
                             status="Assessment")

        Disaster(**disaster_attr).save()

        location_stats_service = StatsSummaryService(location=self.district)
        queryset = location_stats_service.aggregate_stats()
        serialized_object = SummaryStatsSerializer(queryset)
        serialized_data = {
            'disasters': {
                'count': 1,
                'affected': 1,
                'types': {
                    'Flood': 1
                }
            }
        }

        self.assertEqual(serialized_data, serialized_object.data)
Example #16
0
    def test_editing_existing_message_is_valid(self):
        district = Location(
            **dict(name='Kampala', type='district', parent=None)).save()
        disaster_type = DisasterType(
            **dict(name="Fire", description="Fire")).save()

        disaster_attributes = dict(name=disaster_type,
                                   locations=[district],
                                   description="Big Flood",
                                   date="2014-12-01 00:00:00",
                                   status="Assessment")
        disaster = Disaster(**disaster_attributes).save()

        message = RapidProMessage(**self.serialized_data).save()

        data = self.serialized_data.copy()
        data['disaster'] = disaster.id

        serializer = RapidProMessageSerializer(message, data=data)

        self.assertTrue(serializer.is_valid())
        new_message = serializer.save()

        self.assertTrue(message.id, new_message.id)
        self.assertEqual(disaster, new_message.disaster)
Example #17
0
    def test_should_serialize_location_stats_service_integration(self):
        disaster_type = DisasterType(
            **dict(name='Flood', description="Some flood"))
        disaster_type.save()

        disaster_attr = dict(name=disaster_type,
                             locations=[self.district],
                             description="Big Flood",
                             date="2014-12-01",
                             status="Assessment")

        Disaster(**disaster_attr).save()

        location_stats_service = LocationStatsService(location=self.district)
        queryset = location_stats_service.aggregate_stats()
        serialized_object = LocationStatsSerializer(queryset)
        serialized_data = {
            'messages': {
                'count': 1,
                'percentage': 50,
                'reporter_ratio': 0
            },
            'disasters': {
                'count': 1,
                'percentage': 100,
                'reporter_ratio': 0
            }
        }

        self.assertEqual(serialized_data, serialized_object.data)
    def setUp(self):
        self.date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
        self.date_time = self.date_time.replace(tzinfo=pytz.utc)
        self.district = Location(**dict(name='Kampala', parent=None, type='district')).save()
        self.village = Location(**dict(name='Bukoto', parent=self.district, type='village')).save()
        self.mobile_user = UserProfile(**dict(name='timothy', phone="+256775019449",
                                             location=self.village, email=None)).save()

        self.fire_type = DisasterType(**dict(name="Fire", description="Fire")).save()
        disaster_attributes = dict(name=self.fire_type, locations=[self.district],
                                   description="Big Fire", date="2014-12-01 00:00:00", status="Assessment")
        self.disaster = Disaster(**disaster_attributes).save()

        self.text_format = "NECOC.%s. There is a fire"
        text = self.text_format % self.village.name
        self.expected_message = dict(phone="+256775019449", text=text, time=self.date_time, relayer=234,
                                     run=23243)
        self.message = dict(phone_no="+256775019449", text=text, received_at=self.date_time,
                            relayer_id=234, run_id=23243)

        self.api_user, created = User.objects.get_or_create(**dict(username='******'))
        self.auto_message_response = dict(phone_numbers=[u'+256775019449'], text=settings.AUTO_RESPONSE_MESSAGE)

        self.cao_group, created = Group.objects.get_or_create(name='CAO')
        self.cao_user = User.objects.create(username='******', group=self.cao_group, email='*****@*****.**')
        self.cao_user.set_password('password')
        self.login_url = '/login/'
        self.login_data = {
            'username': '******',
            'password': '******'
        }

        AdminSetting(**dict(name='enable_automatic_response')).save()
        AdminSetting(**dict(name='enable_volunteer_profiles')).save()
Example #19
0
    def test_should_retrieve_message_count_affected_types_of_a_subcounty(self):
        Disaster(**self.disaster_attr).save()
        fire_type = DisasterType(**dict(name='Fire', description="whatever")).save()
        attr2 = self.disaster_attr.copy()
        attr2["locations"] = [Location(**dict(name='Location that is not Kampala', type='district')).save()]
        attr2["name"] = fire_type
        Disaster(**attr2).save()

        location_stats_service = StatsSummaryService(location=self.bukoto)
        stats = location_stats_service.aggregate_stats()
        disasters_stats = stats.disasters

        self.assertEqual(1, disasters_stats.count)

        self.assertEqual(1, disasters_stats.affected)
        self.assertEqual({'Flood': 1}, disasters_stats.types)
    def test_should_filter_csv_when_date_range_is_specified(self):
        new_date_time = datetime.datetime(2015, 9, 17, 16, 0, 49, 807000)
        new_date_time = new_date_time.replace(tzinfo=pytz.utc)
        disaster_type = DisasterType(**dict(name='Fire', description="Some fire")).save()
        disaster_attr = dict(name=disaster_type, locations=[self.district], description="Big Fire",
                                  date=self.date_time,
                                  status="Assessment")
        kampala_disaster = Disaster(**disaster_attr).save()

        RapidProMessage(**self.message).save()
        self.message['received_at'] = new_date_time
        RapidProMessage(**self.message).save()
        expected_response = "phone,text,source,location,time\n" \
                            "+256775019449,NECOC.Bukoto. There is a fire,timothy,Kampala >> Bukoto,%s,Fire" % self.date_time

        response = self.client.get(self.CSV_ENDPOINT, {'dfrom' : 'undefined', 'dto': '2014-11-26', 'format': 'csv'})
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))

        response = self.client.get(self.CSV_ENDPOINT, {'dfrom' : '2015-01-01', 'dto': 'undefined', 'format': 'csv'})
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))

        response = self.client.get(self.CSV_ENDPOINT, {'dfrom' : '2014-01-01', 'dto': '2014-11-26', 'format': 'csv'})
        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))

        response = self.client.get(self.CSV_ENDPOINT, {'dfrom' : '2014-01-01', 'dto': '2015-11-26', 'format': 'csv'})
        self.assertEqual(200, response.status_code)
        self.assertEqual(2, len(response.data))

        self.assertTrue(isinstance(response.accepted_renderer, CSVRenderer))
Example #21
0
    def test_should_retrieve_summary_stats_in_the_country(self):
        Disaster(**self.disaster_attr).save()
        Disaster(**self.disaster_attr_bukoto).save()

        expected_serialized_data = {
            'disasters': {
                'count': 2,
                'affected': 2,
                'types': {
                    'Flood': 2
                }
            }
        }

        response = self.client.get(self.API_ENDPOINT, format='json')
        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)
    def test_should_get_a_single_disaster(self):
        disaster = Disaster(**self.disaster).save()
        response = self.client.get(self.API_ENDPOINT + str(disaster.id) + '/', format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(self.disaster_to_post['status'], response.data['status'])
        self.assertEqual(self.disaster_to_post['date'], str(response.data['date']))
        self.assertEqual(self.disaster_to_post['description'], response.data['description'])
    def test_should_get_a_list_of_disasters(self):
        Disaster(**self.disaster).save()
        response = self.client.get(self.API_ENDPOINT, format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertEqual(self.disaster_to_post['status'], response.data[0]['status'])
        self.assertEqual(self.disaster_to_post['date'], str(response.data[0]['date']))
        self.assertEqual(self.disaster_to_post['description'], response.data[0]['description'])
Example #24
0
    def test_in_country_empty_GET_parameter_query(self):
        Disaster(**self.disaster_attr).save()
        Disaster(**self.disaster_attr_bukoto).save()

        expected_serialized_data = {
            'disasters': {
                'count': 2,
                'affected': 2,
                'types': {
                    'Flood': 2
                }
            }
        }

        from_ = self.date_time - datetime.timedelta(days=1)
        from_ = str(from_.date())
        to_ = self.date_time + datetime.timedelta(days=1)
        to_ = str(to_.date())

        response = self.client.get(
            self.API_ENDPOINT + '?format=json&from=%s&to=%s&disaster_type=' %
            (from_, to_))
        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)

        expected_serialized_data = {
            'disasters': {
                'count': 0,
                'affected': 0,
                'types': {}
            }
        }

        response = self.client.get(self.API_ENDPOINT +
                                   '?format=json&from=%s&disaster_type=&to=' %
                                   to_)
        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)

        response = self.client.get(self.API_ENDPOINT +
                                   '?format=json&to=%s&disaster_type=&from=' %
                                   from_)
        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)
Example #25
0
    def test_should_filter_stats_in_country_by_disaster_types(self):
        kampala_disaster = Disaster(**self.disaster_attr).save()
        bukoto_disaster = Disaster(**self.disaster_attr_bukoto).save()

        expected_serialized_data = {
            'disasters': {
                'count': 2,
                'affected': 2,
                'types': {
                    'Flood': 2
                }
            }
        }

        response = self.client.get(self.API_ENDPOINT +
                                   '?format=json&disaster_type=%s' %
                                   str(self.disaster_type.id))

        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)

        disaster_type2 = DisasterType(
            **dict(name='Fire', description="Some fire")).save()
        disaster_attr = self.disaster_attr.copy()
        disaster_attr['name'] = disaster_type2
        Disaster(**disaster_attr).save()

        expected_serialized_data = {
            'disasters': {
                'count': 1,
                'affected': 1,
                'types': {
                    'Fire': 1
                }
            }
        }

        response = self.client.get(self.API_ENDPOINT +
                                   '?format=json&disaster_type=%s' %
                                   str(disaster_type2.id))

        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)
 def test_should_auto_associate_message_to_disaster_when_posted_from_subcounty(self):
     disaster_type = DisasterType(**dict(name='Flood', description="Some flood")).save()
     disaster_attr = dict(name=disaster_type, locations=[self.district], description="Big Flood",
                               date=self.date_time,
                               status="Assessment")
     kampala_disaster = Disaster(**disaster_attr).save()
     text = "NECOC.%s. flood here and allover the place!" % self.village.name
     self.message['text'] = text
     saved_message = RapidProMessage(**self.message).save()
     self.assertEqual(saved_message.disaster, kampala_disaster)
Example #27
0
    def test_should_retrieve_message_stats_in_all_locations(self):
        RapidProMessage(**self.message).save()
        RapidProMessage(**self.message_bukoto).save()
        Disaster(**self.disaster_attr).save()
        Disaster(**self.disaster_attr_bukoto).save()

        multi_location_stats_service = MultiLocationStatsService(location=None)
        stats = multi_location_stats_service.stats()
        self.assertEqual(2, len(stats))

        self.assertEqual(1, stats['Kampala'].messages.count)
        self.assertEqual(50, stats['Kampala'].messages.percentage)
        self.assertEqual(1, stats['Kampala'].disasters.count)
        self.assertEqual(50, stats['Kampala'].disasters.percentage)

        self.assertEqual(1, stats['Bukoto'].messages.count)
        self.assertEqual(50, stats['Bukoto'].messages.percentage)
        self.assertEqual(1, stats['Bukoto'].disasters.count)
        self.assertEqual(50, stats['Bukoto'].disasters.percentage)
Example #28
0
    def test_should_retrieve_message_count_affected_types_countrywide(self):
        wakiso = Location(**dict(name='Wakiso', type='district', parent=None)).save()
        wakiso_disaster_attr = self.disaster_attr.copy()
        wakiso_disaster_attr['locations'] = [wakiso]
        Disaster(**wakiso_disaster_attr).save()

        Disaster(**self.disaster_attr).save()

        attr2 = self.disaster_attr.copy()
        attr2["status"] = "Closed"
        Disaster(**attr2).save()

        location_stats_service = StatsSummaryService(location=None)
        stats = location_stats_service.aggregate_stats()
        disasters_stats = stats.disasters

        self.assertEqual(3, disasters_stats.count)
        self.assertEqual(2, disasters_stats.affected)
        self.assertEqual({'Flood': 3}, disasters_stats.types)
    def test_filter_stats_in_subcounties_in_district_by_disaster_type(self):
        RapidProMessage(**self.message).save()

        bugolobi_name = 'Bugolobi'
        bugolobi = Location(**dict(
            name=bugolobi_name, parent=self.kampala, type='subcounty')).save()
        text = "NECOC.%s. flood" % bugolobi_name
        message_bugolobi = dict(phone_no='123444',
                                text=text,
                                received_at=self.date_time,
                                relayer_id=234,
                                run_id=23243)

        Disaster(**self.disaster_attr).save()
        disaster_attr_bugolobi = self.disaster_attr.copy()
        disaster_attr_bugolobi["locations"] = [bugolobi]
        disaster_bugolobi = Disaster(**disaster_attr_bugolobi).save()
        message_bugolobi['disaster'] = disaster_bugolobi

        RapidProMessage(**message_bugolobi).save()

        expected_serialized_data = {
            'bugolobi': {
                'messages': {
                    'count': 1,
                    'percentage': 100,
                    'reporter_ratio': 0
                },
                'disasters': {
                    'count': 1,
                    'percentage': 50,
                    'reporter_ratio': 0
                }
            }
        }

        url = self.API_ENDPOINT + '%s/?format=json&disaster_type=%s' % (str(
            self.kampala.name.lower()), str(self.disaster_type.id))
        response = self.client.get(url, format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(expected_serialized_data, response.data)
    def test_should_return_csv_with_all_records_when_empty_filters_are_used_on_csv_endpoint(self):
        Disaster(**self.disaster).save()
        disaster_attr2 = self.disaster.copy()
        disaster_attr2['status'] = "Closed"
        Disaster(**disaster_attr2).save()
        disaster2 = self.disaster.copy()
        disaster2['name'] = self.disaster_type2
        sdisaster = Disaster(**disaster2).save()

        response = self.client.get(self.CSV_ENDPOINT + '?status=undefined&from=undefined&to=undefined', format='csv')

        expected_response = "name,description,location,status,date\r\n" \
                            "Fire,Big Flood,%s,Assessment,2014-12-01 00:00:00" % (self.district.name,)
        expected_response = expected_response + "\r\nFire,Big Flood,%s,Closed,2014-12-01 00:00:00" % (self.district.name)
        expected_response = expected_response + "\r\nFlood,Big Flood,%s,Assessment,2014-12-01 00:00:00" % (self.district.name)

        self.assertEqual(200, response.status_code)
        self.assertEqual(3, len(response.data))
        self.assertTrue(isinstance(response.accepted_renderer, CSVRenderer))
        self.assertEqual(collections.Counter(split_text(expected_response)), collections.Counter(split_text(response.content)))
    def test_should_return_only_district_disasters_for_CAO(self):
        self.user.group = self.cao_group
        self.user.save()
        self.mobile_user.user = self.user
        self.mobile_user.save()

        masaka_district = Location(**dict(name='Masaka', parent=None, type='district')).save()

        masaka_disaster = dict(name=self.disaster_type, locations=[masaka_district],
                             description="Big Flood", date="2014-12-01 00:00:00", status="Assessment")

        Disaster(**self.disaster).save()
        Disaster(**masaka_disaster).save()
        response = self.client.get(self.API_ENDPOINT, format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertEqual(self.disaster_to_post['status'], response.data[0]['status'])
        self.assertEqual(self.disaster_to_post['date'], str(response.data[0]['date']))
        self.assertEqual(self.disaster_to_post['description'], response.data[0]['description'])
        self.assertEqual('Kampala', response.data[0]['locations'][0]['name'])
    def test_should_post_a_disaster(self):
        response = self.client.post(self.API_ENDPOINT, data=json.dumps(self.disaster_to_post), content_type="application/json")
        self.assertEqual(201, response.status_code)

        retrieved_disaster = Disaster.objects(description="Big Flood")
        self.assertEqual(1, retrieved_disaster.count())
Example #33
0
 def query_by_disaster_type(self, disaster_type, queryset=None):
     disaster_type = DisasterType.objects(id=disaster_type).first()
     disasters = Disaster.objects(name=disaster_type)
     return queryset.filter(disaster__in=disasters)