#23128: _get_FIELD_display doesn't work with Field.get_choices using Iterators
-------------------------------------+-------------------------------------
     Reporter:  areski               |                    Owner:  Seha16
         Type:  Bug                  |                   Status:  closed
    Component:  Database layer       |                  Version:  master
  (models, ORM)                      |               Resolution:
     Severity:  Normal               |  worksforme
     Keywords:  get_choices          |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by timgraham):

 * status:  assigned => closed
 * resolution:   => worksforme


Old description:

> '''Context:'''
> This issue has been noted when fixing
> https://code.djangoproject.com/ticket/23112
>

> '''Current behavior:'''
> Using _get_FIELD_display on a Field.choices using Iterators, you will get
> the first tuple value.
>

> '''Expected behavior:'''
> Expected result is to get the human-readable name in from the iterator
> tuple.
>

> '''Tests to reproduce the issue:'''
>
> Edit `tests/model_fields/tests.py` add inside `class
> ChoicesTests(test.TestCase)` the following tests:
>
>     # def test_iterator_choices_and_field_display(self):
>     #     """
>     #     Check that get_choices works with Iterators (#23112).
>     #     """
>     #     self.assertEqual(WhizIter(c=1).get_c_display(), 'val-1')    # A
> nested value
>     #     self.assertEqual(WhizIter(c=9).get_c_display(), 9)          #
> Invalid value
>     #     self.assertEqual(WhizIter(c=None).get_c_display(), None)    #
> Blank value
>     #     self.assertEqual(WhizIter(c='').get_c_display(), '')        #
> Empty value

New description:

 '''Context:'''
 This issue has been noted when fixing
 https://code.djangoproject.com/ticket/23112


 '''Current behavior:'''
 Using _get_FIELD_display on a Field.choices using Iterators, you will get
 the first tuple value.


 '''Expected behavior:'''
 Expected result is to get the human-readable name in from the iterator
 tuple.


 '''Tests to reproduce the issue:'''

 Edit `tests/model_fields/tests.py` add inside `class
 ChoicesTests(test.TestCase)` the following tests:
 {{{
 def test_iterator_choices_and_field_display(self):
     """
     Check that get_choices works with Iterators (#23112).
     """
     self.assertEqual(WhizIter(c=1).get_c_display(), 'val-1') # A nested
 value
     self.assertEqual(WhizIter(c=9).get_c_display(), 9) # Invalid value
     self.assertEqual(WhizIter(c=None).get_c_display(), None) # Blank value
     self.assertEqual(WhizIter(c='').get_c_display(), '') # Empty value
 }}}

--

Comment:

 I don't think there's an issue here. The `Counter` used for the choices in
 `WhizIter` generates values starting from two, not one so the provided
 test isn't correct. If you change the first line to
 `self.assertEqual(WhizIter(c=2).get_c_display(), 'val-2')` it passes.

--
Ticket URL: <https://code.djangoproject.com/ticket/23128#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.eff998b75c93271714dedb4c91b91462%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to