44from pathlib import Path
55from unittest .mock import patch , MagicMock
66from src .gtfs_flex_validation import GTFSFlexValidation
7+ from src .config import Settings
78
89DOWNLOAD_FILE_PATH = f'{ Path .cwd ()} /downloads'
910SAVED_FILE_PATH = f'{ Path .cwd ()} /tests/unit_tests/test_files'
1617SCHEMA_VERSION = 'v2.0'
1718
1819
19- class TestSuccessWithWithMacOSFile (unittest .TestCase ):
20+ class TestSuccessWithMacOSFile (unittest .TestCase ):
2021 @patch .object (GTFSFlexValidation , 'download_single_file' )
2122 def setUp (self , mock_download_single_file ):
2223 os .makedirs (DOWNLOAD_FILE_PATH , exist_ok = True )
@@ -33,6 +34,7 @@ def setUp(self, mock_download_single_file):
3334 self .validator .file_path = file_path
3435 self .validator .file_relative_path = MAC_SUCCESS_FILE_NAME
3536 self .validator .container_name = None
37+ self .validator .settings = Settings ()
3638 mock_download_single_file .return_value = file_path
3739
3840 def tearDown (self ):
@@ -47,11 +49,11 @@ def test_validate_with_valid_file(self):
4749
4850 # Act
4951 is_valid , _ = self .validator .validate ()
50- (is_valid )
5152
5253 # Assert
5354 self .assertTrue (is_valid )
5455
56+
5557class TestSuccessGTFSFlexValidation (unittest .TestCase ):
5658
5759 @patch .object (GTFSFlexValidation , 'download_single_file' )
@@ -64,13 +66,16 @@ def setUp(self, mock_download_single_file):
6466 shutil .copyfile (source , destination )
6567
6668 file_path = f'{ DOWNLOAD_FILE_PATH } /{ SUCCESS_FILE_NAME } '
69+ dl_folder_path = os .path .join (DOWNLOAD_FILE_PATH , 'dummy-uuid' ) # Mock the UUID generation
70+ os .makedirs (dl_folder_path , exist_ok = True ) # Ensure this directory is created in the test
6771
6872 with patch .object (GTFSFlexValidation , '__init__' , return_value = None ):
6973 self .validator = GTFSFlexValidation (file_path = file_path , storage_client = MagicMock ())
7074 self .validator .file_path = file_path
7175 self .validator .file_relative_path = SUCCESS_FILE_NAME
7276 self .validator .container_name = None
73- mock_download_single_file .return_value = file_path
77+ self .validator .settings = Settings ()
78+ mock_download_single_file .return_value = os .path .join (dl_folder_path , SUCCESS_FILE_NAME )
7479
7580 def tearDown (self ):
7681 pass
@@ -123,6 +128,33 @@ def test_download_single_file(self):
123128 content = f .read ()
124129 self .assertEqual (content , b'file_content' )
125130
131+ def test_download_multiple_file_with_same_name (self ):
132+ # Arrange
133+ file_upload_path = DOWNLOAD_FILE_PATH
134+ self .validator .storage_client = MagicMock ()
135+ self .validator .storage_client .get_file_from_url = MagicMock ()
136+ file = MagicMock ()
137+ file .file_path = 'text_file.txt'
138+ file .get_stream = MagicMock (return_value = b'file_content' )
139+ self .validator .storage_client .get_file_from_url .return_value = file
140+
141+ # Act
142+ first_downloaded_file_path = self .validator .download_single_file (file_upload_path = file_upload_path )
143+ second_downloaded_file_path = self .validator .download_single_file (file_upload_path = file_upload_path )
144+
145+ # Assert
146+ self .assertNotEqual (first_downloaded_file_path , second_downloaded_file_path ,
147+ "The downloaded file paths should be different for files with the same name." )
148+
149+ # Check if the get_file_from_url was called for both download attempts
150+ self .assertEqual (self .validator .storage_client .get_file_from_url .call_count , 2 ,
151+ "get_file_from_url should be called twice for two downloads." )
152+ file .get_stream .assert_called ()
153+
154+ # Additional assertions to verify that the paths indeed point to different locations
155+ self .assertTrue (first_downloaded_file_path .startswith (DOWNLOAD_FILE_PATH ))
156+ self .assertTrue (second_downloaded_file_path .startswith (DOWNLOAD_FILE_PATH ))
157+
126158 def test_clean_up_file (self ):
127159 # Arrange
128160 file_upload_path = DOWNLOAD_FILE_PATH
@@ -149,7 +181,7 @@ def test_clean_up_folder(self):
149181 GTFSFlexValidation .clean_up = MagicMock ()
150182
151183 # Assert
152- # self.assertFalse(os.path.exists(directory_name))
184+ self .assertFalse (os .path .exists (directory_name ))
153185
154186
155187class TestFailureGTFSFlexValidation (unittest .TestCase ):
@@ -170,6 +202,7 @@ def setUp(self, mock_download_single_file):
170202 self .validator .file_path = file_path
171203 self .validator .file_relative_path = FAILURE_FILE_NAME
172204 self .validator .container_name = None
205+ self .validator .settings = MagicMock ()
173206 mock_download_single_file .return_value = file_path
174207
175208 def tearDown (self ):
@@ -223,19 +256,20 @@ def test_download_single_file_exception(self):
223256 self .validator .storage_client .get_file_from_url = MagicMock ()
224257 file = MagicMock ()
225258 file .file_path = 'text_file.txt'
226- file .get_stream = MagicMock (return_value = b'file_content' )
259+ file .get_stream = MagicMock (side_effect = FileNotFoundError ( "Mocked FileNotFoundError" ) )
227260 self .validator .storage_client .get_file_from_url .return_value = file
228261
229- # Act
230- downloaded_file_path = self .validator .download_single_file (file_upload_path = file_upload_path )
262+ # Create the mock folder that would be used
263+ unique_id = "mocked-uuid"
264+ self .validator .settings .get_unique_id = MagicMock ()
265+ self .validator .settings .get_unique_id .return_value = unique_id
231266
232- # Assert
233- self .validator .storage_client .get_file_from_url .assert_called_once_with (self .validator .container_name ,
234- file_upload_path )
235- file .get_stream .assert_called_once ()
236- with open (downloaded_file_path , 'rb' ) as f :
237- content = f .read ()
238- self .assertEqual (content , b'file_content' )
267+ dl_folder_path = os .path .join (DOWNLOAD_FILE_PATH , unique_id )
268+ os .makedirs (dl_folder_path , exist_ok = True )
269+
270+ # Act & Assert
271+ with self .assertRaises (FileNotFoundError ):
272+ self .validator .download_single_file (file_upload_path = file_upload_path )
239273
240274
241275if __name__ == '__main__' :
0 commit comments