def setUp(self):
        # main start date
        self.main_start = datetime(2015, 1, 1, 18, 00)
        self.main_end = datetime(2015, 1, 1, 20, 00)

        # Create an event with a recurrence of monthly on the 3rd monday.
        self.event = mommy.make(
            'events.Event', start=self.main_start, end=self.main_end, recurrences='RRULE:FREQ=MONTHLY;BYDAY=3MO')

        self.first_occ = Occurrence.from_event(self.event, datetime(2015, 1, 18, 16, 00))
        self.first_occ.save()
        self.second_occ = Occurrence.from_event(self.event, datetime(2015, 2, 18, 16, 00))
        self.second_occ.save()
    def test_set_template_no_template_string(self, mock_get_template_context):
        # Create occurrence
        occurrence_start = datetime(2015, 2, 1, 12, 00)
        occurrence_end = datetime(2015, 2, 1, 18, 00)
        occ = Occurrence.from_event(self.event, occurrence_start, occurrence_end)

        # getting context returns mock context string
        mock_get_template_context.is_callable().returns('context')

        # Expact the default template to be set on the occurrence
        occ.default_a_field_template = 'default jinja template'

        # Jinja template engine is selected and is passed the template string, render is called on the template
        mock_jinja_engine = fudge.Fake()
        mock_template = mock_jinja_engine.expects('from_string').with_args('default jinja template').returns_fake()
        mock_template.expects('render').with_args('context').returns('rendered context')

        mock_engines = {
            'jinja2': mock_jinja_engine
        }

        patched_engines = fudge.patch_object(events_models, 'engines', mock_engines)

        # call set_template
        occ.set_template('a_field')

        self.assertEqual(occ.a_field, 'rendered context')

        patched_engines.restore()
    def test_update_template_description(self, mock_set_template):
        mock_set_template.is_callable().with_args('description', 'jinja description template')

        occurrence_start = datetime(2015, 2, 1, 12, 00)
        occurrence_end = datetime(2015, 2, 1, 18, 00)
        occ = Occurrence.from_event(self.event, occurrence_start, occurrence_end)

        occ.update_template_description()
 def test_from_event_no_occurrence_end_created(self, mock_title, mock_description):
     # provised mocked template functions as we expect them not to be called
     occurrence_start = datetime(2015, 2, 1, 12, 00)
     occ = Occurrence.from_event(self.event, occurrence_start)
     self.assertEqual(occ.start, occurrence_start)
     self.assertEqual(occ.original_start, occurrence_start)
     orig_end = datetime(2015, 2, 1, 16, 00)
     self.assertEqual(occ.end, orig_end)
     self.assertEqual(occ.original_end, orig_end)
     self.assertEqual(occ.event, self.event)
     self.assertEqual(occ.location, self.event.location)
    def test_get_template_context(self):
        # Create occurrence
        occurrence_start = datetime(2015, 2, 1, 12, 00)
        occurrence_end = datetime(2015, 2, 1, 18, 00)
        occ = Occurrence.from_event(self.event, occurrence_start, occurrence_end)

        context = occ.get_template_context()

        self.assertEqual(context, {
            'occ': occ,
            'event': self.event
        })
 def test_from_event_occurrence_created(self, mock_title, mock_description):
     # An alternative end time is provided
     occurrence_start = datetime(2015, 2, 1, 12, 00)
     occurrence_end = datetime(2015, 2, 1, 18, 00)
     occ = Occurrence.from_event(self.event, occurrence_start, occurrence_end)
     self.assertEqual(occ.start, occurrence_start)
     self.assertEqual(occ.original_start, occurrence_start)
     orig_end = datetime(2015, 2, 1, 18, 00)
     self.assertEqual(occ.end, orig_end)
     self.assertEqual(occ.original_end, orig_end)
     self.assertEqual(occ.event, self.event)
     self.assertEqual(occ.location, self.event.location)
    def test_from_event_occurrence_use_templates_enabled(self, mock_title, mock_description):
        # Check to make sure they are called
        mock_title.expects_call()
        mock_description.expects_call()

        occurrence_start = datetime(2015, 2, 1, 12, 00)
        occ = Occurrence.from_event(self.event, occurrence_start, use_templates=True)
        self.assertEqual(occ.start, occurrence_start)
        self.assertEqual(occ.original_start, occurrence_start)
        orig_end = datetime(2015, 2, 1, 16, 00)
        self.assertEqual(occ.end, orig_end)
        self.assertEqual(occ.event, self.event)
        self.assertEqual(occ.location, self.event.location)
 def test_from_event_wrong_occurrence_start(self):
     with self.assertRaises(AssertionError):
         Occurrence.from_event(self.event, 'string date')
 def test_from_event_no_occurrence_start(self):
     with self.assertRaises(TypeError):
         Occurrence.from_event(self.event)