Coverage for databases/sync_tests/types/test_json.py: 97%

62 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 

9def test_create(client: Prisma) -> None: 

10 """Creating a model with Json data""" 

11 model = client.types.create( 

12 data={ 

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

14 }, 

15 ) 

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

17 

18 model = client.types.create( 

19 data={ 

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

21 }, 

22 ) 

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

24 

25 model = client.types.create( 

26 data={ 

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

28 }, 

29 ) 

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

31 

32 model = client.types.create( 

33 data={ 

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

35 }, 

36 ) 

37 assert model.json_obj == 'Scotland' 

38 

39 model = client.types.create( 

40 data={ 

41 'json_obj': Json(1), 

42 }, 

43 ) 

44 assert model.json_obj == 1 

45 

46 model = client.types.create( 

47 data={ 

48 'json_obj': Json(None), 

49 }, 

50 ) 

51 assert model.json_obj is None 

52 

53 model = client.types.create( 

54 data={ 

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

56 }, 

57 ) 

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

59 

60 model = client.types.create( 

61 data={ 

62 'json_obj': Json(19.3273823), 

63 }, 

64 ) 

65 assert model.json_obj == 19.3273823 

66 

67 

68def test_keys(client: Prisma) -> None: 

69 """Handling of non-string keys""" 

70 model = client.types.create( 

71 data={ 

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

73 }, 

74 ) 

75 assert model.json_obj is not None 

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

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

78 

79 model = client.types.create( 

80 data={ 

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

82 }, 

83 ) 

84 assert model.json_obj is not None 

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

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

87 

88 model = client.types.create( 

89 data={ 

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

91 }, 

92 ) 

93 assert model.json_obj is not None 

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

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

96 

97 model = client.types.create( 

98 data={ 

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

100 }, 

101 ) 

102 assert model.json_obj is not None 

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

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

105 

106 

107def test_base_filtering(client: Prisma) -> None: 

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

109 found = client.types.find_first( 

110 where={ 

111 'json_obj': { 

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

113 }, 

114 }, 

115 ) 

116 assert found is None 

117 

118 model = client.types.create( 

119 data={ 

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

121 }, 

122 ) 

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

124 

125 found = client.types.find_first( 

126 where={ 

127 'json_obj': { 

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

129 }, 

130 }, 

131 ) 

132 assert found is not None 

133 assert found.id == model.id 

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

135 

136 found = client.types.find_first( 

137 where={ 

138 'json_obj': { 

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

140 }, 

141 }, 

142 ) 

143 assert found is None 

144 

145 found = client.types.find_first( 

146 where={ 

147 'json_obj': { 

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

149 }, 

150 }, 

151 ) 

152 assert found is not None 

153 assert found.id == model.id 

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

155 

156 

157def test_unserializable_type(client: Prisma) -> None: 

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

159 with pytest.raises(TypeError) as exc: 

160 client.types.create( 

161 data={ 

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

163 }, 

164 ) 

165 

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

167 

168 

169def test_json_schema() -> None: 

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

171 if PYDANTIC_V2: 171 ↛ 191line 171 didn't jump to line 191, because the condition on line 171 was never false

172 assert model_json_schema(Types) == IsPartialDict( 

173 properties=IsPartialDict( 

174 { 

175 'json_obj': { 

176 'anyOf': [ 

177 { 

178 'contentMediaType': 'application/json', 

179 'contentSchema': {}, 

180 'type': 'string', 

181 }, 

182 {'type': 'null'}, 

183 ], 

184 'default': None, 

185 'title': 'Json Obj', 

186 } 

187 } 

188 ), 

189 ) 

190 else: 

191 assert model_json_schema(Types) == IsPartialDict( 

192 properties=IsPartialDict( 

193 { 

194 'json_obj': { 

195 'title': 'Json Obj', 

196 'type': 'string', 

197 'format': 'json-string', 

198 } 

199 } 

200 ), 

201 )