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

32 statements  

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

1import pytest 

2from pydantic import BaseModel 

3 

4from prisma import Prisma 

5from prisma.models import Types 

6 

7from ...._types import DatabaseMapping, SupportedDatabase 

8from ...._compat import LiteralString 

9 

10 

11class Queries(BaseModel): 

12 select: LiteralString 

13 select_null: LiteralString 

14 

15 

16_mysql_queries = Queries( 

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

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

19) 

20 

21_postgresql_queries = Queries( 

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

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

24) 

25 

26RAW_QUERIES: DatabaseMapping[Queries] = { 

27 'mysql': _mysql_queries, 

28 'mariadb': _mysql_queries, 

29 'sqlite': Queries( 

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

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

32 ), 

33 'postgresql': _postgresql_queries, 

34 'cockroachdb': _postgresql_queries, 

35} 

36 

37 

38@pytest.mark.asyncio 

39async def test_query_first( 

40 client: Prisma, 

41 database: SupportedDatabase, 

42) -> None: 

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

44 queries = RAW_QUERIES[database] 

45 

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

47 

48 found = await client.query_first(queries.select, 12522) 

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

50 assert found['bigint'] == 12522 

51 

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

53 assert model is not None 

54 assert model.id == record.id 

55 assert model.bigint == 12522 

56 

57 

58@pytest.mark.asyncio 

59async def test_query_first_optional( 

60 client: Prisma, 

61 database: SupportedDatabase, 

62) -> None: 

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

64 queries = RAW_QUERIES[database] 

65 

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

67 

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

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

70 assert found['optional_bigint'] is None 

71 

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

73 assert model is not None 

74 assert model.id == record.id 

75 assert model.optional_bigint is None