Package jsondata :: Module Selftest
[hide private]
[frames] | no frames]

Source Code for Module jsondata.Selftest

  1  # -*- coding: utf-8 -*- 
  2  """Test of basic features for the user by '--selftest'. 
  3   
  4  This module is used by 'jsondc' when the opverify_data_schemaardoced  
  5  basic functional checks by calling 'runselftest'. 
  6   
  7  The display of actions and results could be activated and  
  8  raised by multiple repetition of the '-v' option. 
  9   
 10  The following data and schema are applied: 
 11      0. jsondata/data.json + jsondata/schema.jsd 
 12      1. jsondata/datacheck.json + jsondata/datacheck.jsd 
 13   
 14  The performed process flow is: 
 15      0. load 
 16      1. validate 
 17      2. verify 
 18   
 19  By default either 'True' is returned, or in case of a failed test 
 20  and/or error condition an exception is raised. 
 21  """ 
 22  __author__ = 'Arno-Can Uestuensoez' 
 23  __license__ = "Artistic-License-2.0 + Forced-Fairplay-Constraints" 
 24  __copyright__ = "Copyright (C) 2015-2016 Arno-Can Uestuensoez @Ingenieurbuero Arno-Can Uestuensoez" 
 25  __version__ = '0.2.12' 
 26  __uuid__='63b597d6-4ada-4880-9f99-f5e0961351fb' 
 27   
 28  import os,sys 
 29  import json#,jsonschema 
 30   
 31  try: 
 32      from jsondata.JSONPointer import JSONPointer 
 33  except Exception as e: 
 34      print "\n#\n#*** Set 'PYTHONPATH' ("+str(e)+")\n#\n" 
 35  try: 
 36      from jsondata.JSONDataSerializer import JSONDataSerializer as ConfigData 
 37      from jsondata.JSONDataSerializer import MODE_SCHEMA_OFF,MODE_SCHEMA_DRAFT3 
 38  except Exception as e: 
 39      print "\n#\n#*** Set 'PYTHONPATH' ("+str(e)+")\n#\n" 
 40  # name of application, used for several filenames as default 
 41  _APPNAME = "selftest" 
 42   
 43  # Sets display for inetractive JSON/JSONschema design. 
 44  _interactive = False 
 45   
 46  debug = False 
 47  _verbose = 0 
 48   
49 -def runselftest(appname="selftest",**kargs):
50 """Performs the selftest returns True or False. 51 Executes some the basic runtime test cases for user verification. 52 53 Args: 54 appname: Name of the application. Changing this may break the 55 selftest. 56 default:=selftest 57 58 **kargs: 59 debug: Displays extended state data for developers. 60 Requires __debug__==True. 61 verbose: Extends the amount of the display of 62 processing data. 63 _verbose=#levels: Extends the amount of the display 64 of processing data by given number of levels 65 at once. 66 67 Returns: 68 Selftest object. 69 70 Raises: 71 bypassed subsystems 72 73 """ 74 global debug 75 global _verbose 76 77 # set display mode for errors 78 _interactive = kargs.get('interactive',False) 79 _verbose = 0 80 81 debug = kargs.get('debug',False) 82 83 for _o,_a in kargs.items(): 84 if _o == 'verbose': 85 _verbose += 1 86 elif _o == '_verbose': 87 _verbose += _a 88 89 # 90 # load tests 91 load_data(appname) 92 load_appname(appname) 93 94 # 95 # validation tests 96 verify_data_schema(appname) 97 verify_appname_schema(appname) 98 99 # 100 # JSONPointer tests 101 jsonpointer_data_schema(appname) 102 jsonpointer_selftest_data(appname) 103 jsonpointer_selftest_data_schema(appname) 104 105 return True
106
107 -def printverbose(lvl,args):
108 if lvl<=_verbose or debug: 109 print args
110
111 -def load_data(appname):
112 """Loads and verifies the self test 'data.json'. 113 114 Therefore the result of the creation of JSONDataSerializer 115 is compared to the load by json.load(). 116 """ 117 position = os.path.abspath(os.path.dirname(__file__))+os.sep 118 119 # case=0: 120 datafile = position+"data.json" 121 schemafile = position+"schema.jsd" 122 123 printverbose(2,"#------------------------------------------") 124 printverbose(1,"load_data: load "+str(datafile)) 125 printverbose(2,"#------------------------------------------") 126 127 if debug: 128 printverbose(0,"DBG:self.schemafile= "+str(datafile)) 129 printverbose(0,"DBG:self.schemafile= "+str(schemafile)) 130 131 132 _kargs = {} 133 _kargs['debug'] = debug 134 _kargs['verbose'] = _verbose>2 135 _kargs['pathlist'] = [position] 136 _kargs['datafile'] = datafile 137 _kargs['schemafile'] = schemafile 138 _kargs['validator'] = MODE_SCHEMA_OFF 139 configdata = ConfigData(appname,**_kargs) 140 141 # jval 142 if not os.path.isfile(datafile): 143 raise BaseException("Missing JSON data:file="+str(datafile)) 144 # load data 145 with open(datafile) as data_file: 146 jval = json.load(data_file) 147 if jval == None: 148 raise BaseException("Failed to load data:"+str(data_file)) 149 150 printverbose(2, "check data...") 151 for l in ['address',]: 152 printverbose(2,"check data["+l+"]...") 153 for n in ['streetAddress',"city","houseNumber",]: 154 cdata = configdata.data[l][n] 155 jdata = jval[l][n] 156 assert cdata == jdata 157 printverbose(2,"check data["+l+"]["+str(n)+"]...OK") 158 159 for l in ['phoneNumber',]: 160 printverbose(2,"check data["+l+"]...") 161 for n in [0,]: 162 printverbose(2,"check data["+l+"]["+str(n)+"]...") 163 for p in ['type',"number",]: 164 cdata = configdata.data[l][n][p] 165 jdata = jval[l][n][p] 166 assert cdata == jdata 167 printverbose(2,"check data["+l+"]["+str(n)+"]["+str(p)+"]...OK") 168 printverbose(2,"")
169
170 -def load_appname(appname):
171 """Loads and verifies the self test 'selftest.json'. 172 173 Therefore the result of the creation of JSONDataSerializer 174 is compared to the load by json.load(). 175 """ 176 position = os.path.abspath(os.path.dirname(__file__))+os.sep 177 178 # case=0: 179 datafile = position+appname+".json" 180 schemafile = position+appname+".jsd" 181 182 printverbose(2,"#------------------------------------------") 183 printverbose(1,"load_appname: load "+str(datafile)) 184 printverbose(2,"#------------------------------------------") 185 186 if debug: 187 printverbose(0,"DBG:self.schemafile= "+str(datafile)) 188 printverbose(0,"DBG:self.schemafile= "+str(schemafile)) 189 190 191 _kargs = {} 192 _kargs['debug'] = debug 193 _kargs['verbose'] = _verbose>2 194 _kargs['pathlist'] = [position] 195 _kargs['validator'] = MODE_SCHEMA_OFF 196 _kargs['datafile'] = datafile 197 _kargs['schemafile'] = schemafile 198 configdata = ConfigData(appname,**_kargs) 199 200 # jval 201 if not os.path.isfile(datafile): 202 raise BaseException("Missing JSON data:file="+str(datafile)) 203 # load data 204 with open(datafile) as data_file: 205 jval = json.load(data_file) 206 if jval == None: 207 raise BaseException("Failed to load data:"+str(data_file)) 208 209 printverbose(2,"check data[customers]...") 210 211 for l in ['domestic','abroad',]: 212 printverbose(2,"check data[customers]["+l+"]...") 213 for n in [0,1,]: 214 printverbose(2,"check data[customers]["+l+"]["+str(n)+"]...") 215 cdata = configdata.data["customers"][l][n]["name"] 216 jdata = jval["customers"][l][n]["name"] 217 assert cdata == jdata 218 printverbose(2,"check data[customers]["+l+"]["+str(n)+"][name]...OK") 219 220 cdata = configdata.data["customers"][l][n]["industries"] 221 jdata = configdata.data["customers"][l][n]["industries"] 222 assert cdata == jdata 223 printverbose(2,"check data[customers]["+l+"]["+str(n)+"][industries]...OK") 224 225 for p in [0,1,]: 226 cdata = configdata.data["customers"][l][n]["products"][p]["name"] 227 jdata = configdata.data["customers"][l][n]["products"][p]["name"] 228 assert cdata == jdata 229 printverbose(2,"check data[customers]["+l+"]["+str(n)+"][products]["+str(p)+"][name]...OK") 230 231 cdata = configdata.data["customers"][l][n]["products"][p]["quantities"] 232 jdata = configdata.data["customers"][l][n]["products"][p]["quantities"] 233 assert cdata == jdata 234 printverbose(2,"check data[customers]["+l+"]["+str(n)+"][products]["+str(p)+"][quantities]...OK") 235 236 cdata = configdata.data["customers"][l][n]["products"][p]["priority"] 237 jdata = configdata.data["customers"][l][n]["products"][p]["priority"] 238 assert cdata == jdata 239 printverbose(2,"check data[customers]["+l+"]["+str(n)+"][products]["+str(p)+"][priority]...OK") 240 241 printverbose(2,"")
242
243 -def verify_data_schema(appname):
244 """Loads and validates the self test 'data.json' and 'schema.jsd'. 245 246 Therefore the result of the creation of JSONDataSerializer is performed 247 with draft3 validation by jsonschema.validate(). 248 """ 249 position = os.path.abspath(os.path.dirname(__file__))+os.sep 250 251 # case=0: 252 datafile = position+"data.json" 253 schemafile = position+"schema.jsd" 254 255 printverbose(2,"#------------------------------------------") 256 printverbose(1,"verify_data_schema: load and validate "+str(datafile)) 257 printverbose(1,"verify_data_schema: load and validate "+str(schemafile)) 258 printverbose(2,"#------------------------------------------") 259 260 if debug: 261 printverbose(0,"DBG:self.schemafile= "+str(datafile)) 262 printverbose(0,"DBG:self.schemafile= "+str(schemafile)) 263 264 265 _kargs = {} 266 _kargs['debug'] = debug 267 _kargs['verbose'] = _verbose>2 268 _kargs['pathlist'] = [position] 269 _kargs['datafile'] = datafile 270 _kargs['schemafile'] = schemafile 271 _kargs['validator'] = MODE_SCHEMA_DRAFT3 272 configdata = ConfigData(appname,**_kargs) 273 printverbose(2,"")
274
275 -def verify_appname_schema(appname):
276 """Loads and validates the self test 'selftest.json' and 'selftest.jsd'. 277 278 Therefore the result of the creation of JSONDataSerializer is performed 279 with draft3 validation by jsonschema.validate(). 280 """ 281 position = os.path.abspath(os.path.dirname(__file__))+os.sep 282 283 # 284 datafile = position+appname+".json" 285 schemafile = position+appname+".jsd" 286 287 printverbose(2,"#------------------------------------------") 288 printverbose(1,"verify_appname_schema: load and validate "+str(datafile)) 289 printverbose(1,"verify_appname_schema: load and validate "+str(schemafile)) 290 printverbose(2,"#------------------------------------------") 291 292 if debug: 293 printverbose(0,"DBG:self.schemafile= "+str(datafile)) 294 printverbose(0,"DBG:self.schemafile= "+str(schemafile)) 295 296 297 _kargs = {} 298 _kargs['debug'] = debug 299 _kargs['verbose'] = _verbose>2 300 _kargs['pathlist'] = [position] 301 _kargs['datafile'] = datafile 302 _kargs['schemafile'] = schemafile 303 _kargs['validator'] = MODE_SCHEMA_DRAFT3 304 configdata = ConfigData(appname,**_kargs) 305 printverbose(2,"")
306
307 -def jsonpointer_data_schema(appname):
308 """Loads and verifies by using JSONPointer access 'data.json'. 309 """ 310 position = os.path.abspath(os.path.dirname(__file__))+os.sep 311 312 # case=0: 313 datafile = position+"data.json" 314 schemafile = position+"schema.jsd" 315 316 printverbose(2,"#------------------------------------------") 317 printverbose(1,"jsonpointer_data_schema: load "+str(datafile)) 318 printverbose(1,"jsonpointer_data_schema: load "+str(schemafile)) 319 printverbose(2,"#------------------------------------------") 320 321 if debug: 322 printverbose(0,"DBG:self.schemafile= "+str(datafile)) 323 printverbose(0,"DBG:self.schemafile= "+str(schemafile)) 324 325 326 _kargs = {} 327 _kargs['debug'] = debug 328 _kargs['verbose'] = _verbose>2 329 _kargs['pathlist'] = [position] 330 _kargs['validator'] = MODE_SCHEMA_DRAFT3 331 _kargs['datafile'] = datafile 332 _kargs['schemafile'] = schemafile 333 configdata = ConfigData(appname,**_kargs) 334 335 336 jsonptr = JSONPointer('/address') 337 if not jsonptr: 338 raise BaseException("Failed to create JSONPointer") 339 jsonptrdata = jsonptr.get_node(configdata.data) 340 jsx=str(jsonptrdata) 341 ref = "{u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}" 342 assert jsx == ref 343 344 jsonptr = JSONPointer('/address/streetAddress') 345 if not jsonptr: 346 raise BaseException("Failed to create JSONPointer") 347 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 348 assert jsonptrdata == "21 2nd Street" 349 350 jsonptr = JSONPointer('/address/city') 351 if not jsonptr: 352 raise BaseException("Failed to create JSONPointer") 353 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 354 assert jsonptrdata == "New York" 355 356 jsonptr = JSONPointer('/address/houseNumber') 357 if not jsonptr: 358 raise BaseException("Failed to create JSONPointer") 359 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 360 assert jsonptrdata == 12 361 362 printverbose(2,"")
363
364 -def jsonpointer_selftest_data(appname):
365 """Loads and verifies by using JSONPointer access 'selftest.json'. 366 """ 367 position = os.path.abspath(os.path.dirname(__file__))+os.sep 368 369 # 370 datafile = position+"data.json" 371 schemafile = position+"schema.jsd" 372 # datafile = position+appname+".json" 373 # schemafile = position+appname+".jsd" 374 375 printverbose(2,"#------------------------------------------") 376 printverbose(1,"jsonpointer_selftest_data: load "+str(datafile)) 377 printverbose(1,"jsonpointer_selftest_data: load "+str(schemafile)) 378 printverbose(2,"#------------------------------------------") 379 380 if debug: 381 printverbose(0,"DBG:self.schemafile= "+str(datafile)) 382 printverbose(0,"DBG:self.schemafile= "+str(schemafile)) 383 384 385 _kargs = {} 386 _kargs['debug'] = debug 387 _kargs['verbose'] = _verbose>2 388 _kargs['pathlist'] = [position] 389 _kargs['validator'] = MODE_SCHEMA_DRAFT3 390 _kargs['datafile'] = datafile 391 _kargs['schemafile'] = schemafile 392 configdata = ConfigData(appname,**_kargs) 393 394 395 jsonptr = JSONPointer('/address') 396 if not jsonptr: 397 raise BaseException("Failed to create JSONPointer") 398 jsonptrdata = jsonptr.get_node(configdata.data) 399 jsx=str(jsonptrdata) 400 ref = "{u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}" 401 assert jsx == ref 402 403 jsonptr = JSONPointer('/phoneNumber/0/type') 404 if not jsonptr: 405 raise BaseException("Failed to create JSONPointer") 406 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 407 assert jsonptrdata == "home" 408 409 jsonptr = JSONPointer('/phoneNumber/0/number') 410 if not jsonptr: 411 raise BaseException("Failed to create JSONPointer") 412 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 413 assert jsonptrdata == "212 555-1234" 414 415 jsonptr = JSONPointer('/phoneNumber/0/active') 416 if not jsonptr: 417 raise BaseException("Failed to create JSONPointer") 418 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 419 assert jsonptrdata == True 420 421 jsonptr = JSONPointer('/phoneNumber/0/private') 422 if not jsonptr: 423 raise BaseException("Failed to create JSONPointer") 424 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 425 assert jsonptrdata == False 426 427 jsonptr = JSONPointer('/phoneNumber/0/addons') 428 if not jsonptr: 429 raise BaseException("Failed to create JSONPointer") 430 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 431 assert jsonptrdata == None 432 433 jsonptr = JSONPointer('/phoneNumber/0/index') 434 if not jsonptr: 435 raise BaseException("Failed to create JSONPointer") 436 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 437 assert jsonptrdata == 0 438 439 jsonptr = JSONPointer('/phoneNumber/0/testnumber') 440 if not jsonptr: 441 raise BaseException("Failed to create JSONPointer") 442 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 443 assert jsonptrdata == 1.5 444 445 printverbose(2,"")
446
447 -def jsonpointer_selftest_data_schema(appname):
448 """Loads and verifies by using JSONPointer access 'selftest.json'. 449 """ 450 position = os.path.abspath(os.path.dirname(__file__))+os.sep 451 452 # 453 datafile = position+appname+".json" 454 schemafile = position+appname+".jsd" 455 456 printverbose(2,"#------------------------------------------") 457 printverbose(1,"jsonpointer_selftest_data_schema: load "+str(datafile)) 458 printverbose(1,"jsonpointer_selftest_data_schema: load "+str(schemafile)) 459 printverbose(2,"#------------------------------------------") 460 461 if debug: 462 printverbose(0,"DBG:self.schemafile= "+str(datafile)) 463 printverbose(0,"DBG:self.schemafile= "+str(schemafile)) 464 465 466 _kargs = {} 467 _kargs['debug'] = debug 468 _kargs['verbose'] = _verbose>2 469 _kargs['pathlist'] = [position] 470 _kargs['validator'] = MODE_SCHEMA_DRAFT3 471 _kargs['datafile'] = datafile 472 _kargs['schemafile'] = schemafile 473 configdata = ConfigData(appname,**_kargs) 474 475 476 for l in ['domestic','abroad',]: 477 for n in [0,1,]: 478 479 basep = '/customers/'+str(l)+'/'+str(n) 480 jsonptr = JSONPointer(basep+'/name') 481 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 482 jsx=str(jsonptrdata) 483 ref = "customer"+str(n) 484 assert jsx == ref 485 486 jsonptr = JSONPointer(basep+'/industries') 487 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 488 jsx=str(jsonptrdata) 489 ref = "industry"+str(n) 490 assert jsx == ref 491 492 for p in [0,1,]: # products 493 basep = '/customers/'+str(l)+'/'+str(n)+'/products/'+str(p) 494 495 prodlist = { 496 'name': "product"+str(p), 497 "quantities": 2000+p, 498 "priority": 0+p, 499 "quota": 1.5+p 500 } 501 for k,v in prodlist.items(): 502 attr = k # attribute 503 ref = v # attribute value 504 # 505 jsonptr = JSONPointer(basep+'/'+attr) 506 jsonptrdata = jsonptr.get_node_or_value(configdata.data) 507 jsx=jsonptrdata 508 509 #assert jsx == ref 510 511 try: 512 assert jsx == ref 513 except AssertionError as e: 514 print >>sys.stderr, "ERROR:AssertionError:k="+str(k)+" / v="+str(v)+" / type="+str(type(v)) 515 print >>sys.stderr, "ERROR:AssertionError:jsx="+str(jsx)+" / type="+str(type(jsx)) 516 assert jsx == ref 517 #raise AssertionError 518 #raise Exception 519 520 printverbose(2,"")
521