def test_downloadMedia(self): class MyLittleRequests(object): @staticmethod def get(*args, **kwargs): self.assertEqual(args[0], self.url) is_streaming = kwargs.get("stream") class MyLittleResponse(object): if is_streaming: content = "binary content".encode("UTF-8") @staticmethod def iter_content(chunk_size): assert chunk_size is None or chunk_size >= 1024 for char in "binary content": yield char.encode("UTF-8") @staticmethod def raise_for_status(): pass return MyLittleResponse # Test that the given file object is used m = Media(self.url, self.size, self.type) m.requests_session = MyLittleRequests fd = io.BytesIO() m.download(fd) self.assertEqual(fd.getvalue().decode("UTF-8"), "binary content") fd.close() # Test that the given filename is used with tempfile.NamedTemporaryFile(delete=False) as fd: filename = fd.name try: m.download(filename) with open(filename, "rb") as fd: self.assertEqual(fd.read().decode("UTF-8"), "binary content") finally: os.remove(filename)
def test_getDuration(self, mock_tinytag, mock_open, mock_rm): # Create our fake requests module mock_requests = mock.Mock() # Prepare the response which the code will get from requests.get() mock_requests_response = mock.Mock() # The content (supposed to be binary mp3 file) mock_requests_response.content = "binary data here" # The content, as returned by an iterator (supposed to be chunks of # mp3-file) mock_requests_response.iter_content.return_value = range(5) # Make sure our fake response is returned by requests.get() mock_requests.get.return_value = mock_requests_response # Return the correct number of seconds from TinyTag seconds = 14 * 60 mock_tinytag.get.return_value.duration = seconds # Now do the actual testing m = Media(self.url, self.size, self.type) m.requests_session = mock_requests m.fetch_duration() self.assertAlmostEqual(m.duration.total_seconds(), seconds, places=0) # Check that the underlying libraries were used correctly self.assertEqual(mock_requests.get.call_args[0][0], self.url) if 'stream' in mock_requests.get.call_args[1] and \ mock_requests.get.call_args[1]['stream']: # The request is streamed, so iter_content was used self.assertEqual(mock_requests_response.iter_content.call_count, 1) fd = mock_open.return_value.__enter__.return_value expected = [((i,),) for i in range(5)] self.assertEqual(fd.write.call_args_list, expected) else: # The entire file was downloaded in one go mock_open.return_value.__enter__.return_value.\ write.assert_called_once_with("binary data here") mock_rm.assert_called_once_with(mock_open.return_value. __enter__.return_value.name)
def test_getDuration(self, mock_tinytag, mock_open, mock_rm): # Create our fake requests module mock_requests = mock.Mock() # Prepare the response which the code will get from requests.get() mock_requests_response = mock.Mock() # The content (supposed to be binary mp3 file) mock_requests_response.content = "binary data here" # The content, as returned by an iterator (supposed to be chunks of # mp3-file) mock_requests_response.iter_content.return_value = range(5) # Make sure our fake response is returned by requests.get() mock_requests.get.return_value = mock_requests_response # Return the correct number of seconds from TinyTag seconds = 14 * 60 mock_tinytag.get.return_value.duration = seconds # Now do the actual testing m = Media(self.url, self.size, self.type) m.requests_session = mock_requests m.fetch_duration() self.assertAlmostEqual(m.duration.total_seconds(), seconds, places=0) # Check that the underlying libraries were used correctly self.assertEqual(mock_requests.get.call_args[0][0], self.url) if 'stream' in mock_requests.get.call_args[1] and \ mock_requests.get.call_args[1]['stream']: # The request is streamed, so iter_content was used self.assertEqual(mock_requests_response.iter_content.call_count, 1) fd = mock_open.return_value.__enter__.return_value expected = [((i, ), ) for i in range(5)] self.assertEqual(fd.write.call_args_list, expected) else: # The entire file was downloaded in one go mock_open.return_value.__enter__.return_value.\ write.assert_called_once_with("binary data here") mock_rm.assert_called_once_with( mock_open.return_value.__enter__.return_value.name)