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

36 statements  

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

1from prisma import Prisma 

2 

3 

4def test_update_many(client: Prisma) -> None: 

5 """Filters update correct records 

6 

7 TODO: refactor this test, its a messs 

8 """ 

9 posts = [ 

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

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

12 ] 

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

14 assert count == 2 

15 

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

17 assert post is not None 

18 assert post.published is True 

19 

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

21 assert count == 0 

22 

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

24 assert count == 1 

25 

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

27 assert post is not None 

28 assert post.published is False 

29 

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

31 assert count == 1 

32 

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

34 assert post is not None 

35 assert post.views == 10 

36 

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

38 assert count == 1 

39 

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

41 assert post is not None 

42 

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

44 assert post is None 

45 

46 

47def test_setting_to_null(client: Prisma) -> None: 

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

49 post = client.post.create( 

50 data={ 

51 'title': 'Foo', 

52 'published': True, 

53 'description': 'Description', 

54 } 

55 ) 

56 assert post.description == 'Description' 

57 

58 count = client.post.update_many( 

59 where={}, 

60 data={'description': None}, 

61 ) 

62 assert count == 1 

63 

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

65 assert found is not None 

66 assert found.id == post.id 

67 assert found.title == 'Foo' 

68 assert found.description is None