Coverage for databases/tests/test_update_many.py: 100%

39 statements  

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

1import pytest 

2 

3from prisma import Prisma 

4 

5 

6@pytest.mark.asyncio 

7async def test_update_many(client: Prisma) -> None: 

8 """Filters update correct records 

9 

10 TODO: refactor this test, its a messs 

11 """ 

12 posts = [ 

13 await client.post.create({'title': 'Test post 1', 'published': False}), 

14 await client.post.create({'title': 'Test post 2', 'published': False}), 

15 ] 

16 count = await client.post.update_many(where={'published': False}, data={'published': True}) 

17 assert count == 2 

18 

19 post = await client.post.find_unique(where={'id': posts[0].id}) 

20 assert post is not None 

21 assert post.published is True 

22 

23 count = await client.post.update_many(where={'published': False}, data={'published': True}) 

24 assert count == 0 

25 

26 count = await client.post.update_many(where={'id': posts[0].id}, data={'published': False}) 

27 assert count == 1 

28 

29 post = await client.post.find_unique(where={'id': posts[0].id}) 

30 assert post is not None 

31 assert post.published is False 

32 

33 count = await client.post.update_many(where={'published': False}, data={'views': 10}) 

34 assert count == 1 

35 

36 post = await client.post.find_unique(where={'id': posts[0].id}) 

37 assert post is not None 

38 assert post.views == 10 

39 

40 count = await client.post.update_many(where={'id': posts[0].id}, data={'id': 'sdhajd'}) 

41 assert count == 1 

42 

43 post = await client.post.find_unique(where={'id': 'sdhajd'}) 

44 assert post is not None 

45 

46 post = await client.post.find_unique(where={'id': posts[0].id}) 

47 assert post is None 

48 

49 

50@pytest.mark.asyncio 

51async def test_setting_to_null(client: Prisma) -> None: 

52 """Setting a field to None sets the database record to None""" 

53 post = await client.post.create( 

54 data={ 

55 'title': 'Foo', 

56 'published': True, 

57 'description': 'Description', 

58 } 

59 ) 

60 assert post.description == 'Description' 

61 

62 count = await client.post.update_many( 

63 where={}, 

64 data={'description': None}, 

65 ) 

66 assert count == 1 

67 

68 found = await client.post.find_unique(where={'id': post.id}) 

69 assert found is not None 

70 assert found.id == post.id 

71 assert found.title == 'Foo' 

72 assert found.description is None