Skip to content

Commit 7c9f889

Browse files
authored
Merge pull request #9 from GreptimeTeam/test/psycopg3
test: add psycopg3 tests
2 parents 2051f92 + 23e92af commit 7c9f889

3 files changed

Lines changed: 106 additions & 8 deletions

File tree

python-tests/pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ requires-python = ">=3.11"
55
dependencies = [
66
"mysql-connector-python>=8.0.33",
77
"psycopg2-binary>=2.9.9",
8+
"psycopg[binary]>=3.1.0",
89
"pytz>=2023.3",
910
"opentelemetry-api>=1.24.0",
1011
"opentelemetry-sdk>=1.24.0",

python-tests/tests/test_greptimedb_driver.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import mysql.connector
2626
import psycopg2
2727
import psycopg2.extras
28+
import psycopg
29+
from psycopg import sql
2830

2931
# Configure logging
3032
logging.basicConfig(
@@ -93,6 +95,29 @@ def connect(self, driver_type: str, timezone: Optional[str] = None):
9395
with self.conn.cursor() as cursor:
9496
cursor.execute(f"SET TIME ZONE = '{timezone}'")
9597
self.conn.commit()
98+
99+
elif driver_type == "postgresql3":
100+
host = self._get_env("POSTGRES_HOST", "127.0.0.1")
101+
port = int(self._get_env("POSTGRES_PORT", "4003"))
102+
db = self._get_env("DB_NAME", "public")
103+
104+
logger.info(
105+
f"Connecting to PostgreSQL (psycopg3): {host}:{port}/{db} (timezone={timezone})"
106+
)
107+
self.conn = psycopg.connect(
108+
host=host,
109+
port=port,
110+
dbname=db,
111+
user=username,
112+
password=password,
113+
)
114+
115+
if timezone:
116+
with self.conn.cursor() as cursor:
117+
cursor.execute(
118+
sql.SQL("SET TIME ZONE = {}").format(sql.Literal(timezone))
119+
)
120+
self.conn.commit()
96121
else:
97122
raise ValueError(f"Unknown driver: {driver_type}")
98123

@@ -146,15 +171,16 @@ def format_timestamp_as_utc(self, ts) -> str:
146171

147172
def get_cursor(self, prepared=False):
148173
"""Get cursor. Use prepared=True for MySQL parameterized queries."""
149-
return (
150-
self.conn.cursor(prepared=True)
151-
if self.driver == "mysql" and prepared
152-
else self.conn.cursor()
153-
)
174+
if self.driver == "mysql" and prepared:
175+
return self.conn.cursor(prepared=True)
176+
elif self.driver == "postgresql3":
177+
return self.conn.cursor()
178+
else:
179+
return self.conn.cursor()
154180

155181
def parse_binary_result(self, value, driver):
156182
"""Parse binary data. NOTE: PostgreSQL returns hex string format."""
157-
if driver == "postgresql":
183+
if driver in ("postgresql", "postgresql3"):
158184
if isinstance(value, memoryview):
159185
value = bytes(value)
160186
if isinstance(value, bytes):
@@ -176,7 +202,7 @@ def test_instance():
176202
instance.teardown()
177203

178204

179-
@pytest.mark.parametrize("driver", ["mysql", "postgresql"])
205+
@pytest.mark.parametrize("driver", ["mysql", "postgresql", "postgresql3"])
180206
def test_crud_operations(test_instance, driver):
181207
"""
182208
Test comprehensive CRUD operations on a single table with all supported GreptimeDB data types.
@@ -530,7 +556,7 @@ def test_timezone_insert_and_select(test_instance, driver):
530556
raise
531557

532558

533-
@pytest.mark.parametrize("driver", ["mysql", "postgresql"])
559+
@pytest.mark.parametrize("driver", ["mysql", "postgresql", "postgresql3"])
534560
def test_batch_insert(test_instance, driver):
535561
"""
536562
Test batch insert using executemany().

python-tests/uv.lock

Lines changed: 71 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)