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

21 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2024-04-28 15:17 +0000

1import pytest 

2from pydantic import BaseModel 

3 

4from prisma import Base64, 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 

14 

15_mysql_queries = Queries( 

16 select='SELECT * FROM Types WHERE id = ?', 

17) 

18 

19_postgresql_queries = Queries( 

20 select='SELECT * FROM "Types" WHERE id = $1', 

21) 

22 

23RAW_QUERIES: DatabaseMapping[Queries] = { 

24 'mysql': _mysql_queries, 

25 'mariadb': _mysql_queries, 

26 'sqlite': Queries( 

27 select='SELECT * FROM Types WHERE id = ?', 

28 ), 

29 'postgresql': _postgresql_queries, 

30 'cockroachdb': _postgresql_queries, 

31} 

32 

33 

34@pytest.mark.asyncio 

35async def test_query_first( 

36 client: Prisma, 

37 database: SupportedDatabase, 

38) -> None: 

39 """Standard usage of Bytes in raw SELECT queries""" 

40 queries = RAW_QUERIES[database] 

41 

42 record = await client.types.create({'bytes': Base64.encode(b'foo')}) 

43 

44 found = await client.query_first(queries.select, record.id) 

45 assert found is not None 

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

47 assert found['bytes'] == 'Zm9v' 

48 

49 model = await client.query_first(queries.select, record.id, model=Types) 

50 assert model is not None 

51 assert model.id == record.id 

52 assert model.bytes.decode_str() == 'foo'