Skip to content

Commit 23bc3d0

Browse files
committed
Added tests for custom decoder and encoder
Signed-off-by: chandr-andr (Kiselev Aleksandr) <[email protected]>
1 parent d2a63c5 commit 23bc3d0

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

python/psqlpy/_internal/__init__.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class QueryResult:
1313

1414
def result(
1515
self: Self,
16-
custom_decoders: dict[str, Callable[[list[int]], Any]] | None = None,
16+
custom_decoders: dict[str, Callable[[bytes], Any]] | None = None,
1717
) -> list[dict[Any, Any]]:
1818
"""Return result from database as a list of dicts."""
1919
def as_class(

python/tests/test_value_converter.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from tests.conftest import DefaultPydanticModel, DefaultPythonModelClass
99

1010
from psqlpy import ConnectionPool
11+
from psqlpy._internal.extra_types import PyCustomType
1112
from psqlpy.extra_types import (
1213
BigInt,
1314
Integer,
@@ -425,3 +426,52 @@ class TopLevelModel(BaseModel):
425426
)
426427

427428
assert isinstance(model_result[0], TopLevelModel)
429+
430+
431+
async def test_custom_type_as_parameter(
432+
psql_pool: ConnectionPool,
433+
) -> None:
434+
"""Tests that we can use `PyCustomType`."""
435+
await psql_pool.execute("DROP TABLE IF EXISTS for_test")
436+
await psql_pool.execute(
437+
"CREATE TABLE for_test (nickname VARCHAR)",
438+
)
439+
440+
await psql_pool.execute(
441+
querystring="INSERT INTO for_test VALUES ($1)",
442+
parameters=[PyCustomType(b"Some Real Nickname")],
443+
)
444+
445+
qs_result = await psql_pool.execute(
446+
"SELECT * FROM for_test",
447+
)
448+
449+
result = qs_result.result()
450+
assert result[0]["nickname"] == "Some Real Nickname"
451+
452+
453+
async def test_custom_decoder(
454+
psql_pool: ConnectionPool,
455+
) -> None:
456+
await psql_pool.execute("DROP TABLE IF EXISTS for_test")
457+
await psql_pool.execute(
458+
"CREATE TABLE for_test (geo_point POINT)",
459+
)
460+
461+
await psql_pool.execute(
462+
"INSERT INTO for_test VALUES ('(1, 1)')",
463+
)
464+
465+
def point_encoder(point_bytes: bytes) -> str:
466+
return "Just An Example"
467+
468+
qs_result = await psql_pool.execute(
469+
"SELECT * FROM for_test",
470+
)
471+
result = qs_result.result(
472+
custom_decoders={
473+
"geo_point": point_encoder,
474+
},
475+
)
476+
477+
assert result[0]["geo_point"] == "Just An Example"

0 commit comments

Comments
 (0)