Coverage for tests/test_audio_file_utils.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-19 00:41 +0100

1# -*- encoding: utf-8 -*- 

2"""Unit tests for testing the AudioFileUtils class.""" 

3 

4import unittest 

5from unittest.mock import Mock 

6 

7import numpy as np 

8import sounddevice 

9 

10from nendo.utils import AudioFileUtils, play_signal 

11 

12 

13class TestingAudioFileUtils(unittest.TestCase): 

14 """Unit test class for testing the AudioFileUtils class.""" 

15 

16 def test_supported_files(self): 

17 """Test the supported fileypes.""" 

18 audio_utils = AudioFileUtils() 

19 

20 self.assertTrue(audio_utils.is_supported_filetype("test.WAV")) 

21 self.assertTrue(audio_utils.is_supported_filetype("test.mp3")) 

22 self.assertTrue(audio_utils.is_supported_filetype("test.aiff")) 

23 self.assertTrue(audio_utils.is_supported_filetype("test.flac")) 

24 self.assertTrue(audio_utils.is_supported_filetype("test.ogg")) 

25 self.assertFalse(audio_utils.is_supported_filetype("test.m4a")) 

26 

27 def test_unsupported_files(self): 

28 """Test an unsupported filetype.""" 

29 audio_utils = AudioFileUtils() 

30 self.assertFalse(audio_utils.is_supported_filetype("test.wma")) 

31 

32 def test_play_signal(self): 

33 """Test playing the signal.""" 

34 sounddevice.play = Mock() 

35 sounddevice.wait = Mock() 

36 

37 signal = np.array([0, 1, 2, 3, 4]) 

38 sr = 4 

39 play_signal(signal, sr) 

40 

41 sounddevice.play.assert_called_once() 

42 sounddevice.wait.assert_called_once() 

43 

44 # manually asserting call args because mock.assert_called_once_with() 

45 # doesn't work with numpy arrays 

46 np.testing.assert_array_equal(sounddevice.play.call_args[0][0], signal.T) 

47 self.assertEqual(sounddevice.play.call_args[1]["samplerate"], sr) 

48 self.assertEqual(sounddevice.play.call_args[1]["loop"], False)