Coverage for databases/utils.py: 100%

45 statements  

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

1from __future__ import annotations 

2 

3import os 

4from typing import Set 

5from pathlib import Path 

6from typing_extensions import Literal, get_args, override 

7 

8from pydantic import BaseModel 

9from syrupy.location import PyTestLocation 

10from syrupy.extensions.amber import AmberSnapshotExtension 

11 

12from ._types import DatabaseMapping 

13from ._compat import LiteralString 

14 

15DatabaseFeature = Literal[ 

16 'enum', 

17 'json', 

18 'date', 

19 'arrays', 

20 'array_push', 

21 'json_arrays', 

22 'raw_queries', 

23 'create_many', 

24 'transactions', 

25 'case_sensitivity', 

26] 

27 

28 

29class DatabaseConfig(BaseModel): 

30 id: str 

31 name: str 

32 env_var: str 

33 bools_are_ints: bool 

34 autoincrement_id: str 

35 unsupported_features: Set[DatabaseFeature] 

36 default_date_func: str 

37 

38 # TODO: run this under coverage 

39 def supports_feature(self, feature: DatabaseFeature) -> bool: # pragma: no cover 

40 if feature not in get_args(DatabaseFeature): 

41 raise RuntimeError(f'Unknown feature: {feature}') 

42 

43 return feature not in self.unsupported_features 

44 

45 

46# ------------------ Test helpers ------------------ 

47 

48from .constants import ( 

49 TESTS_DIR, 

50 SYNC_TESTS_DIR, 

51) 

52 

53SHARED_SNAPSHOTS_DIR = Path(__file__).parent.joinpath('__shared_snapshots__') 

54CURRENT_DATABASE = os.environ.get('PRISMA_DATABASE') 

55 

56 

57class AmberSharedExtension(AmberSnapshotExtension): 

58 """Syrupy extension that stores the snapshots in a parent __shared_snapshots__ dir""" 

59 

60 @classmethod 

61 @override 

62 def dirname(cls, *, test_location: PyTestLocation) -> str: 

63 test_dir = Path(test_location.filepath).parent 

64 if test_dir.is_relative_to(SYNC_TESTS_DIR): 

65 rel_dir = test_dir.relative_to(SYNC_TESTS_DIR) 

66 else: 

67 rel_dir = test_dir.relative_to(TESTS_DIR) 

68 

69 return str(SHARED_SNAPSHOTS_DIR.joinpath(rel_dir)) 

70 

71 

72class RawQueries(BaseModel): 

73 """Raw queries defined globally so they can be easily referenced in test functions""" 

74 

75 count_posts: LiteralString 

76 

77 find_user_by_id: LiteralString 

78 find_user_by_id_limit_1: LiteralString 

79 

80 find_post_by_id: LiteralString 

81 find_posts_not_published: LiteralString 

82 

83 select_unknown_table: LiteralString 

84 update_unique_post_title: LiteralString 

85 update_unique_post_new_title: LiteralString 

86 test_query_raw_no_result: LiteralString 

87 test_execute_raw_no_result: LiteralString 

88 

89 

90_mysql_queries = RawQueries( 

91 count_posts=""" 

92 SELECT COUNT(*) as count 

93 FROM Post 

94 """, 

95 find_post_by_id=""" 

96 SELECT * 

97 FROM Post 

98 WHERE id = ? 

99 """, 

100 find_user_by_id=""" 

101 SELECT * 

102 FROM User 

103 WHERE User.id = ? 

104 """, 

105 find_user_by_id_limit_1=""" 

106 SELECT * 

107 FROM User 

108 WHERE User.id = ? 

109 LIMIT 1 

110 """, 

111 select_unknown_table=""" 

112 SELECT * 

113 FROM bad_table; 

114 """, 

115 find_posts_not_published=""" 

116 SELECT id, published 

117 FROM Post 

118 WHERE published = false 

119 """, 

120 test_query_raw_no_result=""" 

121 SELECT * 

122 FROM Post 

123 WHERE id = 'sdldsd' 

124 """, 

125 update_unique_post_title=""" 

126 UPDATE Post 

127 SET title = 'My edited title' 

128 WHERE id = ? 

129 """, 

130 update_unique_post_new_title=""" 

131 UPDATE Post 

132 SET title = 'My new title' 

133 WHERE id = ? 

134 """, 

135 test_execute_raw_no_result=""" 

136 UPDATE Post 

137 SET title = 'updated title' 

138 WHERE id = 'sdldsd' 

139 """, 

140) 

141 

142_postgresql_queries = RawQueries( 

143 count_posts=""" 

144 SELECT COUNT(*) as count 

145 FROM "Post" 

146 """, 

147 find_post_by_id=""" 

148 SELECT * 

149 FROM "Post" 

150 WHERE id = $1 

151 """, 

152 find_user_by_id=""" 

153 SELECT * 

154 FROM "User" 

155 WHERE "User".id = $1 

156 """, 

157 find_user_by_id_limit_1=""" 

158 SELECT * 

159 FROM "User" 

160 WHERE "User".id = $1 

161 LIMIT 1 

162 """, 

163 select_unknown_table=""" 

164 SELECT * 

165 FROM bad_table; 

166 """, 

167 find_posts_not_published=""" 

168 SELECT id, published 

169 FROM "Post" 

170 WHERE published = false 

171 """, 

172 test_query_raw_no_result=""" 

173 SELECT * 

174 FROM "Post" 

175 WHERE id = 'sdldsd' 

176 """, 

177 update_unique_post_title=""" 

178 UPDATE "Post" 

179 SET title = 'My edited title' 

180 WHERE id = $1 

181 """, 

182 update_unique_post_new_title=""" 

183 UPDATE "Post" 

184 SET title = 'My new title' 

185 WHERE id = $1 

186 """, 

187 test_execute_raw_no_result=""" 

188 UPDATE "Post" 

189 SET title = 'updated title' 

190 WHERE id = 'sdldsd' 

191 """, 

192) 

193 

194RAW_QUERIES_MAPPING: DatabaseMapping[RawQueries] = { 

195 'postgresql': _postgresql_queries, 

196 'cockroachdb': _postgresql_queries, 

197 'mysql': _mysql_queries, 

198 'mariadb': _mysql_queries, 

199 'sqlite': RawQueries( 

200 count_posts=""" 

201 SELECT COUNT(*) as count 

202 FROM Post 

203 """, 

204 find_post_by_id=""" 

205 SELECT * 

206 FROM Post 

207 WHERE id = ? 

208 """, 

209 find_user_by_id=""" 

210 SELECT * 

211 FROM User 

212 WHERE User.id = ? 

213 """, 

214 find_user_by_id_limit_1=""" 

215 SELECT * 

216 FROM User 

217 WHERE User.id = ? 

218 LIMIT 1 

219 """, 

220 select_unknown_table=""" 

221 SELECT * 

222 FROM bad_table; 

223 """, 

224 find_posts_not_published=""" 

225 SELECT id, published 

226 FROM Post 

227 WHERE published = false 

228 """, 

229 test_query_raw_no_result=""" 

230 SELECT * 

231 FROM Post 

232 WHERE id = 'sdldsd' 

233 """, 

234 update_unique_post_title=""" 

235 UPDATE Post 

236 SET title = 'My edited title' 

237 WHERE id = ? 

238 """, 

239 update_unique_post_new_title=""" 

240 UPDATE Post 

241 SET title = 'My new title' 

242 WHERE id = ? 

243 """, 

244 test_execute_raw_no_result=""" 

245 UPDATE Post 

246 SET title = 'updated title' 

247 WHERE id = 'sdldsd' 

248 """, 

249 ), 

250}