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.
* easy: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:1>
* 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>
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>
* status: closed => new
* resolution: invalid =>
--
Ticket URL: <https://code.djangoproject.com/ticket/30089#comment:4>
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>
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>
* 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>
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>
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>
* 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>