1+ import unittest
2+ from pathlib import Path
3+ from unittest .mock import Mock , call
4+
5+ from cirro .file_utils import upload_directory , get_files_in_directory , get_files_stats
6+
7+
8+ class TestFileUtils (unittest .TestCase ):
9+ def setUp (self ):
10+ self .mock_s3_client = Mock ()
11+ self .mock_s3_client .upload_file = Mock ()
12+ self .test_bucket = 'project-1a1a'
13+ self .test_prefix = 'datasets/1a1a/data'
14+
15+ def test_get_file_stats (self ):
16+ directory = Path (__file__ ).parent / 'data'
17+ files = get_files_in_directory (directory )
18+ files = [Path (directory , f ) for f in files ]
19+ stats = get_files_stats (files = files )
20+ self .assertGreater (stats .size , 0 )
21+ self .assertGreater (stats .number_of_files , 0 )
22+ self .assertIn ('KB' , stats .size_friendly )
23+
24+ def test_upload_directory_pathlike (self ):
25+ test_path = Path ('/Users/test/Documents/dataset1' )
26+ test_files = [
27+ Path ('/Users/test/Documents/dataset1/test_file.fastq' ),
28+ Path ('/Users/test/Documents/dataset1/folder1/test_file.fastq' ),
29+ ]
30+ upload_directory (directory = test_path ,
31+ files = test_files ,
32+ s3_client = self .mock_s3_client ,
33+ bucket = self .test_bucket ,
34+ prefix = self .test_prefix )
35+
36+ # The function should upload files relative to the directory path.
37+ self .mock_s3_client .upload_file .assert_has_calls ([
38+ call (file_path = test_files [0 ], bucket = self .test_bucket , key = f'{ self .test_prefix } /test_file.fastq' ),
39+ call (file_path = test_files [1 ], bucket = self .test_bucket , key = f'{ self .test_prefix } /folder1/test_file.fastq' )
40+ ], any_order = True )
41+
42+ def test_upload_directory_string (self ):
43+ test_path = 'data'
44+ test_files = [
45+ 'file1.txt' ,
46+ 'folder1/file2.txt'
47+ ]
48+ upload_directory (directory = test_path ,
49+ files = test_files ,
50+ s3_client = self .mock_s3_client ,
51+ bucket = self .test_bucket ,
52+ prefix = self .test_prefix )
53+
54+ # The function should upload files relative to the directory path,
55+ # but also format file_path into the Path object.
56+ self .mock_s3_client .upload_file .assert_has_calls ([
57+ call (file_path = Path (test_path , test_files [0 ]),
58+ bucket = self .test_bucket ,
59+ key = f'{ self .test_prefix } /file1.txt' ),
60+ call (file_path = Path (test_path , test_files [1 ]),
61+ bucket = self .test_bucket ,
62+ key = f'{ self .test_prefix } /folder1/file2.txt' )
63+ ], any_order = True )
64+
65+ def test_upload_directory_different_types (self ):
66+ test_path = Path ('s3://bucket/dataset1' )
67+ test_files = [
68+ 's3://bucket/dataset1/file2.txt'
69+ ]
70+ with self .assertRaises (ValueError ):
71+ upload_directory (directory = test_path ,
72+ files = test_files ,
73+ s3_client = self .mock_s3_client ,
74+ bucket = self .test_bucket ,
75+ prefix = self .test_prefix )
0 commit comments