Coverage for databases/sync_tests/types/raw_queries/test_bigint.py: 100%

29 statements  

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

1from pydantic import BaseModel 

2 

3from prisma import Prisma 

4from prisma.models import Types 

5 

6from ...._types import DatabaseMapping, SupportedDatabase 

7from ...._compat import LiteralString 

8 

9 

10class Queries(BaseModel): 

11 select: LiteralString 

12 select_null: LiteralString 

13 

14 

15_mysql_queries = Queries( 

16 select='SELECT * FROM Types WHERE `bigint` = ?', 

17 select_null='SELECT * FROM Types WHERE `optional_bigint` IS NULL', 

18) 

19 

20_postgresql_queries = Queries( 

21 select='SELECT * FROM "Types" WHERE bigint = $1', 

22 select_null='SELECT * FROM "Types" WHERE optional_bigint IS NULL', 

23) 

24 

25RAW_QUERIES: DatabaseMapping[Queries] = { 

26 'mysql': _mysql_queries, 

27 'mariadb': _mysql_queries, 

28 'sqlite': Queries( 

29 select='SELECT * FROM Types WHERE bigint = ?', 

30 select_null='SELECT * FROM Types WHERE optional_bigint IS NULL', 

31 ), 

32 'postgresql': _postgresql_queries, 

33 'cockroachdb': _postgresql_queries, 

34} 

35 

36 

37def test_query_first( 

38 client: Prisma, 

39 database: SupportedDatabase, 

40) -> None: 

41 """Standard usage of BigInt in raw SELECT queries""" 

42 queries = RAW_QUERIES[database] 

43 

44 record = client.types.create({'bigint': 12522}) 

45 

46 found = client.query_first(queries.select, 12522) 

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

48 assert found['bigint'] == 12522 

49 

50 model = client.query_first(queries.select, 12522, model=Types) 

51 assert model is not None 

52 assert model.id == record.id 

53 assert model.bigint == 12522 

54 

55 

56def test_query_first_optional( 

57 client: Prisma, 

58 database: SupportedDatabase, 

59) -> None: 

60 """Use of BigInt in raw SELECT queries with optional/nullable results""" 

61 queries = RAW_QUERIES[database] 

62 

63 record = client.types.create({'optional_bigint': None}) 

64 

65 found = client.query_first(queries.select_null) 

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

67 assert found['optional_bigint'] is None 

68 

69 model = client.query_first(queries.select_null, model=Types) 

70 assert model is not None 

71 assert model.id == record.id 

72 assert model.optional_bigint is None