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

Commit b1d05b2

Browse files
author
Sung Won Chung
committed
draft tests
1 parent 979f5ec commit b1d05b2

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

data_diff/databases/duckdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ def select_table_schema(self, path: DbPath) -> str:
170170

171171
if database:
172172
info_schema_path.insert(0, database)
173-
dynamic_database_clause = f"'{database}'"
173+
dynamic_database_clause = f"'{database}'"
174174
else:
175175
dynamic_database_clause = "current_catalog()"
176176

tests/test_duckdb.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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)
43+
44+
45+
if __name__ == "__main__":
46+
unittest.main()

0 commit comments

Comments
 (0)