-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_database_connection.py
More file actions
171 lines (135 loc) · 4.87 KB
/
Copy pathtest_database_connection.py
File metadata and controls
171 lines (135 loc) · 4.87 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
"""
Test script for database connection system
"""
import asyncio
import sys
import os
# Add the terradev_cli module to the path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'terradev_cli'))
from terradev_cli.core.database_connection import (
create_sqlite_connection,
create_postgresql_connection,
query_database,
upsert_database,
get_database_connection,
DatabaseConnectionManager,
DatabaseType,
DatabaseConnectionConfig
)
async def test_sqlite_connection():
"""Test SQLite connection creation and operations"""
print("Testing SQLite Connection...")
try:
# Create SQLite connection
connection_id = await create_sqlite_connection(
path="/tmp/test_terradev.db",
table_prefix="test_"
)
print(f"✓ Created SQLite connection: {connection_id}")
# Get connection info
conn_info = await get_database_connection(connection_id)
print(f"✓ Connection info: {conn_info}")
# Test query
results = await query_database(
connection_id,
"SELECT * FROM test_dataset_versions LIMIT 10"
)
print(f"✓ Query executed: {results}")
# Test upsert
success = await upsert_database(
connection_id,
"dataset_versions",
{"version_hash": "test123", "dataset_id": "test_dataset"},
conflict_columns=["version_hash"]
)
print(f"✓ Upsert executed: {success}")
print("✓ SQLite connection test passed\n")
return True
except Exception as e:
print(f"✗ SQLite connection test failed: {e}\n")
return False
async def test_postgresql_connection():
"""Test PostgreSQL connection creation and operations"""
print("Testing PostgreSQL Connection...")
try:
# Create PostgreSQL connection (with test credentials)
connection_id = await create_postgresql_connection(
host="localhost",
port=5432,
database="test_db",
user="test_user",
password="test_password",
table_prefix="test_"
)
print(f"✓ Created PostgreSQL connection: {connection_id}")
# Get connection info
conn_info = await get_database_connection(connection_id)
print(f"✓ Connection info: {conn_info}")
# Test query
results = await query_database(
connection_id,
"SELECT * FROM test_workflow_runs LIMIT 10"
)
print(f"✓ Query executed: {results}")
# Test upsert
success = await upsert_database(
connection_id,
"workflow_runs",
{"run_id": "test_run_123", "status": "completed"},
conflict_columns=["run_id"]
)
print(f"✓ Upsert executed: {success}")
print("✓ PostgreSQL connection test passed\n")
return True
except Exception as e:
print(f"✗ PostgreSQL connection test failed: {e}\n")
return False
async def test_connection_manager():
"""Test connection manager functionality"""
print("Testing Connection Manager...")
try:
manager = DatabaseConnectionManager()
# Test connection ID generation
conn_id_1 = manager.generate_connection_id()
conn_id_2 = manager.generate_connection_id()
print(f"✓ Generated connection IDs: {conn_id_1}, {conn_id_2}")
assert conn_id_1 != conn_id_2, "Connection IDs should be unique"
# Test connection config
config = DatabaseConnectionConfig(
database_type=DatabaseType.SQLITE,
connection_id=conn_id_1,
table_prefix="test_",
sqlite_path="/tmp/test_manager.db"
)
print(f"✓ Created connection config: {config.database_type}")
print("✓ Connection manager test passed\n")
return True
except Exception as e:
print(f"✗ Connection manager test failed: {e}\n")
return False
async def main():
"""Run all tests"""
print("=" * 50)
print("Database Connection System Tests")
print("=" * 50 + "\n")
results = []
# Test connection manager
results.append(await test_connection_manager())
# Test SQLite connection
results.append(await test_sqlite_connection())
# Test PostgreSQL connection
results.append(await test_postgresql_connection())
# Summary
print("=" * 50)
print(f"Test Results: {sum(results)}/{len(results)} passed")
print("=" * 50)
if all(results):
print("✓ All tests passed!")
return 0
else:
print("✗ Some tests failed")
return 1
if __name__ == "__main__":
exit_code = asyncio.run(main())
sys.exit(exit_code)