This repository was archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 281
/
Copy pathtest_duckdb.py
42 lines (33 loc) · 2.23 KB
/
test_duckdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import unittest
from data_diff.databases import duckdb as duckdb_differ
import os
import uuid
test_duckdb_filepath = str(uuid.uuid4()) + ".duckdb"
class TestDuckDBTableSchemaMethods(unittest.TestCase):
def setUp(self):
# Create a new duckdb file
self.duckdb_conn = duckdb_differ.DuckDB(filepath=test_duckdb_filepath)
def tearDown(self):
# Optional: delete file after tests
os.remove(test_duckdb_filepath)
def test_normalize_table_path(self):
self.assertEqual(self.duckdb_conn._normalize_table_path(("test_table",)), (None, "main", "test_table"))
self.assertEqual(
self.duckdb_conn._normalize_table_path(("test_schema", "test_table")), (None, "test_schema", "test_table")
)
self.assertEqual(
self.duckdb_conn._normalize_table_path(("test_database", "test_schema", "test_table")),
("test_database", "test_schema", "test_table"),
)
with self.assertRaises(ValueError):
self.duckdb_conn._normalize_table_path(("test_database", "test_schema", "test_table", "extra"))
def test_select_table_schema(self):
db_path = ("test_table",)
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()"
self.assertEqual(self.duckdb_conn.select_table_schema(db_path), expected_sql)
db_path = ("custom_schema", "test_table")
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()"
self.assertEqual(self.duckdb_conn.select_table_schema(db_path), expected_sql)
db_path = ("custom_db", "custom_schema", "test_table")
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'"
self.assertEqual(self.duckdb_conn.select_table_schema(db_path), expected_sql)