Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import json 

2import os 

3import sys 

4 

5import pytest 

6 

7sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "/../../../../") 

8from sdc_etl_libs.sdc_data_schema.schema_exceptions import DataSchemaFailedValidation 

9from sdc_etl_libs.sdc_data_schema.schema_validation import (ResultTypeStatuses, SchemaValidation) 

10 

11def test_validation__key_list_value_opts__no_duplicates(): 

12 """ 

13 Ensure that if a key has "data_type" set to "list" and has "allow_duplicates" in "list_value_opts" set to "False", 

14 then a list without duplicates does not raise an error. Note that if "allow_duplicates" is not set, defaults to False. 

15 ##################################################################################################################### 

16 # key_lvo_1 = {"required": False, "data_type": list, "list_value_opts": {"data_type": str, "allow_duplicates": False}} # 

17 # key_lvo_2 = {"required": False, "data_type": list, "list_value_opts": {"data_type": str}} # 

18 ##################################################################################################################### 

19 """ 

20 

21 # Test list without duplicates when allow_duplicates is set to False 

22 ep_schema_raw = """ 

23 { 

24 "type": "test", 

25 "tag": "main_source", 

26 "info": { 

27 "type": "test", 

28 "access": { 

29 "type": "test_list_value_opts", 

30 "test_details": { 

31 "key_lvo_1": ["cat", "dog", "seal"] 

32 } 

33 }, 

34 "opts": { 

35 } 

36 } 

37 } 

38 """ 

39 

40 ep_schema = json.loads(ep_schema_raw) 

41 validation = SchemaValidation() 

42 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

43 

44 assert validation.ttl_errors == 0 

45 assert { 

46 'status': 'ERROR', 

47 'reason': 'DUPLICATE_VALUE', 

48 'section': 'endpoint:info:access:test_details', 

49 'note': 'Duplicate value "cat" found within list "key_lvo_1". Please remove duplicates or set "allowed_duplicates" for "key_lvo_1" to "True".' 

50 } not in validation.results 

51 assert { 

52 'status': 'ERROR', 

53 'reason': 'DUPLICATE_VALUE', 

54 'section': 'endpoint:info:access:test_details', 

55 'note': 'Duplicate value "dog" found within list "key_lvo_1". Please remove duplicates or set "allowed_duplicates" for "key_lvo_1" to "True".' 

56 } not in validation.results 

57 assert { 

58 'status': 'ERROR', 

59 'reason': 'DUPLICATE_VALUE', 

60 'section': 'endpoint:info:access:test_details', 

61 'note': 'Duplicate value "seal" found within list "key_lvo_1". Please remove duplicates or set "allowed_duplicates" for "key_lvo_1" to "True".' 

62 } not in validation.results 

63 

64 # Test list without duplicates when allow_duplicates is not set (and therefore should default to False) 

65 ep_schema_raw = """ 

66 { 

67 "type": "test", 

68 "tag": "main_source", 

69 "info": { 

70 "type": "test", 

71 "access": { 

72 "type": "test_list_value_opts", 

73 "test_details": { 

74 "key_lvo_2": ["cat", "dog", "seal"] 

75 } 

76 }, 

77 "opts": { 

78 } 

79 } 

80 } 

81 """ 

82 

83 ep_schema = json.loads(ep_schema_raw) 

84 validation = SchemaValidation() 

85 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

86 

87 assert validation.ttl_errors == 0 

88 assert { 

89 'status': 'ERROR', 

90 'reason': 'DUPLICATE_VALUE', 

91 'section': 'endpoint:info:access:test_details', 

92 'note': 'Duplicate value "cat" found within list "key_lvo_2". Please remove duplicates or set "allowed_duplicates" for "key_lvo_2" to "True".' 

93 } not in validation.results 

94 assert { 

95 'status': 'ERROR', 

96 'reason': 'DUPLICATE_VALUE', 

97 'section': 'endpoint:info:access:test_details', 

98 'note': 'Duplicate value "dog" found within list "key_lvo_2". Please remove duplicates or set "allowed_duplicates" for "key_lvo_2" to "True".' 

99 } not in validation.results 

100 assert { 

101 'status': 'ERROR', 

102 'reason': 'DUPLICATE_VALUE', 

103 'section': 'endpoint:info:access:test_details', 

104 'note': 'Duplicate value "seal" found within list "key_lvo_2". Please remove duplicates or set "allowed_duplicates" for "key_lvo_2" to "True".' 

105 } not in validation.results 

106 

107 

108def test_validation__key_list_value_opts__duplicates_present(): 

109 """ 

110 Ensure that if a key has "data_type" set to "list" and has "allow_duplicates" in "list_value_opts" set to "False", 

111 then a list with duplicates raises an error. If "allow_duplicates" is set to "True", an error should not be raised. 

112 Note that if "allow_duplicates" is not set, defaults to False. 

113 ##################################################################################################################### 

114 # key_lvo_1 = {"required": False, "data_type": list, "list_value_opts": {"data_type": str, "allow_duplicates": False}} # 

115 # key_lvo_2 = {"required": False, "data_type": list, "list_value_opts": {"data_type": str}} # 

116 # key_lvo_3 = {"required": False, "data_type": list, "list_value_opts": {"data_type": str, "allow_duplicates": True}} # 

117 ##################################################################################################################### 

118 """ 

119 

120 # Test list with duplicates when allow_duplicates is set to False 

121 ep_schema_raw = """ 

122 { 

123 "type": "test", 

124 "tag": "main_source", 

125 "info": { 

126 "type": "test", 

127 "access": { 

128 "type": "test_list_value_opts", 

129 "test_details": { 

130 "key_lvo_1": ["cat", "seal", "seal"] 

131 } 

132 }, 

133 "opts": { 

134 } 

135 } 

136 } 

137 """ 

138 

139 ep_schema = json.loads(ep_schema_raw) 

140 validation = SchemaValidation() 

141 

142 with pytest.raises(DataSchemaFailedValidation): 

143 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

144 

145 assert { 

146 'status': 'ERROR', 

147 'reason': 'DUPLICATE_VALUE', 

148 'section': 'endpoint:info:access:test_details', 

149 'note': 'Duplicate value "seal" found within list "key_lvo_1". Please remove duplicates or set "allowed_duplicates" for "key_lvo_1" to "True".' 

150 } in validation.results 

151 

152 # Test list with duplicates when allow_duplicates is not set (and therefore should default to False) 

153 ep_schema_raw = """ 

154 { 

155 "type": "test", 

156 "tag": "main_source", 

157 "info": { 

158 "type": "test", 

159 "access": { 

160 "type": "test_list_value_opts", 

161 "test_details": { 

162 "key_lvo_2": ["cat", "seal", "seal"] 

163 } 

164 }, 

165 "opts": { 

166 } 

167 } 

168 } 

169 """ 

170 

171 ep_schema = json.loads(ep_schema_raw) 

172 validation = SchemaValidation() 

173 

174 with pytest.raises(DataSchemaFailedValidation): 

175 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

176 

177 assert { 

178 'status': 'ERROR', 

179 'reason': 'DUPLICATE_VALUE', 

180 'section': 'endpoint:info:access:test_details', 

181 'note': 'Duplicate value "seal" found within list "key_lvo_2". Please remove duplicates or set "allowed_duplicates" for "key_lvo_2" to "True".' 

182 } in validation.results 

183 

184 # Test list with duplicates when allow_duplicates is set to True 

185 ep_schema_raw = """ 

186 { 

187 "type": "test", 

188 "tag": "main_source", 

189 "info": { 

190 "type": "test", 

191 "access": { 

192 "type": "test_list_value_opts", 

193 "test_details": { 

194 "key_lvo_3": ["cat", "seal", "seal"] 

195 } 

196 }, 

197 "opts": { 

198 } 

199 } 

200 } 

201 """ 

202 

203 ep_schema = json.loads(ep_schema_raw) 

204 validation = SchemaValidation() 

205 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

206 

207 assert validation.ttl_errors == 0 

208 assert { 

209 'status': 'ERROR', 

210 'reason': 'DUPLICATE_VALUE', 

211 'section': 'endpoint:info:access:test_details', 

212 'note': 'Duplicate value "seal" found within list "key_lvo_3". Please remove duplicates or set "allowed_duplicates" for "key_lvo_3" to "True".' 

213 } not in validation.results 

214 

215 

216def test_validation__key_list_value_opts__non_empty_list(): 

217 """ 

218 Ensure that if a key has "data_type" set to "list" and has "allow_empty_list" in "list_value_opts" set to "False", 

219 then a non-empty list does not raise an error. Note that if "allow_empty_list" is not set, defaults to False. 

220 ##################################################################################################################### 

221 # key_lvo_4 = {"required": False, "data_type": list, "list_value_opts": {"data_type": str, "allow_empty_list": False}} # 

222 # key_lvo_5 = {"required": False, "data_type": list, "list_value_opts": {"data_type": str}} # 

223 ##################################################################################################################### 

224 """ 

225 

226 # Test non-empty list when allow_empty_list is set to False 

227 ep_schema_raw = """ 

228 { 

229 "type": "test", 

230 "tag": "main_source", 

231 "info": { 

232 "type": "test", 

233 "access": { 

234 "type": "test_list_value_opts", 

235 "test_details": { 

236 "key_lvo_4": ["cat", "dog", "seal"] 

237 } 

238 }, 

239 "opts": { 

240 } 

241 } 

242 } 

243 """ 

244 

245 ep_schema = json.loads(ep_schema_raw) 

246 validation = SchemaValidation() 

247 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

248 

249 assert validation.ttl_errors == 0 

250 assert { 

251 'status': 'ERROR', 

252 'reason': 'EMPTY_LIST', 

253 'section': 'endpoint:info:access:test_details', 

254 'note': 'List "key_lvo_4" is empty. Please insert values or set "allowed_empty_list" for "key_lvo_4" to "True".' 

255 } not in validation.results 

256 

257 # Test non-empty list when allow_empty_list is not set (and therefore should default to False) 

258 ep_schema_raw = """ 

259 { 

260 "type": "test", 

261 "tag": "main_source", 

262 "info": { 

263 "type": "test", 

264 "access": { 

265 "type": "test_list_value_opts", 

266 "test_details": { 

267 "key_lvo_5": ["cat", "dog", "seal"] 

268 } 

269 }, 

270 "opts": { 

271 } 

272 } 

273 } 

274 """ 

275 

276 ep_schema = json.loads(ep_schema_raw) 

277 validation = SchemaValidation() 

278 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

279 

280 assert validation.ttl_errors == 0 

281 assert { 

282 'status': 'ERROR', 

283 'reason': 'EMPTY_LIST', 

284 'section': 'endpoint:info:access:test_details', 

285 'note': 'List "key_lvo_5" is empty. Please insert values or set "allowed_empty_list" for "key_lvo_5" to "True".' 

286 } not in validation.results 

287 

288 

289def test_validation__key_list_value_opts__empty_list(): 

290 """ 

291 Ensure that if a key has "data_type" set to "list" and has "allow_empty_list" in "list_value_opts" set to "False", 

292 then an empty list raises an error. If "allow_empty_list" is set to "True", an error should not be raised. 

293 Note that if "allow_empty_list" is not set, defaults to False. 

294 ##################################################################################################################### 

295 # key_lvo_4 = {"required": False, "data_type": list, "list_value_opts": {"data_type": str, "allow_empty_list": False}} # 

296 # key_lvo_5 = {"required": False, "data_type": list, "list_value_opts": {"data_type": str}} # 

297 # key_lvo_6 = {"required": False, "data_type": list, "list_value_opts": {"data_type": str, "allow_empty_list": True}} # 

298 ##################################################################################################################### 

299 """ 

300 

301 # Test list with empty list when allow_empty_list is set to False 

302 ep_schema_raw = """ 

303 { 

304 "type": "test", 

305 "tag": "main_source", 

306 "info": { 

307 "type": "test", 

308 "access": { 

309 "type": "test_list_value_opts", 

310 "test_details": { 

311 "key_lvo_4": [] 

312 } 

313 }, 

314 "opts": { 

315 } 

316 } 

317 } 

318 """ 

319 

320 ep_schema = json.loads(ep_schema_raw) 

321 validation = SchemaValidation() 

322 

323 with pytest.raises(DataSchemaFailedValidation): 

324 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

325 

326 assert { 

327 'status': 'ERROR', 

328 'reason': 'EMPTY_LIST', 

329 'section': 'endpoint:info:access:test_details', 

330 'note': 'List "key_lvo_4" is empty. Please insert values or set "allowed_empty_list" for "key_lvo_4" to "True".' 

331 } in validation.results 

332 

333 # Test empty list when allow_empty_list is not set (and therefore should default to False) 

334 ep_schema_raw = """ 

335 { 

336 "type": "test", 

337 "tag": "main_source", 

338 "info": { 

339 "type": "test", 

340 "access": { 

341 "type": "test_list_value_opts", 

342 "test_details": { 

343 "key_lvo_5": [] 

344 } 

345 }, 

346 "opts": { 

347 } 

348 } 

349 } 

350 """ 

351 

352 ep_schema = json.loads(ep_schema_raw) 

353 validation = SchemaValidation() 

354 

355 with pytest.raises(DataSchemaFailedValidation): 

356 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

357 

358 assert { 

359 'status': 'ERROR', 

360 'reason': 'EMPTY_LIST', 

361 'section': 'endpoint:info:access:test_details', 

362 'note': 'List "key_lvo_5" is empty. Please insert values or set "allowed_empty_list" for "key_lvo_5" to "True".' 

363 } in validation.results 

364 

365 # Test empty list when allow_empty_list is set to True 

366 ep_schema_raw = """ 

367 { 

368 "type": "test", 

369 "tag": "main_source", 

370 "info": { 

371 "type": "test", 

372 "access": { 

373 "type": "test_list_value_opts", 

374 "test_details": { 

375 "key_lvo_6": [] 

376 } 

377 }, 

378 "opts": { 

379 } 

380 } 

381 } 

382 """ 

383 

384 ep_schema = json.loads(ep_schema_raw) 

385 validation = SchemaValidation() 

386 schema = validation.validate_schema(ep_schema, validation_type_="endpoint") 

387 

388 assert validation.ttl_errors == 0 

389 assert { 

390 'status': 'ERROR', 

391 'reason': 'EMPTY_LIST', 

392 'section': 'endpoint:info:access:test_details', 

393 'note': 'List "key_lvo_6" is empty. Please insert values or set "allowed_empty_list" for "key_lvo_6" to "True".' 

394 } not in validation.results