#33219: django.test.client missconversion of json with internal dicts
-------------------------------------+-------------------------------------
               Reporter:  caperutxa  |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Testing    |        Version:  3.2
  framework                          |       Keywords:  test.client,
               Severity:  Normal     |  complex json
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 django.test.client is used to tests post resquest in a django
 restframework application with json as file content.

 For basic json works : {'name': 'Mateu', 'number': 25}

 But if the json has internal dictionaries it is transformed to an array:
 From {'name': 'Mateu', 'number': 25, sales: {'Hubble':25, 'Web':4,
 'telescope': 0}}
 To <QueryDict: {'name': 'Mateu', 'number': 25, sales: ['Hubble', 'Web',
 'telescope']}
 Then it is not possible to iterate over all sales key-value pairs

 Also, if in the code a sales list is requested, it give only the last
 value (in that case telescope)

 sales = request.data['sales']  --> sales will be 'telescope' instead of an
 array at least

 NOTE, that starting the real server and run a request via curl or via web
 browser, it works as expected (a json with internal dictionaries or
 arrays)

 ----

 django-cors-headers         3.10.0
 django-extensions           3.1.3
 django-storages             1.11.1
 djangorestframework         3.12.4
 pytest-django               4.4.0

 linux system (ubuntu)

 ----

 Sample code (set the urls yourself)

 Inside views

 {{{
     @api_view(['POST'])
     def store_iteration(request):
         print(request.data)
         print(request.data['sales'])
 }}}

 Inside tests

 {{{
     class IterationsTestCase(TestCase):
         def setUp(self):
             self.client = Client()

         def test_fake_up(self):
             response = self.client.post('/api/test_it',
                               {'name': 'Mateu', 'number': 25, sales:
 {'Hubble':25, 'Web':4, 'telescope': 0}},
                               format='json')
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33219>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.9f666c46e20d1c62e60419cf173ecc6b%40djangoproject.com.

Reply via email to