Coverage for databases/sync_tests/test_partials.py: 100%
44 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-08-27 18:25 +0000
1import pytest
3from prisma.errors import UnknownRelationalFieldError
4from prisma.partials import (
5 UserOnlyName,
6 PostWithAuthor,
7 PostOnlyPublished,
8 PostWithCustomAuthor,
9)
12class SubclassedPostOnlyPublished(PostOnlyPublished):
13 @property
14 def is_public(self) -> bool:
15 return self.published
18@pytest.mark.prisma
19def test_scalar_fields() -> None:
20 """Querying using a partial model that only has scalar fields"""
21 post = PostOnlyPublished.prisma().create(
22 {
23 'title': 'foo',
24 }
25 )
26 assert post.published is False
27 assert not hasattr(post, 'title')
30@pytest.mark.prisma
31def test_relational_fields() -> None:
32 """Querying using a partial model that has relational fields defined"""
33 post = PostWithAuthor.prisma().create(
34 {
35 'title': 'foo',
36 'author': {
37 'create': {
38 'name': 'Robert',
39 }
40 },
41 }
42 )
43 assert post.title == 'foo'
44 assert post.author is None
46 post2 = PostWithAuthor.prisma().find_first(
47 where={
48 'id': post.id,
49 },
50 include={
51 'author': True,
52 },
53 )
54 assert post2 is not None
55 assert post2.title == 'foo'
56 assert post2.author is not None
57 assert post2.author.name == 'Robert'
60@pytest.mark.prisma
61def test_include_missing_relational_field() -> None:
62 """Specifying a relational field to include that isn't present on the partial model"""
63 user = UserOnlyName.prisma().create(
64 {
65 'name': 'Robert',
66 'profile': {
67 'create': {
68 'description': "Robert's profile",
69 'country': 'Scotland',
70 }
71 },
72 },
73 )
74 assert user.name == 'Robert'
76 with pytest.raises(UnknownRelationalFieldError) as exc:
77 UserOnlyName.prisma().find_first(
78 where={'name': 'Robert'},
79 include={
80 'profile': True,
81 },
82 )
84 assert exc.match(r'Field: "profile" either does not exist or is not a relational field on the UserOnlyName model')
87@pytest.mark.prisma
88def test_custom_relational_fields() -> None:
89 """Querying using a partial model that has custom relational fields defined"""
90 post = PostWithCustomAuthor.prisma().create(
91 {
92 'title': 'foo',
93 'author': {
94 'create': {
95 'name': 'Robert',
96 }
97 },
98 }
99 )
100 assert post.title == 'foo'
101 assert post.author is None
103 post2 = PostWithCustomAuthor.prisma().find_first(
104 where={
105 'id': post.id,
106 },
107 include={
108 'author': True,
109 },
110 )
111 assert post2 is not None
112 assert post2.title == 'foo'
113 assert post2.author is not None
114 assert post2.author.name == 'Robert'
115 assert isinstance(post2.author, UserOnlyName)
118@pytest.mark.prisma
119def test_custom_model() -> None:
120 """Querying using a partial model that has been subclassed"""
121 post = SubclassedPostOnlyPublished.prisma().create(
122 {
123 'title': 'foo',
124 'published': True,
125 }
126 )
127 assert post.is_public