|
| 1 | +import unittest |
| 2 | +from data_diff.databases import duckdb as duckdb_differ |
| 3 | +import os |
| 4 | +import uuid |
| 5 | + |
| 6 | +test_duckdb_filepath = str(uuid.uuid4()) + ".duckdb" |
| 7 | + |
| 8 | + |
| 9 | +class TestDuckDBTableSchemaMethods(unittest.TestCase): |
| 10 | + def setUp(self): |
| 11 | + # Create a new duckdb file |
| 12 | + self.duckdb_conn = duckdb_differ.DuckDB(filepath=test_duckdb_filepath) |
| 13 | + |
| 14 | + def tearDown(self): |
| 15 | + # Optional: delete file after tests |
| 16 | + os.remove(test_duckdb_filepath) |
| 17 | + |
| 18 | + def test_normalize_table_path(self): |
| 19 | + self.assertEqual(self.duckdb_conn._normalize_table_path(("test_table",)), (None, "main", "test_table")) |
| 20 | + self.assertEqual( |
| 21 | + self.duckdb_conn._normalize_table_path(("test_schema", "test_table")), (None, "test_schema", "test_table") |
| 22 | + ) |
| 23 | + self.assertEqual( |
| 24 | + self.duckdb_conn._normalize_table_path(("test_database", "test_schema", "test_table")), |
| 25 | + ("test_database", "test_schema", "test_table"), |
| 26 | + ) |
| 27 | + |
| 28 | + with self.assertRaises(ValueError): |
| 29 | + self.duckdb_conn._normalize_table_path(("test_database", "test_schema", "test_table", "extra")) |
| 30 | + |
| 31 | + 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) |
| 35 | + |
| 36 | + db_path = ("custom_schema", "test_table") |
| 37 | + 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()" |
| 38 | + self.assertEqual(self.duckdb_conn.select_table_schema(db_path), expected_sql) |
| 39 | + |
| 40 | + db_path = ("custom_db", "custom_schema", "test_table") |
| 41 | + 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'" |
| 42 | + self.assertEqual(self.duckdb_conn.select_table_schema(db_path), expected_sql) |
0 commit comments