def setUp(self): # event that only occurs once self.event = EventFactory(rule=None) # event that occurs for one week daily with one custom occurrence self.event_daily = EventFactory() self.occurrence = OccurrenceFactory(event=self.event, title='foo_occurrence')
def setUp(self): self.not_found_event = EventFactory( set__start=-24, set__end=-24, set__creation_date=-24, rule=None) self.event = EventFactory() self.occurrence = OccurrenceFactory( event=self.event, title='foo_occurrence') self.single_time_event = EventFactory(rule=None)
class EventTestCase(TestCase): """Tests for the ``Event`` model.""" longMessage = True def setUp(self): self.not_found_event = EventFactory( set__start=-24, set__end=-24, set__creation_date=-24, rule=None) self.event = EventFactory() self.occurrence = OccurrenceFactory( event=self.event, title='foo_occurrence') self.single_time_event = EventFactory(rule=None) def test_create_occurrence(self): """Test for ``_create_occurrence`` method.""" occurrence = self.event._create_occurrence(now()) self.assertEqual(type(occurrence), Occurrence, msg=( 'Method ``_create_occurrence`` did not output the right type.')) def test_get_occurrence_gen(self): """Test for the ``_get_occurrence_gen`` method""" occurrence_gen = self.event._get_occurrence_gen( now(), now() + timedelta(days=8)) occ_list = [occ for occ in occurrence_gen] self.assertEqual(len(occ_list), 7, msg=( 'The method ``_get_occurrence_list`` did not return the expected' ' amount of items.')) occurrence_gen = self.not_found_event._get_occurrence_gen( now(), now() + timedelta(days=8)) occ_list = [occ for occ in occurrence_gen] self.assertEqual(len(occ_list), 0, msg=( 'The method ``_get_occurrence_list`` did not return the expected' ' amount of items.')) def test_get_occurrences(self): occurrence_gen = self.event.get_occurrences( now(), now() + timedelta(days=7)) occ_list = [occ for occ in occurrence_gen] self.assertEqual(len(occ_list), 7, msg=( 'Method ``get_occurrences`` did not output the correct amount' ' of occurrences.')) occurrence_gen = self.event.get_occurrences( now(), now() + timedelta(days=7)) self.assertEqual(occurrence_gen.next().title, 'foo_occurrence', msg=( 'The persistent occurrence should have been first in the list.'))
def setUp(self): # single, not recurring event self.event = EventFactory(rule=None, end_recurring_period=None) self.event_occurrence = self.event.get_occurrences( self.event.start).next() # recurring event weekly on mondays over 6 weeks self.rule = RuleFactory( name='weekly', frequency=FREQUENCIES['WEEKLY'], params=json.dumps({'byweekday': 0})) self.rec_event = EventFactory( rule=self.rule, start=now(), set__end_recurring_period=41) self.rec_occurrence_list = [ occ for occ in self.rec_event.get_occurrences( self.rec_event.start, self.rec_event.end_recurring_period)] self.rec_occurrence = self.rec_occurrence_list[2]
def test_delete_period(self): """Test for the ``delete_period`` function.""" occurrence = OccurrenceFactory() occurrence.delete_period('all') self.assertEqual(Occurrence.objects.all().count(), 0, msg=('Should delete only the first occurrence.')) event = EventFactory(set__start=0, set__end=0) occurrence = OccurrenceFactory(event=event, set__start=0, set__end=0) occurrence.delete_period('this one') self.assertEqual(Occurrence.objects.all().count(), 0, msg=('Should delete only the first occurrence.')) event = EventFactory(set__start=0, set__end=0) occurrence = OccurrenceFactory(event=event, set__start=0, set__end=0) occurrence.delete_period('following') self.assertEqual(Event.objects.all().count(), 0, msg=('Should delete the event and the occurrence.')) occurrence_1 = OccurrenceFactory() occurrence_2 = OccurrenceFactory(event=occurrence_1.event) period = occurrence_2.event.end_recurring_period occurrence_2.delete_period('this one') self.assertGreater(period, occurrence_2.event.end_recurring_period, msg=('Should shorten event period, if last' ' occurencce is deleted.')) occurrence_2 = OccurrenceFactory(event=occurrence_1.event) occurrence_3 = OccurrenceFactory(event=occurrence_1.event) period = occurrence_2.event.end_recurring_period occurrence_2.delete_period('this one') self.assertTrue(Occurrence.objects.get(pk=occurrence_2.pk).cancelled, msg=('Should set the occurrence to cancelled.')) period = occurrence_3.event.end_recurring_period occurrence_3.delete_period('following') self.assertEqual( Occurrence.objects.all().count(), 0, msg=('Should delete all occurrences with this start date.'))
def setUp(self): # single, not recurring event self.event = EventFactory(rule=None, end_recurring_period=None) self.event_occurrence = next(self.event.get_occurrences( self.event.start)) # recurring event weekly on mondays over 6 weeks self.rule = RuleFactory( name='weekly', frequency=FREQUENCIES['WEEKLY'], params=json.dumps({'byweekday': 0})) self.rec_event = EventFactory( rule=self.rule, start=now(), set__end_recurring_period=41, created_by=mixer.blend('auth.User'), ) self.rec_occurrence_list = [ occ for occ in self.rec_event.get_occurrences( self.rec_event.start, self.rec_event.end_recurring_period)] self.rec_occurrence = self.rec_occurrence_list[1]
class EventTestCase(TestCase): """Tests for the ``Event`` model.""" longMessage = True def setUp(self): self.not_found_event = EventFactory(set__start=-24, set__end=-24, set__creation_date=-24, rule=None) self.event = EventFactory() self.occurrence = OccurrenceFactory(event=self.event, title='foo_occurrence') self.single_time_event = EventFactory(rule=None) def test_create_occurrence(self): """Test for ``_create_occurrence`` method.""" occurrence = self.event._create_occurrence(now()) self.assertEqual( type(occurrence), Occurrence, msg=('Method ``_create_occurrence`` did not output the right type.' )) def test_get_occurrence_gen(self): """Test for the ``_get_occurrence_gen`` method""" occurrence_gen = self.event._get_occurrence_gen( now(), now() + timedelta(days=8)) occ_list = [occ for occ in occurrence_gen] self.assertEqual( len(occ_list), 7, msg= ('The method ``_get_occurrence_list`` did not return the expected' ' amount of items.')) occurrence_gen = self.not_found_event._get_occurrence_gen( now(), now() + timedelta(days=8)) occ_list = [occ for occ in occurrence_gen] self.assertEqual( len(occ_list), 0, msg= ('The method ``_get_occurrence_list`` did not return the expected' ' amount of items.')) def test_get_occurrences(self): occurrence_gen = self.event.get_occurrences(now(), now() + timedelta(days=7)) occ_list = [occ for occ in occurrence_gen] self.assertEqual( len(occ_list), 7, msg=('Method ``get_occurrences`` did not output the correct amount' ' of occurrences.')) occurrence_gen = self.event.get_occurrences(now(), now() + timedelta(days=7)) self.assertEqual( occurrence_gen.next().title, 'foo_occurrence', msg=( 'The persistent occurrence should have been first in the list.' )) def test_get_parent_category(self): """Tests for the ``get_parent_category`` method.""" result = self.event.get_parent_category() self.assertEqual( result, self.event.category, msg=("If the event's category has no parent, it should return the" " category")) cat2 = EventCategoryFactory() self.event.category.parent = cat2 self.event.save() result = self.event.get_parent_category() self.assertEqual( result, self.event.category.parent, msg=("If the event's category has a parent, it should return that" " parent")) def test_save_autocorrection(self): event = EventFactory(rule=None) event.end = event.end - timedelta(hours=2) event.save() self.assertEqual(event.start, event.end)
def test_save_autocorrection(self): event = EventFactory(rule=None) event.end = event.end - timedelta(hours=2) event.save() self.assertEqual(event.start, event.end)
class OccurrenceFormTestCase(TestCase): """Test for the ``OccurrenceForm`` form class.""" longMessage = True def setUp(self): # single, not recurring event self.event = EventFactory(rule=None, end_recurring_period=None) self.event_occurrence = next(self.event.get_occurrences( self.event.start)) # recurring event weekly on mondays over 6 weeks self.rule = RuleFactory( name='weekly', frequency=FREQUENCIES['WEEKLY'], params=json.dumps({'byweekday': 0})) self.rec_event = EventFactory( rule=self.rule, start=now(), set__end_recurring_period=41, created_by=mixer.blend('auth.User'), ) self.rec_occurrence_list = [ occ for occ in self.rec_event.get_occurrences( self.rec_event.start, self.rec_event.end_recurring_period)] self.rec_occurrence = self.rec_occurrence_list[1] def test_form(self): """Test if ``OccurrenceForm`` is valid and saves correctly.""" # Test for not recurring event data = model_to_dict(self.event_occurrence) initial = data.copy() data.update({ 'decision': OCCURRENCE_DECISIONS['all'], 'title': 'changed'}) form = OccurrenceForm(data=data, initial=initial) self.assertTrue(form.is_valid(), msg=( 'The OccurrenceForm should be valid')) form.save() event = Event.objects.get(pk=self.event.pk) self.assertEqual(event.title, 'changed', msg=( 'When save is called, the event\'s title should be "changed".')) # Test for recurring event # Case 1: Altering occurrence 3 to be on a tuesday. data = model_to_dict(self.rec_occurrence) initial = data.copy() data.update({ 'decision': OCCURRENCE_DECISIONS['this one'], 'title': 'different'}) form = OccurrenceForm(data=data, initial=initial) self.assertTrue(form.is_valid(), msg=( 'The OccurrenceForm should be valid')) form.save() self.assertEqual(Occurrence.objects.all().count(), 1, msg=( 'After one occurrence has changed, there should be one persistent' ' occurrence.')) occ = Occurrence.objects.get() self.assertEqual(occ.title, 'different', msg=( 'When save is called, the occurrence\'s title should be' ' "different".')) # Case 2: Altering the description of "all" on the first occurrence # should also change 3rd one occ_to_use = self.rec_occurrence_list[0] data = model_to_dict(occ_to_use) initial = data.copy() new_start = occ_to_use.start + timedelta(hours=1) data.update({ 'decision': OCCURRENCE_DECISIONS['all'], 'description': 'has changed', 'start': new_start}) form = OccurrenceForm(data=data, initial=initial) self.assertTrue(form.is_valid(), msg=( 'The OccurrenceForm should be valid')) form.save() self.assertEqual(Occurrence.objects.all().count(), 1, msg=( 'After one occurrence has changed, there should be one persistent' ' occurrence.')) occ = Occurrence.objects.get() self.assertEqual(occ.title, 'different', msg=( 'When save is called, the occurrence\'s title should still be' ' "different".')) self.assertEqual(occ.description, 'has changed', msg=( 'When save is called, the occurrence\'s description should be' ' "has changed".')) self.assertEqual( occ.start, self.rec_occurrence.start + timedelta(hours=1), msg=( 'When save is called, the occurrence\'s start time should be' ' set forward one hour.')) # Case 3: Altering everthing from occurrence 4 to 6 to one day later occ_to_use = self.rec_occurrence_list[4] data = model_to_dict(occ_to_use) initial = data.copy() new_start = occ_to_use.start - timedelta(days=1) data.update({ 'decision': OCCURRENCE_DECISIONS['following'], 'start': new_start}) form = OccurrenceForm(data=data, initial=initial) self.assertTrue(form.is_valid(), msg=( 'The OccurrenceForm should be valid')) form.save() self.assertEqual(Event.objects.all().count(), 3, msg=( 'After changing occurrence 4-6, a new event should have been' ' created.')) event1 = Event.objects.get(pk=self.rec_event.pk) event2 = Event.objects.exclude( pk__in=[self.rec_event.pk, self.event.pk]).get() self.assertEqual( event1.end_recurring_period, event2.start - timedelta(days=1), msg=( 'The end recurring period of the old event should be the same' ' as the start of the new event minus one day.')) self.assertEqual( event2.end_recurring_period, self.rec_event.end_recurring_period, msg=( 'The end recurring period of the new event should be the' ' old end recurring period of the old event.'))
class EventTestCase(TestCase): """Tests for the ``Event`` model.""" longMessage = True def setUp(self): self.not_found_event = EventFactory( set__start=-24, set__end=-24, set__creation_date=-24, rule=None) self.event = EventFactory() self.occurrence = OccurrenceFactory( event=self.event, title='foo_occurrence') self.single_time_event = EventFactory(rule=None) def test_create_occurrence(self): """Test for ``_create_occurrence`` method.""" occurrence = self.event._create_occurrence(now()) self.assertEqual(type(occurrence), Occurrence, msg=( 'Method ``_create_occurrence`` did not output the right type.')) def test_get_occurrence_gen(self): """Test for the ``_get_occurrence_gen`` method""" occurrence_gen = self.event._get_occurrence_gen( now(), now() + timedelta(days=8)) occ_list = [occ for occ in occurrence_gen] self.assertEqual(len(occ_list), 7, msg=( 'The method ``_get_occurrence_list`` did not return the expected' ' amount of items.')) occurrence_gen = self.not_found_event._get_occurrence_gen( now(), now() + timedelta(days=8)) occ_list = [occ for occ in occurrence_gen] self.assertEqual(len(occ_list), 0, msg=( 'The method ``_get_occurrence_list`` did not return the expected' ' amount of items.')) def test_get_occurrences(self): occurrence_gen = self.event.get_occurrences( now(), now() + timedelta(days=7)) occ_list = [occ for occ in occurrence_gen] self.assertEqual(len(occ_list), 7, msg=( 'Method ``get_occurrences`` did not output the correct amount' ' of occurrences.')) occurrence_gen = self.event.get_occurrences( now(), now() + timedelta(days=7)) self.assertEqual(occurrence_gen.next().title, 'foo_occurrence', msg=( 'The persistent occurrence should have been first in the list.')) def test_get_parent_category(self): """Tests for the ``get_parent_category`` method.""" result = self.event.get_parent_category() self.assertEqual(result, self.event.category, msg=( "If the event's category has no parent, it should return the" " category")) cat2 = EventCategoryFactory() self.event.category.parent = cat2 self.event.save() result = self.event.get_parent_category() self.assertEqual(result, self.event.category.parent, msg=( "If the event's category has a parent, it should return that" " parent")) def test_save_autocorrection(self): event = EventFactory(rule=None) event.end = event.end - timedelta(hours=2) event.save() self.assertEqual(event.start, event.end)