@@ -327,4 +327,215 @@ def test_speakers_api_integration_disabled(self, temp_dir):
327327 mock_convert .assert_called_once ()
328328
329329 # Should return the expected URL (with timestamp in filename)
330- assert ".mp3" in result
330+ assert ".mp3" in result
331+
332+
333+ class TestAudioCompressionParameter :
334+ """Test cases for the new audio_compression parameter functionality"""
335+
336+ def test_convert_uploaded_file_compression_disabled (self , temp_dir , test_audio_file ):
337+ """Test convert_uploaded_file with compression explicitly disabled"""
338+ filename = "test_audio.wav"
339+ test_audio_file (filename )
340+
341+ with patch ('roundware.lib.convertaudio.compress_audio_file' ) as mock_compress :
342+ with patch ('roundware.lib.convertaudio.normalize_audio_file' ) as mock_normalize :
343+ with patch ('roundware.lib.convertaudio.convert_audio_file' ) as mock_convert :
344+ mock_convert .return_value = None
345+
346+ result = convert_uploaded_file (filename , enable_compression = False )
347+
348+ # Compression should NOT be called
349+ mock_compress .assert_not_called ()
350+
351+ # Normalization should still be called
352+ mock_normalize .assert_called_once ()
353+
354+ # Conversion should still be called
355+ assert mock_convert .call_count == 2 # m4a and mp3
356+
357+ assert result == "test_audio.mp3"
358+
359+ def test_convert_uploaded_file_compression_enabled (self , temp_dir , test_audio_file ):
360+ """Test convert_uploaded_file with compression explicitly enabled"""
361+ filename = "test_audio.wav"
362+ test_audio_file (filename )
363+
364+ with patch ('roundware.lib.convertaudio.compress_audio_file' ) as mock_compress :
365+ with patch ('roundware.lib.convertaudio.normalize_audio_file' ) as mock_normalize :
366+ with patch ('roundware.lib.convertaudio.convert_audio_file' ) as mock_convert :
367+ mock_convert .return_value = None
368+
369+ result = convert_uploaded_file (filename , enable_compression = True )
370+
371+ # Compression should be called
372+ mock_compress .assert_called_once ()
373+
374+ # Normalization should still be called
375+ mock_normalize .assert_called_once ()
376+
377+ # Conversion should still be called
378+ assert mock_convert .call_count == 2 # m4a and mp3
379+
380+ assert result == "test_audio.mp3"
381+
382+ def test_convert_uploaded_file_default_parameter (self , temp_dir , test_audio_file ):
383+ """Test convert_uploaded_file with default parameter (uses settings)"""
384+ filename = "test_audio.wav"
385+ test_audio_file (filename )
386+
387+ with patch ('roundware.lib.convertaudio.compress_audio_file' ) as mock_compress :
388+ with patch ('roundware.lib.convertaudio.normalize_audio_file' ) as mock_normalize :
389+ with patch ('roundware.lib.convertaudio.convert_audio_file' ) as mock_convert :
390+ mock_convert .return_value = None
391+
392+ # Test with settings.AUDIO_COMPRESSION_ENABLED = True
393+ with override_settings (AUDIO_COMPRESSION_ENABLED = True ):
394+ result = convert_uploaded_file (filename ) # No compression parameter
395+
396+ # Compression should be called (uses settings)
397+ mock_compress .assert_called_once ()
398+
399+ # Normalization should still be called
400+ mock_normalize .assert_called_once ()
401+
402+ assert result == "test_audio.mp3"
403+
404+ def test_speakers_api_compression_parameter_true (self , temp_dir ):
405+ """Test speakers API with audio_compression=true parameter"""
406+ from roundware .lib .api import save_speaker_from_request
407+
408+ # Create a mock request with audio_compression=true
409+ mock_request = MagicMock ()
410+ mock_file = MagicMock ()
411+ mock_file .name = "test_audio.wav"
412+ mock_file .file .read .return_value = b"fake audio content"
413+ mock_request .FILES = {'file' : mock_file }
414+ mock_request .data = {'project' : 1 , 'audio_compression' : True }
415+ mock_request .get_host .return_value = 'localhost:8000'
416+
417+ with patch ('roundware.lib.api.convertaudio.convert_uploaded_file' ) as mock_convert :
418+ mock_convert .return_value = "test_audio.mp3"
419+
420+ result = save_speaker_from_request (mock_request )
421+
422+ # Should call convert_uploaded_file with enable_compression=True
423+ # The filename will include a timestamp, so we check the call args
424+ call_args = mock_convert .call_args
425+ assert call_args [0 ][0 ].startswith ("speaker-project1-test_audio-" ) # filename
426+ assert call_args [0 ][1 ] is True # enable_compression
427+
428+ # Should return the expected URL
429+ assert ".mp3" in result
430+
431+ def test_speakers_api_compression_parameter_false (self , temp_dir ):
432+ """Test speakers API with audio_compression=false parameter"""
433+ from roundware .lib .api import save_speaker_from_request
434+
435+ # Create a mock request with audio_compression=false
436+ mock_request = MagicMock ()
437+ mock_file = MagicMock ()
438+ mock_file .name = "test_audio.wav"
439+ mock_file .file .read .return_value = b"fake audio content"
440+ mock_request .FILES = {'file' : mock_file }
441+ mock_request .data = {'project' : 1 , 'audio_compression' : False }
442+ mock_request .get_host .return_value = 'localhost:8000'
443+
444+ with patch ('roundware.lib.api.convertaudio.convert_uploaded_file' ) as mock_convert :
445+ mock_convert .return_value = "test_audio.mp3"
446+
447+ result = save_speaker_from_request (mock_request )
448+
449+ # Should call convert_uploaded_file with enable_compression=False
450+ # The filename will include a timestamp, so we check the call args
451+ call_args = mock_convert .call_args
452+ assert call_args [0 ][0 ].startswith ("speaker-project1-test_audio-" ) # filename
453+ assert call_args [0 ][1 ] is False # enable_compression
454+
455+ # Should return the expected URL
456+ assert ".mp3" in result
457+
458+ def test_speakers_api_compression_parameter_default (self , temp_dir ):
459+ """Test speakers API without audio_compression parameter (defaults to False)"""
460+ from roundware .lib .api import save_speaker_from_request
461+
462+ # Create a mock request without audio_compression parameter
463+ mock_request = MagicMock ()
464+ mock_file = MagicMock ()
465+ mock_file .name = "test_audio.wav"
466+ mock_file .file .read .return_value = b"fake audio content"
467+ mock_request .FILES = {'file' : mock_file }
468+ mock_request .data = {'project' : 1 } # No audio_compression parameter
469+ mock_request .get_host .return_value = 'localhost:8000'
470+
471+ with patch ('roundware.lib.api.convertaudio.convert_uploaded_file' ) as mock_convert :
472+ mock_convert .return_value = "test_audio.mp3"
473+
474+ result = save_speaker_from_request (mock_request )
475+
476+ # Should call convert_uploaded_file with enable_compression=False (default)
477+ # The filename will include a timestamp, so we check the call args
478+ call_args = mock_convert .call_args
479+ assert call_args [0 ][0 ].startswith ("speaker-project1-test_audio-" ) # filename
480+ assert call_args [0 ][1 ] is False # enable_compression (default)
481+
482+ # Should return the expected URL
483+ assert ".mp3" in result
484+
485+ def test_speakers_api_compression_parameter_string_values (self , temp_dir ):
486+ """Test speakers API with string values for audio_compression parameter"""
487+ from roundware .lib .api import save_speaker_from_request
488+
489+ # Test with "true" string
490+ mock_request = MagicMock ()
491+ mock_file = MagicMock ()
492+ mock_file .name = "test_audio.wav"
493+ mock_file .file .read .return_value = b"fake audio content"
494+ mock_request .FILES = {'file' : mock_file }
495+ mock_request .data = {'project' : 1 , 'audio_compression' : 'true' }
496+ mock_request .get_host .return_value = 'localhost:8000'
497+
498+ with patch ('roundware.lib.api.convertaudio.convert_uploaded_file' ) as mock_convert :
499+ mock_convert .return_value = "test_audio.mp3"
500+
501+ result = save_speaker_from_request (mock_request )
502+
503+ # Should call convert_uploaded_file with enable_compression=True
504+ # The filename will include a timestamp, so we check the call args
505+ call_args = mock_convert .call_args
506+ assert call_args [0 ][0 ].startswith ("speaker-project1-test_audio-" ) # filename
507+ assert call_args [0 ][1 ] is True # enable_compression (string "true" converted)
508+
509+ # Should return the expected URL
510+ assert ".mp3" in result
511+
512+ def test_admin_upload_compression_disabled (self , temp_dir ):
513+ """Test that admin uploads have compression disabled by default"""
514+ from roundware .rw .file_utils import handle_speaker_audio_upload
515+
516+ # Create a mock speaker and request
517+ mock_speaker = MagicMock ()
518+ mock_speaker .project .id = 1
519+ mock_speaker .code = 'admin_test'
520+
521+ mock_uploaded_file = MagicMock ()
522+ mock_uploaded_file .name = 'admin_audio.wav'
523+ mock_uploaded_file .chunks .return_value = [b'fake audio content' ]
524+
525+ with patch ('roundware.lib.convertaudio.convert_uploaded_file' ) as mock_convert :
526+ mock_convert .return_value = "admin_audio.mp3"
527+
528+ result = handle_speaker_audio_upload (mock_uploaded_file , mock_speaker )
529+
530+ # Should call convert_uploaded_file with enable_compression=False
531+ # The filename will include a timestamp, so we check the call args
532+ call_args = mock_convert .call_args
533+ if call_args and len (call_args [0 ]) >= 2 :
534+ assert call_args [0 ][0 ].startswith ("speaker-project1-admin_test-" ) # filename
535+ assert call_args [0 ][1 ] is False # enable_compression (admin default)
536+ else :
537+ # If called with keyword arguments, check those
538+ assert mock_convert .call_args [1 ]['enable_compression' ] is False
539+
540+ # Should return the expected URI
541+ assert "admin_audio.mp3" in result
0 commit comments