Beispiel #1
0
    def test_create_query_with_source(self, bigquery_client_mock, timer_mock):
        client = self.create_mock_client()
        bigquery_client_mock.from_service_account_json.return_value = client
        start_date = date(2020, 5, 27)
        end_date = date(2020, 5, 28)

        for source, column in AMO_TO_BQ_DOWNLOAD_COLUMN_MAPPING.items():
            expected_query = f"""
SELECT submission_date, total_downloads, {column}
FROM `project.dataset.{AMO_STATS_DOWNLOAD_VIEW}`
WHERE hashed_addon_id = @hashed_addon_id
AND submission_date BETWEEN @submission_date_start AND @submission_date_end
ORDER BY submission_date DESC
LIMIT 365"""

            get_download_series(
                addon=self.addon,
                start_date=start_date,
                end_date=end_date,
                source=source,
            )

            client.query.assert_called_with(expected_query, job_config=mock.ANY)
            timer_mock.assert_called_with(
                f'stats.get_download_series.bigquery.{source}'
            )
    def test_create_query(self, bigquery_client_mock, timer_mock):
        client = self.create_mock_client()
        bigquery_client_mock.from_service_account_json.return_value = client
        start_date = date(2020, 5, 27)
        end_date = date(2020, 5, 28)
        expected_query = f"""
SELECT submission_date, total_downloads
FROM `project.dataset.{AMO_STATS_DOWNLOAD_VIEW}`
WHERE addon_id = @addon_id
AND submission_date BETWEEN @submission_date_start AND @submission_date_end
ORDER BY submission_date DESC
LIMIT 365"""

        get_download_series(addon=self.addon,
                            start_date=start_date,
                            end_date=end_date)

        client.query.assert_called_once_with(expected_query,
                                             job_config=mock.ANY)
        parameters = self.get_job_config_named_parameters(client.query)
        assert parameters == [
            {
                'parameterType': {
                    'type': 'STRING'
                },
                'parameterValue': {
                    'value': self.addon.guid
                },
                'name': 'addon_id',
            },
            {
                'parameterType': {
                    'type': 'DATE'
                },
                'parameterValue': {
                    'value': str(start_date)
                },
                'name': 'submission_date_start',
            },
            {
                'parameterType': {
                    'type': 'DATE'
                },
                'parameterValue': {
                    'value': str(end_date)
                },
                'name': 'submission_date_end',
            },
        ]
        timer_mock.assert_called_once_with(
            'stats.get_download_series.bigquery.no_source')
    def test_create_client(self, bigquery_client_mock):
        client = self.create_mock_client()
        bigquery_client_mock.from_service_account_json.return_value = client

        credentials = 'path/to/credentials.json'
        with override_settings(GOOGLE_APPLICATION_CREDENTIALS=credentials):
            get_download_series(
                addon=self.addon,
                start_date=date(2020, 5, 27),
                end_date=date(2020, 5, 28),
            )

        bigquery_client_mock.from_service_account_json.assert_called_once_with(
            credentials)