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

23 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 Prisma 

5from prisma.models import Lists 

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 Lists WHERE `id` = ?', 

17) 

18 

19_postgresql_queries = Queries( 

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

21) 

22 

23RAW_QUERIES: DatabaseMapping[Queries] = { 

24 'mysql': _mysql_queries, 

25 'mariadb': _mysql_queries, 

26 'sqlite': Queries( 

27 select='SELECT * FROM Lists 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 usgae of String[] value in raw queries""" 

40 models = [ 

41 await client.lists.create({}), 

42 await client.lists.create( 

43 data={ 

44 'strings': ['a', 'b', 'c'], 

45 }, 

46 ), 

47 ] 

48 queries = RAW_QUERIES[database] 

49 

50 model = await client.query_first(queries.select, models[0].id, model=Lists) 

51 assert model is not None 

52 assert model.strings == [] 

53 

54 model = await client.query_first(queries.select, models[1].id, model=Lists) 

55 assert model is not None 

56 assert model.strings == ['a', 'b', 'c'] 

57 

58 found = await client.query_first(queries.select, models[1].id) 

59 assert found is not None 

60 assert found['strings'] == ['a', 'b', 'c'] 

61 

62 # checks what the behaviour is for empty arrays 

63 assert found['ints'] is None