Пример #1
0
 def test_schema_setter(self):
     from gcloud.bigquery.table import SchemaField
     client = _Client(self.PROJECT)
     table = _Table()
     job = self._makeOne(self.JOB_NAME, table, [self.SOURCE1], client)
     full_name = SchemaField('full_name', 'STRING', mode='REQUIRED')
     age = SchemaField('age', 'INTEGER', mode='REQUIRED')
     job.schema = [full_name, age]
     self.assertEqual(job.schema, [full_name, age])
Пример #2
0
 def test_table_w_schema(self):
     from gcloud.bigquery.table import SchemaField
     from gcloud.bigquery.table import Table
     conn = _Connection({})
     client = _Client(project=self.PROJECT, connection=conn)
     dataset = self._makeOne(self.DS_NAME, client=client)
     full_name = SchemaField('full_name', 'STRING', mode='REQUIRED')
     age = SchemaField('age', 'INTEGER', mode='REQUIRED')
     table = dataset.table('table_name', schema=[full_name, age])
     self.assertTrue(isinstance(table, Table))
     self.assertEqual(table.name, 'table_name')
     self.assertTrue(table._dataset is dataset)
     self.assertEqual(table.schema, [full_name, age])
Пример #3
0
 def test_schema_setter_invalid_field(self):
     from gcloud.bigquery.table import SchemaField
     client = _Client(self.PROJECT)
     table = _Table()
     job = self._makeOne(self.JOB_NAME, table, [self.SOURCE1], client)
     full_name = SchemaField('full_name', 'STRING', mode='REQUIRED')
     with self.assertRaises(ValueError):
         job.schema = [full_name, object()]
Пример #4
0
    def test_begin_w_alternate_client(self):
        from gcloud.bigquery.table import SchemaField
        PATH = 'projects/%s/jobs' % self.PROJECT
        RESOURCE = self._makeResource(ended=True)
        LOAD_CONFIGURATION = {
            'sourceUris': [self.SOURCE1],
            'destinationTable': {
                'projectId': self.PROJECT,
                'datasetId': self.DS_NAME,
                'tableId': self.TABLE_NAME,
            },
            'allowJaggedRows': True,
            'allowQuotedNewlines': True,
            'createDisposition': 'CREATE_NEVER',
            'encoding': 'ISO-8559-1',
            'fieldDelimiter': '|',
            'ignoreUnknownValues': True,
            'maxBadRecords': 100,
            'quote': "'",
            'skipLeadingRows': 1,
            'sourceFormat': 'CSV',
            'writeDisposition': 'WRITE_TRUNCATE',
            'schema': {
                'fields': [
                    {
                        'name': 'full_name',
                        'type': 'STRING',
                        'mode': 'REQUIRED'
                    },
                    {
                        'name': 'age',
                        'type': 'INTEGER',
                        'mode': 'REQUIRED'
                    },
                ]
            }
        }
        RESOURCE['configuration']['load'] = LOAD_CONFIGURATION
        conn1 = _Connection()
        client1 = _Client(project=self.PROJECT, connection=conn1)
        conn2 = _Connection(RESOURCE)
        client2 = _Client(project=self.PROJECT, connection=conn2)
        table = _Table()
        full_name = SchemaField('full_name', 'STRING', mode='REQUIRED')
        age = SchemaField('age', 'INTEGER', mode='REQUIRED')
        job = self._makeOne(self.JOB_NAME,
                            table, [self.SOURCE1],
                            client1,
                            schema=[full_name, age])

        job.allow_jagged_rows = True
        job.allow_quoted_newlines = True
        job.create_disposition = 'CREATE_NEVER'
        job.encoding = 'ISO-8559-1'
        job.field_delimiter = '|'
        job.ignore_unknown_values = True
        job.max_bad_records = 100
        job.quote_character = "'"
        job.skip_leading_rows = 1
        job.source_format = 'CSV'
        job.write_disposition = 'WRITE_TRUNCATE'

        job.begin(client=client2)

        self.assertEqual(len(conn1._requested), 0)
        self.assertEqual(len(conn2._requested), 1)
        req = conn2._requested[0]
        self.assertEqual(req['method'], 'POST')
        self.assertEqual(req['path'], '/%s' % PATH)
        SENT = {
            'jobReference': {
                'projectId': self.PROJECT,
                'jobId': self.JOB_NAME,
            },
            'configuration': {
                'load': LOAD_CONFIGURATION,
            },
        }
        self.assertEqual(req['data'], SENT)
        self._verifyResourceProperties(job, RESOURCE)