|
| 1 | +import unittest |
| 2 | +from messagebird import Client, ErrorException |
| 3 | +from datetime import datetime |
| 4 | + |
| 5 | +try: |
| 6 | + from unittest.mock import Mock |
| 7 | +except ImportError: |
| 8 | + # mock was added to unittest in Python 3.3, but was an external library |
| 9 | + # before. |
| 10 | + from mock import Mock |
| 11 | + |
| 12 | + |
| 13 | +class TestVoiceRecording(unittest.TestCase): |
| 14 | + |
| 15 | + def test_voice_recording_view(self): |
| 16 | + http_client = Mock() |
| 17 | + http_client.request.return_value = '{"data":[{"id":"12345678-9012-3456-7890-123456789012","format":"wav","legId":"87654321-0987-6543-2109-876543210987","status":"done","duration":32,"type":"transfer","createdAt":"2018-01-01T00:00:01Z","updatedAt":"2018-01-01T00:00:05Z","deletedAt":null}],"_links":{"file":"/calls/12348765-4321-0987-6543-210987654321/legs/87654321-0987-6543-2109-876543210987/recordings/12345678-9012-3456-7890-123456789012.wav","self":"/calls/12345678-9012-3456-7890-123456789012/legs/12348765-4321-0987-6543-210987654321/recordings/12345678-9012-3456-7890-123456789012"},"pagination":{"totalCount":0,"pageCount":0,"currentPage":0,"perPage":0}}' |
| 18 | + |
| 19 | + voice_recording = Client('', http_client).voice_recording_view('12348765-4321-0987-6543-210987654321', '87654321-0987-6543-2109-876543210987', '12345678-9012-3456-7890-123456789012') |
| 20 | + |
| 21 | + http_client.request.assert_called_once_with('https://voice.messagebird.com/calls/12348765-4321-0987-6543-210987654321/legs/87654321-0987-6543-2109-876543210987/recordings/12345678-9012-3456-7890-123456789012', 'GET', None) |
| 22 | + |
| 23 | + self.assertEqual('12345678-9012-3456-7890-123456789012', voice_recording.id) |
| 24 | + self.assertEqual('done', voice_recording.status) |
| 25 | + self.assertEqual('wav', voice_recording.format) |
| 26 | + self.assertEqual(datetime(2018, 1, 1, 0, 0, 1), voice_recording.createdAt) |
| 27 | + self.assertEqual(datetime(2018, 1, 1, 0, 0, 5), voice_recording.updatedAt) |
| 28 | + self.assertEqual(2, len(voice_recording._links)) |
| 29 | + self.assertIsInstance(str(voice_recording), str) |
| 30 | + |
| 31 | + def test_voice_recording_list(self): |
| 32 | + http_client = Mock() |
| 33 | + http_client.request.return_value = '{"data":[{"id":"12345678-9012-3456-7890-123456789012","format":"wav","legId":"87654321-0987-6543-2109-876543210987","status":"done","duration":32,"type":"transfer","createdAt":"2018-01-01T00:00:01Z","updatedAt":"2018-01-01T00:00:05Z","deletedAt":null,"_links":{"file":"/calls/12348765-4321-0987-6543-210987654321/legs/7654321-0987-6543-2109-876543210987/recordings/12345678-9012-3456-7890-123456789012.wav","self":"/calls/12348765-4321-0987-6543-210987654321/legs/7654321-0987-6543-2109-876543210987/recordings/12345678-9012-3456-7890-123456789012"}},{"id":"12345678-9012-3456-7890-123456789013","format":"wav","legId":"87654321-0987-6543-2109-876543210987","status":"done","duration":12,"type":"transfer","createdAt":"2019-01-01T00:00:01Z","updatedAt":"2019-01-01T00:00:05Z","deletedAt":null,"_links":{"file":"/calls/12348765-4321-0987-6543-210987654321/legs/7654321-0987-6543-2109-876543210987/recordings/12345678-9012-3456-7890-123456789013.wav","self":"/calls/12348765-4321-0987-6543-210987654321/legs/7654321-0987-6543-2109-876543210987/recordings/12345678-9012-3456-7890-123456789013"}}],"_links":{"self":"/calls/12348765-4321-0987-6543-210987654321/legs/7654321-0987-6543-2109-876543210987/recordings?page=1"},"pagination":{"totalCount":2,"pageCount":1,"currentPage":1,"perPage":10}}' |
| 34 | + |
| 35 | + voice_recordings = Client('', http_client).voice_recording_list_recordings('12348765-4321-0987-6543-210987654321', '87654321-0987-6543-2109-876543210987') |
| 36 | + |
| 37 | + http_client.request.assert_called_once_with('https://voice.messagebird.com/calls/12348765-4321-0987-6543-210987654321/legs/87654321-0987-6543-2109-876543210987/recordings', 'GET', None) |
| 38 | + |
| 39 | + recordings_check = { |
| 40 | + '12345678-9012-3456-7890-123456789012': { "id": '12345678-9012-3456-7890-123456789012', "duration": 32, "year": 2018 }, |
| 41 | + '12345678-9012-3456-7890-123456789013': { "id": '12345678-9012-3456-7890-123456789013', "duration": 12, "year": 2019 } |
| 42 | + } |
| 43 | + |
| 44 | + for item in voice_recordings._items: |
| 45 | + recording_specific = recordings_check.get(item.id) |
| 46 | + self.assertEqual(recording_specific['id'], item.id) |
| 47 | + self.assertEqual(recording_specific['duration'], item.duration) |
| 48 | + self.assertEqual('done', item.status) |
| 49 | + self.assertEqual('wav', item.format) |
| 50 | + self.assertEqual(datetime(recording_specific['year'], 1, 1, 0, 0, 1), item.createdAt) |
| 51 | + self.assertEqual(datetime(recording_specific['year'], 1, 1, 0, 0, 5), item.updatedAt) |
| 52 | + self.assertEqual(2, len(item._links)) |
| 53 | + self.assertIsInstance(str(voice_recordings), str) |
| 54 | + |
| 55 | + def test_voice_recording_download(self): |
| 56 | + http_client = Mock() |
| 57 | + http_client.request.return_value = '{"data":null,"errors":[{"message":"No recording found for ID `00000000-0000-0000-0000-000000000000`.","code":13}],"pagination":{"totalCount":0,"pageCount":0,"currentPage":0,"perPage":0}}' |
| 58 | + |
| 59 | + with self.assertRaises(ErrorException): |
| 60 | + voice_recording = Client('', http_client).voice_recording_download('12348765-4321-0987-6543-210987654321', '87654321-0987-6543-2109-876543210987', '12345678-9012-3456-7890-123456789012') |
| 61 | + |
| 62 | + http_client.request.return_value = '{"data":[{"id":"12345678-9012-3456-7890-123456789012","format":"wav","legId":"87654321-0987-6543-2109-876543210987","status":"done","duration":32,"type":"transfer","createdAt":"2018-01-01T00:00:01Z","updatedAt":"2018-01-01T00:00:05Z","deletedAt":null}],"pagination":{"totalCount":0,"pageCount":0,"currentPage":0,"perPage":0}}' |
| 63 | + |
| 64 | + with self.assertRaises(ErrorException): |
| 65 | + voice_recording = Client('', http_client).voice_recording_download('12348765-4321-0987-6543-210987654321', '87654321-0987-6543-2109-876543210987', '12345678-9012-3456-7890-123456789012') |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +if __name__ == '__main__': |
| 70 | + unittest.main() |
0 commit comments