示例#1
0
    def setUp(self):
        self.location_name = 'Kampala'
        text = "NECOC.%s. fire baba fire" % self.location_name
        date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
        phone_number = "+256775019449"
        self.message = dict(phone_no=phone_number,
                            text=text,
                            received_at=date_time,
                            relayer_id=234,
                            run_id=23243)
        self.district = Location(**dict(
            name=self.location_name, parent=None, type='district')).save()
        self.bukoto_name = 'Bukoto'
        self.bukoto = Location(**dict(
            name=self.bukoto_name, parent=None, type='district')).save()
        text = "NECOC.%s. flood" % self.bukoto_name
        self.message_bukoto = dict(phone_no=phone_number,
                                   text=text,
                                   received_at=date_time,
                                   relayer_id=234,
                                   run_id=23243)

        disaster_type = DisasterType(
            **dict(name='Flood', description="Some flood"))
        disaster_type.save()

        self.disaster_attr = dict(name=disaster_type,
                                  locations=[self.district],
                                  description="Big Flood",
                                  date="2014-12-01",
                                  status="Assessment")
    def setUp(self):
        date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
        phone_number = "256775019449"
        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=phone_number,
                                              location=self.village,
                                              email=None)).save()

        self.poll_attr = dict(name="Disaster",
                              question="How many disasters are in your area?",
                              keyword="some_word",
                              target_locations=[str(self.village.id)])
        self.poll = Poll(**self.poll_attr).save()

        self.text_format = "NECOCPoll %s there are 4 or 5"
        text = self.text_format % self.poll_attr['keyword']

        self.poll_response = dict(phone_no=phone_number,
                                  text=text,
                                  received_at=date_time,
                                  relayer_id=234,
                                  run_id=23243)
        self.serialized_data = dict(phone=phone_number,
                                    time=date_time,
                                    relayer=234,
                                    run=23243,
                                    text=text)
示例#3
0
    def test_should_know_its_children(self):
        district = Location(**dict(name='Kampala', parent=None, type='district')).save()
        bukoto = Location(**dict(name='Bukoto', parent=district, type='village')).save()
        wakiso = Location(**dict(name='Wakiso', parent=district, type='village')).save()

        district_children = district.children()

        self.assertEqual(2, len(district_children))
        self.assertIn(bukoto, district_children)
        self.assertIn(wakiso, district_children)
示例#4
0
    def test_should_filter_locations_by_no_parent(self):
        kampala = Location(**self.district).save()
        village = dict(name='Wakiso', type='village', parent=kampala.id)
        Location(**village).save()

        response = self.client.get(self.LOCATION_ENDPOINT +
                                   "?parent=&format=json")

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertDictContainsSubset(self.expected_district, response.data[0])
示例#5
0
    def test_should_filter_locations_by_type(self):
        village = dict(name='Wakiso', type='village')
        Location(**self.district).save()
        Location(**village).save()

        response = self.client.get(self.LOCATION_ENDPOINT + '?type=district',
                                   format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertDictContainsSubset(self.expected_district, response.data[0])
示例#6
0
    def setUp(self):
        self.user = self.login_user()
        self.district_to_post = dict(name='Kampala', type='district')
        self.district = dict(name='Kampala', type='district')

        self.kampala = Location(**self.district).save()
        self.wakiso_attr = dict(name='Wakiso',
                                type='county',
                                parent=self.kampala.id)
        self.masaka_attr = dict(name='masaka', type='county')

        self.wakiso = Location(**self.wakiso_attr).save()
        self.masaka = Location(**self.masaka_attr).save()
示例#7
0
    def test_should_filter_locations_by_parent(self):
        kampala = Location(**self.district).save()
        village = dict(name='Wakiso', type='village')
        Location(parent=kampala, **village).save()

        response = self.client.get(self.LOCATION_ENDPOINT, {
            "parent": kampala.id,
            "format": "json"
        })

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertDictContainsSubset(village, response.data[0])
示例#8
0
    def test_should_get_a_list_of_locations(self):
        Location(**self.district).save()
        response = self.client.get(self.LOCATION_ENDPOINT, format='json')

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))
        self.assertDictContainsSubset(self.expected_district, response.data[0])
示例#9
0
    def test_messages_by_from_and_to_date_and_location_when_initial_queryset_is_empty(
            self):
        location_name = 'Abim'
        district = Location(
            **dict(name=location_name, parent=None, type='district')).save()
        message_attr = self.message.copy()
        message_attr['location'] = district
        message_attr['received_at'] = datetime.datetime(
            2014, 12, 17, 16, 0, 49, 807000)
        message = RapidProMessageBase(**message_attr).save()

        self.message['location'] = district
        message1 = RapidProMessageBase(**self.message).save()

        location_messages = RapidProMessageBase.from_(
            district, **{
                'from': '2014-09-17',
                'to': '2014-10-17'
            })

        self.assertEqual(1, location_messages.count())
        self.assertIn(message1, location_messages)
        self.assertNotIn(message, location_messages)

        empty_queryset = RapidProMessageBase.objects().none()

        location_messages = RapidProMessageBase.from_(
            district, empty_queryset, **{
                'from': '2014-09-17',
                'to': '2014-10-17'
            })

        self.assertEqual(0, location_messages.count())
示例#10
0
    def test_should_post_a_location_without_a_parent(self):
        response = self.client.post(self.LOCATION_ENDPOINT,
                                    data=self.district_to_post)
        self.assertEqual(201, response.status_code)

        retrieved_location = Location.objects(**self.district)
        self.assertEqual(1, retrieved_location.count())
示例#11
0
    def test_get_messages_given_from_date_and_to_date(self):
        location_name = 'Abim'
        district = Location(
            **dict(name=location_name, parent=None, type='district')).save()
        message_attr = self.message.copy()
        message_attr['location'] = district
        message_attr['received_at'] = datetime.datetime(
            2014, 12, 17, 16, 0, 49, 807000)
        message = RapidProMessageBase(**message_attr).save()

        self.message['location'] = district
        message1 = RapidProMessageBase(**self.message).save()

        location_messages = RapidProMessageBase.from_(
            district, **{
                'from': '2014-09-17',
                'to': '2014-10-17'
            })

        self.assertEqual(1, location_messages.count())
        self.assertIn(message1, location_messages)
        self.assertNotIn(message, location_messages)

        location_messages = RapidProMessageBase.from_(
            district, **{
                'from': None,
                'to': None
            })
        self.assertEqual(2, location_messages.count())
示例#12
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)
示例#13
0
 def get_non_children_queryset(self):
     fields = Location._fields_ordered
     query_params = {key: value or None for key, value in self.request.GET.items() if key in fields}
     user_profile = UserProfile.objects(user=self.request.user).first()
     user_group = self.request.user.group.name
     if user_profile and user_group in getattr(settings, "DISTRICT_GROUPS", []):
         user_locations = get_user_district_locations(self.request.user)
         query_params.update({"id__in": user_locations})
     return Location.objects(**query_params)
示例#14
0
    def setUp(self):
        date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
        phone_number = "256775019449"

        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=phone_number,
                                              location=self.village,
                                              email=None)).save()

        self.message = dict(phone_no=phone_number,
                            text="NECOC There is a fire",
                            received_at=date_time,
                            relayer_id=234,
                            run_id=23243)
示例#15
0
    def test_get_messages_from_children_are_also_added(self):
        location_name = 'Abim'
        district = Location(
            **dict(name=location_name, parent=None, type='district')).save()
        message_attr = self.message.copy()
        message_attr['location'] = district
        message = RapidProMessageBase(**message_attr).save()

        message1_attr = message_attr.copy()
        location_name = 'Wakiso'
        district_son = Location(**dict(
            name=location_name, parent=district, type='village')).save()
        message1_attr["location"] = district_son
        message1 = RapidProMessageBase(**message1_attr).save()

        location_messages = RapidProMessageBase.from_(district)

        self.assertEqual(2, location_messages.count())
        self.assertIn(message, location_messages)
        self.assertIn(message1, location_messages)
    def test_should_post_a_location_with_a_parent(self):
        response = self.client.post(self.LOCATION_ENDPOINT, data=self.district_to_post)
        saved_district_id = response.data['id']
        self.assertIsNotNone(saved_district_id)

        village_to_post = dict(name='Bukoto', type='village', parent=saved_district_id)
        response = self.client.post(self.LOCATION_ENDPOINT, data=village_to_post)
        self.assertEqual(201, response.status_code)

        retrieved_message = Location.objects(**village_to_post)
        self.assertEqual(1, retrieved_message.count())
示例#17
0
    def test_should_filter_location_children_by_county_location_type(self):
        wakiso_subcounty_attr = dict(name='Wakiso Subcounty',
                                     type='subcounty',
                                     parent=self.wakiso.id)
        masaka_subcounty_attr = dict(name='masaka Subcounty',
                                     type='subcounty',
                                     parent=self.masaka.id)
        wakiso_subcounty = Location(**wakiso_subcounty_attr).save()
        masaka_subcounty = Location(**masaka_subcounty_attr).save()

        response = self.client.get(self.LOCATION_ENDPOINT +
                                   "?county=%s&type=subcounty&format=json" %
                                   self.wakiso.id)

        self.assertEqual(200, response.status_code)
        self.assertEqual(1, len(response.data))

        wakiso_subcounty_attr['id'] = str(wakiso_subcounty.id)

        for key in ['id', 'name', 'type']:
            self.assertEqual(wakiso_subcounty_attr[key], response.data[0][key])
示例#18
0
    def test_get_messages_from_children_are_also_added(self):
        location_name = 'Abim'
        text = "NECOC.%s. fire baba fire" % location_name
        district = Location(
            **dict(name=location_name, parent=None, type='district')).save()
        message_attr = self.message.copy()
        message_attr['text'] = text
        message = RapidProMessage(**message_attr).save()

        message1_attr = message_attr.copy()
        location_name = 'Wakiso'
        text = "NECOC.%s. fire baba fire" % location_name
        district_son = Location(**dict(
            name=location_name, parent=district, type='village')).save()
        message1_attr["text"] = text
        message1 = RapidProMessage(**message1_attr).save()

        location_messages = RapidProMessage.from_(district)

        self.assertEqual(2, location_messages.count())
        self.assertIn(message, location_messages)
        self.assertIn(message1, location_messages)
示例#19
0
 def get_non_children_queryset(self):
     fields = Location._fields_ordered
     query_params = {
         key: value or None
         for key, value in self.request.GET.items() if key in fields
     }
     user_profile = UserProfile.objects(user=self.request.user).first()
     user_group = self.request.user.group.name
     if user_profile and user_group in getattr(settings, 'DISTRICT_GROUPS',
                                               []):
         user_locations = get_user_district_locations(self.request.user)
         query_params.update({'id__in': user_locations})
     return Location.objects(**query_params)
示例#20
0
 def setUp(self):
     self.date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
     phone_number = "+256775019449"
     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=phone_number,
                                           location=self.village,
                                           email=None)).save()
     text = "NECOC.%s. There is a fire" % self.village.name
     self.message = dict(phone_no=phone_number,
                         text=text,
                         received_at=self.date_time,
                         relayer_id=234,
                         run_id=23243)
     self.serialized_data = dict(phone=phone_number,
                                 time=self.date_time,
                                 relayer=234,
                                 run=23243,
                                 text=text)
     AdminSetting(**dict(name='enable_volunteer_profiles')).save()
示例#21
0
    def test_get_messages_from_a_location(self):
        location_name = 'Abim'
        district = Location(
            **dict(name=location_name, parent=None, type='district')).save()
        message_attr = self.message.copy()
        message_attr['location'] = district
        message = RapidProMessageBase(**message_attr).save()
        message1 = RapidProMessageBase(**self.message).save()

        location_messages = RapidProMessageBase.from_(district)

        self.assertEqual(1, location_messages.count())
        self.assertIn(message, location_messages)
        self.assertNotIn(message1, location_messages)
示例#22
0
 def setUp(self):
     self.user = self.login_user()
     self.initial_password = '******'
     self.user.set_password(self.initial_password)
     self.password_data = dict(old_password=self.initial_password,
                               new_password='******',
                               confirm_password='******')
     self.district = Location(
         **dict(name='Kampala', type='district', parent=None)).save()
     self.mobile_user_attr = dict(name='tim',
                                  phone='+256775019500',
                                  location=self.district.id,
                                  email='*****@*****.**',
                                  user=self.user)
示例#23
0
    def test_should_post_a_location_with_a_parent(self):
        response = self.client.post(self.LOCATION_ENDPOINT,
                                    data=self.district_to_post)
        saved_district_id = response.data['id']
        self.assertIsNotNone(saved_district_id)

        village_to_post = dict(name='Bukoto',
                               type='village',
                               parent=saved_district_id)
        response = self.client.post(self.LOCATION_ENDPOINT,
                                    data=village_to_post)
        self.assertEqual(201, response.status_code)

        retrieved_message = Location.objects(**village_to_post)
        self.assertEqual(1, retrieved_message.count())
示例#24
0
    def setUp(self):
        self.location_name = 'Kampala'
        text = "NECOC.%s. fire baba fire" % self.location_name
        date_time = datetime.datetime(2014, 9, 17, 16, 0, 49, 807000)
        phone_number = "+256775019449"
        self.message = dict(phone_no=phone_number,
                            text=text,
                            received_at=date_time,
                            relayer_id=234,
                            run_id=23243)
        self.district = Location(**dict(
            name=self.location_name, parent=None, type='district')).save()

        RapidProMessage(**self.message).save()
        message1 = self.message.copy()
        message1["text"] = "some message that is not coming from Kampala"
        RapidProMessage(**message1).save()
示例#25
0
    def test_should_save_location_with_a_parent(self):
        district = Location(**dict(name='Kampala', parent=None, type='district'))
        district.save()
        village = dict(name='Bukoto', parent=district, type='village')
        Location(**village).save()

        saved_villages = Location.objects(**village)
        self.assertEqual(1, saved_villages.count())

        saved_village = saved_villages[0]
        self.assertEqual(village['name'], saved_village.name)
        self.assertEqual(district['name'], saved_village.parent.name)
示例#26
0
    def test_get_messages_from_a_location(self):
        location_name = 'Abim'
        text = "NECOC.%s. fire baba fire" % location_name
        district = Location(
            **dict(name=location_name, parent=None, type='district')).save()
        message_attr = self.message.copy()
        message_attr['text'] = text
        message = RapidProMessage(**message_attr).save()

        message1_attr = message_attr.copy()
        message1_attr["text"] = " message without location"
        message1 = RapidProMessage(**message1_attr).save()

        location_messages = RapidProMessage.from_(district)

        self.assertEqual(1, location_messages.count())
        self.assertIn(message, location_messages)
        self.assertNotIn(message1, location_messages)
    def test_should_post_a_location_without_a_parent(self):
        response = self.client.post(self.LOCATION_ENDPOINT, data=self.district_to_post)
        self.assertEqual(201, response.status_code)

        retrieved_location = Location.objects(**self.district)
        self.assertEqual(1, retrieved_location.count())
示例#28
0
    def test_should_save_location_with_no_parent(self):
        district = dict(name='Kampala', parent=None, type='district')

        Location(**district).save()
        saved_districts = Location.objects(**district)
        self.assertEqual(1, saved_districts.count())
示例#29
0
    def test_to_string_child(self):
        kampala = Location(**(dict(name='Kampala', parent=None, type='district'))).save()
        bukoto = Location(**(dict(name='Bukoto', parent=kampala, type='village'))).save()

        self.assertEqual('Kampala >> Bukoto', str(bukoto))
示例#30
0
 def test_should_serialize_location_object(self):
     location = Location(**self.location).save()
     serialized_object = LocationSerializer(location)
     self.assertDictContainsSubset(self.serialized_location,
                                   serialized_object.data)
     self.assertIsNotNone(serialized_object.data['id'])
示例#31
0
 def tearDown(self):
     Location.drop_collection()
示例#32
0
    def test_to_string_parent(self):
        district = dict(name='Kampala', parent=None, type='district')
        location = Location(**district).save()

        self.assertEqual('Kampala', str(location))
示例#33
0
 def test_should_throw_validation_exception_when_type_is_not_valid(self):
     invalid_type_location = Location(**dict(name='Kampala', parent=None, type='mazrui'))
     self.assertRaises(ValidationError, invalid_type_location.save)