How to use test_render_custom_template method in localstack

Best Python code snippet using localstack_python

test_widgets.py

Source:test_widgets.py Github

copy

Full Screen

...192193194class ClearableFileInputTests(SimpleTestCase):195196 def test_render_custom_template(self):197 widget = ClearableFileInput()198 widget.template_with_initial = (199 '%(initial_text)s: <img src="%(initial_url)s" alt="%(initial)s" /> '200 '%(clear_template)s<br />%(input_text)s: %(input)s'201 )202 self.assertHTMLEqual(203 widget.render('myfile', FakeFieldFile()),204 'Currently: <img src="something" alt="something" /> '205 '<input type="checkbox" name="myfile-clear" id="myfile-clear_id" /> '206 '<label for="myfile-clear_id">Clear</label><br />Change: <input type="file" name="myfile" />'207 )208=======209# -*- coding: utf-8 -*-210from __future__ import unicode_literals211from django.contrib.admin.tests import AdminSeleniumTestCase212from django.forms import (213 CheckboxSelectMultiple, ClearableFileInput, RadioSelect, TextInput,214)215from django.forms.widgets import (216 ChoiceFieldRenderer, ChoiceInput, RadioFieldRenderer,217)218from django.test import SimpleTestCase, override_settings219from django.urls import reverse220from django.utils import six221from django.utils.encoding import force_text, python_2_unicode_compatible222from django.utils.safestring import SafeData223from ..models import Article224class FormsWidgetTests(SimpleTestCase):225 def test_radiofieldrenderer(self):226 # RadioSelect uses a RadioFieldRenderer to render the individual radio inputs.227 # You can manipulate that object directly to customize the way the RadioSelect228 # is rendered.229 w = RadioSelect()230 r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))231 inp_set1 = []232 inp_set2 = []233 inp_set3 = []234 inp_set4 = []235 for inp in r:236 inp_set1.append(str(inp))237 inp_set2.append('%s<br />' % inp)238 inp_set3.append('<p>%s %s</p>' % (inp.tag(), inp.choice_label))239 inp_set4.append(240 '%s %s %s %s %s' % (241 inp.name,242 inp.value,243 inp.choice_value,244 inp.choice_label,245 inp.is_checked(),246 )247 )248 self.assertHTMLEqual('\n'.join(inp_set1), """<label><input checked="checked" type="radio" name="beatle" value="J" /> John</label>249<label><input type="radio" name="beatle" value="P" /> Paul</label>250<label><input type="radio" name="beatle" value="G" /> George</label>251<label><input type="radio" name="beatle" value="R" /> Ringo</label>""")252 self.assertHTMLEqual('\n'.join(inp_set2), """<label><input checked="checked" type="radio" name="beatle" value="J" /> John</label><br />253<label><input type="radio" name="beatle" value="P" /> Paul</label><br />254<label><input type="radio" name="beatle" value="G" /> George</label><br />255<label><input type="radio" name="beatle" value="R" /> Ringo</label><br />""")256 self.assertHTMLEqual('\n'.join(inp_set3), """<p><input checked="checked" type="radio" name="beatle" value="J" /> John</p>257<p><input type="radio" name="beatle" value="P" /> Paul</p>258<p><input type="radio" name="beatle" value="G" /> George</p>259<p><input type="radio" name="beatle" value="R" /> Ringo</p>""")260 self.assertHTMLEqual('\n'.join(inp_set4), """beatle J J John True261beatle J P Paul False262beatle J G George False263beatle J R Ringo False""")264 # A RadioFieldRenderer object also allows index access to individual RadioChoiceInput265 w = RadioSelect()266 r = w.get_renderer('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))267 self.assertHTMLEqual(str(r[1]), '<label><input type="radio" name="beatle" value="P" /> Paul</label>')268 self.assertHTMLEqual(269 str(r[0]),270 '<label><input checked="checked" type="radio" name="beatle" value="J" /> John</label>'271 )272 self.assertTrue(r[0].is_checked())273 self.assertFalse(r[1].is_checked())274 self.assertEqual((r[1].name, r[1].value, r[1].choice_value, r[1].choice_label), ('beatle', 'J', 'P', 'Paul'))275 # These individual widgets can accept extra attributes if manually rendered.276 self.assertHTMLEqual(277 r[1].render(attrs={'extra': 'value'}),278 '<label><input type="radio" extra="value" name="beatle" value="P" /> Paul</label>'279 )280 with self.assertRaises(IndexError):281 r[10]282 # You can create your own custom renderers for RadioSelect to use.283 class MyRenderer(RadioFieldRenderer):284 def render(self):285 return '<br />\n'.join(six.text_type(choice) for choice in self)286 w = RadioSelect(renderer=MyRenderer)287 self.assertHTMLEqual(288 w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))),289 """<label><input type="radio" name="beatle" value="J" /> John</label><br />290<label><input type="radio" name="beatle" value="P" /> Paul</label><br />291<label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br />292<label><input type="radio" name="beatle" value="R" /> Ringo</label>"""293 )294 # Or you can use custom RadioSelect fields that use your custom renderer.295 class CustomRadioSelect(RadioSelect):296 renderer = MyRenderer297 w = CustomRadioSelect()298 self.assertHTMLEqual(299 w.render('beatle', 'G', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))),300 """<label><input type="radio" name="beatle" value="J" /> John</label><br />301<label><input type="radio" name="beatle" value="P" /> Paul</label><br />302<label><input checked="checked" type="radio" name="beatle" value="G" /> George</label><br />303<label><input type="radio" name="beatle" value="R" /> Ringo</label>"""304 )305 # You can customize rendering with outer_html/inner_html renderer variables (#22950)306 class MyRenderer(RadioFieldRenderer):307 # str is just to test some Python 2 issue with bytestrings308 outer_html = str('<div{id_attr}>{content}</div>')309 inner_html = '<p>{choice_value}{sub_widgets}</p>'310 w = RadioSelect(renderer=MyRenderer)311 output = w.render('beatle', 'J',312 choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')),313 attrs={'id': 'bar'})314 self.assertIsInstance(output, SafeData)315 self.assertHTMLEqual(316 output,317 """<div id="bar">318<p><label for="bar_0"><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></p>319<p><label for="bar_1"><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></p>320<p><label for="bar_2"><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></p>321<p><label for="bar_3"><input type="radio" id="bar_3" value="R" name="beatle" /> Ringo</label></p>322</div>""")323 def test_subwidget(self):324 # Each subwidget tag gets a separate ID when the widget has an ID specified325 self.assertHTMLEqual("\n".join(c.tag() for c in CheckboxSelectMultiple(attrs={'id': 'abc'}).subwidgets('letters', list('ac'), choices=zip(list('abc'), list('ABC')))), """<input checked="checked" type="checkbox" name="letters" value="a" id="abc_0" />326<input type="checkbox" name="letters" value="b" id="abc_1" />327<input checked="checked" type="checkbox" name="letters" value="c" id="abc_2" />""")328 # Each subwidget tag does not get an ID if the widget does not have an ID specified329 self.assertHTMLEqual("\n".join(c.tag() for c in CheckboxSelectMultiple().subwidgets('letters', list('ac'), choices=zip(list('abc'), list('ABC')))), """<input checked="checked" type="checkbox" name="letters" value="a" />330<input type="checkbox" name="letters" value="b" />331<input checked="checked" type="checkbox" name="letters" value="c" />""")332 # The id_for_label property of the subwidget should return the ID that is used on the subwidget's tag333 self.assertHTMLEqual("\n".join('<input type="checkbox" name="letters" value="%s" id="%s" />' % (c.choice_value, c.id_for_label) for c in CheckboxSelectMultiple(attrs={'id': 'abc'}).subwidgets('letters', [], choices=zip(list('abc'), list('ABC')))), """<input type="checkbox" name="letters" value="a" id="abc_0" />334<input type="checkbox" name="letters" value="b" id="abc_1" />335<input type="checkbox" name="letters" value="c" id="abc_2" />""")336 def test_sub_widget_html_safe(self):337 widget = TextInput()338 subwidget = next(widget.subwidgets('username', 'John Doe'))339 self.assertTrue(hasattr(subwidget, '__html__'))340 self.assertEqual(force_text(subwidget), subwidget.__html__())341 def test_choice_input_html_safe(self):342 widget = ChoiceInput('choices', 'CHOICE1', {}, ('CHOICE1', 'first choice'), 0)343 self.assertTrue(hasattr(ChoiceInput, '__html__'))344 self.assertEqual(force_text(widget), widget.__html__())345 def test_choice_field_renderer_html_safe(self):346 renderer = ChoiceFieldRenderer('choices', 'CHOICE1', {}, [('CHOICE1', 'first_choice')])347 renderer.choice_input_class = lambda *args: args348 self.assertTrue(hasattr(ChoiceFieldRenderer, '__html__'))349 self.assertEqual(force_text(renderer), renderer.__html__())350@override_settings(ROOT_URLCONF='forms_tests.urls')351class LiveWidgetTests(AdminSeleniumTestCase):352 available_apps = ['forms_tests'] + AdminSeleniumTestCase.available_apps353 def test_textarea_trailing_newlines(self):354 """355 Test that a roundtrip on a ModelForm doesn't alter the TextField value356 """357 article = Article.objects.create(content="\nTst\n")358 self.selenium.get('%s%s' % (self.live_server_url,359 reverse('article_form', args=[article.pk])))360 self.selenium.find_element_by_id('submit').submit()361 article = Article.objects.get(pk=article.pk)362 # Should be "\nTst\n" after #19251 is fixed363 self.assertEqual(article.content, "\r\nTst\r\n")364@python_2_unicode_compatible365class FakeFieldFile(object):366 """367 Quacks like a FieldFile (has a .url and unicode representation), but368 doesn't require us to care about storages etc.369 """370 url = 'something'371 def __str__(self):372 return self.url373class ClearableFileInputTests(SimpleTestCase):374 def test_render_custom_template(self):375 widget = ClearableFileInput()376 widget.template_with_initial = (377 '%(initial_text)s: <img src="%(initial_url)s" alt="%(initial)s" /> '378 '%(clear_template)s<br />%(input_text)s: %(input)s'379 )380 self.assertHTMLEqual(381 widget.render('myfile', FakeFieldFile()),382 'Currently: <img src="something" alt="something" /> '383 '<input type="checkbox" name="myfile-clear" id="myfile-clear_id" /> '384 '<label for="myfile-clear_id">Clear</label><br />Change: <input type="file" name="myfile" />'385 )...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful