def test_need_manager_for_location(self):
        """
            checks that get_days_with_needs() returns only dates later than datetime.now()
        """
        now = datetime.now()
        yesterday_start = now - timedelta(1)
        yesterday_end = yesterday_start + timedelta(hours=1)
        tomorrow_start = now + timedelta(1)
        tomorrow_end = tomorrow_start + timedelta(hours=1)

        location = LocationFactory.create()

        yesterday_need = NeedFactory.create(location=location,
                                            starting_time=yesterday_start,
                                            ending_time=yesterday_end)
        tomorrow_need = NeedFactory.create(location=location,
                                           starting_time=tomorrow_start,
                                           ending_time=tomorrow_end)

        assert Location.objects.count(
        ) == 1, "test case assumes that needs have been created for the same location, as the NeedFactory indeed does at the time of writing of this test case"
        assert Location.objects.all()[0] == location

        needs = Need.open_needs.at_location(location=location)

        assert needs.count(
        ) == 1, "only 1 need should be found with Need.open_needs"
        assert needs[0] == tomorrow_need, "wrong shift was found"
        assert needs[0].ending_time > now, "the time has to be in the future"
def create_need(start_hour, end_hour):
    """
    Tiny helper because setting time periods is awkward till we remove the FK relationship.
    """
    start = datetime.datetime(2015, 1, 1, start_hour)
    end = datetime.datetime(2015, 1, 1, end_hour)
    return NeedFactory.create(starting_time=start, ending_time=end)
def create_need(start_hour, end_hour, location=None):
    """
    Tiny helper because setting time periods is awkward till we remove the FK relationship.
    """
    create_args = dict(starting_time=datetime(2015, 1, 1, start_hour),
                       ending_time=datetime(2015, 1, 1, end_hour))
    if location:
        create_args['location'] = location
    return NeedFactory.create(**create_args)
    def test_get_days_with_needs_at_location_no_duplicates(self):
        """
        checks that get_days_with_needs() does not return any duplicate dates
        """
        start = datetime.datetime.now()
        end = start + datetime.timedelta(hours=1)
        needs = [NeedFactory.create(starting_time=start, ending_time=end),
                    NeedFactory.create(starting_time=start, ending_time=end)]

        locations = set()
        for n in needs:
            locations.add(n.location)

        assert len(locations) == 1, "test case assumes that needs have been created for the same location, as the NeedFactory indeed does at the time of writing of this test case"
        #TODO change this assumption by explicitly creating needs for the same location

        for l in locations:
            dates = set()
            for d in l.get_days_with_needs():
                assert d[0] not in dates
                dates.add(d[0])
    def test_get_days_with_needs_at_location_end_later_than_now(self):
        """
            checks that get_days_with_needs() returns only dates later than datetime.now()
        """
        now = datetime.datetime.now()
        yesterday_start = now - datetime.timedelta(1)
        yesterday_end = yesterday_start+datetime.timedelta(hours=1)
        tomorrow_start = now + datetime.timedelta(1)
        tomorrow_end = tomorrow_start+datetime.timedelta(hours=1)

        needs = [NeedFactory.create(starting_time=yesterday_start, ending_time=yesterday_end),
                    NeedFactory.create(starting_time=tomorrow_start, ending_time=tomorrow_end)]

        locations = set()
        for n in needs:
            locations.add(n.location)

        assert len(locations) == 1, "test case assumes that needs have been created for the same location, as the NeedFactory indeed does at the time of writing of this test case"
        #TODO change this assumption by explicitly creating needs for the same location

        for l in locations:
            for day in l.get_days_with_needs():
                assert isinstance(day[0], datetime.datetime)
                assert day[0] > datetime.datetime.now()
Beispiel #6
0
    def handle(self, *args, **options):
        if options['flush']:
            print "delete all data in app tables"
            RegistrationProfile.objects.all().delete()
            Need.objects.all().delete()
            Location.objects.all().delete()
            Topics.objects.all().delete()

            # delete geographic information
            Country.objects.all().delete()
            Region.objects.all().delete()
            Area.objects.all().delete()
            Place.objects.all().delete()

            User.objects.filter().exclude(is_superuser=True).delete()

        # create regional data
        places = list()
        for i in range(0, 10):
            places.append(PlaceFactory.create())

        # create shifts for number of days
        for day in range(0, options['days'][0]):
            for i in range(2, 23):
                topic = TopicFactory.create(title=random.choice(HELPTOPICS))
                location = LocationFactory.create(
                    name="Shelter" + str(random.randint(0, 9)),
                    place=places[random.randint(0,
                                                len(places) - 1)],
                    additional_info=LOREM)
                need = NeedFactory.create(starting_time=self.gen_date(hour=i -
                                                                      1,
                                                                      day=day),
                                          ending_time=self.gen_date(hour=i,
                                                                    day=day),
                                          topic=topic,
                                          location=location)
                # assign random volunteer for each need
                reg_user = ShiftHelperFactory.create(need=need)
                reg_user.save()
    def handle(self, *args, **options):
        if options['flush']:
            print "delete all data in app tables"
            RegistrationProfile.objects.all().delete()
            Need.objects.all().delete()
            Location.objects.all().delete()
            Topics.objects.all().delete()

            # delete geographic information
            Country.objects.all().delete()
            Region.objects.all().delete()
            Area.objects.all().delete()
            Place.objects.all().delete()

            User.objects.filter().exclude(is_superuser=True).delete()

        # create regional data
        places = list()
        for i in range(0,10):
            places.append(PlaceFactory.create())

        # create shifts for number of days
        for day in range(0, options['days'][0]):
            for i in range(2, 23):
                topic = TopicFactory.create(title=random.choice(HELPTOPICS))
                location = LocationFactory.create(
                    name="Shelter" + str(random.randint(0,9)),
                    place=places[random.randint(0,len(places)-1)],
                    additional_info=LOREM
                )
                need = NeedFactory.create(
                    starting_time=self.gen_date(hour=i-1, day=day),
                    ending_time=self.gen_date(hour=i, day=day),
                    topic=topic,
                    location=location
                )
                # assign random volunteer for each need
                reg_user = ShiftHelperFactory.create(need=need)
                reg_user.save()
    def handle(self, *args, **options):
        if options['flush']:
            print "delete all data in app tables"
            RegistrationProfile.objects.all().delete()
            Need.objects.all().delete()
            Location.objects.all().delete()
            Topics.objects.all().delete()
            User.objects.filter().exclude(is_superuser=True).delete()

        # create shifts for number of days
        for day in range(0, options['days'][0]):
            for i in range(2, 23):
                topic = TopicFactory.create(title=random.choice(HELPTOPICS))
                location = LocationFactory.create(name="Shelter" + str(random.randint(0,9)), additional_info=LOREM)
                need = NeedFactory.create(
                    starting_time=self.gen_date(hour=i-1, day=day),
                    ending_time=self.gen_date(hour=i, day=day),
                    topic=topic,
                    location=location
                )
                # assign random volunteer for each need
                reg_user = RegistrationProfileFactory.create()
                reg_user.needs.add(need)
                reg_user.save()