-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfileio.py
More file actions
62 lines (50 loc) · 1.89 KB
/
fileio.py
File metadata and controls
62 lines (50 loc) · 1.89 KB
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
import os
import unittest
from devoutils.fileio import FileReader, FileWriter
class TestFileIO(unittest.TestCase):
__DATA = ["id,val", "1,uno", "2,dooss", "3,treeeee"]
__DATA_CSV = [['id', 'val'],
['1', 'uno'],
['2', 'dooss'],
['3', 'treeeee']]
__CLEAR_FILE = "".join((os.path.dirname(os.path.abspath(__file__)),
os.sep, 'test_lookup.csv'))
__GZIP_FILE = "".join((os.path.dirname(os.path.abspath(__file__)),
os.sep, 'test_lookup.csv.gz'))
def setUp(self):
os.chdir("{!s}{!s}".format(os.path.dirname(os.path.abspath(__file__)),
os.sep))
def test_simple_file(self):
reader = FileReader(self.__CLEAR_FILE)
data = []
for d in reader:
data.append(d.strip())
self.assertEqual(data, self.__DATA)
def test_csv_file(self):
reader = FileReader(self.__CLEAR_FILE, is_csv=True)
data = []
for d in reader:
data.append(d)
self.assertEqual(data, self.__DATA_CSV)
def test_gzip_file(self):
reader = FileReader(self.__GZIP_FILE, is_gzip=True)
data = []
for d in reader:
data.append(d.strip())
self.assertEqual(data, self.__DATA)
def test_gz_csvFile(self):
reader = FileReader(self.__GZIP_FILE, is_csv=True, is_gzip=True)
data = []
for d in reader:
data.append(d)
self.assertEqual(data, self.__DATA_CSV)
def test_write_gz_csv_file(self):
writer = FileWriter("file.csv.gz", is_csv=True, is_target_gzip=True)
__DATA_CSV = [['id', 'val'],
['1', 'uno'],
['2', 'dooss'],
['3', 'treeeee']]
for d in __DATA_CSV:
writer.write(d)
if __name__ == '__main__':
unittest.main()