def test_create_table_already_exists(self): # type: () -> None node, leftover = apply_rule(statement_rule, tokenize( 'CREATE TABLE project.dataset.table (a int64, b string);')) self.assertFalse(leftover) table_context = DatasetTableContext({'project': {'dataset': {}}}) original_table = TypedDataFrame(pd.DataFrame([], columns=['x', 'y', 'z']), [BQScalarType.STRING, BQScalarType.INTEGER, BQScalarType.BOOLEAN]) table_context.set(('project', 'dataset', 'table'), original_table) assert isinstance(node, Statement) with self.assertRaisesRegexp(ValueError, 'Already Exists'): node.execute(table_context) return
def test_create_table_if_not_exists_and_it_does(self): # type: () -> None node, leftover = apply_rule(statement_rule, tokenize( 'CREATE TABLE IF NOT EXISTS project.dataset.table (a int64, b string);')) self.assertFalse(leftover) table_context = DatasetTableContext({'project': {'dataset': {}}}) original_table = TypedDataFrame(pd.DataFrame([], columns=['x', 'y', 'z']), [BQScalarType.STRING, BQScalarType.INTEGER, BQScalarType.BOOLEAN]) table_context.set(('project', 'dataset', 'table'), original_table) assert isinstance(node, Statement) result = node.execute(table_context) self.assertEqual(result.path, ('project', 'dataset', 'table')) table, unused_name = table_context.lookup(result.path) self.assertIs(table, original_table)
def test_create_table(self, statement, already_exists): # type: (str, bool) -> None node, leftover = apply_rule(statement_rule, tokenize(statement)) self.assertFalse(leftover) table_context = DatasetTableContext({'project': {'dataset': {}}}) original_table = TypedDataFrame(pd.DataFrame([], columns=['x', 'y', 'z']), [BQScalarType.STRING, BQScalarType.INTEGER, BQScalarType.BOOLEAN]) if already_exists: table_context.set(('project', 'dataset', 'table'), original_table) assert isinstance(node, Statement) result = node.execute(table_context) self.assertEqual(result.path, ('project', 'dataset', 'table')) table, unused_name = table_context.lookup(result.path) self.assertEqual(list(table.dataframe.columns), ['a', 'b']) self.assertEqual(table.types, [BQScalarType.INTEGER, BQScalarType.STRING])