def test_download_file(url, expected_content):
    with TemporaryFile() as downloaded_file:
        file_size = downloadutils.download_file(url, downloaded_file, is_quiet=True)

        assert 0 == downloaded_file.tell()
        assert file_size > 0
        assert expected_content == downloaded_file.read()
    def download_dump(self, file_prefix):
        """
        Downloads a dump identified by file prefix to destination file.
        :param file_prefix: Prefix of the dump file.
        :return: List of file object, url and size of downloaded file.
        """
        dump_file = TemporaryFile()
        dump_url = self.get_dump_url(file_prefix)
        try:
            dump_size = downloadutils.download_file(dump_url, dump_file,
                                                    is_quiet=self.is_quiet,
                                                    progress_message="Downloading database dump...{0}")
        except DownloadError as e:
            if e.status_code == 400 or e.status_code == 500:
                dump_url = self.get_dump_url(file_prefix, fallback=True)
                dump_size = downloadutils.download_file(dump_url, dump_file,
                                                        is_quiet=self.is_quiet,
                                                        progress_message="Downloading database dump...{0}")
            else:
                raise

        return dump_file, dump_url, dump_size
def test_download_dump_error(url):
    with pytest.raises(DownloadError.DownloadError):
        downloaded_file = TemporaryFile()
        downloadutils.download_file(url, downloaded_file, is_quiet=True)