Coverage for databases/tests/types/test_json.py: 100%

66 statements  

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

1import pytest 

2from dirty_equals import IsPartialDict 

3 

4from prisma import Json, Prisma 

5from prisma.models import Types 

6from prisma._compat import PYDANTIC_V2, model_json_schema 

7 

8 

9@pytest.mark.asyncio 

10async def test_create(client: Prisma) -> None: 

11 """Creating a model with Json data""" 

12 model = await client.types.create( 

13 data={ 

14 'json_obj': Json.keys(country='Scotland'), 

15 }, 

16 ) 

17 assert model.json_obj == {'country': 'Scotland'} 

18 

19 model = await client.types.create( 

20 data={ 

21 'json_obj': Json.keys(countries=['Scotland', 'United Kingdom']), 

22 }, 

23 ) 

24 assert model.json_obj == {'countries': ['Scotland', 'United Kingdom']} 

25 

26 model = await client.types.create( 

27 data={ 

28 'json_obj': Json(['Scotland']), 

29 }, 

30 ) 

31 assert model.json_obj == ['Scotland'] 

32 

33 model = await client.types.create( 

34 data={ 

35 'json_obj': Json('Scotland'), 

36 }, 

37 ) 

38 assert model.json_obj == 'Scotland' 

39 

40 model = await client.types.create( 

41 data={ 

42 'json_obj': Json(1), 

43 }, 

44 ) 

45 assert model.json_obj == 1 

46 

47 model = await client.types.create( 

48 data={ 

49 'json_obj': Json(None), 

50 }, 

51 ) 

52 assert model.json_obj is None 

53 

54 model = await client.types.create( 

55 data={ 

56 'json_obj': Json({'hello': None}), 

57 }, 

58 ) 

59 assert model.json_obj == {'hello': None} 

60 

61 model = await client.types.create( 

62 data={ 

63 'json_obj': Json(19.3273823), 

64 }, 

65 ) 

66 assert model.json_obj == 19.3273823 

67 

68 

69@pytest.mark.asyncio 

70async def test_keys(client: Prisma) -> None: 

71 """Handling of non-string keys""" 

72 model = await client.types.create( 

73 data={ 

74 'json_obj': Json({None: 'hello'}), 

75 }, 

76 ) 

77 assert model.json_obj is not None 

78 assert model.json_obj == {'null': 'hello'} 

79 assert model.json_obj['null'] == 'hello' 

80 

81 model = await client.types.create( 

82 data={ 

83 'json_obj': Json({True: 2}), 

84 }, 

85 ) 

86 assert model.json_obj is not None 

87 assert model.json_obj == {'true': 2} 

88 assert model.json_obj['true'] == 2 

89 

90 model = await client.types.create( 

91 data={ 

92 'json_obj': Json({1: 2}), 

93 }, 

94 ) 

95 assert model.json_obj is not None 

96 assert model.json_obj == {'1': 2} 

97 assert model.json_obj['1'] == 2 

98 

99 model = await client.types.create( 

100 data={ 

101 'json_obj': Json({3.1415: [1, 2]}), 

102 }, 

103 ) 

104 assert model.json_obj is not None 

105 assert model.json_obj == {'3.1415': [1, 2]} 

106 assert model.json_obj['3.1415'] == [1, 2] 

107 

108 

109@pytest.mark.asyncio 

110async def test_base_filtering(client: Prisma) -> None: 

111 """Searching for records by Json values without the preview feature enabled""" 

112 found = await client.types.find_first( 

113 where={ 

114 'json_obj': { 

115 'equals': Json.keys(country='Scotland'), 

116 }, 

117 }, 

118 ) 

119 assert found is None 

120 

121 model = await client.types.create( 

122 data={ 

123 'json_obj': Json.keys(country='Scotland'), 

124 }, 

125 ) 

126 assert model.json_obj == {'country': 'Scotland'} 

127 

128 found = await client.types.find_first( 

129 where={ 

130 'json_obj': { 

131 'equals': Json.keys(country='Scotland'), 

132 }, 

133 }, 

134 ) 

135 assert found is not None 

136 assert found.id == model.id 

137 assert found.json_obj == {'country': 'Scotland'} 

138 

139 found = await client.types.find_first( 

140 where={ 

141 'json_obj': { 

142 'not': Json.keys(country='Scotland'), 

143 }, 

144 }, 

145 ) 

146 assert found is None 

147 

148 found = await client.types.find_first( 

149 where={ 

150 'json_obj': { 

151 'not': Json.keys(countries=['Scotland']), 

152 }, 

153 }, 

154 ) 

155 assert found is not None 

156 assert found.id == model.id 

157 assert found.json_obj == {'country': 'Scotland'} 

158 

159 

160@pytest.mark.asyncio 

161async def test_unserializable_type(client: Prisma) -> None: 

162 """Error is raised when an unserializable type is encountered""" 

163 with pytest.raises(TypeError) as exc: 

164 await client.types.create( 

165 data={ 

166 'json_obj': Json.keys(foo=Prisma), # type: ignore 

167 }, 

168 ) 

169 

170 assert exc.match(r'Type <class \'prisma.client.Prisma\'> not serializable') 

171 

172 

173def test_json_schema() -> None: 

174 """Ensure a JSON Schema definition can be created""" 

175 if PYDANTIC_V2: 

176 assert model_json_schema(Types) == IsPartialDict( 

177 properties=IsPartialDict( 

178 { 

179 'json_obj': { 

180 'anyOf': [ 

181 { 

182 'contentMediaType': 'application/json', 

183 'contentSchema': {}, 

184 'type': 'string', 

185 }, 

186 {'type': 'null'}, 

187 ], 

188 'default': None, 

189 'title': 'Json Obj', 

190 } 

191 } 

192 ), 

193 ) 

194 else: 

195 assert model_json_schema(Types) == IsPartialDict( 

196 properties=IsPartialDict( 

197 { 

198 'json_obj': { 

199 'title': 'Json Obj', 

200 'type': 'string', 

201 'format': 'json-string', 

202 } 

203 } 

204 ), 

205 )