Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Updated forms unit tests to reflect new fields and widgets from [3955…

…] and [3956]

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3957 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 964ccd10233dd03067f1b91b4312071f390fb452 1 parent c0eab79
Adrian Holovaty authored

Showing 1 changed file with 209 additions and 0 deletions. Show diff stats Hide diff stats

  1. 209  tests/regressiontests/forms/tests.py
209  tests/regressiontests/forms/tests.py
@@ -29,6 +29,84 @@
29 29
 >>> w.render('email', '', attrs={'class': 'special'})
30 30
 u'<input type="text" class="special" name="email" />'
31 31
 
  32
+# PasswordInput Widget ############################################################
  33
+
  34
+>>> w = PasswordInput()
  35
+>>> w.render('email', '')
  36
+u'<input type="password" name="email" />'
  37
+>>> w.render('email', None)
  38
+u'<input type="password" name="email" />'
  39
+>>> w.render('email', 'test@example.com')
  40
+u'<input type="password" name="email" value="test@example.com" />'
  41
+>>> w.render('email', 'some "quoted" & ampersanded value')
  42
+u'<input type="password" name="email" value="some &quot;quoted&quot; &amp; ampersanded value" />'
  43
+>>> w.render('email', 'test@example.com', attrs={'class': 'fun'})
  44
+u'<input type="password" name="email" value="test@example.com" class="fun" />'
  45
+
  46
+You can also pass 'attrs' to the constructor:
  47
+>>> w = PasswordInput(attrs={'class': 'fun'})
  48
+>>> w.render('email', '')
  49
+u'<input type="password" class="fun" name="email" />'
  50
+>>> w.render('email', 'foo@example.com')
  51
+u'<input type="password" class="fun" value="foo@example.com" name="email" />'
  52
+
  53
+'attrs' passed to render() get precedence over those passed to the constructor:
  54
+>>> w = PasswordInput(attrs={'class': 'pretty'})
  55
+>>> w.render('email', '', attrs={'class': 'special'})
  56
+u'<input type="password" class="special" name="email" />'
  57
+
  58
+# HiddenInput Widget ############################################################
  59
+
  60
+>>> w = HiddenInput()
  61
+>>> w.render('email', '')
  62
+u'<input type="hidden" name="email" />'
  63
+>>> w.render('email', None)
  64
+u'<input type="hidden" name="email" />'
  65
+>>> w.render('email', 'test@example.com')
  66
+u'<input type="hidden" name="email" value="test@example.com" />'
  67
+>>> w.render('email', 'some "quoted" & ampersanded value')
  68
+u'<input type="hidden" name="email" value="some &quot;quoted&quot; &amp; ampersanded value" />'
  69
+>>> w.render('email', 'test@example.com', attrs={'class': 'fun'})
  70
+u'<input type="hidden" name="email" value="test@example.com" class="fun" />'
  71
+
  72
+You can also pass 'attrs' to the constructor:
  73
+>>> w = HiddenInput(attrs={'class': 'fun'})
  74
+>>> w.render('email', '')
  75
+u'<input type="hidden" class="fun" name="email" />'
  76
+>>> w.render('email', 'foo@example.com')
  77
+u'<input type="hidden" class="fun" value="foo@example.com" name="email" />'
  78
+
  79
+'attrs' passed to render() get precedence over those passed to the constructor:
  80
+>>> w = HiddenInput(attrs={'class': 'pretty'})
  81
+>>> w.render('email', '', attrs={'class': 'special'})
  82
+u'<input type="hidden" class="special" name="email" />'
  83
+
  84
+# FileInput Widget ############################################################
  85
+
  86
+>>> w = FileInput()
  87
+>>> w.render('email', '')
  88
+u'<input type="file" name="email" />'
  89
+>>> w.render('email', None)
  90
+u'<input type="file" name="email" />'
  91
+>>> w.render('email', 'test@example.com')
  92
+u'<input type="file" name="email" value="test@example.com" />'
  93
+>>> w.render('email', 'some "quoted" & ampersanded value')
  94
+u'<input type="file" name="email" value="some &quot;quoted&quot; &amp; ampersanded value" />'
  95
+>>> w.render('email', 'test@example.com', attrs={'class': 'fun'})
  96
+u'<input type="file" name="email" value="test@example.com" class="fun" />'
  97
+
  98
+You can also pass 'attrs' to the constructor:
  99
+>>> w = FileInput(attrs={'class': 'fun'})
  100
+>>> w.render('email', '')
  101
+u'<input type="file" class="fun" name="email" />'
  102
+>>> w.render('email', 'foo@example.com')
  103
+u'<input type="file" class="fun" value="foo@example.com" name="email" />'
  104
+
  105
+'attrs' passed to render() get precedence over those passed to the constructor:
  106
+>>> w = HiddenInput(attrs={'class': 'pretty'})
  107
+>>> w.render('email', '', attrs={'class': 'special'})
  108
+u'<input type="hidden" class="special" name="email" />'
  109
+
32 110
 # Textarea Widget #############################################################
33 111
 
34 112
 >>> w = Textarea()
@@ -77,6 +155,87 @@
77 155
 >>> w.render('is_cool', '', attrs={'class': 'special'})
78 156
 u'<input type="checkbox" class="special" name="is_cool" />'
79 157
 
  158
+# Select Widget ###############################################################
  159
+
  160
+>>> w = Select()
  161
+>>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
  162
+<select name="beatle">
  163
+<option value="J" selected="selected">John</option>
  164
+<option value="P">Paul</option>
  165
+<option value="G">George</option>
  166
+<option value="R">Ringo</option>
  167
+</select>
  168
+
  169
+If the value is None, none of the options are selected:
  170
+>>> print w.render('beatle', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
  171
+<select name="beatle">
  172
+<option value="J">John</option>
  173
+<option value="P">Paul</option>
  174
+<option value="G">George</option>
  175
+<option value="R">Ringo</option>
  176
+</select>
  177
+
  178
+If the value corresponds to a label (but not to an option value), none of the options are selected:
  179
+>>> print w.render('beatle', 'John', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
  180
+<select name="beatle">
  181
+<option value="J">John</option>
  182
+<option value="P">Paul</option>
  183
+<option value="G">George</option>
  184
+<option value="R">Ringo</option>
  185
+</select>
  186
+
  187
+The value is compared to its str():
  188
+>>> print w.render('num', 2, choices=[('1', '1'), ('2', '2'), ('3', '3')])
  189
+<select name="num">
  190
+<option value="1">1</option>
  191
+<option value="2" selected="selected">2</option>
  192
+<option value="3">3</option>
  193
+</select>
  194
+>>> print w.render('num', '2', choices=[(1, 1), (2, 2), (3, 3)])
  195
+<select name="num">
  196
+<option value="1">1</option>
  197
+<option value="2" selected="selected">2</option>
  198
+<option value="3">3</option>
  199
+</select>
  200
+>>> print w.render('num', 2, choices=[(1, 1), (2, 2), (3, 3)])
  201
+<select name="num">
  202
+<option value="1">1</option>
  203
+<option value="2" selected="selected">2</option>
  204
+<option value="3">3</option>
  205
+</select>
  206
+
  207
+The 'choices' argument can be any iterable:
  208
+>>> def get_choices():
  209
+...     for i in range(5):
  210
+...         yield (i, i)
  211
+>>> print w.render('num', 2, choices=get_choices())
  212
+<select name="num">
  213
+<option value="0">0</option>
  214
+<option value="1">1</option>
  215
+<option value="2" selected="selected">2</option>
  216
+<option value="3">3</option>
  217
+<option value="4">4</option>
  218
+</select>
  219
+
  220
+You can also pass 'choices' to the constructor:
  221
+>>> w = Select(choices=[(1, 1), (2, 2), (3, 3)])
  222
+>>> print w.render('num', 2)
  223
+<select name="num">
  224
+<option value="1">1</option>
  225
+<option value="2" selected="selected">2</option>
  226
+<option value="3">3</option>
  227
+</select>
  228
+
  229
+If 'choices' is passed to both the constructor and render(), then they'll both be in the output:
  230
+>>> print w.render('num', 2, choices=[(4, 4), (5, 5)])
  231
+<select name="num">
  232
+<option value="1">1</option>
  233
+<option value="2" selected="selected">2</option>
  234
+<option value="3">3</option>
  235
+<option value="4">4</option>
  236
+<option value="5">5</option>
  237
+</select>
  238
+
80 239
 # CharField ###################################################################
81 240
 
82 241
 >>> f = CharField(required=False)
@@ -353,6 +512,56 @@
353 512
 ...
354 513
 ValidationError: [u'Enter a valid e-mail address.']
355 514
 
  515
+# URLField ##################################################################
  516
+
  517
+>>> f = URLField()
  518
+>>> f.to_python('http://example.com')
  519
+u'http://example.com'
  520
+>>> f.to_python('http://www.example.com')
  521
+u'http://www.example.com'
  522
+>>> f.to_python('foo')
  523
+Traceback (most recent call last):
  524
+...
  525
+ValidationError: [u'Enter a valid URL.']
  526
+>>> f.to_python('example.com')
  527
+Traceback (most recent call last):
  528
+...
  529
+ValidationError: [u'Enter a valid URL.']
  530
+>>> f.to_python('http://')
  531
+Traceback (most recent call last):
  532
+...
  533
+ValidationError: [u'Enter a valid URL.']
  534
+>>> f.to_python('http://example')
  535
+Traceback (most recent call last):
  536
+...
  537
+ValidationError: [u'Enter a valid URL.']
  538
+>>> f.to_python('http://example.')
  539
+Traceback (most recent call last):
  540
+...
  541
+ValidationError: [u'Enter a valid URL.']
  542
+>>> f.to_python('http://.com')
  543
+Traceback (most recent call last):
  544
+...
  545
+ValidationError: [u'Enter a valid URL.']
  546
+
  547
+URLField takes an optional verify_exists parameter, which is False by default.
  548
+This verifies that the URL is live on the Internet and doesn't return a 404 or 500:
  549
+>>> f = URLField(verify_exists=True)
  550
+>>> f.to_python('http://www.google.com')
  551
+u'http://www.google.com'
  552
+>>> f.to_python('http://example')
  553
+Traceback (most recent call last):
  554
+...
  555
+ValidationError: [u'Enter a valid URL.']
  556
+>>> f.to_python('http://www.jfoiwjfoi23jfoijoaijfoiwjofiwjefewl.com') # bad domain
  557
+Traceback (most recent call last):
  558
+...
  559
+ValidationError: [u'This URL appears to be a broken link.']
  560
+>>> f.to_python('http://google.com/we-love-microsoft.html') # good domain, bad page
  561
+Traceback (most recent call last):
  562
+...
  563
+ValidationError: [u'This URL appears to be a broken link.']
  564
+
356 565
 # BooleanField ################################################################
357 566
 
358 567
 >>> f = BooleanField()

0 notes on commit 964ccd1

Please sign in to comment.
Something went wrong with that request. Please try again.