def test_has_changed_with_same_id(self): # Father Christmas renamed to Santa Claus, but this time the ID of the # child object remained the same. It should now be detected as the same # object event_page = EventPage(title="Event page", slug="event") event_page.speakers.add( EventPageSpeaker( id=1, first_name="Father", last_name="Christmas", sort_order=0, )) modified_event_page = EventPage(title="Event page", slug="event") modified_event_page.speakers.add( EventPageSpeaker( id=1, first_name="Santa", last_name="Claus", sort_order=0, )) modified_event_page.speakers.add( EventPageSpeaker( first_name="Father", last_name="Ted", sort_order=1, )) comparison = self.comparison_class( EventPage._meta.get_field('speakers'), [ curry(self.field_comparison_class, EventPageSpeaker._meta.get_field('first_name')), curry(self.field_comparison_class, EventPageSpeaker._meta.get_field('last_name')), ], event_page, modified_event_page, ) self.assertFalse(comparison.is_field) self.assertTrue(comparison.is_child_relation) self.assertEqual(comparison.field_label(), "Speakers") self.assertTrue(comparison.has_changed()) # Check mapping objs_a = list(comparison.val_a.all()) objs_b = list(comparison.val_b.all()) map_forwards, map_backwards, added, deleted = comparison.get_mapping( objs_a, objs_b) self.assertEqual(map_forwards, {0: 0}) # Map Father Christmas to Santa Claus self.assertEqual(map_backwards, {0: 0}) # Map Santa Claus to Father Christmas self.assertEqual(added, [1]) # Add Father Ted self.assertEqual(deleted, [])
def test_has_changed(self): # Father Christmas renamed to Santa Claus. And Father Ted added. # Father Christmas should be mapped to Father Ted because they # are most alike. Santa claus should be displayed as "new" event_page = EventPage(title="Event page", slug="event") event_page.speakers.add( EventPageSpeaker( first_name="Father", last_name="Christmas", sort_order=0, )) modified_event_page = EventPage(title="Event page", slug="event") modified_event_page.speakers.add( EventPageSpeaker( first_name="Santa", last_name="Claus", sort_order=0, )) modified_event_page.speakers.add( EventPageSpeaker( first_name="Father", last_name="Ted", sort_order=1, )) comparison = self.comparison_class( EventPage._meta.get_field('speakers'), [ curry(self.field_comparison_class, EventPageSpeaker._meta.get_field('first_name')), curry(self.field_comparison_class, EventPageSpeaker._meta.get_field('last_name')), ], event_page, modified_event_page, ) self.assertFalse(comparison.is_field) self.assertTrue(comparison.is_child_relation) self.assertEqual(comparison.field_label(), "Speakers") self.assertTrue(comparison.has_changed()) # Check mapping objs_a = list(comparison.val_a.all()) objs_b = list(comparison.val_b.all()) map_forwards, map_backwards, added, deleted = comparison.get_mapping( objs_a, objs_b) self.assertEqual(map_forwards, {0: 1}) # Map Father Christmas to Father Ted self.assertEqual(map_backwards, {1: 0}) # Map Father Ted ot Father Christmas self.assertEqual(added, [0]) # Add Santa Claus self.assertEqual(deleted, [])
def test_has_changed(self): comparison = self.comparison_class( EventPage._meta.get_field('feed_image'), EventPage(feed_image=self.test_image_1), EventPage(feed_image=self.test_image_2), ) self.assertEqual( comparison.htmldiff(), '<span class="deletion">Test image 1</span><span class="addition">Test image 2</span>' ) self.assertIsInstance(comparison.htmldiff(), SafeText) self.assertTrue(comparison.has_changed())
def test_hasnt_changed(self): comparison = self.comparison_class( EventPage._meta.get_field('feed_image'), EventPage(feed_image=self.test_image_1), EventPage(feed_image=self.test_image_1), ) self.assertTrue(comparison.is_field) self.assertFalse(comparison.is_child_relation) self.assertEqual(comparison.field_label(), "Feed image") self.assertEqual(comparison.htmldiff(), 'Test image 1') self.assertIsInstance(comparison.htmldiff(), SafeText) self.assertFalse(comparison.has_changed())
def make_event_section(self, name): event_index = self.home_page.add_child(instance=EventIndex(title=name)) event_index.add_child(instance=EventPage(title='First Event', location='Bar', audience='public', cost='free', date_from='2001-01-01')) event_index.add_child(instance=EventPage(title='Second Event', location='Baz', audience='public', cost='free', date_from='2001-01-01')) return event_index
def test_hasnt_changed(self): comparison = self.comparison_class( EventPage._meta.get_field('audience'), EventPage(audience="public"), EventPage(audience="public"), ) self.assertTrue(comparison.is_field) self.assertFalse(comparison.is_child_relation) self.assertEqual(comparison.field_label(), "Audience") self.assertEqual(comparison.htmldiff(), 'Public') self.assertIsInstance(comparison.htmldiff(), SafeText) self.assertFalse(comparison.has_changed())
def test_has_changed(self): comparison = self.comparison_class( EventPage._meta.get_field('audience'), EventPage(audience="public"), EventPage(audience="private"), ) self.assertEqual( comparison.htmldiff(), '<span class="deletion">Public</span><span class="addition">Private</span>' ) self.assertIsInstance(comparison.htmldiff(), SafeText) self.assertTrue(comparison.has_changed())
def test_with_page_type(self): # Add a page that is not a SimplePage event_page = EventPage( title="event", location='the moon', audience='public', cost='free', date_from='2001-01-01', ) self.root_page.add_child(instance=event_page) # Add a page with a child page event_index_page = EventIndex(title="events", ) self.root_page.add_child(instance=event_index_page) event_index_page.add_child(instance=EventPage( title="other event", location='the moon', audience='public', cost='free', date_from='2001-01-01', )) # Send request response = self.get({'page_type': 'tests.simplepage'}) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'tuiuiuadmin/chooser/browse.html') self.assertEqual(response.context['page_type_string'], 'tests.simplepage') pages = { page.id: page for page in response.context['pages'].object_list } # Child page is a simple page directly underneath root # so should appear in the list self.assertIn(self.child_page.id, pages) self.assertTrue(pages[self.child_page.id].can_choose) self.assertFalse(pages[self.child_page.id].can_descend) # Event page is not a simple page and is not descendable either # so should not appear in the list self.assertNotIn(event_page.id, pages) # Event index page is not a simple page but has a child and is therefore descendable # so should appear in the list self.assertIn(event_index_page.id, pages) self.assertFalse(pages[event_index_page.id].can_choose) self.assertTrue(pages[event_index_page.id].can_descend)
def test_render(self): EventPageForm = self.EventPageObjectList.get_form_class(EventPage) event = EventPage(title='Abergavenny sheepdog trials') form = EventPageForm(instance=event) object_list = self.EventPageObjectList(instance=event, form=form) result = object_list.render() # result should contain ObjectList furniture self.assertIn('<ul class="objects">', result) # result should contain h2 headings (including labels) for children self.assertInHTML( '<h2><label for="id_date_from">Start date</label></h2>', result) # result should include help text for children self.assertIn( '<div class="object-help help">Not required if event is on a single day</div>', result) # result should contain rendered content from descendants self.assertIn('Abergavenny sheepdog trials</textarea>', result) # this result should not include fields that are not covered by the panel definition self.assertNotIn('signup_link', result)
def test_render(self): EventPageForm = self.EventPageTabbedInterface.get_form_class(EventPage) event = EventPage(title='Abergavenny sheepdog trials') form = EventPageForm(instance=event) tabbed_interface = self.EventPageTabbedInterface(instance=event, form=form) result = tabbed_interface.render() # result should contain tab buttons self.assertIn( '<a href="#tab-event-details" class="active">Event details</a>', result) self.assertIn('<a href="#tab-speakers" class="">Speakers</a>', result) # result should contain tab panels self.assertIn('<div class="tab-content">', result) self.assertIn('<section id="tab-event-details" class="shiny active">', result) self.assertIn('<section id="tab-speakers" class=" ">', result) # result should contain rendered content from descendants self.assertIn('Abergavenny sheepdog trials</textarea>', result) # this result should not include fields that are not covered by the panel definition self.assertNotIn('signup_link', result)
def test_with_multiple_page_types(self): # Add a page that is not a SimplePage event_page = EventPage( title="foo", location='the moon', audience='public', cost='free', date_from='2001-01-01', ) self.root_page.add_child(instance=event_page) # Send request response = self.get({ 'q': "foo", 'page_type': 'tests.simplepage,tests.eventpage' }) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'tuiuiuadmin/chooser/_search_results.html') self.assertEqual(response.context['page_type_string'], 'tests.simplepage,tests.eventpage') pages = {page.id: page for page in response.context['pages']} # Simple page in results, as before self.assertIn(self.child_page.id, pages) # Event page should now also be choosable self.assertIn(event_page.id, pages)
def test_hasnt_changed_with_different_id(self): # Both of the child objects have the same field content but have a # different ID so they should be detected as separate objects event_page = EventPage(title="Event page", slug="event") event_page.speakers.add( EventPageSpeaker( id=1, first_name="Father", last_name="Christmas", )) modified_event_page = EventPage(title="Event page", slug="event") modified_event_page.speakers.add( EventPageSpeaker( id=2, first_name="Father", last_name="Christmas", )) comparison = self.comparison_class( EventPage._meta.get_field('speakers'), [ curry(self.field_comparison_class, EventPageSpeaker._meta.get_field('first_name')), curry(self.field_comparison_class, EventPageSpeaker._meta.get_field('last_name')), ], event_page, modified_event_page, ) self.assertFalse(comparison.is_field) self.assertTrue(comparison.is_child_relation) self.assertEqual(comparison.field_label(), "Speakers") self.assertTrue(comparison.has_changed()) # Check mapping objs_a = list(comparison.val_a.all()) objs_b = list(comparison.val_b.all()) map_forwards, map_backwards, added, deleted = comparison.get_mapping( objs_a, objs_b) self.assertEqual(map_forwards, {}) self.assertEqual(map_backwards, {}) self.assertEqual(added, [0]) # Add new Father Christmas self.assertEqual(deleted, [0]) # Delete old Father Christmas
def test_get_edit_handler(self): """ Forms for pages should have a base class of TuiuiuAdminPageForm. """ EditHandler = EventPage.get_edit_handler() EventPageForm = EditHandler.get_form_class(EventPage) # The generated form should inherit from TuiuiuAdminPageForm self.assertTrue(issubclass(EventPageForm, TuiuiuAdminPageForm))
def test_hasnt_changed(self): # Two event pages with speaker called "Father Christmas". Neither of # the speaker objects have an ID so this tests that the code can match # the two together by field content. event_page = EventPage(title="Event page", slug="event") event_page.speakers.add( EventPageSpeaker( first_name="Father", last_name="Christmas", )) modified_event_page = EventPage(title="Event page", slug="event") modified_event_page.speakers.add( EventPageSpeaker( first_name="Father", last_name="Christmas", )) comparison = self.comparison_class( EventPage._meta.get_field('speakers'), [ curry(self.field_comparison_class, EventPageSpeaker._meta.get_field('first_name')), curry(self.field_comparison_class, EventPageSpeaker._meta.get_field('last_name')), ], event_page, modified_event_page, ) self.assertFalse(comparison.is_field) self.assertTrue(comparison.is_child_relation) self.assertEqual(comparison.field_label(), "Speakers") self.assertFalse(comparison.has_changed()) # Check mapping objs_a = list(comparison.val_a.all()) objs_b = list(comparison.val_b.all()) map_forwards, map_backwards, added, deleted = comparison.get_mapping( objs_a, objs_b) self.assertEqual(map_forwards, {0: 0}) self.assertEqual(map_backwards, {0: 0}) self.assertEqual(added, []) self.assertEqual(deleted, [])
def setUp(self): self.EventPageForm = get_form_for_model(EventPage, form_class=TuiuiuAdminPageForm, formsets=[]) self.event = EventPage(title='Abergavenny sheepdog trials', date_from=date(2014, 7, 20), date_to=date(2014, 7, 21)) self.EndDatePanel = FieldPanel( 'date_to', classname='full-width').bind_to_model(EventPage)
def setUp(self): self.EventPageForm = get_form_for_model(EventPage, form_class=TuiuiuAdminPageForm, formsets=[]) self.event = EventPage(title='Abergavenny sheepdog trials', date_from=date(2014, 7, 20), date_to=date(2014, 7, 21)) self.DatesPanel = FieldRowPanel([ FieldPanel('date_from', classname='col4'), FieldPanel('date_to', classname='coltwo'), ]).bind_to_model(EventPage)
def test_render_form_content(self): EventPageForm = self.EventPageTabbedInterface.get_form_class(EventPage) event = EventPage(title='Abergavenny sheepdog trials') form = EventPageForm(instance=event) tabbed_interface = self.EventPageTabbedInterface(instance=event, form=form) result = tabbed_interface.render_form_content() # rendered output should contain field content as above self.assertIn('Abergavenny sheepdog trials</textarea>', result) # rendered output should NOT include fields that are in the model but not represented # in the panel definition self.assertNotIn('signup_link', result)
def wrapper(*args, **kwargs): # Create some pages event_index = Page.objects.get(url_path='/home/events/') for i in range(100): event = EventPage( title="Event " + str(i), slug='event-' + str(i), location='the moon', audience='public', cost='free', date_from='2001-01-01', live=True, ) event_index.add_child(instance=event) return test(*args, **kwargs)