Exemplo n.º 1
0
    def test_should_ignore_view_already_exists_error(self, _create_http):
        # given
        http_mock = Mock(wraps=HttpMockSequence(
            [({
                'status': '200'
            },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_test_schema.json')),
             ({
                 'status': '409'
             },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_tables_insert_409.json')),
             ({
                 'status': '409'
             },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_tables_insert_409.json'))]))
        _create_http.return_value = http_mock
        model_provider = Mock()
        model_provider.list_views. \
            return_value = [View("group", "name1", "SELECT * FROM TABLE"),
                            View("group", "name2", "SELECT * FROM TABLE")]

        under_test = ModelCreator(model_provider)

        # when
        under_test.create_missing_views()

        # then
        calls = http_mock.mock_calls
        self.assertEqual(3, len(calls))
Exemplo n.º 2
0
    def test_should_ignore_dataset_already_exists_error(self, _create_http):
        # given
        http_mock = Mock(wraps=HttpMockSequence(
            [({
                'status': '200'
            },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_test_schema.json')),
             ({
                 'status': '409'
             },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_datasets_insert_409.json')),
             ({
                 'status': '409'
             },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_datasets_insert_409.json')
              )]))
        _create_http.return_value = http_mock
        model_provider = Mock()
        model_provider.list_groups.return_value = [
            "existing_dataset1", "existing_dataset2"
        ]

        under_test = ModelCreator(model_provider)

        # when
        under_test.create_missing_datasets()

        # then
        calls = http_mock.mock_calls
        self.assertEqual(3, len(calls))
Exemplo n.º 3
0
    def test_should_create_views(self, _create_http):
        # given
        http_mock = Mock(wraps=HttpMockSequence(
            [({
                'status': '200'
            },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_test_schema.json')),
             ({
                 'status': '200'
             },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_tables_insert_200.json')),
             ({
                 'status': '200'
             },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_tables_insert_200.json'))]))
        _create_http.return_value = http_mock
        model_provider = Mock()
        model_provider.list_views. \
            return_value = [View("group", "name1", "SELECT * FROM TABLE"),
                            View("group", "name2", "SELECT * FROM TABLE")]

        under_test = ModelCreator(model_provider)

        # when
        under_test.create_missing_views()

        # then
        calls = http_mock.mock_calls
        self.assertEqual(3, len(calls))
        json_request = test_utils.get_body_from_http_request(calls[1])
        self.assertTrue('description' in json_request)
        self.assertTrue('query' in json_request['view'])
Exemplo n.º 4
0
    def test_should_create_tables(self, _create_http):
        # given
        http_mock = Mock(wraps=HttpMockSequence(
            [({
                'status': '200'
            },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_test_schema.json')),
             ({
                 'status': '200'
             },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_tables_insert_200.json')),
             ({
                 'status': '200'
             },
              test_utils.content(
                  'tests/json_samples/bigquery_v2_tables_insert_200.json'))]))
        _create_http.return_value = http_mock
        model_provider = Mock()
        model_provider.list_tables.return_value = [
            Table("group", "name1", {}),
            Table("group", "name2", {})
        ]

        under_test = ModelCreator(model_provider)

        # when
        under_test.create_missing_tables()

        # then
        calls = http_mock.mock_calls
        self.assertEqual(3, len(calls))
Exemplo n.º 5
0
    def test_iterating_tables(self, _create_http):  # nopep8 pylint: disable=W0613
        # given
        _create_http.return_value = HttpMockSequence([
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/bigquery_v2_test_schema.json')),
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/bigquery_v2_table_list_page_1.json')),
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/bigquery_v2_table_list_page_last.json'))
        ])

        under_test = BigQuery()

        # when
        dataset_ids = under_test.list_table_ids("project123", "dataset_id")

        # then
        self.assertEqual(self.count(dataset_ids), 5)
Exemplo n.º 6
0
    def test_streaming_row(self, _stream_metadata, _create_http):  # nopep8 pylint: disable=W0613
        # given
        _create_http.return_value = HttpMockSequence([
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/bigquery_v2_test_schema.json')),
            ({
                'status': '200'
            },
             test_utils.content('tests/json_samples/'
                                'bigquery_v2_stream_response.json'))
        ])
        under_test = BigQuery()
        data = {'key': 'value'}
        row = Row('dataset_id', 'table_id', 'insert_id', data=data)

        # when
        under_test.stream_stats(row)

        # then
        stream_data = _stream_metadata.call_args_list[0][0][0]
        json_payload = stream_data['rows'][0]['json']
        insert_id = stream_data['rows'][0]['insertId']
        self.assertEqual(insert_id, row.insert_id)
        self.assertEqual(json_payload, row.data)
Exemplo n.º 7
0
    def test_listing_table_partitions(self, _create_http):  # nopep8 pylint: disable=W0613
        # given
        _create_http.return_value = HttpMockSequence([
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/bigquery_v2_test_schema.json')),
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/bigquery_v2_query_for_partitions.json')),
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/'
                 'bigquery_v2_query_for_partitions_results_1.json')),
            ({
                'status': '200'
            },
             test_utils.content(
                 'tests/json_samples/'
                 'bigquery_v2_query_for_partitions_results_last.json'))
        ])
        under_test = BigQuery()

        # when
        partitions = under_test.list_table_partitions("project123",
                                                      "dataset123", "table123")

        # then
        self.assertEqual(self.count(partitions), 5)
        self.assertEqual(partitions[0]['partitionId'], '20170317')
        self.assertEqual(partitions[0]['creationTime'],
                         '2017-03-17 14:32:17.755000')
        self.assertEqual(partitions[0]['lastModifiedTime'],
                         '2017-03-17 14:32:19.289000')
Exemplo n.º 8
0
    def test_should_propagate_dataset_500_error(self, _create_http):
        # given
        http_mock = Mock(wraps=HttpMockSequence([({
            'status': '200'
        }, test_utils.content('tests/json_samples/bigquery_v2_test_schema.json'
                              )), ({
                                  'status': '500'
                              }, '')]))
        _create_http.return_value = http_mock
        model_provider = Mock()
        model_provider.list_groups.return_value = ["missing_dataset1"]

        under_test = ModelCreator(model_provider)

        # when
        with self.assertRaises(HttpError) as context:
            under_test.create_missing_datasets()

        # then
        calls = http_mock.mock_calls
        self.assertEqual(2, len(calls))
        self.assertEqual(500, context.exception.resp.status)