Esempio n. 1
0
    def testGetTableDetailsNestedSchema(self):
        client = Mock()

        schema_field_0 = Mock(field_type='STRING',
                              description='this field is a string and a child',
                              mode='NULLABLE')
        schema_field_0.name = 'child'

        schema_field_1 = Mock(field_type='RECORD',
                              description='this field is a record',
                              mode='NULLABLE',
                              fields=[schema_field_0])
        schema_field_1.name = 'record'

        table = Mock(project='project_id',
                     dataset_id='dataset_id',
                     table_id='table_id',
                     description='description of table',
                     labels={
                         'label_0': 'value_0',
                         'label_1': 'value_1'
                     },
                     created=datetime.datetime(2020,
                                               7,
                                               14,
                                               13,
                                               23,
                                               45,
                                               67,
                                               tzinfo=None),
                     expires=datetime.datetime(2021,
                                               7,
                                               14,
                                               13,
                                               23,
                                               45,
                                               67,
                                               tzinfo=None),
                     location='US',
                     modified=datetime.datetime(2020,
                                                7,
                                                15,
                                                15,
                                                11,
                                                23,
                                                32,
                                                tzinfo=None),
                     self_link='https://bigquery.googleapis.com/bigquery' + \
                        '/v2/projects/project_id/datasets/dataset_id',
                     num_rows=123456,
                     num_bytes=2000000,
                     schema=[schema_field_1])
        client.get_table = Mock(return_value=table)
        bigquery = BigQueryService(client)

        expected = {
            'details': {
                'id':
                    'project_id.dataset_id.table_id',
                'name':
                    'table_id',
                'description':
                    'description of table',
                'labels': ['label_0: value_0', 'label_1: value_1'],
                'date_created':
                    '2020-07-14T13:23:45.000067',
                'expires':
                    '2021-07-14T13:23:45.000067',
                'location':
                    'US',
                'last_modified':
                    '2020-07-15T15:11:23.000032',
                'project':
                    'project_id',
                'dataset':
                    'dataset_id',
                'link':
                    'https://bigquery.googleapis.com/bigquery' + \
                        '/v2/projects/project_id/datasets/dataset_id',
                'num_rows':
                    123456,
                'num_bytes':
                    2000000,
                'schema': [{
                    'name': 'record',
                    'type': 'RECORD',
                    'description': 'this field is a record',
                    'mode': 'NULLABLE'
                }, {
                    'name': 'record.child',
                    'type': 'STRING',
                    'description': 'this field is a string and a child',
                    'mode': 'NULLABLE'
                }]
            }
        }

        result = bigquery.get_table_details('some_table_id')
        self.assertEqual(expected, result)
Esempio n. 2
0
    def testGetTableDetailsEmptyFields(self):
        client = Mock()

        table = Mock(project='project_id',
                     dataset_id='dataset_id',
                     table_id='table_id',
                     description=None,
                     labels={},
                     created=datetime.datetime(2020,
                                               7,
                                               14,
                                               13,
                                               23,
                                               45,
                                               67,
                                               tzinfo=None),
                     expires=None,
                     location='US',
                     modified=datetime.datetime(2020,
                                                7,
                                                15,
                                                15,
                                                11,
                                                23,
                                                32,
                                                tzinfo=None),
                     self_link='https://bigquery.googleapis.com/' + \
                'bigquery/v2/projects/project_id/datasets/dataset_id',
                     num_rows=0,
                     num_bytes=0,
                     schema=[])
        client.get_table = Mock(return_value=table)
        bigquery = BigQueryService(client)

        expected = {
            'details': {
                'id':
                    'project_id.dataset_id.table_id',
                'name':
                    'table_id',
                'description':
                    None,
                'labels':
                    None,
                'date_created':
                    '2020-07-14T13:23:45.000067',
                'expires':
                    None,
                'location':
                    'US',
                'last_modified':
                    '2020-07-15T15:11:23.000032',
                'project':
                    'project_id',
                'dataset':
                    'dataset_id',
                'link':
                    'https://bigquery.googleapis.com/' + \
                        'bigquery/v2/projects/project_id/datasets/dataset_id',
                'num_rows':
                    0,
                'num_bytes':
                    0,
                'schema': []
            }
        }

        result = bigquery.get_table_details('some_table_id')
        self.assertEqual(expected, result)