forked from wireservice/csvkit
/
test_csvclean.py
55 lines (40 loc) · 1.53 KB
/
test_csvclean.py
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
#!/usr/bin/env python
import os
import six
try:
import unittest2 as unittest
except ImportError:
import unittest
from csvkit.utilities.csvclean import CSVClean
class TestCSVClean(unittest.TestCase):
def test_simple(self):
args = ['examples/bad.csv']
output_file = six.StringIO()
utility = CSVClean(args, output_file)
utility.main()
self.assertTrue(os.path.exists('examples/bad_err.csv'))
self.assertTrue(os.path.exists('examples/bad_out.csv'))
try:
with open('examples/bad_err.csv') as f:
next(f)
self.assertEqual(next(f)[0], '1')
self.assertEqual(next(f)[0], '2')
self.assertRaises(StopIteration, next, f)
with open('examples/bad_out.csv') as f:
next(f)
self.assertEqual(next(f)[0], '0')
self.assertRaises(StopIteration, next, f)
finally:
# Cleanup
os.remove('examples/bad_err.csv')
os.remove('examples/bad_out.csv')
def test_dry_run(self):
args = ['-n', 'examples/bad.csv']
output_file = six.StringIO()
utility = CSVClean(args, output_file)
utility.main()
self.assertFalse(os.path.exists('examples/bad_err.csv'))
self.assertFalse(os.path.exists('examples/bad_out.csv'))
output = six.StringIO(output_file.getvalue())
self.assertEqual(next(output)[:6], 'Line 1')
self.assertEqual(next(output)[:6], 'Line 2')