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

21 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 Lists 

5 

6from ....._types import DatabaseMapping, SupportedDatabase 

7from ....._compat import LiteralString 

8 

9 

10class Queries(BaseModel): 

11 select: LiteralString 

12 

13 

14_mysql_queries = Queries( 

15 select='SELECT * FROM Lists WHERE `id` = ?', 

16) 

17 

18_postgresql_queries = Queries( 

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

20) 

21 

22RAW_QUERIES: DatabaseMapping[Queries] = { 

23 'mysql': _mysql_queries, 

24 'mariadb': _mysql_queries, 

25 'sqlite': Queries( 

26 select='SELECT * FROM Lists WHERE id = ?', 

27 ), 

28 'postgresql': _postgresql_queries, 

29 'cockroachdb': _postgresql_queries, 

30} 

31 

32 

33def test_query_first( 

34 client: Prisma, 

35 database: SupportedDatabase, 

36) -> None: 

37 """Standard usgae of String[] value in raw queries""" 

38 models = [ 

39 client.lists.create({}), 

40 client.lists.create( 

41 data={ 

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

43 }, 

44 ), 

45 ] 

46 queries = RAW_QUERIES[database] 

47 

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

49 assert model is not None 

50 assert model.strings == [] 

51 

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

53 assert model is not None 

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

55 

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

57 assert found is not None 

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

59 

60 # checks what the behaviour is for empty arrays 

61 assert found['ints'] is None