2020sys .modules ["lib.logging_utils" ] = MagicMock (init_logger = MagicMock (return_value = mock_logger ))
2121sys .modules ["server.lib.vcon_redis" ] = MagicMock ()
2222
23- from server .storage .s3 import _create_s3_client , save , get , default_options
23+ from server .storage .s3 import _create_s3_client , _build_s3_key , save , get , default_options
2424
2525
2626class TestCreateS3Client :
@@ -183,14 +183,47 @@ def test_create_client_with_various_aws_regions(self):
183183 )
184184
185185
186+ class TestBuildS3Key :
187+ """Tests for the _build_s3_key helper function."""
188+
189+ def test_build_key_without_prefix (self ):
190+ """Test key building without s3_path prefix."""
191+ key = _build_s3_key ("test-uuid" )
192+ assert key == "test-uuid.vcon"
193+
194+ def test_build_key_with_prefix (self ):
195+ """Test key building with s3_path prefix."""
196+ key = _build_s3_key ("test-uuid" , "vcons" )
197+ assert key == "vcons/test-uuid.vcon"
198+
199+ def test_build_key_with_trailing_slash_prefix (self ):
200+ """Test key building with trailing slash in prefix."""
201+ key = _build_s3_key ("test-uuid" , "vcons/" )
202+ assert key == "vcons/test-uuid.vcon"
203+
204+ def test_build_key_with_none_prefix (self ):
205+ """Test key building with None prefix."""
206+ key = _build_s3_key ("test-uuid" , None )
207+ assert key == "test-uuid.vcon"
208+
209+ def test_build_key_with_empty_prefix (self ):
210+ """Test key building with empty string prefix."""
211+ key = _build_s3_key ("test-uuid" , "" )
212+ assert key == "test-uuid.vcon"
213+
214+ def test_build_key_with_nested_prefix (self ):
215+ """Test key building with nested prefix."""
216+ key = _build_s3_key ("test-uuid" , "data/vcons/archive" )
217+ assert key == "data/vcons/archive/test-uuid.vcon"
218+
219+
186220class TestSave :
187221 """Tests for the save function."""
188222
189223 @pytest .fixture
190224 def mock_vcon (self ):
191225 """Create a mock vCon object."""
192226 mock = MagicMock ()
193- mock .created_at = "2024-01-15T10:30:00"
194227 mock .dumps .return_value = '{"uuid": "test-uuid", "vcon": "1.0.0"}'
195228 return mock
196229
@@ -222,7 +255,7 @@ def test_save_basic(self, mock_vcon, base_opts):
222255
223256 call_args = mock_s3 .put_object .call_args
224257 assert call_args .kwargs ["Bucket" ] == "test-bucket"
225- assert call_args .kwargs ["Key" ] == "2024/01/15/ test-uuid.vcon"
258+ assert call_args .kwargs ["Key" ] == "test-uuid.vcon"
226259
227260 def test_save_with_s3_path_prefix (self , mock_vcon , base_opts ):
228261 """Test save operation with s3_path prefix."""
@@ -241,7 +274,7 @@ def test_save_with_s3_path_prefix(self, mock_vcon, base_opts):
241274 save ("test-uuid" , base_opts )
242275
243276 call_args = mock_s3 .put_object .call_args
244- assert call_args .kwargs ["Key" ] == "vcons/2024/01/15/ test-uuid.vcon"
277+ assert call_args .kwargs ["Key" ] == "vcons/test-uuid.vcon"
245278
246279 def test_save_with_region (self , mock_vcon , base_opts ):
247280 """Test save operation with region specified."""
@@ -282,34 +315,6 @@ def test_save_raises_exception_on_error(self, mock_vcon, base_opts):
282315 with pytest .raises (Exception , match = "S3 Error" ):
283316 save ("test-uuid" , base_opts )
284317
285- def test_save_with_different_dates (self , base_opts ):
286- """Test save with various created_at dates."""
287- test_cases = [
288- ("2024-01-01T00:00:00" , "2024/01/01" ),
289- ("2024-12-31T23:59:59" , "2024/12/31" ),
290- ("2023-06-15T12:00:00" , "2023/06/15" ),
291- ]
292-
293- for created_at , expected_path in test_cases :
294- mock_vcon = MagicMock ()
295- mock_vcon .created_at = created_at
296- mock_vcon .dumps .return_value = '{"uuid": "test-uuid"}'
297-
298- with patch ("server.storage.s3.VconRedis" ) as mock_redis_class , \
299- patch ("server.storage.s3.boto3.client" ) as mock_boto_client :
300-
301- mock_redis = MagicMock ()
302- mock_redis .get_vcon .return_value = mock_vcon
303- mock_redis_class .return_value = mock_redis
304-
305- mock_s3 = MagicMock ()
306- mock_boto_client .return_value = mock_s3
307-
308- save ("test-uuid" , base_opts )
309-
310- call_args = mock_s3 .put_object .call_args
311- assert call_args .kwargs ["Key" ] == f"{ expected_path } /test-uuid.vcon"
312-
313318
314319class TestGet :
315320 """Tests for the get function."""
0 commit comments