def test_bulk_to_python(self): Contact.objects.bulk_create( Contact(pk=i, heading=str(i)) for i in range(3)) class TestStreamBlock(blocks.StreamBlock): contact = ContactExpandable() block = TestStreamBlock() value = blocks.StreamValue(block, [ { 'type': 'contact', 'value': { 'contact': 0 } }, { 'type': 'contact', 'value': { 'contact': 1 } }, { 'type': 'contact', 'value': { 'contact': 2 } }, ], is_lazy=True) # This verifies that the block does a single database query to retrieve # all 3 Contacts, even though they are split up between three different # ContactExpandable blocks. with self.assertNumQueries(1): block.render(value)
def test_bulk_to_python(self): class TestStreamBlock(blocks.StreamBlock): contacts = ContactExpandableGroup() block = TestStreamBlock() value = blocks.StreamValue(block, [ { 'type': 'contacts', 'value': { 'heading': 'First', 'expandables': [{ 'contact': pk } for pk in reversed(range(5))], }, }, { 'type': 'contacts', 'value': { 'heading': 'Second', 'expandables': [{ 'contact': pk } for pk in reversed(range(5, 10))], }, }, ], is_lazy=True) # This verifies that the block does a single database query to retrieve # all 10 Contacts, even though they are split up between two different # ContactExpandableGroup blocks. with self.assertNumQueries(1): html = block.render(value)
def test_serve_post_returns_400_for_invalid_form_id_no_form_present(self): page = BrowsePage(title='test', slug='test') page.content = blocks.StreamValue(page.content.stream_block, [{ 'type': 'full_width_text', 'value': [] }], True) save_new_page(page) request = self.factory.post('/', {'form_id': 'form-content-0'}) response = page.serve_post(request) self.assertIsInstance(response, HttpResponseBadRequest)
def test_doesnt_pull_in_media_for_nonexistent_child_blocks(self): page = BrowsePage() page.content = blocks.StreamValue(page.content.stream_block, [ { 'type': 'full_width_text', 'value': [], }, ], True) # The page media should only include the default BrowsePae media, and # shouldn't add any additional files because of the FullWithText. self.assertEqual(page.media, ['secondary-navigation.js'])
def test_page_pulls_in_child_block_media(self): page = CFGOVPage() page.sidefoot = blocks.StreamValue(page.sidefoot.stream_block, [ { 'type': 'email_signup', 'value': { 'heading': 'Heading' } }, ], True) self.assertEqual(page.media, ['email-signup.js'])
def test_serve_post_valid_calls_feedback_block_handler(self): """A valid post should call the feedback block handler. This returns a redirect to the calling page and also uses the Django messages framework to set a message. """ page = BrowsePage(title='test', slug='test') page.content = blocks.StreamValue(page.content.stream_block, [{ 'type': 'feedback', 'value': 'something' }], True) save_new_page(page) request = self.factory.post('/', {'form_id': 'form-content-0'}) SessionMiddleware().process_request(request) MessageMiddleware().process_request(request) response = page.serve_post(request) self.assertEqual((response.status_code, response['Location']), (302, request.path))