Coverage for databases/tests/types/raw_queries/test_datetime.py: 100%
22 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1import datetime
3import pytest
4from pydantic import BaseModel
6from prisma import Prisma
7from prisma.models import Types
9from ...._types import DatabaseMapping, SupportedDatabase
10from ...._compat import LiteralString
13class Queries(BaseModel):
14 select: LiteralString
17_mysql_queries = Queries(
18 select='SELECT * FROM Types WHERE `id` = ?',
19)
21_postgresql_queries = Queries(
22 select='SELECT * FROM "Types" WHERE id = $1',
23)
25RAW_QUERIES: DatabaseMapping[Queries] = {
26 'mysql': _mysql_queries,
27 'mariadb': _mysql_queries,
28 'sqlite': Queries(
29 select='SELECT * FROM Types WHERE id = ?',
30 ),
31 'postgresql': _postgresql_queries,
32 'cockroachdb': _postgresql_queries,
33}
36@pytest.mark.asyncio
37async def test_query_first(
38 client: Prisma,
39 database: SupportedDatabase,
40) -> None:
41 """Standard usage of a DateTime field in raw SELECT queries"""
42 queries = RAW_QUERIES[database]
44 datetime_ = datetime.datetime(
45 year=2022,
46 month=12,
47 day=25,
48 hour=12,
49 minute=1,
50 second=34,
51 tzinfo=datetime.timezone.utc,
52 )
54 record = await client.types.create({'datetime_': datetime_})
56 found = await client.query_first(queries.select, record.id)
57 assert found['id'] == record.id
58 assert found['datetime_'] == '2022-12-25T12:01:34+00:00'
60 model = await client.query_first(queries.select, record.id, model=Types)
61 assert model is not None
62 assert model.id == record.id
63 assert model.datetime_ == datetime_