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

1from decimal import Decimal 

2 

3from pydantic import BaseModel 

4 

5from prisma import Prisma 

6from prisma.models import Types 

7 

8from ...._types import DatabaseMapping, SupportedDatabase 

9from ...._compat import LiteralString 

10 

11 

12class Queries(BaseModel): 

13 select: LiteralString 

14 select_null: LiteralString 

15 

16 

17_mysql_queries = Queries( 

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

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

20) 

21 

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) 

26 

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} 

37 

38 

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] 

45 

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

47 

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

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

50 assert found['decimal_'] == 1 

51 

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

53 

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

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

56 assert found['decimal_'] == 1.24343336464224 

57 

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') 

62 

63 

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] 

70 

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

72 

73 found = client.query_first(queries.select_null) 

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

75 assert found['optional_decimal'] is None 

76 

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