Ticket #16644: 16644.patch

File 16644.patch, 14.2 KB (added by Aymeric Augustin, 13 years ago)
  • tests/modeltests/model_forms/tests.py

     
    658658<tr><th>Pub date:</th><td><input type="text" name="pub_date" /></td></tr>
    659659<tr><th>Writer:</th><td><select name="writer">
    660660<option value="" selected="selected">---------</option>
    661 <option value="2">Bob Woodward</option>
    662 <option value="1">Mike Royko</option>
     661<option value="%s">Bob Woodward</option>
     662<option value="%s">Mike Royko</option>
    663663</select></td></tr>
    664664<tr><th>Article:</th><td><textarea rows="10" cols="40" name="article"></textarea></td></tr>
    665665<tr><th>Categories:</th><td><select multiple="multiple" name="categories">
    666 <option value="1">Entertainment</option>
    667 <option value="2">It&#39;s a test</option>
    668 <option value="3">Third test</option>
     666<option value="%s">Entertainment</option>
     667<option value="%s">It&#39;s a test</option>
     668<option value="%s">Third test</option>
    669669</select><br /><span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></td></tr>
    670670<tr><th>Status:</th><td><select name="status">
    671671<option value="" selected="selected">---------</option>
    672672<option value="1">Draft</option>
    673673<option value="2">Pending</option>
    674674<option value="3">Live</option>
    675 </select></td></tr>''')
     675</select></td></tr>''' % (w_woodward.pk, w_royko.pk, c1.pk, c2.pk, c3.pk))
    676676
    677677        # You can restrict a form to a subset of the complete list of fields
    678678        # by providing a 'fields' argument. If you try to save a
     
    706706<li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
    707707<li>Writer: <select name="writer">
    708708<option value="">---------</option>
    709 <option value="2">Bob Woodward</option>
    710 <option value="1" selected="selected">Mike Royko</option>
     709<option value="%s">Bob Woodward</option>
     710<option value="%s" selected="selected">Mike Royko</option>
    711711</select></li>
    712712<li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
    713713<li>Categories: <select multiple="multiple" name="categories">
    714 <option value="1">Entertainment</option>
    715 <option value="2">It&#39;s a test</option>
    716 <option value="3">Third test</option>
     714<option value="%s">Entertainment</option>
     715<option value="%s">It&#39;s a test</option>
     716<option value="%s">Third test</option>
    717717</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    718718<li>Status: <select name="status">
    719719<option value="" selected="selected">---------</option>
    720720<option value="1">Draft</option>
    721721<option value="2">Pending</option>
    722722<option value="3">Live</option>
    723 </select></li>''')
     723</select></li>''' % (w_woodward.pk, w_royko.pk, c1.pk, c2.pk, c3.pk))
    724724        f = TestArticleForm({
    725725                'headline': u'Test headline',
    726726                'slug': 'test-headline',
     
    760760<li>Pub date: <input type="text" name="pub_date" value="1988-01-04" /></li>
    761761<li>Writer: <select name="writer">
    762762<option value="">---------</option>
    763 <option value="2">Bob Woodward</option>
    764 <option value="1" selected="selected">Mike Royko</option>
     763<option value="%s">Bob Woodward</option>
     764<option value="%s" selected="selected">Mike Royko</option>
    765765</select></li>
    766766<li>Article: <textarea rows="10" cols="40" name="article">Hello.</textarea></li>
    767767<li>Categories: <select multiple="multiple" name="categories">
    768 <option value="1" selected="selected">Entertainment</option>
    769 <option value="2">It&#39;s a test</option>
    770 <option value="3">Third test</option>
     768<option value="%s" selected="selected">Entertainment</option>
     769<option value="%s">It&#39;s a test</option>
     770<option value="%s">Third test</option>
    771771</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    772772<li>Status: <select name="status">
    773773<option value="" selected="selected">---------</option>
    774774<option value="1">Draft</option>
    775775<option value="2">Pending</option>
    776776<option value="3">Live</option>
    777 </select></li>''')
     777</select></li>''' % (w_woodward.pk, w_royko.pk, c1.pk, c2.pk, c3.pk))
    778778
    779779        # Initial values can be provided for model forms
    780780        f = TestArticleForm(
     
    788788<li>Pub date: <input type="text" name="pub_date" /></li>
    789789<li>Writer: <select name="writer">
    790790<option value="" selected="selected">---------</option>
    791 <option value="2">Bob Woodward</option>
    792 <option value="1">Mike Royko</option>
     791<option value="%s">Bob Woodward</option>
     792<option value="%s">Mike Royko</option>
    793793</select></li>
    794794<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
    795795<li>Categories: <select multiple="multiple" name="categories">
    796 <option value="1" selected="selected">Entertainment</option>
    797 <option value="2" selected="selected">It&#39;s a test</option>
    798 <option value="3">Third test</option>
     796<option value="%s" selected="selected">Entertainment</option>
     797<option value="%s" selected="selected">It&#39;s a test</option>
     798<option value="%s">Third test</option>
    799799</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    800800<li>Status: <select name="status">
    801801<option value="" selected="selected">---------</option>
    802802<option value="1">Draft</option>
    803803<option value="2">Pending</option>
    804804<option value="3">Live</option>
    805 </select></li>''')
     805</select></li>''' % (w_woodward.pk, w_royko.pk, c1.pk, c2.pk, c3.pk))
    806806
    807807        f = TestArticleForm({
    808808                'headline': u'New headline',
     
    882882<li>Pub date: <input type="text" name="pub_date" /></li>
    883883<li>Writer: <select name="writer">
    884884<option value="" selected="selected">---------</option>
    885 <option value="2">Bob Woodward</option>
    886 <option value="1">Mike Royko</option>
     885<option value="%s">Bob Woodward</option>
     886<option value="%s">Mike Royko</option>
    887887</select></li>
    888888<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
    889889<li>Categories: <select multiple="multiple" name="categories">
    890 <option value="1">Entertainment</option>
    891 <option value="2">It&#39;s a test</option>
    892 <option value="3">Third</option>
     890<option value="%s">Entertainment</option>
     891<option value="%s">It&#39;s a test</option>
     892<option value="%s">Third</option>
    893893</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    894894<li>Status: <select name="status">
    895895<option value="" selected="selected">---------</option>
    896896<option value="1">Draft</option>
    897897<option value="2">Pending</option>
    898898<option value="3">Live</option>
    899 </select></li>''')
     899</select></li>''' % (w_woodward.pk, w_royko.pk, c1.pk, c2.pk, c3.pk))
    900900
    901901        c4 = Category.objects.create(name='Fourth', url='4th')
    902902        self.assertEqual(c4.name, 'Fourth')
    903         self.assertEqual(Writer.objects.create(name='Carl Bernstein').name, 'Carl Bernstein')
     903        w_bernstein = Writer.objects.create(name='Carl Bernstein')
     904        self.assertEqual(w_bernstein.name, 'Carl Bernstein')
    904905        self.assertEqual(f.as_ul(), '''<li>Headline: <input type="text" name="headline" maxlength="50" /></li>
    905906<li>Slug: <input type="text" name="slug" maxlength="50" /></li>
    906907<li>Pub date: <input type="text" name="pub_date" /></li>
    907908<li>Writer: <select name="writer">
    908909<option value="" selected="selected">---------</option>
    909 <option value="2">Bob Woodward</option>
    910 <option value="3">Carl Bernstein</option>
    911 <option value="1">Mike Royko</option>
     910<option value="%s">Bob Woodward</option>
     911<option value="%s">Carl Bernstein</option>
     912<option value="%s">Mike Royko</option>
    912913</select></li>
    913914<li>Article: <textarea rows="10" cols="40" name="article"></textarea></li>
    914915<li>Categories: <select multiple="multiple" name="categories">
    915 <option value="1">Entertainment</option>
    916 <option value="2">It&#39;s a test</option>
    917 <option value="3">Third</option>
    918 <option value="4">Fourth</option>
     916<option value="%s">Entertainment</option>
     917<option value="%s">It&#39;s a test</option>
     918<option value="%s">Third</option>
     919<option value="%s">Fourth</option>
    919920</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></li>
    920921<li>Status: <select name="status">
    921922<option value="" selected="selected">---------</option>
    922923<option value="1">Draft</option>
    923924<option value="2">Pending</option>
    924925<option value="3">Live</option>
    925 </select></li>''')
     926</select></li>''' % (w_woodward.pk, w_bernstein.pk, w_royko.pk, c1.pk, c2.pk, c3.pk, c4.pk))
    926927
    927928        # ModelChoiceField ############################################################
    928929
    929930        f = forms.ModelChoiceField(Category.objects.all())
    930931        self.assertEqual(list(f.choices), [
    931932            (u'', u'---------'),
    932             (1, u'Entertainment'),
    933             (2, u"It's a test"),
    934             (3, u'Third'),
    935             (4, u'Fourth')])
     933            (c1.pk, u'Entertainment'),
     934            (c2.pk, u"It's a test"),
     935            (c3.pk, u'Third'),
     936            (c4.pk, u'Fourth')])
    936937        with self.assertRaises(ValidationError):
    937938            f.clean('')
    938939        with self.assertRaises(ValidationError):
     
    967968        f.queryset = Category.objects.exclude(name='Fourth')
    968969        self.assertEqual(list(f.choices), [
    969970            (u'', u'---------'),
    970             (1, u'Entertainment'),
    971             (2, u"It's a test"),
    972             (3, u'Third')])
     971            (c1.pk, u'Entertainment'),
     972            (c2.pk, u"It's a test"),
     973            (c3.pk, u'Third')])
    973974        self.assertEqual(f.clean(c3.id).name, 'Third')
    974975        with self.assertRaises(ValidationError):
    975976            f.clean(c4.id)
     
    977978        # check that we can safely iterate choices repeatedly
    978979        gen_one = list(f.choices)
    979980        gen_two = f.choices
    980         self.assertEqual(gen_one[2], (2, u"It's a test"))
     981        self.assertEqual(gen_one[2], (c2.pk, u"It's a test"))
    981982        self.assertEqual(list(gen_two), [
    982983            (u'', u'---------'),
    983             (1, u'Entertainment'),
    984             (2, u"It's a test"),
    985             (3, u'Third')])
     984            (c1.pk, u'Entertainment'),
     985            (c2.pk, u"It's a test"),
     986            (c3.pk, u'Third')])
    986987
    987988        # check that we can override the label_from_instance method to print custom labels (#4620)
    988989        f.queryset = Category.objects.all()
    989990        f.label_from_instance = lambda obj: "category " + str(obj)
    990991        self.assertEqual(list(f.choices), [
    991992            (u'', u'---------'),
    992             (1, 'category Entertainment'),
    993             (2, "category It's a test"),
    994             (3, 'category Third'),
    995             (4, 'category Fourth')])
     993            (c1.pk, 'category Entertainment'),
     994            (c2.pk, "category It's a test"),
     995            (c3.pk, 'category Third'),
     996            (c4.pk, 'category Fourth')])
    996997
    997998        # ModelMultipleChoiceField ####################################################
    998999
    9991000        f = forms.ModelMultipleChoiceField(Category.objects.all())
    10001001        self.assertEqual(list(f.choices), [
    1001             (1, u'Entertainment'),
    1002             (2, u"It's a test"),
    1003             (3, u'Third'),
    1004             (4, u'Fourth')])
     1002            (c1.pk, u'Entertainment'),
     1003            (c2.pk, u"It's a test"),
     1004            (c3.pk, u'Third'),
     1005            (c4.pk, u'Fourth')])
    10051006        with self.assertRaises(ValidationError):
    10061007            f.clean(None)
    10071008        with self.assertRaises(ValidationError):
     
    10461047        # queryset can be changed after the field is created.
    10471048        f.queryset = Category.objects.exclude(name='Fourth')
    10481049        self.assertEqual(list(f.choices), [
    1049             (1, u'Entertainment'),
    1050             (2, u"It's a test"),
    1051             (3, u'Third')])
     1050            (c1.pk, u'Entertainment'),
     1051            (c2.pk, u"It's a test"),
     1052            (c3.pk, u'Third')])
    10521053        self.assertEqual(map(lambda o: o.name, f.clean([c3.id])), ["Third"])
    10531054        with self.assertRaises(ValidationError):
    10541055            f.clean([c4.id])
     
    10581059        f.queryset = Category.objects.all()
    10591060        f.label_from_instance = lambda obj: "multicategory " + str(obj)
    10601061        self.assertEqual(list(f.choices), [
    1061             (1, 'multicategory Entertainment'),
    1062             (2, "multicategory It's a test"),
    1063             (3, 'multicategory Third'),
    1064             (4, 'multicategory Fourth')])
     1062            (c1.pk, 'multicategory Entertainment'),
     1063            (c2.pk, "multicategory It's a test"),
     1064            (c3.pk, 'multicategory Third'),
     1065            (c4.pk, 'multicategory Fourth')])
    10651066
    10661067        # OneToOneField ###############################################################
    10671068
     
    10821083        form = WriterProfileForm()
    10831084        self.assertEqual(form.as_p(), '''<p><label for="id_writer">Writer:</label> <select name="writer" id="id_writer">
    10841085<option value="" selected="selected">---------</option>
    1085 <option value="2">Bob Woodward</option>
    1086 <option value="3">Carl Bernstein</option>
    1087 <option value="4">Joe Better</option>
    1088 <option value="1">Mike Royko</option>
     1086<option value="%s">Bob Woodward</option>
     1087<option value="%s">Carl Bernstein</option>
     1088<option value="%s">Joe Better</option>
     1089<option value="%s">Mike Royko</option>
    10891090</select></p>
    1090 <p><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></p>''')
     1091<p><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></p>''' % (w_woodward.pk, w_bernstein.pk, bw.pk, w_royko.pk))
    10911092
    10921093        data = {
    10931094            'writer': unicode(w_woodward.pk),
     
    11001101        form = WriterProfileForm(instance=instance)
    11011102        self.assertEqual(form.as_p(), '''<p><label for="id_writer">Writer:</label> <select name="writer" id="id_writer">
    11021103<option value="">---------</option>
    1103 <option value="2" selected="selected">Bob Woodward</option>
    1104 <option value="3">Carl Bernstein</option>
    1105 <option value="4">Joe Better</option>
    1106 <option value="1">Mike Royko</option>
     1104<option value="%s" selected="selected">Bob Woodward</option>
     1105<option value="%s">Carl Bernstein</option>
     1106<option value="%s">Joe Better</option>
     1107<option value="%s">Mike Royko</option>
    11071108</select></p>
    1108 <p><label for="id_age">Age:</label> <input type="text" name="age" value="65" id="id_age" /></p>''')
     1109<p><label for="id_age">Age:</label> <input type="text" name="age" value="65" id="id_age" /></p>''' % (w_woodward.pk, w_bernstein.pk, bw.pk, w_royko.pk))
    11091110
    11101111    def test_phone_number_field(self):
    11111112        f = PhoneNumberForm({'phone': '(312) 555-1212', 'description': 'Assistance'})
Back to Top