def test_copy_bq_views(self, mock_table_exists, mock_copy_view):
        """Check that copy_view is called when the view does not exist in the destination dataset."""
        self.mock_client.list_tables.return_value = [self.mock_view]
        self.mock_client.get_table.return_value = self.mock_view
        mock_table_exists.side_effect = self.table_exists_side_effect

        copy_bq_views(source_project_id=self.mock_source_project_id,
                      source_dataset_id=self.mock_source_dataset_id,
                      destination_project_id=self.mock_destination_project_id,
                      destination_dataset_id=self.mock_destination_dataset_id)

        expected_view = BigQueryView(
            project_id=self.mock_destination_project_id,
            dataset_id=self.mock_destination_dataset_id,
            view_id=self.mock_view.view_id,
            view_query_template=self.mock_view.view_query,
            should_materialize=True)

        expected_destination_dataset_ref = bigquery.DatasetReference(
            project=self.mock_destination_project_id,
            dataset_id=self.mock_destination_dataset_id)

        mock_copy_view.assert_called()
        self.assertEqual(expected_view,
                         mock_copy_view.call_args_list[0][1].get('view'))
        self.assertEqual(
            self.mock_destination_project_id, mock_copy_view.call_args_list[0]
            [1].get('destination_client').project_id)
        self.assertEqual(
            expected_destination_dataset_ref,
            mock_copy_view.call_args_list[0][1].get('destination_dataset_ref'))
Exemple #2
0
    def test_copy_bq_views_raw_project_id(
            self, mock_table_exists: mock.MagicMock,
            mock_copy_view: mock.MagicMock) -> None:
        """Check that copy_view is called, even when the project_id is in the view_query_template."""
        view_with_project_id = BigQueryView(
            project_id=self.mock_source_project_id,
            dataset_id=self.mock_source_dataset_id,
            view_id="test_view",
            description="test_view description",
            view_query_template=
            f"SELECT * FROM {self.mock_source_project_id}.other_dataset.table LIMIT 0",
            should_materialize=True,
        )

        self.mock_client.list_tables.return_value = [view_with_project_id]
        self.mock_client.get_table.return_value = view_with_project_id
        mock_table_exists.side_effect = self.table_exists_side_effect

        copy_bq_views(
            source_project_id=self.mock_source_project_id,
            source_dataset_id=self.mock_source_dataset_id,
            destination_project_id=self.mock_destination_project_id,
            destination_dataset_id=self.mock_destination_dataset_id,
        )

        expected_view = BigQueryView(
            project_id=self.mock_destination_project_id,
            dataset_id=self.mock_destination_dataset_id,
            view_id="test_view",
            description="test_view description",
            view_query_template=
            f"SELECT * FROM {self.mock_destination_project_id}.other_dataset.table LIMIT 0",
            should_materialize=True,
        )

        expected_destination_dataset_ref = bigquery.DatasetReference(
            project=self.mock_destination_project_id,
            dataset_id=self.mock_destination_dataset_id,
        )

        mock_copy_view.assert_called()
        self.assertEqual(expected_view,
                         mock_copy_view.call_args_list[0][1].get("view"))
        self.assertEqual(
            self.mock_destination_project_id,
            mock_copy_view.call_args_list[0][1].get(
                "destination_client").project_id,
        )
        self.assertEqual(
            expected_destination_dataset_ref,
            mock_copy_view.call_args_list[0][1].get("destination_dataset_ref"),
        )
    def test_copy_bq_views_already_exists(self, mock_table_exists,
                                          mock_copy_view):
        """Check that copy_view is not called when the view already exists in the destination dataset."""
        self.mock_client.list_tables.return_value = [self.mock_view]
        self.mock_client.get_table.return_value = self.mock_view
        mock_table_exists.return_value = True

        copy_bq_views(source_project_id=self.mock_source_project_id,
                      source_dataset_id=self.mock_source_dataset_id,
                      destination_project_id=self.mock_destination_project_id,
                      destination_dataset_id=self.mock_destination_dataset_id)

        mock_copy_view.assert_not_called()
    def test_copy_bq_views_not_table(self, mock_table_exists, mock_copy_view):
        """Check that copy_view is not called on a table that does not have a view_query."""
        table_ref = bigquery.TableReference(self.mock_source_dataset,
                                            'table_not_view')
        schema_fields = [bigquery.SchemaField('fake_schema_field', 'STRING')]
        table_not_view = bigquery.Table(table_ref, schema_fields)
        self.mock_client.list_tables.return_value = [table_not_view]
        self.mock_client.get_table.return_value = table_not_view
        mock_table_exists.side_effect = self.table_exists_side_effect

        copy_bq_views(source_project_id=self.mock_source_project_id,
                      source_dataset_id=self.mock_source_dataset_id,
                      destination_project_id=self.mock_destination_project_id,
                      destination_dataset_id=self.mock_destination_dataset_id)

        mock_copy_view.assert_not_called()