Coverage for databases/sync_tests/types/raw_queries/test_decimal.py: 100%
34 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
1from decimal import Decimal
3from pydantic import BaseModel
5from prisma import Prisma
6from prisma.models import Types
8from ...._types import DatabaseMapping, SupportedDatabase
9from ...._compat import LiteralString
12class Queries(BaseModel):
13 select: LiteralString
14 select_null: LiteralString
17_mysql_queries = Queries(
18 select='SELECT * FROM Types WHERE decimal_ = ?',
19 select_null='SELECT * FROM Types WHERE optional_decimal IS NULL',
20)
22_postgresql_queries = Queries(
23 select='SELECT * FROM "Types" WHERE decimal_ = $1::numeric',
24 select_null='SELECT * FROM "Types" WHERE optional_decimal IS NULL',
25)
27RAW_QUERIES: DatabaseMapping[Queries] = {
28 'mysql': _mysql_queries,
29 'mariadb': _mysql_queries,
30 'sqlite': Queries(
31 select='SELECT * FROM Types WHERE decimal_ = ?',
32 select_null='SELECT * FROM Types WHERE optional_decimal IS NULL',
33 ),
34 'postgresql': _postgresql_queries,
35 'cockroachdb': _postgresql_queries,
36}
39def test_query_first(
40 client: Prisma,
41 database: SupportedDatabase,
42) -> None:
43 """Standard usage of decimal_ in raw SELECT queries"""
44 queries = RAW_QUERIES[database]
46 record = client.types.create({'decimal_': Decimal(1)})
48 found = client.query_first(queries.select, Decimal(1))
49 assert found['id'] == record.id
50 assert found['decimal_'] == 1
52 record2 = client.types.create({'decimal_': Decimal('1.24343336464224')})
54 found = client.query_first(queries.select, Decimal('1.24343336464224'))
55 assert found['id'] == record2.id
56 assert found['decimal_'] == 1.24343336464224
58 model = client.query_first(queries.select, Decimal('1.24343336464224'), model=Types)
59 assert model is not None
60 assert model.id == record2.id
61 assert model.decimal_ == Decimal('1.24343336464224')
64def test_query_first_optional(
65 client: Prisma,
66 database: SupportedDatabase,
67) -> None:
68 """Use of decimal in raw SELECT queries with optional/nullable results"""
69 queries = RAW_QUERIES[database]
71 record = client.types.create({'optional_decimal': None})
73 found = client.query_first(queries.select_null)
74 assert found['id'] == record.id
75 assert found['optional_decimal'] is None
77 model = client.query_first(queries.select_null, model=Types)
78 assert model is not None
79 assert model.id == record.id
80 assert model.optional_decimal is None