1
+ import gzip
1
2
import os
2
3
import unittest
3
4
@@ -12,7 +13,8 @@ def test_basic_example_a(self):
12
13
"""Test loading of a simple CSV file
13
14
"""
14
15
test_file = os .path .join (DATA_DIR , 'TypedCsvReader.example.a.csv' )
15
- test_file = TypedColumnReader .TypedColumnReader (test_file , column_sep = ',' )
16
+ csv_file = open (test_file )
17
+ test_file = TypedColumnReader .TypedColumnReader (csv_file , column_sep = ',' )
16
18
num_lines = 0
17
19
first_row = {}
18
20
for row in test_file :
@@ -22,34 +24,40 @@ def test_basic_example_a(self):
22
24
self .assertEqual (2 , num_lines )
23
25
self .assertEqual ('A string' , first_row ['one' ])
24
26
self .assertEqual ('and finally' , first_row ['four' ])
27
+ csv_file .close ()
25
28
26
29
def test_basic_example_a_with_supplied_header (self ):
27
30
"""Test loading of a simple CSV file with a provided header
28
31
"""
29
32
test_file = os .path .join (DATA_DIR , 'TypedCsvReader.example.a-no-header.csv' )
30
- test_file = TypedColumnReader .TypedColumnReader (test_file ,
33
+ csv_file = open (test_file )
34
+ test_file = TypedColumnReader .TypedColumnReader (csv_file ,
31
35
column_sep = ',' ,
32
36
header = 'one,two:int,three:float,four:string' )
33
37
num_lines = 0
34
38
for _ in test_file :
35
39
num_lines += 1
36
40
self .assertEqual (2 , num_lines )
41
+ csv_file .close ()
37
42
38
43
def test_basic_example_a_gzip (self ):
39
44
"""Test loading of a simple CSV file (gzipped)
40
45
"""
41
46
test_file = os .path .join (DATA_DIR , 'TypedCsvReader.example.a.csv.gz' )
42
- test_file = TypedColumnReader .TypedColumnReader (test_file , column_sep = ',' )
47
+ csv_file = gzip .open (test_file , 'rt' )
48
+ test_file = TypedColumnReader .TypedColumnReader (csv_file , column_sep = ',' )
43
49
num_lines = 0
44
50
for _ in test_file :
45
51
num_lines += 1
46
52
self .assertEqual (2 , num_lines )
53
+ csv_file .close ()
47
54
48
55
def test_basic_example_b_unknown_type (self ):
49
56
"""Test loading of a simple CSV file with a column type that is unknown
50
57
"""
51
58
test_file = os .path .join (DATA_DIR , 'TypedCsvReader.example.b.csv' )
52
- test_file = TypedColumnReader .TypedColumnReader (test_file , column_sep = ',' )
59
+ csv_file = open (test_file )
60
+ test_file = TypedColumnReader .TypedColumnReader (csv_file , column_sep = ',' )
53
61
num_lines = 0
54
62
got_exception = False
55
63
try :
@@ -61,12 +69,14 @@ def test_basic_example_b_unknown_type(self):
61
69
got_exception = True
62
70
self .assertTrue (got_exception )
63
71
self .assertEqual (0 , num_lines )
72
+ csv_file .close ()
64
73
65
74
def test_basic_example_c_too_many_colons (self ):
66
75
"""Test loading of a simple CSV file with a column that has too many colons
67
76
"""
68
77
test_file = os .path .join (DATA_DIR , 'TypedCsvReader.example.c.csv' )
69
- test_file = TypedColumnReader .TypedColumnReader (test_file , column_sep = ',' )
78
+ csv_file = open (test_file )
79
+ test_file = TypedColumnReader .TypedColumnReader (csv_file , column_sep = ',' )
70
80
num_lines = 0
71
81
got_exception = False
72
82
try :
@@ -79,12 +89,14 @@ def test_basic_example_c_too_many_colons(self):
79
89
got_exception = True
80
90
self .assertTrue (got_exception )
81
91
self .assertEqual (0 , num_lines )
92
+ csv_file .close ()
82
93
83
94
def test_basic_example_d_wrong_type (self ):
84
95
"""Test loading of a simple CSV file with a column that has a string as an int
85
96
"""
86
97
test_file = os .path .join (DATA_DIR , 'TypedCsvReader.example.d.csv' )
87
- test_file = TypedColumnReader .TypedColumnReader (test_file , column_sep = ',' )
98
+ csv_file = open (test_file )
99
+ test_file = TypedColumnReader .TypedColumnReader (csv_file , column_sep = ',' )
88
100
num_lines = 0
89
101
got_exception = False
90
102
try :
@@ -98,22 +110,26 @@ def test_basic_example_d_wrong_type(self):
98
110
got_exception = True
99
111
self .assertTrue (got_exception )
100
112
self .assertEqual (0 , num_lines )
113
+ csv_file .close ()
101
114
102
115
def test_basic_example_d_tabs (self ):
103
116
"""Test loading of a simple CSV file with tab (default) separators
104
117
"""
105
118
test_file = os .path .join (DATA_DIR , 'TypedCsvReader.example.e.csv' )
106
- test_file = TypedColumnReader .TypedColumnReader (test_file )
119
+ csv_file = open (test_file )
120
+ test_file = TypedColumnReader .TypedColumnReader (csv_file )
107
121
num_lines = 0
108
122
for _ in test_file :
109
123
num_lines += 1
110
124
self .assertEqual (2 , num_lines )
125
+ csv_file .close ()
111
126
112
127
def test_basic_example_d_too_many_values (self ):
113
128
"""Test loading of a simple CSV file with too many values
114
129
"""
115
130
test_file = os .path .join (DATA_DIR , 'TypedCsvReader.example.f.csv' )
116
- test_file = TypedColumnReader .TypedColumnReader (test_file , column_sep = ',' )
131
+ csv_file = open (test_file )
132
+ test_file = TypedColumnReader .TypedColumnReader (csv_file , column_sep = ',' )
117
133
num_lines = 0
118
134
got_exception = False
119
135
try :
@@ -126,3 +142,4 @@ def test_basic_example_d_too_many_values(self):
126
142
got_exception = True
127
143
self .assertTrue (got_exception )
128
144
self .assertEqual (0 , num_lines )
145
+ csv_file .close ()
0 commit comments