Skip to content

Commit c372917

Browse files
Add error validation test cases
1 parent 51b5a53 commit c372917

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pybigquery/sqlalchemy_bigquery.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ def _split_table_name(full_table_name):
363363
dataset, table_name = table_name_split
364364
elif len(table_name_split) == 3:
365365
project, dataset, table_name = table_name_split
366-
# TODO: Get a test for an else statement here
366+
else:
367+
raise ValueError(f"Did not understand table_name: {full_table_name}")
367368

368369
return (project, dataset, table_name)
369370

@@ -384,7 +385,15 @@ def _table_reference(self, provided_schema_name, provided_table_name,
384385
elif len(provided_schema_name_split) == 2:
385386
project_id_from_schema = provided_schema_name_split[0]
386387
dataset_id_from_schema = provided_schema_name_split[1]
388+
else:
389+
raise ValueError(f"Did not understand schema: {provided_schema_name}")
387390
# TODO: Get a test for an else statement here
391+
if (dataset_id_from_schema and dataset_id_from_table and
392+
dataset_id_from_schema != dataset_id_from_table):
393+
raise ValueError(f"dataset_id specified in schema and table_name disagree: got {dataset_id_from_schema} in schema, and {dataset_id_from_table} in table_name")
394+
if (project_id_from_schema and project_id_from_table and
395+
project_id_from_schema != project_id_from_table):
396+
raise ValueError(f"project_id specified in schema and table_name disagree: got {project_id_from_schema} in schema, and {project_id_from_table} in table_name")
388397
project_id = project_id_from_schema or project_id_from_table or client_project
389398
dataset_id = dataset_id_from_schema or dataset_id_from_table or self.dataset_id
390399

test/test_sqlalchemy_bigquery.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,20 @@ def test_table_reference(dialect, provided_schema_name,
500500
assert ref.dataset_id == 'dataset'
501501
assert ref.project == 'project'
502502

503+
@pytest.mark.parametrize('provided_schema_name,provided_table_name,client_project',
504+
[
505+
('project.dataset', 'other_dataset.table', 'project'),
506+
('project.dataset', 'other_project.dataset.table', 'project'),
507+
('project.dataset.something_else', 'table', 'project'),
508+
(None, 'project.dataset.table.something_else', 'project'),
509+
])
510+
def test_invalid_table_reference(dialect, provided_schema_name,
511+
provided_table_name, client_project):
512+
with pytest.raises(ValueError):
513+
dialect._table_reference(provided_schema_name,
514+
provided_table_name,
515+
client_project)
516+
503517

504518
def test_has_table(engine, engine_using_test_dataset):
505519
assert engine.has_table('sample', 'test_pybigquery') is True

0 commit comments

Comments
 (0)