Example #1
0
    def test_download_text_to_file(
            self, session_patch: unittest.mock.MagicMock) -> None:
        """
        Tests the method which let us set download the text of a given
        url for the case that we want the response into a file.
        """

        destination = tempfile.NamedTemporaryFile(delete=False)

        given = "https://exmaple.org"

        download_helper = DownloadHelper(given)

        session_patch.return_value.text = "Hello, World!"
        session_patch.return_value.status_code = 200

        download_helper.download_text(destination=destination.name)

        destination.seek(0)

        expected = b"Hello, World!"
        actual = destination.read()

        self.assertEqual(expected, actual)
Example #2
0
    def test_download_text(self,
                           session_patch: unittest.mock.MagicMock) -> None:
        """
        Tests the method which let us set download the text of a given
        url.
        """

        given = "https://exmaple.org"

        download_helper = DownloadHelper(given)

        session_patch.return_value.text = "Hello, World!"
        session_patch.return_value.status_code = 200

        expected = "Hello, World!"
        actual = download_helper.download_text()

        self.assertEqual(expected, actual)
Example #3
0
    def test_download_text_response_not_ok(
            self, session_patch: unittest.mock.MagicMock) -> None:
        """
        Tests the method which let us set download the text of a given
        url for the case that the response is not ok.
        """

        destination = tempfile.NamedTemporaryFile(delete=False)

        given = "https://exmaple.org"

        download_helper = DownloadHelper(given)

        session_patch.return_value.status_code = 500

        self.assertRaises(
            PyFunceble.helpers.exceptions.UnableToDownload,
            lambda: download_helper.download_text(destination=destination.name
                                                  ),
        )