File: test_in2csv.py

package info (click to toggle)
csvkit 2.1.0-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 40,660 kB
  • sloc: python: 4,921; perl: 1,000; makefile: 131; sql: 4
file content (332 lines) | stat: -rw-r--r-- 13,565 bytes parent folder | download
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import io
import os
import sys
from unittest.mock import patch

from csvkit.utilities.in2csv import In2CSV, launch_new_instance
from tests.utils import CSVKitTestCase, EmptyFileTests, stdin_as_string


class TestIn2CSV(CSVKitTestCase, EmptyFileTests):
    Utility = In2CSV
    default_args = ['-f', 'csv']

    def assertConverted(self, input_format, input_filename, output_filename, additional_args=[]):
        output = self.get_output(['-f', input_format, input_filename] + additional_args)

        with open(output_filename) as f:
            self.assertEqual(output, f.read())

    def test_launch_new_instance(self):
        with patch.object(sys, 'argv', [self.Utility.__name__.lower(), 'examples/dummy.csv']):
            launch_new_instance()

    def test_version(self):
        with self.assertRaises(SystemExit) as e:
            self.get_output(['-V'])

        self.assertEqual(e.exception.code, 0)

    def test_args(self):
        for args in ([], ['-']):
            with self.subTest(args=args):
                self.assertError(
                    launch_new_instance,
                    [],
                    'You must specify a format when providing input as piped data via STDIN.',
                    args=args,
                )

    def test_options(self):
        for options, args, message in (
            (
                [],
                ['dummy.unknown'],
                'Unable to automatically determine the format of the input file. '
                'Try specifying a format with --format.',
            ),
            (
                ['-n'],
                ['dummy.csv'],
                'You cannot use the -n or --names options with non-Excel files.',
            ),
        ):
            with self.subTest(args=options + args):
                self.assertError(launch_new_instance, options, message, args=args)

    def test_locale(self):
        self.assertConverted('csv', 'examples/test_locale.csv',
                             'examples/test_locale_converted.csv', ['--locale', 'de_DE'])

    def test_add_bom(self):
        self.assertConverted('csv', 'examples/test_utf8.csv',
                             'examples/test_utf8_bom.csv', ['--add-bom'])

    def test_no_blanks(self):
        self.assertConverted('csv', 'examples/blanks.csv', 'examples/blanks_converted.csv')

    def test_blanks(self):
        self.assertConverted('csv', 'examples/blanks.csv', 'examples/blanks.csv', ['--blanks'])

    def test_null_value(self):
        input_file = io.BytesIO(b'a,b\nn/a,\\N')

        with stdin_as_string(input_file):
            self.assertLines(['-f', 'csv', '--null-value', '\\N'], [
                'a,b',
                ',',
            ])

        input_file.close()

    def test_null_value_blanks(self):
        input_file = io.BytesIO(b'a,b\nn/a,\\N')

        with stdin_as_string(input_file):
            self.assertLines(['-f', 'csv', '--null-value', '\\N', '--blanks'], [
                'a,b',
                'n/a,',
            ])

        input_file.close()

    def test_no_leading_zeroes(self):
        self.assertConverted('csv', 'examples/test_no_leading_zeroes.csv',
                             'examples/test_no_leading_zeroes.csv', ['--no-leading-zeroes'])

    def test_date_format(self):
        self.assertConverted('csv', 'examples/test_date_format.csv',
                             'examples/test_date_format_converted.csv', ['--date-format', '%d/%m/%Y'])

    def test_date_format_default(self):
        self.assertConverted('csv', 'examples/test_date_format.csv', 'examples/test_date_format.csv')

    def test_numeric_date_format(self):
        self.assertConverted('csv', 'examples/test_numeric_date_format.csv',
                             'examples/test_date_format_converted.csv', ['--date-format', '%Y%m%d'])

    def test_numeric_date_format_default(self):
        self.assertConverted('csv', 'examples/test_numeric_date_format.csv', 'examples/test_numeric_date_format.csv')

    def test_date_like_number(self):
        self.assertConverted('csv', 'examples/date_like_number.csv', 'examples/date_like_number.csv')

    def test_convert_csv(self):
        self.assertConverted('csv', 'examples/testfixed_converted.csv', 'examples/testfixed_converted.csv')

    def test_convert_csv_with_skip_lines(self):
        self.assertConverted('csv', 'examples/test_skip_lines.csv', 'examples/dummy.csv',
                             ['--skip-lines', '3', '--no-inference'])

    def test_convert_tsv(self):
        self.assertConverted('csv', 'examples/dummy.tsv', 'examples/dummy.csv', ['--no-inference'])

    def test_convert_tsv_streaming(self):
        self.assertConverted('csv', 'examples/dummy.tsv', 'examples/dummy.csv',
                             ['--no-inference', '--snifflimit', '0', '--tabs'])

    def test_convert_dbf(self):
        self.assertConverted('dbf', 'examples/testdbf.dbf', 'examples/testdbf_converted.csv')

    def test_convert_json(self):
        self.assertConverted('json', 'examples/testjson.json', 'examples/testjson_converted.csv')

    def test_convert_geojson(self):
        self.assertConverted('geojson', 'examples/test_geojson.json', 'examples/test_geojson.csv')

    def test_convert_ndjson(self):
        self.assertConverted('ndjson', 'examples/testjson_multiline.json', 'examples/testjson_multiline_converted.csv')

    def test_convert_nested_json(self):
        self.assertConverted('json', 'examples/testjson_nested.json', 'examples/testjson_nested_converted.csv')

    def test_convert_xls(self):
        self.assertConverted('xls', 'examples/test.xls', 'examples/testxls_converted.csv')

    def test_convert_xls_with_sheet(self):
        self.assertConverted('xls', 'examples/sheets.xls', 'examples/testxls_converted.csv', ['--sheet', 'data'])

    def test_convert_xls_with_unicode_sheet(self):
        self.assertLines(['--sheet', 'ʤ', 'examples/sheets.xls'], [
            'a,b,c',
            '1.0,2.0,3.0',
        ])

    def test_convert_xls_with_skip_lines(self):
        self.assertConverted('xls', 'examples/test_skip_lines.xls',
                             'examples/testxls_converted.csv', ['--skip-lines', '3'])

    def test_convert_xlsx(self):
        self.assertConverted('xlsx', 'examples/test.xlsx', 'examples/testxlsx_converted.csv')

    def test_convert_xlsx_with_sheet(self):
        self.assertConverted('xlsx', 'examples/sheets.xlsx', 'examples/testxlsx_converted.csv', ['--sheet', 'data'])

    def test_convert_xlsx_with_unicode_sheet(self):
        self.assertLines(['--sheet', 'ʤ', '--no-inference', 'examples/sheets.xlsx'], [
            'a,b,c',
            '1,2,3',
        ])

    def test_convert_xlsx_with_skip_lines(self):
        self.assertConverted('xlsx', 'examples/test_skip_lines.xlsx',
                             'examples/testxlsx_converted.csv', ['--skip-lines', '3'])

    def test_names(self):
        self.assertLines(['--names', 'examples/sheets.xlsx'], [
            'not this one',
            'data',
            'ʤ',
        ])

    def test_csv_no_headers(self):
        self.assertConverted('csv', 'examples/no_header_row.csv', 'examples/dummy.csv',
                             ['--no-header-row', '--no-inference'])

    def test_csv_no_headers_streaming(self):
        self.assertConverted('csv', 'examples/no_header_row.csv', 'examples/dummy.csv',
                             ['--no-header-row', '--no-inference', '--snifflimit', '0'])

    def test_csv_datetime_inference(self):
        input_file = io.BytesIO(b'a\n2015-01-01T00:00:00Z')

        with stdin_as_string(input_file):
            self.assertLines(['-f', 'csv'], [
                'a',
                '2015-01-01T00:00:00+00:00',
            ])

        input_file.close()

    def test_csv_no_inference(self):
        self.assertLines(['--no-inference', 'examples/dummy.csv'], [
            'a,b,c',
            '1,2,3',
        ])

    def test_xls_no_inference(self):
        self.assertLines(['--no-inference', 'examples/dummy.xls'], [
            'a,b,c',
            '1.0,2.0,3.0',
        ])

    def test_xlsx_no_inference(self):
        self.assertLines(['--no-inference', 'examples/dummy.xlsx'], [
            'a,b,c',
            '1,2,3',
        ])

    def test_geojson_no_inference(self):
        input_file = io.BytesIO(
            b'{"a": 1, "b": 2, "type": "FeatureCollection", "features": [{"geometry": {}, "properties": '
            b'{"a": 1, "b": 2, "c": 3}}]}')

        with stdin_as_string(input_file):
            self.assertLines(['--no-inference', '-f', 'geojson'], [
                'id,a,b,c,geojson,type,longitude,latitude',
                ',1,2,3,{},,,',
            ])

        input_file.close()

    def test_json_no_inference(self):
        input_file = io.BytesIO(b'[{"a": 1, "b": 2, "c": 3}]')

        with stdin_as_string(input_file):
            self.assertLines(['--no-inference', '-f', 'json'], [
                'a,b,c',
                '1,2,3',
            ])

        input_file.close()

    def test_ndjson_no_inference(self):
        input_file = io.BytesIO(b'{"a": 1, "b": 2, "c": 3}')

        with stdin_as_string(input_file):
            self.assertLines(['--no-inference', '-f', 'ndjson'], [
                'a,b,c',
                '1,2,3',
            ])

        input_file.close()

    def test_names_xls(self):
        output = self.get_output_as_io(['-n', 'examples/sheets.xls'])

        self.assertEqual(next(output), 'not this one\n')
        self.assertEqual(next(output), 'data\n')

    def test_names_xlsx(self):
        output = self.get_output_as_io(['-n', 'examples/sheets.xlsx'])

        self.assertEqual(next(output), 'not this one\n')
        self.assertEqual(next(output), 'data\n')

    def test_convert_xls_with_write_sheets(self):
        try:
            self.assertConverted('xls', 'examples/sheets.xls', 'examples/testxls_converted.csv',
                                 ['--sheet', 'data', '--write-sheets', "ʤ,1"])
            with open('examples/sheets_0.csv') as f, open('examples/testxls_unicode_converted.csv') as g:
                self.assertEqual(f.read(), g.read())
            with open('examples/sheets_1.csv') as f, open('examples/testxls_converted.csv') as g:
                self.assertEqual(f.read(), g.read())
            self.assertFalse(os.path.exists('examples/sheets_2.csv'))
        finally:
            for suffix in (0, 1):
                path = 'examples/sheets_%d.csv' % suffix
                if os.path.exists(path):
                    os.remove(path)

    def test_convert_xlsx_with_write_sheets(self):
        try:
            self.assertConverted('xlsx', 'examples/sheets.xlsx', 'examples/testxlsx_noinference_converted.csv',
                                 ['--no-inference', '--sheet', 'data', '--write-sheets', "ʤ,1"])
            with open('examples/sheets_0.csv') as f, open('examples/testxlsx_unicode_converted.csv') as g:
                self.assertEqual(f.read(), g.read())
            with open('examples/sheets_1.csv') as f, open('examples/testxlsx_noinference_converted.csv') as g:
                self.assertEqual(f.read(), g.read())
            self.assertFalse(os.path.exists('examples/sheets_2.csv'))
        finally:
            for suffix in (0, 1):
                path = 'examples/sheets_%d.csv' % suffix
                if os.path.exists(path):
                    os.remove(path)

    def test_convert_xls_with_write_sheets_with_names(self):
        try:
            self.assertConverted('xls', 'examples/sheets.xls', 'examples/testxls_converted.csv',
                                 ['--sheet', 'data', '--write-sheets', "ʤ,1", '--use-sheet-names'])
            with open('examples/sheets_ʤ.csv', 'r') as f:
                with open('examples/testxls_unicode_converted.csv', 'r') as g:
                    self.assertEqual(f.read(), g.read())
            with open('examples/sheets_data.csv', 'r') as f:
                with open('examples/testxls_converted.csv', 'r') as g:
                    self.assertEqual(f.read(), g.read())
            self.assertFalse(os.path.exists('examples/sheets_0.csv'))
            self.assertFalse(os.path.exists('examples/sheets_1.csv'))
            self.assertFalse(os.path.exists('examples/sheets_2.csv'))
        finally:
            for suffix in ('ʤ', 'data'):
                path = 'examples/sheets_%s.csv' % suffix
                if os.path.exists(path):
                    os.remove(path)

    def test_convert_xlsx_with_write_sheets_with_names(self):
        try:
            self.assertConverted('xlsx', 'examples/sheets.xlsx', 'examples/testxlsx_noinference_converted.csv',
                                 ['--no-inference', '--sheet', 'data', '--write-sheets', "ʤ,1", '--use-sheet-names'])
            with open('examples/sheets_ʤ.csv', 'r') as f:
                with open('examples/testxlsx_unicode_converted.csv', 'r') as g:
                    self.assertEqual(f.read(), g.read())
            with open('examples/sheets_data.csv', 'r') as f:
                with open('examples/testxlsx_noinference_converted.csv', 'r') as g:
                    self.assertEqual(f.read(), g.read())
            self.assertFalse(os.path.exists('examples/sheets_0.csv'))
            self.assertFalse(os.path.exists('examples/sheets_1.csv'))
            self.assertFalse(os.path.exists('examples/sheets_2.csv'))
        finally:
            for suffix in ('ʤ', 'data'):
                path = 'examples/sheets_%s.csv' % suffix
                if os.path.exists(path):
                    os.remove(path)