2121from mrcfile .gzipmrcfile import GzipMrcFile
2222from . import helpers
2323
24+ # Try to import pathlib if we can
25+ pathlib_unavailable = False
26+ try :
27+ from pathlib import Path
28+ except ImportError :
29+ try :
30+ from pathlib2 import Path
31+ except ImportError :
32+ pathlib_unavailable = True
33+
2434
2535class LoadFunctionTest (helpers .AssertRaisesRegexMixin , unittest .TestCase ):
2636
@@ -35,6 +45,7 @@ def setUp(self):
3545 self .test_data = helpers .get_test_data_path ()
3646 self .test_output = tempfile .mkdtemp ()
3747 self .temp_mrc_name = os .path .join (self .test_output , 'test_mrcfile.mrc' )
48+ self .temp_gz_mrc_name = self .temp_mrc_name + '.gz'
3849 self .example_mrc_name = os .path .join (self .test_data , 'EMD-3197.map' )
3950 self .gzip_mrc_name = os .path .join (self .test_data , 'emd_3197.map.gz' )
4051 self .bzip2_mrc_name = os .path .join (self .test_data , 'EMD-3197.map.bz2' )
@@ -50,7 +61,16 @@ def test_normal_opening(self):
5061 assert repr (mrc ) == ("MrcFile('{0}', mode='r')"
5162 .format (self .example_mrc_name ))
5263
53- def test_read_function (self ):
64+ @unittest .skipIf (pathlib_unavailable , "pathlib not available" )
65+ def test_normal_opening_pathlib (self ):
66+ """Single test to ensure pathlib functionality is tested even if there's
67+ a problem with the LoadFunctionTestWithPathlib class"""
68+ path = Path (self .example_mrc_name )
69+ with mrcfile .open (path ) as mrc :
70+ assert repr (mrc ) == ("MrcFile('{0}', mode='r')"
71+ .format (self .example_mrc_name ))
72+
73+ def test_read (self ):
5474 volume = mrcfile .read (self .example_mrc_name )
5575 assert isinstance (volume , np .ndarray )
5676 assert volume .shape , volume .dtype == ((20 , 20 , 20 ), np .float32 )
@@ -75,7 +95,7 @@ def test_new_empty_file(self):
7595 with mrcfile .new (self .temp_mrc_name ) as mrc :
7696 assert repr (mrc ) == ("MrcFile('{0}', mode='w+')"
7797 .format (self .temp_mrc_name ))
78-
98+
7999 def test_new_empty_file_with_open_function (self ):
80100 with mrcfile .open (self .temp_mrc_name , mode = 'w+' ) as mrc :
81101 assert repr (mrc ) == ("MrcFile('{0}', mode='w+')"
@@ -115,9 +135,9 @@ def test_unknown_compression_type(self):
115135 mrcfile .new (self .temp_mrc_name , compression = 'other' )
116136
117137 def test_overwriting_flag (self ):
118- assert not os .path .exists (self .temp_mrc_name )
119- open (self .temp_mrc_name , 'w+' ).close ()
120- assert os .path .exists (self .temp_mrc_name )
138+ assert not os .path .exists (str ( self .temp_mrc_name ) )
139+ open (str ( self .temp_mrc_name ) , 'w+' ).close ()
140+ assert os .path .exists (str ( self .temp_mrc_name ) )
121141 with self .assertRaisesRegex (ValueError , "already exists" ):
122142 mrcfile .new (self .temp_mrc_name )
123143 with self .assertRaisesRegex (ValueError , "already exists" ):
@@ -217,11 +237,25 @@ def test_write(self):
217237
218238 def test_write_with_auto_compression (self ):
219239 data_in = np .random .random ((10 , 10 )).astype (np .float16 )
220- filename = self .temp_mrc_name + '.gz'
221- mrcfile .write (filename , data_in )
222- with mrcfile .open (filename ) as mrc :
240+ mrcfile .write (self .temp_gz_mrc_name , data_in )
241+ with mrcfile .open (self .temp_gz_mrc_name ) as mrc :
223242 assert isinstance (mrc , GzipMrcFile )
224243
225244
245+ @unittest .skipIf (pathlib_unavailable , "pathlib not available" )
246+ class LoadFunctionTestWithPathlib (LoadFunctionTest ):
247+
248+ """Class to run the load function tests using pathlib paths instead of strings."""
249+
250+ def setUp (self ):
251+ super (LoadFunctionTestWithPathlib , self ).setUp ()
252+ self .temp_mrc_name = Path (self .temp_mrc_name )
253+ self .temp_gz_mrc_name = Path (self .temp_gz_mrc_name )
254+ self .example_mrc_name = Path (self .example_mrc_name )
255+ self .gzip_mrc_name = Path (self .gzip_mrc_name )
256+ self .bzip2_mrc_name = Path (self .bzip2_mrc_name )
257+ self .slow_mrc_name = Path (self .slow_mrc_name )
258+
259+
226260if __name__ == '__main__' :
227261 unittest .main ()
0 commit comments