def test_no_duplicates(): field = TemplateChoiceField( match="^django/forms/widgets/template_.+\.html$") assert list(field.choices) == [ (u'django/forms/widgets/template_selector.html', u'Template selector'), (u'django/forms/widgets/template_selector_option.html', u'Template selector option'), ]
def test_choices_using_custom_setting_mapping(): s = { 'admin/404.html': 'Page Not Found', 'admin/500.html': 'Server Error', } with override_settings(TEMPLATESELECTOR_DISPLAY_NAMES=s): x = TemplateChoiceField(match="^admin/[0-9]+.html$") assert set(x.choices) == set(s.items())
def test_choices_using_custom_namer(): def namer(data): return str(UUID('F' * 32)) x = TemplateChoiceField(match="^admin/[0-9]+.html$", display_name=namer) assert set(x.choices) == { ('admin/404.html', 'ffffffff-ffff-ffff-ffff-ffffffffffff'), ('admin/500.html', 'ffffffff-ffff-ffff-ffff-ffffffffffff') }
class MyForm3(Form): field = TemplateChoiceField(match="^admin/_doesnt_exist_.html$")
class MyForm2(Form): field = TemplateChoiceField(match="^admin/404.html$")
class MyForm1(Form): field = TemplateChoiceField(match="^admin/[0-9]+.html$")
class MyForm(Form): a = IntegerField() b = TemplateChoiceField(match="^admin/[0-9]+.html$")
def test_choices(): x = TemplateChoiceField(match="^admin/[0-9]+.html$") assert set(x.choices) == {('admin/404.html', '404'), ('admin/500.html', '500')}
def test_field_warns_if_missing_dollar_at_end(): with pytest.raises(ImproperlyConfigured): TemplateChoiceField(match="^.*")
def test_field_warns_if_missing_caret_at_start(): with pytest.raises(ImproperlyConfigured): TemplateChoiceField(match=".*$")
def test_field_warns_if_providing_dumb_display_name(): with pytest.raises(ImproperlyConfigured): TemplateChoiceField(display_name='goose', match="^.*$")
def test_field_warns_if_providing_coerce_callable(): with pytest.raises(ImproperlyConfigured): TemplateChoiceField(coerce=int, match="^.*$")
class MyForm(Form): field = TemplateChoiceField( match="^django/forms/widgets/template_.+\.html$", required=False)
class MyForm(Form): field = TemplateChoiceField( match="^django/forms/widgets/template_selector.html$", required=True)