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

49 statements  

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

1import pytest 

2 

3from prisma.errors import UnknownRelationalFieldError 

4from prisma.partials import ( 

5 UserOnlyName, 

6 PostWithAuthor, 

7 PostOnlyPublished, 

8 PostWithCustomAuthor, 

9) 

10 

11 

12class SubclassedPostOnlyPublished(PostOnlyPublished): 

13 @property 

14 def is_public(self) -> bool: 

15 return self.published 

16 

17 

18@pytest.mark.asyncio 

19@pytest.mark.prisma 

20async def test_scalar_fields() -> None: 

21 """Querying using a partial model that only has scalar fields""" 

22 post = await PostOnlyPublished.prisma().create( 

23 { 

24 'title': 'foo', 

25 } 

26 ) 

27 assert post.published is False 

28 assert not hasattr(post, 'title') 

29 

30 

31@pytest.mark.asyncio 

32@pytest.mark.prisma 

33async def test_relational_fields() -> None: 

34 """Querying using a partial model that has relational fields defined""" 

35 post = await PostWithAuthor.prisma().create( 

36 { 

37 'title': 'foo', 

38 'author': { 

39 'create': { 

40 'name': 'Robert', 

41 } 

42 }, 

43 } 

44 ) 

45 assert post.title == 'foo' 

46 assert post.author is None 

47 

48 post2 = await PostWithAuthor.prisma().find_first( 

49 where={ 

50 'id': post.id, 

51 }, 

52 include={ 

53 'author': True, 

54 }, 

55 ) 

56 assert post2 is not None 

57 assert post2.title == 'foo' 

58 assert post2.author is not None 

59 assert post2.author.name == 'Robert' 

60 

61 

62@pytest.mark.asyncio 

63@pytest.mark.prisma 

64async def test_include_missing_relational_field() -> None: 

65 """Specifying a relational field to include that isn't present on the partial model""" 

66 user = await UserOnlyName.prisma().create( 

67 { 

68 'name': 'Robert', 

69 'profile': { 

70 'create': { 

71 'description': "Robert's profile", 

72 'country': 'Scotland', 

73 } 

74 }, 

75 }, 

76 ) 

77 assert user.name == 'Robert' 

78 

79 with pytest.raises(UnknownRelationalFieldError) as exc: 

80 await UserOnlyName.prisma().find_first( 

81 where={'name': 'Robert'}, 

82 include={ 

83 'profile': True, 

84 }, 

85 ) 

86 

87 assert exc.match(r'Field: "profile" either does not exist or is not a relational field on the UserOnlyName model') 

88 

89 

90@pytest.mark.asyncio 

91@pytest.mark.prisma 

92async def test_custom_relational_fields() -> None: 

93 """Querying using a partial model that has custom relational fields defined""" 

94 post = await PostWithCustomAuthor.prisma().create( 

95 { 

96 'title': 'foo', 

97 'author': { 

98 'create': { 

99 'name': 'Robert', 

100 } 

101 }, 

102 } 

103 ) 

104 assert post.title == 'foo' 

105 assert post.author is None 

106 

107 post2 = await PostWithCustomAuthor.prisma().find_first( 

108 where={ 

109 'id': post.id, 

110 }, 

111 include={ 

112 'author': True, 

113 }, 

114 ) 

115 assert post2 is not None 

116 assert post2.title == 'foo' 

117 assert post2.author is not None 

118 assert post2.author.name == 'Robert' 

119 assert isinstance(post2.author, UserOnlyName) 

120 

121 

122@pytest.mark.asyncio 

123@pytest.mark.prisma 

124async def test_custom_model() -> None: 

125 """Querying using a partial model that has been subclassed""" 

126 post = await SubclassedPostOnlyPublished.prisma().create( 

127 { 

128 'title': 'foo', 

129 'published': True, 

130 } 

131 ) 

132 assert post.is_public