Skip to content

Commit 431d68c

Browse files
committed
Added support for any custom python class in QueryResult
1 parent 3134679 commit 431d68c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

python/tests/conftest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33
from typing import AsyncGenerator
44

55
import pytest
6+
from pydantic import BaseModel
67

78
from psqlpy import Cursor, PSQLPool
89

910

11+
class DefaultPydanticModel(BaseModel):
12+
"""Validation model for test data based on Pydantic."""
13+
14+
id: int
15+
name: str
16+
17+
18+
class DefaultPythonModelClass:
19+
"""Validation model for test data based on default Python class."""
20+
21+
def __init__(self, id: int, name: str) -> None:
22+
self.id = id
23+
self.name = name
24+
25+
1026
@pytest.fixture()
1127
def anyio_backend() -> str:
1228
"""

python/tests/test_value_converter.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
from tests.conftest import DefaultPydanticModel, DefaultPythonModelClass
3+
4+
from psqlpy import PSQLPool
5+
6+
pytestmark = pytest.mark.anyio
7+
8+
9+
async def test_as_class(
10+
psql_pool: PSQLPool,
11+
table_name: str,
12+
number_database_records: int,
13+
) -> None:
14+
"""Test `as_class()` method."""
15+
select_result = await psql_pool.execute(
16+
f"SELECT * FROM {table_name}",
17+
)
18+
19+
as_pydantic = select_result.as_class(
20+
as_class=DefaultPydanticModel,
21+
)
22+
assert len(as_pydantic) == number_database_records
23+
24+
for single_record in as_pydantic:
25+
assert isinstance(single_record, DefaultPydanticModel)
26+
27+
as_py_class = select_result.as_class(
28+
as_class=DefaultPythonModelClass,
29+
)
30+
31+
assert len(as_py_class) == number_database_records
32+
33+
for single_py_record in as_py_class:
34+
assert isinstance(single_py_record, DefaultPythonModelClass)

0 commit comments

Comments
 (0)