def testAssignTypeToRowWithNullable(self): schema = bigquery_helper._SchemaResponseToDicts([{ 'type': 'INTEGER', 'name': 'int_field_name', 'mode': 'NULLABLE' }, { 'type': 'BOOLEAN', 'name': 'boolean_field_name', 'mode': 'NULLABLE' }, { 'type': 'STRING', 'name': 'string_field_name', 'mode': 'NULLABLE' }, { 'type': 'TIMESTAMP', 'name': 'timestamp_field_name', 'mode': 'NULLABLE' }]) row = {'f': [{'v': None}, {'v': None}, {'v': None}, {'v': None}]} obj = bigquery_helper._AssignTypeToRow(schema, row) self.assertEqual(obj['int_field_name'], None) self.assertEqual(obj['boolean_field_name'], None) self.assertEqual(obj['string_field_name'], None) self.assertEqual(obj['timestamp_field_name'], None)
def testAssignTypeToRowWithUnknownSchema(self): schema = bigquery_helper._SchemaResponseToDicts([{ 'type': 'FOOBAR', 'name': 'field_name', 'mode': 'NULLABLE' }]) row = {'f': [{'v': '123'}]} obj = bigquery_helper._AssignTypeToRow(schema, row) self.assertEqual(obj, {'field_name': '123'})
def testAssignTypeToRow(self): schema = bigquery_helper._SchemaResponseToDicts([ { 'type': 'INTEGER', 'name': 'int_field_name', 'mode': 'NULLABLE' }, { 'type': 'BOOLEAN', 'name': 'boolean_field_name', 'mode': 'NULLABLE' }, { 'type': 'STRING', 'name': 'string_field_name', 'mode': 'NULLABLE' }, { 'type': 'TIMESTAMP', 'name': 'timestamp_field_name', 'mode': 'NULLABLE' }, ]) row = { 'f': [{ 'v': '123' }, { 'v': 'false' }, { 'v': 'tryserver.chromium.mac' }, { 'v': _UTC_TIMESTAMP_OF_START_TIME }] } obj = bigquery_helper._AssignTypeToRow(schema, row) self.assertEqual(obj['int_field_name'], 123) self.assertEqual(obj['boolean_field_name'], False) self.assertEqual(obj['string_field_name'], 'tryserver.chromium.mac') self.assertEqual(obj['timestamp_field_name'], _UTC_DATETIME_OF_START_TIME)
def testSchemaResponseToDicts(self): schema = [{ 'type': 'INTEGER', 'name': 'field_name', 'mode': 'NULLABLE' }] new_schema = bigquery_helper._SchemaResponseToDicts(schema) self.assertEqual(new_schema[0]['name'], 'field_name') self.assertEqual(new_schema[0]['nullable'], True) self.assertTrue('type_conversion_function' in new_schema[0]) self.assertEqual(new_schema[0]['type_conversion_function']('1'), 1) schema = [{ 'type': 'INTEGER', 'name': 'field_name', 'mode': 'REPEATED' }] new_schema = bigquery_helper._SchemaResponseToDicts(schema) self.assertEqual( new_schema[0]['type_conversion_function']([{ 'v': '1' }, { 'v': '2' }], repeated=True), [1, 2]) schema = [{ 'type': 'BOOLEAN', 'name': 'field_name', 'mode': 'NONNULLABLE' }] new_schema = bigquery_helper._SchemaResponseToDicts(schema) self.assertEqual(new_schema[0]['name'], 'field_name') self.assertEqual(new_schema[0]['nullable'], False) self.assertTrue('type_conversion_function' in new_schema[0]) self.assertEqual(new_schema[0]['type_conversion_function']('true'), True) schema = [{ 'type': 'BOOLEAN', 'name': 'field_name', 'mode': 'REPEATED' }] new_schema = bigquery_helper._SchemaResponseToDicts(schema) self.assertEqual( new_schema[0]['type_conversion_function']([{ 'v': 'true' }, { 'v': 'false' }], repeated=True), [True, False]) schema = [{'type': 'STRING', 'name': 'field_name', 'mode': 'NULLABLE'}] new_schema = bigquery_helper._SchemaResponseToDicts(schema) self.assertEqual(new_schema[0]['name'], 'field_name') self.assertEqual(new_schema[0]['nullable'], True) self.assertTrue('type_conversion_function' in new_schema[0]) self.assertEqual(new_schema[0]['type_conversion_function']('str'), 'str') schema = [{'type': 'STRING', 'name': 'field_name', 'mode': 'REPEATED'}] new_schema = bigquery_helper._SchemaResponseToDicts(schema) self.assertEqual( new_schema[0]['type_conversion_function']([{ 'v': 'asdf1' }, { 'v': 'asdf2' }], repeated=True), ['asdf1', 'asdf2']) schema = [{ 'type': 'TIMESTAMP', 'name': 'field_name', 'mode': 'NULLABLE' }] new_schema = bigquery_helper._SchemaResponseToDicts(schema) self.assertEqual(new_schema[0]['name'], 'field_name') self.assertEqual(new_schema[0]['nullable'], True) self.assertTrue('type_conversion_function' in new_schema[0]) self.assertEqual( new_schema[0]['type_conversion_function']( _UTC_TIMESTAMP_OF_START_TIME), _UTC_DATETIME_OF_START_TIME) schema = [{ 'type': 'TIMESTAMP', 'name': 'field_name', 'mode': 'REPEATED' }] new_schema = bigquery_helper._SchemaResponseToDicts(schema) self.assertEqual( new_schema[0]['type_conversion_function']( [{ 'v': _UTC_TIMESTAMP_OF_START_TIME }, { 'v': _UTC_TIMESTAMP_OF_START_TIME }], repeated=True), [_UTC_DATETIME_OF_START_TIME, _UTC_DATETIME_OF_START_TIME])