def humanize_tester(self, test_list, result_list, method, normalize_result_func=escape): for test_content, result in zip(test_list, result_list): t = Template('{%% load humanize %%}{{ test_content|%s }}' % method) rendered = t.render(Context(locals())).strip() self.assertEqual( rendered, normalize_result_func(result), msg="%s test failed, produced '%s', should've produced '%s'" % (method, rendered, result))
def test_no_extra_query_when_accessing_attrs(self): """ ModelChoiceField with RadioSelect widget doesn't produce unnecessary db queries when accessing its BoundField's attrs. """ class ModelChoiceForm(forms.Form): category = forms.ModelChoiceField(Category.objects.all(), widget=forms.RadioSelect) form = ModelChoiceForm() field = form['category'] # BoundField template = Template('{{ field.name }}{{ field }}{{ field.help_text }}') with self.assertNumQueries(1): template.render(Context({'field': field}))
def test_num_queries(self): """ Widgets that render multiple subwidgets shouldn't make more than one database query. """ categories = Category.objects.all() class CategoriesForm(forms.Form): radio = forms.ModelChoiceField(queryset=categories, widget=forms.RadioSelect) checkbox = forms.ModelMultipleChoiceField(queryset=categories, widget=forms.CheckboxSelectMultiple) template = Template( '{% for widget in form.checkbox %}{{ widget }}{% endfor %}' '{% for widget in form.radio %}{{ widget }}{% endfor %}' ) with self.assertNumQueries(2): template.render(Context({'form': CategoriesForm()}))
def test_regress_3871(self): related = RelatedModel.objects.create() relation = RelationModel() relation.fk = related relation.gfk = related relation.save() relation.m2m.add(related) t = Template( '{{ related.test_fk.all.0 }}{{ related.test_gfk.all.0 }}{{ related.test_m2m.all.0 }}' ) self.assertEqual( t.render(Context({'related': related})), ''.join([str(relation.pk)] * 3), )
def test_get_flatpages_with_prefix(self): "The flatpage template tag retrieves unregistered prefixed flatpages by default" out = Template("{% load flatpages %}" "{% get_flatpages '/location/' as location_flatpages %}" "{% for page in location_flatpages %}" "{{ page.title }}," "{% endfor %}").render(Context()) self.assertEqual(out, "A Nested Flatpage,")
def test_get_flatpages_with_prefix_for_anon_user(self): "The flatpage template tag retrieves unregistered prefixed flatpages for an anonymous user" out = Template( "{% load flatpages %}" "{% get_flatpages '/location/' for anonuser as location_flatpages %}" "{% for page in location_flatpages %}" "{{ page.title }}," "{% endfor %}").render(Context({'anonuser': AnonymousUser()})) self.assertEqual(out, "A Nested Flatpage,")
def test_get_flatpages_with_variable_prefix(self): "The prefix for the flatpage template tag can be a template variable" out = Template( "{% load flatpages %}" "{% get_flatpages location_prefix as location_flatpages %}" "{% for page in location_flatpages %}" "{{ page.title }}," "{% endfor %}").render(Context({'location_prefix': '/location/'})) self.assertEqual(out, "A Nested Flatpage,")
def test_get_flatpages_with_prefix_for_user(self): "The flatpage template tag retrieve prefixed flatpages for an authenticated user" me = User.objects.create_user('testuser', '*****@*****.**', 's3krit') out = Template( "{% load flatpages %}" "{% get_flatpages '/location/' for me as location_flatpages %}" "{% for page in location_flatpages %}" "{{ page.title }}," "{% endfor %}").render(Context({'me': me})) self.assertEqual(out, "A Nested Flatpage,Sekrit Nested Flatpage,")
def test_translates_multiple_percent_signs(self): expected = ( '1 % signe pour cent, signes %% 2 pour cent, trois signes de pourcentage %%%' ) trans_tpl = Template( '{% load i18n %}{% trans "1 percent sign %, 2 percent signs %%, ' '3 percent signs %%%" %}') self.assertEqual(trans_tpl.render(Context({})), expected) block_tpl = Template( '{% load i18n %}{% blocktrans %}1 percent sign %, 2 percent signs ' '%%, 3 percent signs %%%{% endblocktrans %}') self.assertEqual(block_tpl.render(Context({})), expected) block_tpl = Template( '{% load i18n %}{% blocktrans %}{{name}} says: 1 percent sign %, ' '2 percent signs %%{% endblocktrans %}') self.assertEqual( block_tpl.render(Context({"name": "Django"})), 'Django dit: 1 pour cent signe %, deux signes de pourcentage %%')
def test_translates_with_percent_symbol_in_the_middle(self): expected = 'Pour cent littérale % avec un symbole au milieu' trans_tpl = Template( '{% load i18n %}{% trans "Literal with a percent % symbol in the middle" %}' ) self.assertEqual(trans_tpl.render(Context({})), expected) block_tpl = Template( '{% load i18n %}{% blocktrans %}Literal with a percent % symbol ' 'in the middle{% endblocktrans %}') self.assertEqual(block_tpl.render(Context({})), expected)
def test_translates_with_a_percent_symbol_at_the_end(self): expected = 'Littérale avec un symbole de pour cent à la fin %' trans_tpl = Template( '{% load i18n %}{% trans "Literal with a percent symbol at the end %" %}' ) self.assertEqual(trans_tpl.render(Context({})), expected) block_tpl = Template( '{% load i18n %}{% blocktrans %}Literal with a percent symbol at ' 'the end %{% endblocktrans %}') self.assertEqual(block_tpl.render(Context({})), expected)
def assertRenderEqual(self, tpl, expected, **context): context = Context(context) tpl = Template(tpl) self.assertEqual(tpl.render(context), expected)
def test_translates_with_string_that_look_like_fmt_spec_with_trans(self): # tests "%s" expected = ( 'On dirait un spec str fmt %s mais ne devrait pas être interprété comme plus disponible' ) trans_tpl = Template( '{% load i18n %}{% trans "Looks like a str fmt spec %s but ' 'should not be interpreted as such" %}') self.assertEqual(trans_tpl.render(Context({})), expected) block_tpl = Template( '{% load i18n %}{% blocktrans %}Looks like a str fmt spec %s but ' 'should not be interpreted as such{% endblocktrans %}') self.assertEqual(block_tpl.render(Context({})), expected) # tests "% o" expected = ( 'On dirait un spec str fmt % o mais ne devrait pas être interprété comme plus disponible' ) trans_tpl = Template( '{% load i18n %}{% trans "Looks like a str fmt spec % o but should not be ' 'interpreted as such" %}') self.assertEqual(trans_tpl.render(Context({})), expected) block_tpl = Template( '{% load i18n %}{% blocktrans %}Looks like a str fmt spec % o but should not be ' 'interpreted as such{% endblocktrans %}') self.assertEqual(block_tpl.render(Context({})), expected)
def test_translates_with_percent_symbol_using_context(self): trans_tpl = Template('{% load i18n %}{% trans "It is 100%" %}') self.assertEqual(trans_tpl.render(Context({})), 'Il est de 100%') trans_tpl = Template( '{% load i18n %}{% trans "It is 100%" context "female" %}') self.assertEqual(trans_tpl.render(Context({})), 'Elle est de 100%') block_tpl = Template( '{% load i18n %}{% blocktrans %}It is 100%{% endblocktrans %}') self.assertEqual(block_tpl.render(Context({})), 'Il est de 100%') block_tpl = Template( '{% load i18n %}{% blocktrans context "female" %}It is 100%{% endblocktrans %}' ) self.assertEqual(block_tpl.render(Context({})), 'Elle est de 100%')
def render(t): return Template(t).render(Context())