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

79 statements  

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

1import pytest 

2 

3import prisma 

4from prisma import Prisma 

5from lib.testing import async_fixture 

6from prisma.models import User, Types, Unique2 

7 

8from ..utils import CURRENT_DATABASE 

9 

10 

11@async_fixture(name='user_id') 

12def user_id_fixture(client: Prisma) -> str: 

13 user = client.user.create({'name': 'Robert'}) 

14 return user.id 

15 

16 

17def test_update(client: Prisma) -> None: 

18 """Standard usage""" 

19 post = client.post.create( 

20 { 

21 'title': 'Hi from Create!', 

22 'published': True, 

23 'description': 'Prisma is a database toolkit that makes databases easy.', 

24 'author': { 

25 'create': { 

26 'name': 'Bob', 

27 }, 

28 }, 

29 } 

30 ) 

31 assert post.author is None 

32 assert post.title == 'Hi from Create!' 

33 updated = client.post.update( 

34 where={ 

35 'id': post.id, 

36 }, 

37 data={ 

38 'title': 'Hi from Update!', 

39 }, 

40 ) 

41 assert updated is not None 

42 assert updated.title == 'Hi from Update!' 

43 assert updated.updated_at != post.updated_at 

44 assert updated.created_at == post.created_at 

45 

46 updated = client.post.update( 

47 where={ 

48 'id': post.id, 

49 }, 

50 include={ 

51 'author': True, 

52 }, 

53 data={ 

54 'published': False, 

55 'description': 'Updated desc.', 

56 }, 

57 ) 

58 assert updated is not None 

59 assert updated.published is False 

60 assert updated.description == 'Updated desc.' 

61 assert updated.author is not None 

62 assert updated.author.name == 'Bob' 

63 

64 

65@pytest.mark.parametrize('method', ['disconnect', 'delete']) 

66def test_update_with_create_disconnect(client: Prisma, user_id: str, method: str) -> None: 

67 """Removing a relational field""" 

68 user = client.user.find_unique(where={'id': user_id}, include={'posts': True}) 

69 assert user is not None 

70 assert user.posts is not None 

71 assert len(user.posts) == 0 

72 

73 updated = client.user.update( 

74 where={'id': user_id}, 

75 data={'posts': {'create': [{'title': 'My post', 'published': True}]}}, 

76 include={'posts': True}, 

77 ) 

78 assert updated is not None 

79 assert updated.posts is not None 

80 assert len(updated.posts) == 1 

81 

82 post_id = updated.posts[0].id 

83 

84 if method == 'disconnect': 

85 updated = client.user.update( 

86 where={'id': user_id}, 

87 data={ 

88 'posts': { 

89 'disconnect': [ 

90 {'id': post_id}, 

91 ] 

92 } 

93 }, 

94 include={'posts': True}, 

95 ) 

96 else: 

97 updated = client.user.update( 

98 where={'id': user_id}, 

99 data={ 

100 'posts': { 

101 'delete': [ 

102 {'id': post_id}, 

103 ], 

104 } 

105 }, 

106 include={'posts': True}, 

107 ) 

108 

109 assert updated is not None 

110 assert updated.posts is not None 

111 assert len(updated.posts) == 0 

112 

113 

114def test_atomic_update(client: Prisma) -> None: 

115 """Atomically incrementing a value by 1""" 

116 post = client.post.create({'title': 'My Post', 'published': False}) 

117 assert post.title == 'My Post' 

118 assert post.views == 0 

119 

120 updated = client.post.update(where={'id': post.id}, data={'views': {'increment': 1}}) 

121 assert updated is not None 

122 assert updated.views == 1 

123 

124 

125def test_update_record_not_found(client: Prisma) -> None: 

126 """Updating a non-existent record returns None""" 

127 post = client.post.update(where={'id': 'wow'}, data={'title': 'Hi from Update!'}) 

128 assert post is None 

129 

130 

131def test_setting_field_to_null(client: Prisma) -> None: 

132 """Updating a field to None sets the database record to None""" 

133 post = client.post.create( 

134 data={ 

135 'title': 'Post', 

136 'published': False, 

137 'description': 'My description', 

138 }, 

139 ) 

140 assert post.description == 'My description' 

141 

142 updated = client.post.update( 

143 where={ 

144 'id': post.id, 

145 }, 

146 data={'description': None}, 

147 ) 

148 assert updated is not None 

149 assert updated.id == post.id 

150 assert updated.description is None 

151 

152 

153def test_setting_non_nullable_field_to_null(client: Prisma) -> None: 

154 """Attempting to set a non-nullable field to null raises an error""" 

155 post = client.post.create( 

156 data={ 

157 'title': 'Post', 

158 'published': False, 

159 }, 

160 ) 

161 assert post.published is False 

162 

163 with pytest.raises(prisma.errors.MissingRequiredValueError) as exc: 

164 client.post.update( 

165 where={ 

166 'id': post.id, 

167 }, 

168 data={'published': None}, # type: ignore 

169 ) 

170 

171 assert exc.match(r'published') 

172 

173 

174@pytest.mark.prisma 

175def test_update_id_field() -> None: 

176 """Setting an ID field""" 

177 user = User.prisma().create( 

178 data={ 

179 'name': 'Robert', 

180 }, 

181 ) 

182 updated = User.prisma().update( 

183 where={ 

184 'id': user.id, 

185 }, 

186 data={ 

187 'id': 'abcd123', 

188 }, 

189 ) 

190 assert updated is not None 

191 assert updated.id == 'abcd123' 

192 

193 

194@pytest.mark.prisma 

195@pytest.mark.skipif( 

196 CURRENT_DATABASE == 'cockroachdb', 

197 reason='https://github.com/prisma/prisma/issues/16612', 

198) 

199def test_update_id_field_atomic() -> None: 

200 """Setting an ID field atomically""" 

201 record = Types.prisma().create({}) 

202 updated = Types.prisma().update( 

203 where={ 

204 'id': record.id, 

205 }, 

206 data={ 

207 'id': { 

208 'increment': 500, 

209 }, 

210 }, 

211 ) 

212 assert updated is not None 

213 assert updated.id == record.id + 500 

214 

215 

216@pytest.mark.prisma 

217def test_update_unique_field() -> None: 

218 """Setting a unique field""" 

219 record = Unique2.prisma().create( 

220 data={ 

221 'name': 'Robert', 

222 'surname': 'Craigie', 

223 } 

224 ) 

225 

226 updated = Unique2.prisma().update( 

227 where={ 

228 'surname': record.surname, 

229 }, 

230 data={ 

231 'surname': 'George', 

232 }, 

233 ) 

234 assert updated is not None 

235 assert updated.name == record.name 

236 assert updated.surname == 'George'