33# --- BEGIN_HEADER ---
44#
55# test_mig_shared_fileio - unit test of the corresponding mig shared module
6- # Copyright (C) 2003-2024 The MiG Project by the Science HPC Center at UCPH
6+ # Copyright (C) 2003-2025 The MiG Project by the Science HPC Center at UCPH
77#
88# This file is part of MiG.
99#
5151
5252
5353class MigSharedFileio__write_chunk (MigTestCase ):
54- # TODO: Add docstrings to this class and its methods
54+ """Test the write_chunk function from mig.shared.fileio module"""
55+
5556 def setUp (self ):
57+ """Initialize test environment for write_chunk tests"""
5658 super (MigSharedFileio__write_chunk , self ).setUp ()
5759 self .tmp_path = temppath (DUMMY_FILE_WRITECHUNK , self )
5860 cleanpath (os .path .dirname (DUMMY_FILE_WRITECHUNK ), self )
5961
6062 def test_return_false_on_invalid_data (self ):
63+ """Test write_chunk returns False with invalid data input"""
6164 self .logger .forgive_errors ()
6265
6366 # NOTE: we make sure to disable any forced stringification here
@@ -66,13 +69,15 @@ def test_return_false_on_invalid_data(self):
6669 self .assertFalse (did_succeed )
6770
6871 def test_return_false_on_invalid_offset (self ):
72+ """Test write_chunk returns False with negative offset value"""
6973 self .logger .forgive_errors ()
7074
7175 did_succeed = fileio .write_chunk (self .tmp_path , DUMMY_BYTES , - 42 ,
7276 self .logger )
7377 self .assertFalse (did_succeed )
7478
7579 def test_return_false_on_invalid_dir (self ):
80+ """Test write_chunk returns False when path is a directory"""
7681 self .logger .forgive_errors ()
7782
7883 os .makedirs (self .tmp_path )
@@ -81,12 +86,14 @@ def test_return_false_on_invalid_dir(self):
8186 self .assertFalse (did_succeed )
8287
8388 def test_creates_directory (self ):
89+ """Test write_chunk creates parent directory when needed"""
8490 fileio .write_chunk (self .tmp_path , DUMMY_BYTES , 0 , self .logger )
8591
8692 path_kind = self .assertPathExists (DUMMY_FILE_WRITECHUNK )
8793 self .assertEqual (path_kind , "file" )
8894
8995 def test_store_bytes (self ):
96+ """Test write_chunk stores byte data correctly at offset 0"""
9097 fileio .write_chunk (self .tmp_path , DUMMY_BYTES , 0 , self .logger )
9198
9299 with open (self .tmp_path , 'rb' ) as file :
@@ -95,6 +102,7 @@ def test_store_bytes(self):
95102 self .assertEqual (content [:], DUMMY_BYTES )
96103
97104 def test_store_bytes_at_offset (self ):
105+ """Test write_chunk stores byte data at specified offset"""
98106 offset = 3
99107
100108 fileio .write_chunk (self .tmp_path , DUMMY_BYTES , offset , self .logger )
@@ -108,6 +116,7 @@ def test_store_bytes_at_offset(self):
108116
109117 @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
110118 def test_store_bytes_in_text_mode (self ):
119+ """Test write_chunk stores byte data in text mode"""
111120 fileio .write_chunk (self .tmp_path , DUMMY_BYTES , 0 , self .logger ,
112121 mode = "r+" )
113122
@@ -118,6 +127,7 @@ def test_store_bytes_in_text_mode(self):
118127
119128 @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
120129 def test_store_unicode (self ):
130+ """Test write_chunk stores unicode data in text mode"""
121131 fileio .write_chunk (self .tmp_path , DUMMY_UNICODE , 0 , self .logger ,
122132 mode = 'r+' )
123133
@@ -128,6 +138,7 @@ def test_store_unicode(self):
128138
129139 @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
130140 def test_store_unicode_in_binary_mode (self ):
141+ """Test write_chunk stores unicode data in binary mode"""
131142 fileio .write_chunk (self .tmp_path , DUMMY_UNICODE , 0 , self .logger ,
132143 mode = 'r+b' )
133144
@@ -138,12 +149,16 @@ def test_store_unicode_in_binary_mode(self):
138149
139150
140151class MigSharedFileio__write_file (MigTestCase ):
152+ """Test the write_file function from mig.shared.fileio module"""
153+
141154 def setUp (self ):
155+ """Initialize test environment for write_file tests"""
142156 super (MigSharedFileio__write_file , self ).setUp ()
143157 self .tmp_path = temppath (DUMMY_FILE_WRITEFILE , self )
144158 cleanpath (os .path .dirname (DUMMY_FILE_WRITEFILE ), self )
145159
146160 def test_return_false_on_invalid_data (self ):
161+ """Test write_file returns False with non-string data input"""
147162 self .logger .forgive_errors ()
148163
149164 # NOTE: we make sure to disable any forced stringification here
@@ -152,32 +167,38 @@ def test_return_false_on_invalid_data(self):
152167 self .assertFalse (did_succeed )
153168
154169 def test_return_false_on_invalid_dir (self ):
170+ """Test write_file returns False when path is a directory"""
155171 self .logger .forgive_errors ()
156172
157173 os .makedirs (self .tmp_path )
158174
159- did_succeed = fileio .write_file (DUMMY_BYTES , self .tmp_path , self .logger )
175+ did_succeed = fileio .write_file (DUMMY_BYTES , self .tmp_path ,
176+ self .logger )
160177 self .assertFalse (did_succeed )
161178
162179 def test_return_false_on_missing_dir (self ):
180+ """Test write_file returns False on missing parent dir"""
163181 self .logger .forgive_errors ()
164182
165- did_succeed = fileio .write_file (DUMMY_BYTES , self .tmp_path , self . logger ,
166- make_parent = False )
183+ did_succeed = fileio .write_file (DUMMY_BYTES , self .tmp_path ,
184+ self . logger , make_parent = False )
167185 self .assertFalse (did_succeed )
168186
169187 def test_creates_directory (self ):
188+ """Test write_file creates parent directory when needed"""
170189 # TODO: temporarily use empty string to avoid any byte/unicode issues
171- # did_succeed = fileio.write_file(DUMMY_BYTES, self.tmp_path, self.logger)
190+ # did_succeed = fileio.write_file(DUMMY_BYTES, self.tmp_path,
191+ # self.logger)
172192 did_succeed = fileio .write_file ('' , self .tmp_path , self .logger )
173193 self .assertTrue (did_succeed )
174194
175195 path_kind = self .assertPathExists (DUMMY_FILE_WRITEFILE )
176196 self .assertEqual (path_kind , "file" )
177197
178- def test_store_bytes (self ):
198+ # TODO: replace next test once we have auto adjust mode in write helper
199+ def test_store_bytes_with_manual_adjust_mode (self ):
200+ """Test write_file stores byte data in with manual adjust mode call"""
179201 mode = 'w'
180- # TODO: remove next once we have auto adjust mode in write helper
181202 mode = fileio ._auto_adjust_mode (DUMMY_BYTES , mode )
182203 did_succeed = fileio .write_file (DUMMY_BYTES , self .tmp_path , self .logger ,
183204 mode = mode )
@@ -190,6 +211,7 @@ def test_store_bytes(self):
190211
191212 @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
192213 def test_store_bytes_in_text_mode (self ):
214+ """Test write_file stores byte data when opening in text mode"""
193215 did_succeed = fileio .write_file (DUMMY_BYTES , self .tmp_path , self .logger ,
194216 mode = "w" )
195217 self .assertTrue (did_succeed )
@@ -201,6 +223,7 @@ def test_store_bytes_in_text_mode(self):
201223
202224 @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
203225 def test_store_unicode (self ):
226+ """Test write_file stores unicode string when opening in text mode"""
204227 did_succeed = fileio .write_file (DUMMY_UNICODE , self .tmp_path ,
205228 self .logger , mode = 'w' )
206229 self .assertTrue (did_succeed )
@@ -212,6 +235,7 @@ def test_store_unicode(self):
212235
213236 @unittest .skip ("TODO: enable again - requires the temporarily disabled auto mode select" )
214237 def test_store_unicode_in_binary_mode (self ):
238+ """Test write_file handles unicode strings when opening in binary mode"""
215239 did_succeed = fileio .write_file (DUMMY_UNICODE , self .tmp_path ,
216240 self .logger , mode = 'wb' )
217241 self .assertTrue (did_succeed )
0 commit comments