Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit c09f9cf

Browse files
author
Sung Won Chung
committed
Add validation for input path in select_table_schema method
1 parent b1d05b2 commit c09f9cf

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

data_diff/databases/duckdb.py

+5
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ def create_connection(self):
166166
def select_table_schema(self, path: DbPath) -> str:
167167
database, schema, table = self._normalize_table_path(path)
168168

169+
# If path only contains one object, raise an error
170+
if len(path) == 1:
171+
raise ValueError('The input path needs to have more than one object in your data diff configuration.\nExpected format: database.schema.table or schema.table')
172+
169173
info_schema_path = ["information_schema", "columns"]
170174

171175
if database:
@@ -179,6 +183,7 @@ def select_table_schema(self, path: DbPath) -> str:
179183
f"WHERE table_name = '{table}' AND table_schema = '{schema}' and table_catalog = {dynamic_database_clause}"
180184
)
181185

186+
182187
def _normalize_table_path(self, path: DbPath) -> DbPath:
183188
if len(path) == 1:
184189
return None, self.default_schema, path[0]

tests/test_duckdb.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def setUp(self):
1212
self.duckdb_conn = duckdb_differ.DuckDB(filepath=test_duckdb_filepath)
1313

1414
def tearDown(self):
15-
# Optional: delete file after tests
1615
os.remove(test_duckdb_filepath)
1716

1817
def test_normalize_table_path(self):
@@ -29,9 +28,16 @@ def test_normalize_table_path(self):
2928
self.duckdb_conn._normalize_table_path(("test_database", "test_schema", "test_table", "extra"))
3029

3130
def test_select_table_schema(self):
32-
db_path = ("test_table",)
33-
expected_sql = "SELECT column_name, data_type, datetime_precision, numeric_precision, numeric_scale FROM information_schema.columns WHERE table_name = 'test_table' AND table_schema = 'main' and table_catalog = current_catalog()"
34-
self.assertEqual(self.duckdb_conn.select_table_schema(db_path), expected_sql)
31+
with self.assertRaises(ValueError) as context:
32+
# Try to call the select_table_schema with only one value in the tuple
33+
db_path = ("test_table",)
34+
self.duckdb_conn.select_table_schema(db_path)
35+
36+
# Check that the message in the ValueError is what you expect
37+
self.assertTrue(
38+
"The input path needs to have more than one object in your data diff configuration.\nExpected format: database.schema.table or schema.table"
39+
in str(context.exception)
40+
)
3541

3642
db_path = ("custom_schema", "test_table")
3743
expected_sql = "SELECT column_name, data_type, datetime_precision, numeric_precision, numeric_scale FROM information_schema.columns WHERE table_name = 'test_table' AND table_schema = 'custom_schema' and table_catalog = current_catalog()"
@@ -40,7 +46,3 @@ def test_select_table_schema(self):
4046
db_path = ("custom_db", "custom_schema", "test_table")
4147
expected_sql = "SELECT column_name, data_type, datetime_precision, numeric_precision, numeric_scale FROM custom_db.information_schema.columns WHERE table_name = 'test_table' AND table_schema = 'custom_schema' and table_catalog = 'custom_db'"
4248
self.assertEqual(self.duckdb_conn.select_table_schema(db_path), expected_sql)
43-
44-
45-
if __name__ == "__main__":
46-
unittest.main()

0 commit comments

Comments
 (0)