Coverage for databases/tests/types/raw_queries/test_decimal.py: 100%

37 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-08-27 18:25 +0000

1from decimal import Decimal 

2 

3import pytest 

4from pydantic import BaseModel 

5 

6from prisma import Prisma 

7from prisma.models import Types 

8 

9from ...._types import DatabaseMapping, SupportedDatabase 

10from ...._compat import LiteralString 

11 

12 

13class Queries(BaseModel): 

14 select: LiteralString 

15 select_null: LiteralString 

16 

17 

18_mysql_queries = Queries( 

19 select='SELECT * FROM Types WHERE decimal_ = ?', 

20 select_null='SELECT * FROM Types WHERE optional_decimal IS NULL', 

21) 

22 

23_postgresql_queries = Queries( 

24 select='SELECT * FROM "Types" WHERE decimal_ = $1::numeric', 

25 select_null='SELECT * FROM "Types" WHERE optional_decimal IS NULL', 

26) 

27 

28RAW_QUERIES: DatabaseMapping[Queries] = { 

29 'mysql': _mysql_queries, 

30 'mariadb': _mysql_queries, 

31 'sqlite': Queries( 

32 select='SELECT * FROM Types WHERE decimal_ = ?', 

33 select_null='SELECT * FROM Types WHERE optional_decimal IS NULL', 

34 ), 

35 'postgresql': _postgresql_queries, 

36 'cockroachdb': _postgresql_queries, 

37} 

38 

39 

40@pytest.mark.asyncio 

41async def test_query_first( 

42 client: Prisma, 

43 database: SupportedDatabase, 

44) -> None: 

45 """Standard usage of decimal_ in raw SELECT queries""" 

46 queries = RAW_QUERIES[database] 

47 

48 record = await client.types.create({'decimal_': Decimal(1)}) 

49 

50 found = await client.query_first(queries.select, Decimal(1)) 

51 assert found['id'] == record.id 

52 assert found['decimal_'] == 1 

53 

54 record2 = await client.types.create({'decimal_': Decimal('1.24343336464224')}) 

55 

56 found = await client.query_first(queries.select, Decimal('1.24343336464224')) 

57 assert found['id'] == record2.id 

58 assert found['decimal_'] == 1.24343336464224 

59 

60 model = await client.query_first(queries.select, Decimal('1.24343336464224'), model=Types) 

61 assert model is not None 

62 assert model.id == record2.id 

63 assert model.decimal_ == Decimal('1.24343336464224') 

64 

65 

66@pytest.mark.asyncio 

67async def test_query_first_optional( 

68 client: Prisma, 

69 database: SupportedDatabase, 

70) -> None: 

71 """Use of decimal in raw SELECT queries with optional/nullable results""" 

72 queries = RAW_QUERIES[database] 

73 

74 record = await client.types.create({'optional_decimal': None}) 

75 

76 found = await client.query_first(queries.select_null) 

77 assert found['id'] == record.id 

78 assert found['optional_decimal'] is None 

79 

80 model = await client.query_first(queries.select_null, model=Types) 

81 assert model is not None 

82 assert model.id == record.id 

83 assert model.optional_decimal is None