[Django] #30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)

9 views
Skip to first unread message

Django

unread,
Jan 9, 2019, 12:36:35 PM1/9/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
---------------------------------------------+------------------------
Reporter: adamgilman | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: 2.1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
---------------------------------------------+------------------------
When constructing tests for forms with SELECT elements which allow
multiple item selection, the test client doesn't accept multiple
selections due to the fact it uses dict internally and results in a
squashed single key

Failing test

{{{#!python

def test_post_multi_select(self):
"POST SELECT multiple data to a view"
post_data = QueryDict(mutable=True)
post_data.update({'value':37})
post_data.update({'value':37})
response = self.client.post('/post_view/', post_data)

# Check some response details
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['data'], ['37', '38'])
self.assertEqual(response.templates[0].name, 'POST Template')
self.assertContains(response, 'Data received')

}}}

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

Django

unread,
Jan 9, 2019, 12:42:09 PM1/9/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
-----------------------------------+--------------------------------------

Reporter: adamgilman | Owner: nobody
Type: Uncategorized | Status: new
Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by adamgilman):

* easy: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:1>

Django

unread,
Jan 9, 2019, 2:57:43 PM1/9/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
-----------------------------------+--------------------------------------
Reporter: adamgilman | Owner: nobody
Type: Uncategorized | Status: closed

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Simon Charette):

* status: new => closed
* resolution: => invalid


Comment:

It looks like you have a bug in your reported test case, you are updating
`post_data` twice with `{'value':37}` and never including `38`.

Please re-open if you reproduce with `.update({'value':38})`.

--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:2>

Django

unread,
Jan 17, 2019, 12:15:08 PM1/17/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
-----------------------------------+--------------------------------------
Reporter: Adam Gilman | Owner: nobody
Type: Uncategorized | Status: closed

Component: Testing framework | Version: 2.1
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Adam Gilman):

Apologies, typo in my original test. Good catch. Updated test and failure
below

{{{#!python

def test_post_multi_select(self):
"POST SELECT multiple data to a view"
post_data = QueryDict(mutable=True)
post_data.update({'value':37})

post_data.update({'value':38})


response = self.client.post('/post_view/', post_data)

# Check some response details
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['data'], ['37', '38'])
self.assertEqual(response.templates[0].name, 'POST Template')
self.assertContains(response, 'Data received')

}}}

{{{#!bash
FAIL: test_post_multi_select (test_client.tests.ClientTest)


POST SELECT multiple data to a view

----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/dev2/.pyenv/versions/3.7.0/lib/python3.7/unittest/case.py",
line 59, in testPartExecutor
yield
File "/Users/dev2/.pyenv/versions/3.7.0/lib/python3.7/unittest/case.py",
line 615, in run
testMethod()
File "/Users/dev2/Documents/GitHub/django/tests/test_client/tests.py",
line 113, in test_post_multi_select


self.assertEqual(response.context['data'], ['37', '38'])

File "/Users/dev2/.pyenv/versions/3.7.0/lib/python3.7/unittest/case.py",
line 839, in assertEqual
assertion_func(first, second, msg=msg)
File "/Users/dev2/.pyenv/versions/3.7.0/lib/python3.7/unittest/case.py",
line 832, in _baseAssertEqual
raise self.failureException(msg)
AssertionError: '38' != ['37', '38']
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:3>

Django

unread,
Jan 17, 2019, 12:15:16 PM1/17/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
-----------------------------------+--------------------------------------
Reporter: Adam Gilman | Owner: nobody

Type: Uncategorized | Status: new
Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Adam Gilman):

* status: closed => new
* resolution: invalid =>


--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:4>

Django

unread,
Jan 17, 2019, 12:20:09 PM1/17/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
-----------------------------------+--------------------------------------
Reporter: Adam Gilman | Owner: nobody

Type: Uncategorized | Status: new
Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Description changed by Adam Gilman:

Old description:

> When constructing tests for forms with SELECT elements which allow
> multiple item selection, the test client doesn't accept multiple
> selections due to the fact it uses dict internally and results in a
> squashed single key
>
> Failing test
>
> {{{#!python
>
> def test_post_multi_select(self):
> "POST SELECT multiple data to a view"
> post_data = QueryDict(mutable=True)
> post_data.update({'value':37})
> post_data.update({'value':37})
> response = self.client.post('/post_view/', post_data)
>
> # Check some response details
> self.assertEqual(response.status_code, 200)
> self.assertEqual(response.context['data'], ['37', '38'])
> self.assertEqual(response.templates[0].name, 'POST Template')
> self.assertContains(response, 'Data received')
>
> }}}

New description:

When constructing tests for forms with SELECT elements which allow
multiple item selection, the test client doesn't accept multiple
selections due to the fact it uses dict internally and results in a
squashed single key

Failing test

{{{#!python
def test_post_multi_select(self):
"POST SELECT multiple data to a view"
post_data = QueryDict(mutable=True)
post_data.update({'value':37})

post_data.update({'value':38})


response = self.client.post('/post_view/', post_data)

# Check some response details
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context['data'], ['37', '38'])
self.assertEqual(response.templates[0].name, 'POST Template')
self.assertContains(response, 'Data received')
}}}

*updated

--

--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:5>

Django

unread,
Jan 18, 2019, 10:55:08 AM1/18/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
-----------------------------------+--------------------------------------
Reporter: Adam Gilman | Owner: nobody

Type: Uncategorized | Status: new
Component: Testing framework | Version: 2.1
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Carlton Gibson):

I'm not sure the test is right here.

`post_view` isn't using `getList()`, so even if `POST` is a `QueryDict`,
we'll only get the single value...


{{{

c = Context({'data': request.POST['value']})

}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:6>

Django

unread,
Jan 18, 2019, 11:03:05 AM1/18/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
-----------------------------------+------------------------------------

Reporter: Adam Gilman | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------
Changes (by Carlton Gibson):

* version: 2.1 => master
* type: Uncategorized => Bug
* stage: Unreviewed => Accepted


Comment:

But this fails:

{{{


post_data = QueryDict(mutable=True)
post_data.update({'value':37})

post_data.update({'value':38})
self.assertEqual(post_data.getlist('value'), [37, 38])
request = RequestFactory().post('/post_view/', post_data)
self.assertEqual(request.POST.getlist('value'), ['37', '38'])
}}}


{{{
self.assertEqual(request.POST.getlist('value'), ['37', '38'])
AssertionError: Lists differ: ['38'] != ['37', '38']
}}}

So this looks valid.

--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:7>

Django

unread,
Jan 21, 2019, 11:50:15 AM1/21/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
-----------------------------------+------------------------------------
Reporter: Adam Gilman | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------

Comment (by Nasir Hussain):

Replying to [comment:7 Carlton Gibson]:
**request.POST** is actual representation of **request.body**. A post
request could have only one parameter of the same name in its body. That's
why it returns a single element. I don't think if it's a bug.

--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:8>

Django

unread,
Jan 21, 2019, 1:35:00 PM1/21/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
-----------------------------------+------------------------------------
Reporter: Adam Gilman | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+------------------------------------

Comment (by Tim Graham):

I didn't look into the cause of the issue but normally a dictionary rather
than a `QueryDict` is used as test client data.

`post_data = {'value': [37, 38]}` passes the test.

--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:9>

Django

unread,
Jan 21, 2019, 2:07:51 PM1/21/19
to django-...@googlegroups.com
#30089: TestClient doesn't accept data with duplicate keys (SELECT multiple)
-----------------------------------+--------------------------------------

Reporter: Adam Gilman | Owner: nobody
Type: Bug | Status: closed

Component: Testing framework | Version: master
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* status: new => closed
* resolution: => invalid
* stage: Accepted => Unreviewed


Comment:

Then it’s incorrect usage. Good. Thanks both!

--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:10>

Reply all
Reply to author
Forward
0 new messages