1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
import io
import sys
from unittest.mock import patch
from csvkit.utilities.csvformat import CSVFormat, launch_new_instance
from tests.utils import CSVKitTestCase, EmptyFileTests, stdin_as_string
class TestCSVFormat(CSVKitTestCase, EmptyFileTests):
Utility = CSVFormat
def test_launch_new_instance(self):
with patch.object(sys, 'argv', [self.Utility.__name__.lower(), 'examples/dummy.csv']):
launch_new_instance()
def test_skip_lines(self):
self.assertLines(['--skip-lines', '3', '-D', '|', 'examples/test_skip_lines.csv'], [
'a|b|c',
'1|2|3',
])
def test_skip_header(self):
self.assertLines(['--skip-header', 'examples/dummy.csv'], [
'1,2,3',
])
def test_skip_header_no_header_row(self):
self.assertLines(['--no-header-row', '--skip-header', 'examples/no_header_row.csv'], [
'1,2,3',
])
def test_no_header_row(self):
self.assertLines(['--no-header-row', 'examples/no_header_row.csv'], [
'a,b,c',
'1,2,3',
])
def test_linenumbers(self):
self.assertLines(['--linenumbers', 'examples/dummy.csv'], [
'line_number,a,b,c',
'1,1,2,3',
])
def test_delimiter(self):
self.assertLines(['-D', '|', 'examples/dummy.csv'], [
'a|b|c',
'1|2|3',
])
def test_tabs(self):
self.assertLines(['-T', 'examples/dummy.csv'], [
'a\tb\tc',
'1\t2\t3',
])
def test_asv(self):
self.assertLines(['-A', 'examples/dummy.csv'], [
'a\x1fb\x1fc\x1e1\x1f2\x1f3\x1e',
], newline_at_eof=False)
def test_quotechar(self):
input_file = io.BytesIO(b'a,b,c\n1#2,3,4\n')
with stdin_as_string(input_file):
self.assertLines(['-Q', '#'], [
'a,b,c',
'#1##2#,3,4',
])
input_file.close()
def test_doublequote(self):
input_file = io.BytesIO(b'a\n"a ""quoted"" string"')
with stdin_as_string(input_file):
self.assertLines(['-P', '#', '-B'], [
'a',
'a #"quoted#" string',
])
input_file.close()
def test_escapechar(self):
input_file = io.BytesIO(b'a,b,c\n1"2,3,4\n')
with stdin_as_string(input_file):
self.assertLines(['-P', '#', '-U', '3'], [
'a,b,c',
'1#"2,3,4',
])
input_file.close()
def test_lineterminator(self):
self.assertLines(['-M', 'XYZ', 'examples/dummy.csv'], [
'a,b,cXYZ1,2,3XYZ',
], newline_at_eof=False)
class TestCSVFormatQuoteNonNumeric(CSVKitTestCase, EmptyFileTests):
Utility = CSVFormat
# New test compared to TestCSVFormat.
def test_locale(self):
self.assertLines(['-U', '2', '--locale', 'de_DE', 'examples/test_locale.csv'], [
'"a","b","c"',
'1.7,200000000,""',
])
def test_launch_new_instance(self):
with patch.object(sys, 'argv', [self.Utility.__name__.lower(), 'examples/dummy.csv']):
launch_new_instance()
def test_skip_lines(self):
self.assertLines(['-U', '2', '--skip-lines', '3', '-D', '|', 'examples/test_skip_lines.csv'], [
'"a"|"b"|"c"',
'1|2|3',
])
def test_skip_header(self):
self.assertLines(['-U', '2', '--skip-header', 'examples/dummy.csv'], [
'1,2,3',
])
def test_skip_header_no_header_row(self):
self.assertLines(['-U', '2', '--no-header-row', '--skip-header', 'examples/no_header_row.csv'], [
'1,2,3',
])
def test_no_header_row(self):
self.assertLines(['-U', '2', '--no-header-row', 'examples/no_header_row.csv'], [
'"a","b","c"',
'1,2,3',
])
def test_linenumbers(self):
self.assertLines(['-U', '2', '--linenumbers', 'examples/dummy.csv'], [
'"line_number","a","b","c"',
'1,1,2,3',
])
def test_delimiter(self):
self.assertLines(['-U', '2', '-D', '|', 'examples/dummy.csv'], [
'"a"|"b"|"c"',
'1|2|3',
])
def test_tabs(self):
self.assertLines(['-U', '2', '-T', 'examples/dummy.csv'], [
'"a"\t"b"\t"c"',
'1\t2\t3',
])
def test_asv(self):
self.assertLines(['-U', '2', '-A', 'examples/dummy.csv'], [
'"a"\x1f"b"\x1f"c"\x1e1\x1f2\x1f3\x1e',
], newline_at_eof=False)
def test_quotechar(self):
input_file = io.BytesIO(b'a,b,c\n1#2,3,4\n')
with stdin_as_string(input_file):
self.assertLines(['-U', '2', '-Q', '#'], [
'#a#,#b#,#c#',
'#1##2#,3,4',
])
input_file.close()
def test_doublequote(self):
input_file = io.BytesIO(b'a\n"a ""quoted"" string"')
with stdin_as_string(input_file):
self.assertLines(['-U', '2', '-P', '#', '-B'], [
'"a"',
'"a #"quoted#" string"',
])
input_file.close()
def test_escapechar(self):
input_file = io.BytesIO(b'a,b,c\n1"2,3,4\n')
with stdin_as_string(input_file):
self.assertLines(['-U', '2', '-P', '#', '-U', '3'], [
'a,b,c',
'1#"2,3,4',
])
input_file.close()
def test_lineterminator(self):
self.assertLines(['-U', '2', '-M', 'XYZ', 'examples/dummy.csv'], [
'"a","b","c"XYZ1,2,3XYZ',
], newline_at_eof=False)
|