|
2 | 2 | import unittest |
3 | 3 |
|
4 | 4 | import os |
| 5 | +import uuid |
5 | 6 |
|
6 | 7 | import tests |
7 | 8 | from tensor_parser import csv_parser |
8 | 9 |
|
9 | 10 | class TestCSVParser(unittest.TestCase): |
10 | 11 |
|
11 | 12 | def test_delim(self): |
12 | | - path = os.path.join(tests.DATA_DIR, 'test.csv') |
13 | | - p = csv_parser.csv_parser(path) |
14 | | - self.assertEqual(p.get_delimiter(), ',') |
| 13 | + tmp_name = str(uuid.uuid4().hex) + '.csv' |
| 14 | + try: |
| 15 | + # make csv |
| 16 | + with open(tmp_name, 'w') as fout: |
| 17 | + print('1,2,3,1.0', file=fout) |
| 18 | + print('1,2,3,1.0', file=fout) |
| 19 | + print('1,2,3,1.0', file=fout) |
| 20 | + p = csv_parser.csv_parser(tmp_name) |
| 21 | + self.assertEqual(p.get_delimiter(), ',') |
| 22 | + self.assertEqual(p.get_header(), ['1', '2', '3', '4']) |
| 23 | + finally: |
| 24 | + os.remove(tmp_name) |
| 25 | + |
| 26 | + def test_gzip(self): |
| 27 | + tmp_name = str(uuid.uuid4().hex) + '.csv.gz' |
| 28 | + try: |
| 29 | + # make csv |
| 30 | + with csv_parser.open_file(tmp_name, 'w') as fout: |
| 31 | + print('1,2,3,1.0', file=fout) |
| 32 | + print('1,2,3,1.0', file=fout) |
| 33 | + print('1,2,3,1.0', file=fout) |
| 34 | + p = csv_parser.csv_parser(tmp_name) |
| 35 | + self.assertEqual(p.get_delimiter(), ',') |
| 36 | + self.assertEqual(p.num_columns(), 4) |
| 37 | + for row in p.rows(): |
| 38 | + self.assertEqual(row, ['1', '2', '3', '1.0']) |
| 39 | + finally: |
| 40 | + os.remove(tmp_name) |
| 41 | + |
| 42 | + |
| 43 | + def test_bz2(self): |
| 44 | + tmp_name = str(uuid.uuid4().hex) + '.csv.bz2' |
| 45 | + try: |
| 46 | + # make csv |
| 47 | + with csv_parser.open_file(tmp_name, 'w') as fout: |
| 48 | + print('1,2,3,1.0', file=fout) |
| 49 | + print('1,2,3,1.0', file=fout) |
| 50 | + print('1,2,3,1.0', file=fout) |
| 51 | + p = csv_parser.csv_parser(tmp_name) |
| 52 | + self.assertEqual(p.get_delimiter(), ',') |
| 53 | + self.assertEqual(p.num_columns(), 4) |
| 54 | + for row in p.rows(): |
| 55 | + self.assertEqual(row, ['1', '2', '3', '1.0']) |
| 56 | + finally: |
| 57 | + os.remove(tmp_name) |
| 58 | + |
| 59 | + |
| 60 | + def test_default_header(self): |
| 61 | + tmp_name = str(uuid.uuid4().hex) + '.tmp' |
| 62 | + try: |
| 63 | + # make csv |
| 64 | + with open(tmp_name, 'w') as fout: |
| 65 | + print('1 2 3 1.0', file=fout) |
| 66 | + print('1 2 3 1.0', file=fout) |
| 67 | + print('1 2 3 1.0', file=fout) |
| 68 | + p = csv_parser.csv_parser(tmp_name) |
| 69 | + self.assertEqual(p.get_header(), ['1', '2', '3', '4']) |
| 70 | + finally: |
| 71 | + os.remove(tmp_name) |
| 72 | + |
| 73 | + |
| 74 | + def test_header(self): |
| 75 | + tmp_name = str(uuid.uuid4().hex) + '.tmp' |
| 76 | + try: |
| 77 | + # make csv |
| 78 | + with open(tmp_name, 'w') as fout: |
| 79 | + print('1 2 3 2.0', file=fout) # "will be header" |
| 80 | + print('1 2 3 1.0', file=fout) |
| 81 | + print('1 2 3 1.0', file=fout) |
| 82 | + print('1 2 3 1.0', file=fout) |
| 83 | + p = csv_parser.csv_parser(tmp_name, has_header=True) |
| 84 | + self.assertEqual(p.get_delimiter(), ' ') |
| 85 | + self.assertEqual(p.num_columns(), 4) |
| 86 | + for row in p.rows(): |
| 87 | + self.assertEqual(row, ['1', '2', '3', '1.0']) |
| 88 | + finally: |
| 89 | + os.remove(tmp_name) |
| 90 | + |
15 | 91 |
|
16 | 92 | if __name__ == '__main__': |
17 | 93 | unittest.main() |
|
0 commit comments