def test_row_as_table_row(self): schema_definition = [ ('s', 'STRING'), ('i', 'INTEGER'), ('f', 'FLOAT'), ('b', 'BOOLEAN'), ('r', 'RECORD')] data_defination = [ 'abc', 123, 123.456, True, {'a': 'b'}] str_def = '{"s": "abc", "i": 123, "f": 123.456, "b": true, "r": {"a": "b"}}' schema = bigquery.TableSchema( fields=[bigquery.TableFieldSchema(name=k, type=v) for k, v in schema_definition]) coder = TableRowJsonCoder(table_schema=schema) test_row = bigquery.TableRow( f=[bigquery.TableCell(v=to_json_value(e)) for e in data_defination]) self.assertEqual(str_def, coder.encode(test_row)) self.assertEqual(test_row, coder.decode(coder.encode(test_row))) # A coder without schema can still decode. self.assertEqual( test_row, TableRowJsonCoder().decode(coder.encode(test_row)))
def test_row_and_no_schema(self): coder = TableRowJsonCoder() test_row = bigquery.TableRow( f=[bigquery.TableCell(v=to_json_value(e)) for e in ['abc', 123, 123.456, True]]) with self.assertRaisesRegexp(AttributeError, r'^The TableRowJsonCoder requires'): coder.encode(test_row)
def test_row_and_no_schema(self): coder = TableRowJsonCoder() test_row = bigquery.TableRow( f=[bigquery.TableCell(v=to_json_value(e)) for e in ['abc', 123, 123.456, True]]) with self.assertRaisesRegexp(AttributeError, r'^The TableRowJsonCoder requires'): coder.encode(test_row)
def test_row_and_no_schema(self): coder = TableRowJsonCoder() test_row = bigquery.TableRow( f=[bigquery.TableCell(v=to_json_value(e)) for e in ['abc', 123, 123.456, True]]) with self.assertRaises(AttributeError) as ctx: coder.encode(test_row) self.assertTrue( ctx.exception.message.startswith('The TableRowJsonCoder requires'))
def json_compliance_exception(self, value): with self.assertRaisesRegexp(ValueError, re.escape(JSON_COMPLIANCE_ERROR)): schema_definition = [('f', 'FLOAT')] schema = bigquery.TableSchema( fields=[bigquery.TableFieldSchema(name=k, type=v) for k, v in schema_definition]) coder = TableRowJsonCoder(table_schema=schema) test_row = bigquery.TableRow( f=[bigquery.TableCell(v=to_json_value(value))]) coder.encode(test_row)
def json_compliance_exception(self, value): with self.assertRaisesRegexp(ValueError, re.escape(JSON_COMPLIANCE_ERROR)): schema_definition = [('f', 'FLOAT')] schema = bigquery.TableSchema( fields=[bigquery.TableFieldSchema(name=k, type=v) for k, v in schema_definition]) coder = TableRowJsonCoder(table_schema=schema) test_row = bigquery.TableRow( f=[bigquery.TableCell(v=to_json_value(value))]) coder.encode(test_row)
def test_row_and_no_schema(self): coder = TableRowJsonCoder() test_row = bigquery.TableRow(f=[ bigquery.TableCell(v=to_json_value(e)) for e in ['abc', 123, 123.456, True] ]) with self.assertRaises(AttributeError) as ctx: coder.encode(test_row) self.assertTrue( ctx.exception.message.startswith('The TableRowJsonCoder requires'))
def test_row_as_table_row(self): schema_definition = [('s', 'STRING'), ('i', 'INTEGER'), ('f', 'FLOAT'), ('b', 'BOOLEAN'), ('n', 'NUMERIC'), ('r', 'RECORD'), ('g', 'GEOGRAPHY')] data_definition = [ 'abc', 123, 123.456, True, decimal.Decimal('987654321.987654321'), { 'a': 'b' }, 'LINESTRING(1 2, 3 4, 5 6, 7 8)' ] str_def = ( '{"s": "abc", ' '"i": 123, ' '"f": 123.456, ' '"b": true, ' '"n": "987654321.987654321", ' '"r": {"a": "b"}, ' '"g": "LINESTRING(1 2, 3 4, 5 6, 7 8)"}') schema = bigquery.TableSchema( fields=[ bigquery.TableFieldSchema(name=k, type=v) for k, v in schema_definition ]) coder = TableRowJsonCoder(table_schema=schema) def value_or_decimal_to_json(val): if isinstance(val, decimal.Decimal): return to_json_value(str(val)) else: return to_json_value(val) test_row = bigquery.TableRow( f=[ bigquery.TableCell(v=value_or_decimal_to_json(e)) for e in data_definition ]) self.assertEqual(str_def, coder.encode(test_row)) self.assertEqual(test_row, coder.decode(coder.encode(test_row))) # A coder without schema can still decode. self.assertEqual( test_row, TableRowJsonCoder().decode(coder.encode(test_row)))
def test_row_as_table_row(self): schema_definition = [ ('s', 'STRING'), ('i', 'INTEGER'), ('f', 'FLOAT'), ('b', 'BOOLEAN'), ('n', 'NUMERIC'), ('r', 'RECORD'), ('g', 'GEOGRAPHY')] data_definition = [ 'abc', 123, 123.456, True, decimal.Decimal('987654321.987654321'), {'a': 'b'}, 'LINESTRING(1 2, 3 4, 5 6, 7 8)'] str_def = ('{"s": "abc", ' '"i": 123, ' '"f": 123.456, ' '"b": true, ' '"n": "987654321.987654321", ' '"r": {"a": "b"}, ' '"g": "LINESTRING(1 2, 3 4, 5 6, 7 8)"}') schema = bigquery.TableSchema( fields=[bigquery.TableFieldSchema(name=k, type=v) for k, v in schema_definition]) coder = TableRowJsonCoder(table_schema=schema) def value_or_decimal_to_json(val): if isinstance(val, decimal.Decimal): return to_json_value(str(val)) else: return to_json_value(val) test_row = bigquery.TableRow( f=[bigquery.TableCell(v=value_or_decimal_to_json(e)) for e in data_definition]) self.assertEqual(str_def, coder.encode(test_row)) self.assertEqual(test_row, coder.decode(coder.encode(test_row))) # A coder without schema can still decode. self.assertEqual( test_row, TableRowJsonCoder().decode(coder.encode(test_row)))
def test_row_as_table_row(self): schema_definition = [('s', 'STRING'), ('i', 'INTEGER'), ('f', 'FLOAT'), ('b', 'BOOLEAN'), ('r', 'RECORD')] data_defination = ['abc', 123, 123.456, True, {'a': 'b'}] str_def = '{"s": "abc", "i": 123, "f": 123.456, "b": true, "r": {"a": "b"}}' schema = bigquery.TableSchema(fields=[ bigquery.TableFieldSchema(name=k, type=v) for k, v in schema_definition ]) coder = TableRowJsonCoder(table_schema=schema) test_row = bigquery.TableRow(f=[ bigquery.TableCell(v=to_json_value(e)) for e in data_defination ]) self.assertEqual(str_def, coder.encode(test_row)) self.assertEqual(test_row, coder.decode(coder.encode(test_row))) # A coder without schema can still decode. self.assertEqual(test_row, TableRowJsonCoder().decode(coder.encode(test_row)))