|
8 | 8 | from tests.conftest import DefaultPydanticModel, DefaultPythonModelClass |
9 | 9 |
|
10 | 10 | from psqlpy import ConnectionPool |
| 11 | +from psqlpy._internal.extra_types import PyCustomType |
11 | 12 | from psqlpy.extra_types import ( |
12 | 13 | BigInt, |
13 | 14 | Integer, |
@@ -425,3 +426,52 @@ class TopLevelModel(BaseModel): |
425 | 426 | ) |
426 | 427 |
|
427 | 428 | 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