1+ import gzip
12import os
23import unittest
34
@@ -12,7 +13,8 @@ def test_basic_example_a(self):
1213 """Test loading of a simple CSV file
1314 """
1415 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 = ',' )
1618 num_lines = 0
1719 first_row = {}
1820 for row in test_file :
@@ -22,34 +24,40 @@ def test_basic_example_a(self):
2224 self .assertEqual (2 , num_lines )
2325 self .assertEqual ('A string' , first_row ['one' ])
2426 self .assertEqual ('and finally' , first_row ['four' ])
27+ csv_file .close ()
2528
2629 def test_basic_example_a_with_supplied_header (self ):
2730 """Test loading of a simple CSV file with a provided header
2831 """
2932 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 ,
3135 column_sep = ',' ,
3236 header = 'one,two:int,three:float,four:string' )
3337 num_lines = 0
3438 for _ in test_file :
3539 num_lines += 1
3640 self .assertEqual (2 , num_lines )
41+ csv_file .close ()
3742
3843 def test_basic_example_a_gzip (self ):
3944 """Test loading of a simple CSV file (gzipped)
4045 """
4146 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 = ',' )
4349 num_lines = 0
4450 for _ in test_file :
4551 num_lines += 1
4652 self .assertEqual (2 , num_lines )
53+ csv_file .close ()
4754
4855 def test_basic_example_b_unknown_type (self ):
4956 """Test loading of a simple CSV file with a column type that is unknown
5057 """
5158 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 = ',' )
5361 num_lines = 0
5462 got_exception = False
5563 try :
@@ -61,12 +69,14 @@ def test_basic_example_b_unknown_type(self):
6169 got_exception = True
6270 self .assertTrue (got_exception )
6371 self .assertEqual (0 , num_lines )
72+ csv_file .close ()
6473
6574 def test_basic_example_c_too_many_colons (self ):
6675 """Test loading of a simple CSV file with a column that has too many colons
6776 """
6877 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 = ',' )
7080 num_lines = 0
7181 got_exception = False
7282 try :
@@ -79,12 +89,14 @@ def test_basic_example_c_too_many_colons(self):
7989 got_exception = True
8090 self .assertTrue (got_exception )
8191 self .assertEqual (0 , num_lines )
92+ csv_file .close ()
8293
8394 def test_basic_example_d_wrong_type (self ):
8495 """Test loading of a simple CSV file with a column that has a string as an int
8596 """
8697 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 = ',' )
88100 num_lines = 0
89101 got_exception = False
90102 try :
@@ -98,22 +110,26 @@ def test_basic_example_d_wrong_type(self):
98110 got_exception = True
99111 self .assertTrue (got_exception )
100112 self .assertEqual (0 , num_lines )
113+ csv_file .close ()
101114
102115 def test_basic_example_d_tabs (self ):
103116 """Test loading of a simple CSV file with tab (default) separators
104117 """
105118 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 )
107121 num_lines = 0
108122 for _ in test_file :
109123 num_lines += 1
110124 self .assertEqual (2 , num_lines )
125+ csv_file .close ()
111126
112127 def test_basic_example_d_too_many_values (self ):
113128 """Test loading of a simple CSV file with too many values
114129 """
115130 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 = ',' )
117133 num_lines = 0
118134 got_exception = False
119135 try :
@@ -126,3 +142,4 @@ def test_basic_example_d_too_many_values(self):
126142 got_exception = True
127143 self .assertTrue (got_exception )
128144 self .assertEqual (0 , num_lines )
145+ csv_file .close ()
0 commit comments