mock.mock.call

Here are the examples of the python api mock.mock.call taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

7 Examples 7

0 Source : test_bigquery.py
with Apache License 2.0
from globocom

    def test_table_exists_same_project(self):
        table = mock.Mock()

        dataset = mock.Mock()
        dataset.table = MagicMock(return_value=table)

        client = mock.Mock()
        client.get_table = MagicMock()
        client.dataset = MagicMock(return_value=dataset)

        bigquery = Bigquery(client)

        with patch("gcloud_utils.bigquery.bigquery.bigquery") as original_bigquery:
            original_bigquery.Client = MagicMock()

            assert bigquery.table_exists(table_id="my_table", dataset_id="my_dataset")
            assert original_bigquery.Client.call_args_list == []
            assert client.get_table.call_args_list == [call(table)]
            assert client.dataset.call_args_list == [call("my_dataset")]
            assert dataset.table.call_args_list == [call("my_table")]

    def test_table_exists_false_same_project(self):

0 Source : test_bigquery.py
with Apache License 2.0
from globocom

    def test_table_exists_other_project(self):
        table = mock.Mock()

        dataset = mock.Mock()
        dataset.table = MagicMock(return_value=table)

        client = mock.Mock()
        client.get_table = MagicMock()
        client.dataset = MagicMock(return_value=dataset)

        other_client = mock.Mock()
        other_client.dataset = MagicMock(return_value=dataset)

        bigquery = Bigquery(client)

        with patch("gcloud_utils.bigquery.bigquery.bigquery") as original_bigquery:
            original_bigquery.Client = MagicMock(return_value=other_client)

            assert bigquery.table_exists(table_id="my_table", dataset_id="my_dataset", project_id="my_project")
            assert original_bigquery.Client.call_args_list == [call("my_project")]
            assert client.get_table.call_args_list == [call(table)]
            assert client.dataset.call_args_list == []
            assert other_client.dataset.call_args_list == [call("my_dataset")]
            assert dataset.table.call_args_list == [call("my_table")]

    def test_table_exists_false_other_project(self):

0 Source : test_bigquery.py
with Apache License 2.0
from globocom

    def test_table_exists_false_other_project(self):
        table = mock.Mock()

        dataset = mock.Mock()
        dataset.table = MagicMock(return_value=table)

        client = mock.Mock()
        client.get_table = MagicMock(side_effect=NotFound("xxx"))
        client.dataset = MagicMock(return_value=dataset)

        other_client = mock.Mock()
        other_client.dataset = MagicMock(return_value=dataset)

        bigquery = Bigquery(client)

        with patch("gcloud_utils.bigquery.bigquery.bigquery") as original_bigquery:
            original_bigquery.Client = MagicMock(return_value=other_client)

            assert not bigquery.table_exists(table_id="my_table", dataset_id="my_dataset", project_id="my_project")
            assert original_bigquery.Client.call_args_list == [call("my_project")]
            assert other_client.dataset.call_args_list == [call("my_dataset")]

0 Source : gcs_file_system_test.py
with GNU General Public License v3.0
from Recidiviz

    def test_copy(self) -> None:
        bucket_path = GcsfsBucketPath.from_absolute_path("gs://my-bucket")
        src_path = GcsfsFilePath.from_directory_and_file_name(bucket_path, "src.txt")
        dst_path = GcsfsFilePath.from_directory_and_file_name(bucket_path, "dst.txt")

        mock_src_blob = create_autospec(Blob)

        mock_dst_blob = create_autospec(Blob)
        mock_dst_blob.rewrite.side_effect = [
            ("rewrite-token", 100, 200),
            (None, 200, 200),
        ]
        mock_dst_bucket = create_autospec(Bucket)

        def mock_get_blob(blob_name: str) -> Blob:
            if blob_name == src_path.file_name:
                return mock_src_blob
            if blob_name == dst_path.file_name:
                return mock_dst_blob
            raise ValueError(f"Unexpected blob: {blob_name}")

        mock_dst_bucket.get_blob.side_effect = mock_get_blob
        mock_dst_bucket.blob.return_value = mock_dst_blob

        self.mock_storage_client.bucket.return_value = mock_dst_bucket

        self.fs.copy(src_path=src_path, dst_path=dst_path)
        mock_dst_blob.rewrite.assert_has_calls(
            calls=[
                call(mock_src_blob, token=None),
                call(mock_src_blob, "rewrite-token"),
            ]
        )

    # Disable retries for "transient" errors to make test easier to mock / follow.
    @patch(

0 Source : gcs_file_system_test.py
with GNU General Public License v3.0
from Recidiviz

    def test_copy_failure(self) -> None:
        bucket_path = GcsfsBucketPath.from_absolute_path("gs://my-bucket")
        src_path = GcsfsFilePath.from_directory_and_file_name(bucket_path, "src.txt")
        dst_path = GcsfsFilePath.from_directory_and_file_name(bucket_path, "dst.txt")

        mock_src_blob = create_autospec(Blob)

        mock_dst_blob = create_autospec(Blob)
        mock_dst_blob.rewrite.side_effect = [
            ("rewrite-token", 100, 200),
            exceptions.TooManyRequests("Too many requests!"),  # type: ignore [attr-defined]
        ]
        mock_dst_bucket = create_autospec(Bucket)

        def mock_get_blob(blob_name: str) -> Blob:
            if blob_name == src_path.file_name:
                return mock_src_blob
            if blob_name == dst_path.file_name:
                return mock_dst_blob
            raise ValueError(f"Unexpected blob: {blob_name}")

        mock_dst_bucket.get_blob.side_effect = mock_get_blob
        mock_dst_bucket.blob.return_value = mock_dst_blob

        self.mock_storage_client.bucket.return_value = mock_dst_bucket

        with self.assertRaisesRegex(
            exceptions.TooManyRequests,  # type: ignore [attr-defined]
            "Too many requests!",
        ):
            self.fs.copy(src_path=src_path, dst_path=dst_path)
        mock_dst_blob.rewrite.assert_has_calls(
            calls=[
                call(mock_src_blob, token=None),
                call(mock_src_blob, "rewrite-token"),
            ]
        )
        # Assert we tried to clean up the destination file
        mock_dst_blob.delete.assert_called()

0 Source : test_views.py
with MIT License
from uktrade

    def test_share_query(self, mock_send_email, query_param, query_factory, client, user):
        query_obj = query_factory.create(created_by_user=user)
        response = client.post(
            f"{reverse('explorer:share_query')}?{query_param}={query_obj.id}",
            data={
                "query": "select 6870+2;",
                "to_user": user.email,
                "message": "A test message",
                "copy_sender": False,
            },
            follow=True,
        )
        assert b"Query shared" in response.content
        mock_send_email.assert_has_calls(
            [
                mock.call(
                    settings.NOTIFY_SHARE_EXPLORER_QUERY_TEMPLATE_ID,
                    user.email,
                    personalisation={
                        "message": "A test message",
                        "sharer_first_name": "Frank",
                    },
                )
            ]
        )

    @pytest.mark.parametrize(

0 Source : test_views.py
with MIT License
from uktrade

    def test_share_query_copy_sender(
        self, mock_send_email, query_param, query_factory, client, user, staff_user
    ):
        query_obj = query_factory.create(created_by_user=user)
        response = client.post(
            f"{reverse('explorer:share_query')}?{query_param}={query_obj.id}",
            data={
                "query": "select 6870+2;",
                "to_user": staff_user.email,
                "message": "A test message",
                "copy_sender": True,
            },
            follow=True,
        )
        assert b"Query shared" in response.content
        mock_send_email.assert_has_calls(
            [
                mock.call(
                    settings.NOTIFY_SHARE_EXPLORER_QUERY_TEMPLATE_ID,
                    "[email protected]",
                    personalisation={
                        "message": "A test message",
                        "sharer_first_name": "Frank",
                    },
                ),
                mock.call(
                    settings.NOTIFY_SHARE_EXPLORER_QUERY_TEMPLATE_ID,
                    "[email protected]",
                    personalisation={
                        "message": "A test message",
                        "sharer_first_name": "Frank",
                    },
                ),
            ],
            any_order=True,
        )