Coverage for tests/test_actions.py: 100%

17 statements  

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

1import pytest 

2 

3from prisma.models import User 

4 

5 

6@pytest.mark.prisma 

7@pytest.mark.asyncio 

8async def test_include_many_order_by() -> None: 

9 """Including a 1-M relation and ordering it works 

10 

11 https://github.com/RobertCraigie/prisma-client-py/issues/234 

12 """ 

13 user = await User.prisma().create( 

14 data={ 

15 'name': 'Robert', 

16 'posts': { 

17 'create': [ 

18 { 

19 'title': 'Post 1', 

20 'published': True, 

21 }, 

22 { 

23 'title': 'Post 2', 

24 'published': False, 

25 }, 

26 ] 

27 }, 

28 }, 

29 include={ 

30 'posts': { 

31 'order_by': { 

32 'title': 'asc', 

33 }, 

34 }, 

35 }, 

36 ) 

37 assert user.name == 'Robert' 

38 assert user.posts is not None 

39 assert len(user.posts) == 2 

40 assert user.posts[0].title == 'Post 1' 

41 assert user.posts[1].title == 'Post 2' 

42 

43 user2 = await User.prisma().find_unique( 

44 where={ 

45 'id': user.id, 

46 }, 

47 include={ 

48 'posts': { 

49 'order_by': { 

50 'title': 'desc', 

51 }, 

52 }, 

53 }, 

54 ) 

55 assert user2 is not None 

56 assert user2.posts is not None 

57 assert len(user2.posts) == 2 

58 assert user2.posts[0].title == 'Post 2' 

59 assert user2.posts[1].title == 'Post 1'