| 1 |
class AppTestDialect(object): |
| 2 |
spaceconfig = dict(usemodules=['_csv']) |
| 3 |
|
| 4 |
def test_register_dialect(self): |
| 5 |
import _csv |
| 6 |
|
| 7 |
attrs = [('delimiter', ','), |
| 8 |
('doublequote', True), |
| 9 |
('escapechar', None), |
| 10 |
('lineterminator', '\r\n'), |
| 11 |
('quotechar', '"'), |
| 12 |
('quoting', _csv.QUOTE_MINIMAL), |
| 13 |
('skipinitialspace', False), |
| 14 |
('strict', False), |
| 15 |
] |
| 16 |
|
| 17 |
for changeattr, newvalue in [('delimiter', ':'), |
| 18 |
('doublequote', False), |
| 19 |
('escapechar', '/'), |
| 20 |
('lineterminator', '---\n'), |
| 21 |
('quotechar', '%'), |
| 22 |
('quoting', _csv.QUOTE_NONNUMERIC), |
| 23 |
('skipinitialspace', True), |
| 24 |
('strict', True)]: |
| 25 |
kwargs = {changeattr: newvalue} |
| 26 |
_csv.register_dialect('foo1', **kwargs) |
| 27 |
d = _csv.get_dialect('foo1') |
| 28 |
assert d.__class__.__name__ == 'Dialect' |
| 29 |
for attr, default in attrs: |
| 30 |
if attr == changeattr: |
| 31 |
expected = newvalue |
| 32 |
else: |
| 33 |
expected = default |
| 34 |
assert getattr(d, attr) == expected |
| 35 |
|
| 36 |
def test_register_dialect_base_1(self): |
| 37 |
import _csv |
| 38 |
_csv.register_dialect('foo1', escapechar='!') |
| 39 |
_csv.register_dialect('foo2', 'foo1', strict=True) |
| 40 |
d1 = _csv.get_dialect('foo1') |
| 41 |
assert d1.escapechar == '!' |
| 42 |
assert d1.strict == False |
| 43 |
d2 = _csv.get_dialect('foo2') |
| 44 |
assert d2.escapechar == '!' |
| 45 |
assert d2.strict == True |
| 46 |
|
| 47 |
def test_register_dialect_base_2(self): |
| 48 |
import _csv |
| 49 |
class Foo1: |
| 50 |
escapechar = '?' |
| 51 |
_csv.register_dialect('foo2', Foo1, strict=True) |
| 52 |
d2 = _csv.get_dialect('foo2') |
| 53 |
assert d2.escapechar == '?' |
| 54 |
assert d2.strict == True |
| 55 |
|
| 56 |
def test_typeerror(self): |
| 57 |
import _csv |
| 58 |
attempts = [("delimiter", '', 123), |
| 59 |
("escapechar", Ellipsis, 'foo', 0), |
| 60 |
("lineterminator", -132), |
| 61 |
("quotechar", '', 25), |
| 62 |
("quoting", 4, '', '\x00'), |
| 63 |
] |
| 64 |
for attempt in attempts: |
| 65 |
name = attempt[0] |
| 66 |
for value in attempt[1:]: |
| 67 |
kwargs = {name: value} |
| 68 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', **kwargs) |
| 69 |
assert name in exc_info.value.args[0] |
| 70 |
|
| 71 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', lineterminator=4) |
| 72 |
assert exc_info.value.args[0] == '"lineterminator" must be a string' |
| 73 |
|
| 74 |
def test_typeerror_delimiter_message(self): |
| 75 |
import _csv |
| 76 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', delimiter=None) |
| 77 |
assert str(exc_info.value) == '"delimiter" must be string, not NoneType' |
| 78 |
|
| 79 |
def test_typeerror_empty_escapechar(self): |
| 80 |
import _csv |
| 81 |
_csv.register_dialect('foo1_escapechar', escapechar=None) |
| 82 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', escapechar='') |
| 83 |
assert str(exc_info.value) == '"escapechar" must be a 1-character string' |
| 84 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', escapechar=b'') |
| 85 |
print(str(exc_info.value)) |
| 86 |
assert str(exc_info.value) == '"escapechar" must be string or None, not bytes' |
| 87 |
|
| 88 |
def test_typeerror_empty_quotechar_but_quoting(self): |
| 89 |
import _csv |
| 90 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', quotechar='', quoting=_csv.QUOTE_ALL) |
| 91 |
assert str(exc_info.value) == '"quotechar" must be a 1-character string' |
| 92 |
exc_info = raises(TypeError, _csv.reader, [], quoting=_csv.QUOTE_NONE, quotechar='') |
| 93 |
print(str(exc_info.value)) |
| 94 |
assert str(exc_info.value) == '"quotechar" must be a 1-character string' |
| 95 |
|
| 96 |
def test_bool_arg(self): |
| 97 |
|
| 98 |
import _csv |
| 99 |
_csv.register_dialect('foo1', doublequote=[]) |
| 100 |
assert _csv.get_dialect('foo1').doublequote == False |
| 101 |
_csv.register_dialect('foo1', skipinitialspace=2) |
| 102 |
assert _csv.get_dialect('foo1').skipinitialspace == True |
| 103 |
_csv.register_dialect('foo1', strict=_csv) |
| 104 |
assert _csv.get_dialect('foo1').strict == True |
| 105 |
|
| 106 |
def test_delimiter(self): |
| 107 |
import _csv |
| 108 |
|
| 109 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', delimiter=":::") |
| 110 |
assert exc_info.value.args[0] == '"delimiter" must be a 1-character string' |
| 111 |
|
| 112 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', delimiter="") |
| 113 |
assert exc_info.value.args[0] == '"delimiter" must be a 1-character string' |
| 114 |
|
| 115 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', delimiter=b",") |
| 116 |
assert exc_info.value.args[0] == '"delimiter" must be string, not bytes' |
| 117 |
|
| 118 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', delimiter=4) |
| 119 |
assert exc_info.value.args[0] == '"delimiter" must be string, not int' |
| 120 |
|
| 121 |
def test_quotechar(self): |
| 122 |
import _csv |
| 123 |
|
| 124 |
exc_info = raises(TypeError, _csv.register_dialect, 'foo1', quotechar=4) |
| 125 |
assert exc_info.value.args[0] == '"quotechar" must be string or None, not int' |
| 126 |
|
| 127 |
def test_line_terminator(self): |
| 128 |
|
| 129 |
import _csv |
| 130 |
_csv.register_dialect('foo1', lineterminator='') |
| 131 |
assert _csv.get_dialect('foo1').lineterminator == '' |
| 132 |
|
| 133 |
def test_unregister_dialect(self): |
| 134 |
import _csv |
| 135 |
_csv.register_dialect('foo1') |
| 136 |
_csv.unregister_dialect('foo1') |
| 137 |
raises(_csv.Error, _csv.get_dialect, 'foo1') |
| 138 |
raises(_csv.Error, _csv.unregister_dialect, 'foo1') |
| 139 |
|
| 140 |
def test_list_dialects(self): |
| 141 |
import _csv |
| 142 |
lst = _csv.list_dialects() |
| 143 |
assert type(lst) is list |
| 144 |
assert 'neverseen' not in lst |
| 145 |
_csv.register_dialect('neverseen') |
| 146 |
lst = _csv.list_dialects() |
| 147 |
assert 'neverseen' in lst |
| 148 |
_csv.unregister_dialect('neverseen') |
| 149 |
lst = _csv.list_dialects() |
| 150 |
assert 'neverseen' not in lst |
| 151 |
|
| 152 |
def test_pickle_dialect(self): |
| 153 |
import _csv |
| 154 |
import copy |
| 155 |
_csv.register_dialect('foo') |
| 156 |
raises(TypeError, copy.copy, _csv.get_dialect('foo')) |