def test_metric_export_state_agnostic(self):
        """Tests the export_configs_for_views_to_export function on the ExportMetricDatasetConfig class when the
        export is state-agnostic."""
        state_agnostic_dataset_export_config = ExportMetricDatasetConfig(
            dataset_id='dataset_id',
            metric_view_builders_to_export=self.views_for_dataset,
            output_directory_uri_template=
            "gs://{project_id}-bucket-without-state-codes",
            state_code_filter=None,
            export_name=None)

        view_configs_to_export = state_agnostic_dataset_export_config.export_configs_for_views_to_export(
            project_id=self.mock_project_id)

        expected_view = self.mock_view_builder.build()

        expected_view_export_configs = [
            ExportMetricBigQueryViewConfig(
                view=expected_view,
                view_filter_clause=None,
                intermediate_table_name=f"{expected_view.view_id}_table",
                output_directory=GcsfsDirectoryPath.from_absolute_path(
                    state_agnostic_dataset_export_config.
                    output_directory_uri_template.format(
                        project_id=self.mock_project_id, )))
        ]

        self.assertEqual(expected_view_export_configs, view_configs_to_export)
    def test_export_dashboard_data_to_cloud_storage_validation_error(self,
                                                                     mock_view_exporter,
                                                                     mock_view_update_manager):
        """Tests the table is created from the view and then extracted."""

        mock_view_exporter.export_and_validate.side_effect = ViewExportValidationError

        # Should not throw
        metric_view_export_manager.export_view_data_to_cloud_storage(mock_state_code, mock_view_exporter)

        view = self.mock_view_builder.build()

        view_export_configs = [ExportMetricBigQueryViewConfig(
            view=view,
            view_filter_clause=" WHERE state_code = 'US_XX'",
            intermediate_table_name=f"{view.view_id}_table_US_XX",
            output_directory=GcsfsDirectoryPath.from_absolute_path(
                "gs://{project_id}-dataset-location/subdirectory/{state_code}".format(
                    project_id=self.mock_project_id,
                    state_code='US_XX',
                )
            )
        )]

        mock_view_update_manager.assert_called()
        mock_view_exporter.export_and_validate.assert_called_with(view_export_configs)
    def test_metric_export_lantern_dashboard_with_state(self):
        """Tests the export_configs_for_views_to_export function on the ExportMetricDatasetConfig class when the
            export is state-specific."""
        lantern_dashboard_with_state_dataset_export_config = ExportMetricDatasetConfig(
            dataset_id='dataset_id',
            metric_view_builders_to_export=self.views_for_dataset,
            output_directory_uri_template="gs://{project_id}-bucket",
            state_code_filter="US_XX",
            export_name="TEST_EXPORT")

        view_configs_to_export = lantern_dashboard_with_state_dataset_export_config.export_configs_for_views_to_export(
            project_id=self.mock_project_id)

        expected_view = self.mock_view_builder.build()

        expected_view_export_configs = [
            ExportMetricBigQueryViewConfig(
                view=expected_view,
                view_filter_clause=" WHERE state_code = 'US_XX'",
                intermediate_table_name=f"{expected_view.view_id}_table_US_XX",
                output_directory=GcsfsDirectoryPath.from_absolute_path(
                    f"gs://{self.mock_project_id}-bucket/US_XX"))
        ]

        self.assertEqual(expected_view_export_configs, view_configs_to_export)
    def test_export_dashboard_data_to_cloud_storage_state_agnostic(self,
                                                                   mock_view_exporter,
                                                                   mock_view_update_manager,
                                                                   mock_view_config):
        """Tests the table is created from the view and then extracted, where the export is not state-specific."""

        state_agnostic_dataset_export_configs = [
            ExportMetricDatasetConfig(
                dataset_id='dataset_id',
                metric_view_builders_to_export=[self.mock_view_builder],
                output_directory_uri_template="gs://{project_id}-bucket-without-state-codes",
                state_code_filter=None,
                export_name=None
            ),
        ]

        mock_view_config.VIEW_BUILDERS_FOR_VIEWS_TO_UPDATE = self.views_to_update
        mock_view_config.METRIC_DATASET_EXPORT_CONFIGS = state_agnostic_dataset_export_configs

        metric_view_export_manager.export_view_data_to_cloud_storage(export_job_filter=None,
                                                                     view_exporter=mock_view_exporter)

        view = self.mock_view_builder.build()

        view_export_configs = [ExportMetricBigQueryViewConfig(
            view=view,
            view_filter_clause=None,
            intermediate_table_name=f"{view.view_id}_table",
            output_directory=GcsfsDirectoryPath.from_absolute_path(
                "gs://{project_id}-bucket-without-state-codes".format(
                    project_id=self.mock_project_id,
                )
            )
        )]

        mock_view_update_manager.assert_called()
        mock_view_exporter.export_and_validate.assert_called_with(view_export_configs)