This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Wrong assert* method in test_csv.test_register_kwargs masks error
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.araujo Nosy List: Alex.Earl, eric.araujo
Priority: normal Keywords: patch

Created on 2010-12-02 04:38 by Alex.Earl, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
fix-test.diff eric.araujo, 2010-12-02 13:43
Messages (9)
msg123055 - (view) Author: Alex Earl (Alex.Earl) Date: 2010-12-02 04:38
in test_csv.py, the follow test is declared.

def test_register_kwargs(self):
    name = 'fedcba'
    csv.register_dialect(name, delimiter=';')
    try:
        self.assertTrue(csv.get_dialect(name).delimiter, '\t')
        self.assertTrue(list(csv.reader('X;Y;Z', name)), ['X', 'Y', 'Z'])
    finally:
        csv.unregister_dialect(name)

The assertTrue method take an expression to test for "true" and a message to display if that expression is false. If the test's goal is to test that delimiter is set so it's not None, then it will output a tab if the test fails. On the second line, the list would be displayed if the list returned from the reader is an empty list. The result of the reader operation is not the list on the right side anyway.


>>> list(csv.reader('X;Y;Z', name))
[['X'], ['', ''], ['Y'], ['', ''], ['Z']]
msg123064 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-02 11:51
Thank you for the report.  The problem is that asserTrue is used instead of assertEqual; attached patch fixes that and reveals a bug.
msg123065 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-02 11:52
This is the output of the test:

FAIL: test_register_kwargs (__main__.TestDialectRegistry)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "Lib/test/test_csv.py", line 326, in test_register_kwargs
    self.assertEqual(list(csv.reader('X;Y;Z', name)), ['X', 'Y', 'Z'])

- [['X'], ['', ''], ['Y'], ['', ''], ['Z']]
+ ['X', 'Y', 'Z']

Do you want to write a fix for this bug?
msg123066 - (view) Author: Alex Earl (Alex.Earl) Date: 2010-12-02 12:21
The internal _csv module which actually implements the reader method expects the first parameter to be an iterable object. Since strings are iterated by character, that is why this is occuring. So, the fix would need to be made in the _csv module, which is a C module. Would a valid fix be to check if the first parameter is a string and then iterate differently?

Also, the first test, which checks if the delimiter should be assertNotEqual if you are testing for '\t'. A better test might be to test to make sure that the default delimiter is not being set (','). So, it might be better to 

self.assertNotEqual(css.get_dialect(name).delimiter, ',')

or something similar.
msg123071 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-02 13:43
I still think this is a bug in the test.  Attached patch fixes it.
msg123075 - (view) Author: Alex Earl (Alex.Earl) Date: 2010-12-02 14:27
The patch looks good to me. The only question I have is that the previous test that was passing a string, is the expected behavior what was being returned before, or would it be useful to turn the string into an iterable over "lines"?
msg123077 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-02 15:03
I don’t think we should change behavior.  Strings are iterable; csv.reader takes an iterable; this is sensible and documented.  See http://docs.python.org/dev/library/csv#csv.reader and the last example of http://docs.python.org/dev/library/csv#examples
msg123078 - (view) Author: Alex Earl (Alex.Earl) Date: 2010-12-02 15:11
Excellent. As long as it's documented, it works for me.
msg123139 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-12-02 22:40
Patch committed as r86940 (py3k), r86941 (3.1) and r86942 (2.7).
History
Date User Action Args
2022-04-11 14:57:09adminsetgithub: 54811
2010-12-02 22:40:57eric.araujosetstatus: open -> closed
resolution: fixed
messages: + msg123139

stage: needs patch -> resolved
2010-12-02 15:11:31Alex.Earlsetmessages: + msg123078
2010-12-02 15:03:01eric.araujosetmessages: + msg123077
2010-12-02 14:27:41Alex.Earlsetmessages: + msg123075
2010-12-02 13:43:21eric.araujosetfiles: + fix-test.diff
assignee: eric.araujo
messages: + msg123071
2010-12-02 13:42:29eric.araujosetfiles: - fix-test.diff
2010-12-02 12:21:34Alex.Earlsetmessages: + msg123066
2010-12-02 11:52:51eric.araujosetfiles: + fix-test.diff
keywords: + patch
messages: + msg123065
2010-12-02 11:51:08eric.araujosettitle: csv test_register_kwargs has invalid message parameters -> Wrong assert* method in test_csv.test_register_kwargs masks error
components: + Library (Lib)

nosy: + eric.araujo
versions: + Python 3.1, Python 3.2, - Python 2.6
messages: + msg123064
stage: needs patch
2010-12-02 04:38:56Alex.Earlcreate