def testCase000(self): """Create an object for data only - no schema. """ global jval global sval global configdata global appname kargs = {} kargs['datafile'] = os.path.dirname( __file__) + os.sep + 'testdata.json' kargs['schemafile'] = os.path.dirname( __file__) + os.sep + 'testdata.jsd' kargs['nodefaultpath'] = True kargs['nosubdata'] = True kargs['pathlist'] = os.path.dirname(__file__) kargs['validator'] = MODE_SCHEMA_OFF configdata = ConfigData(appname, **kargs) dummy4debugbreak = 0 pass
def testCase100(self): """Load and add branch with failure, assure orginal data is not modified after failure. """ global jval global sval global configdata global appname global schemafile # branch to be loaded branchfile = os.path.abspath( os.path.dirname(__file__)) + os.sep + str('branch0.json') # partial schema for branch schema = { 'phoneNumber': configdata.schema['properties']['phoneNumber'] } kargs = {} kargs['schema'] = schema kargs['nodefaultpath'] = True kargs['nosubdata'] = True kargs['datafile'] = branchfile kargs['pathlist'] = os.path.dirname(__file__) kargs['validator'] = MODE_SCHEMA_DRAFT4 branchdata = ConfigData(appname, **kargs) target = configdata.data tconf = """{u'phoneNumber': [{u'type': u'home', u'number': u'212 555-1234'}, {u'type': u'office', u'number': u'313 444-555'}, {u'type': u'mobile', u'number': u'777 666-555'}], u'address': {u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}}""" assert repr(target) == tconf try: ret = configdata.branch_move(target['phoneNumber'], 0, branchdata['phoneNumber'], 0, False) except JSONDataKeyError: pass conf_dat = repr(configdata.data) conf_dat = """{u'phoneNumber': [{u'type': u'home', u'number': u'212 555-1234'}, {u'type': u'office', u'number': u'313 444-555'}, {u'type': u'mobile', u'number': u'777 666-555'}], u'address': {u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}}""" assert repr(configdata.data) == conf_dat pass
def testCase050(self): """Create a configuration object, load again by provided filelist. Load parameters: * appname = 'jsondatacheck' * kargs['datafile'] = datafile * kargs['schemafile'] = schemafile * kargs['nodefaultpath'] = True * kargs['nosubdata'] = True * kargs['pathlist'] = os.path.dirname(__file__) * kargs['validator'] = ConfigData.MODE_SCHEMA_DRAFT3 """ global jval global sval global configdata global appname global schemafile datafile = os.path.abspath( os.path.dirname(__file__)) + os.sep + str('testdata.json') schemafile = os.path.abspath( os.path.dirname(__file__)) + os.sep + str('schema.jsd') kargs = {} kargs['datafile'] = datafile kargs['schemafile'] = schemafile kargs['nodefaultpath'] = True kargs['nosubdata'] = True kargs['pathlist'] = os.path.dirname(__file__) kargs['validator'] = MODE_SCHEMA_DRAFT4 configdata = ConfigData(appname, **kargs) pass
def testCase110(self): """Load and move branch with force, replace previous present list. """ global jval global sval global configdata global appname global schemafile # branch to be loaded branchfile = os.path.abspath( os.path.dirname(__file__)) + os.sep + str('branch0.json') # partial schema for branch schema = { 'phoneNumber': configdata.schema['properties']['phoneNumber'] } kargs = {} kargs['schema'] = schema kargs['nodefaultpath'] = True kargs['nosubdata'] = True kargs['datafile'] = branchfile kargs['pathlist'] = os.path.dirname(__file__) kargs['validator'] = MODE_SCHEMA_DRAFT4 branchdata = ConfigData(appname, **kargs) target = configdata.data tconf = """{u'phoneNumber': [{u'type': u'home', u'number': u'212 555-1234'}, {u'type': u'office', u'number': u'313 444-555'}, {u'type': u'mobile', u'number': u'777 666-555'}], u'address': {u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}}""" assert repr(target) == tconf # ret = configdata.branch_copy(target['phoneNumber'], None, branchdata, True) ret = configdata.branch_copy(target['phoneNumber'], None, branchdata['phoneNumber'], True) assert ret == True conf_dat = repr(configdata.data) conf_dat = """{u'phoneNumber': [{u'type': u'home', u'number': u'111 222-333'}], u'address': {u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}}""" assert repr(configdata.data) == conf_dat pass
def testCase100(self): """Load and add a branch. Do not insert '$schema' key. """ global jval global sval global configdata global appname global schemafile # branch to be loaded branchfile = os.path.abspath( os.path.dirname(__file__)) + os.sep + str('branch0.json') # partial schema for branch, use here a subtree of main schema schema = { 'phoneNumber': configdata.schema['properties']['phoneNumber'] } # import settings kargs = {} kargs['schema'] = schema kargs['nodefaultpath'] = True kargs['nosubdata'] = True kargs['datafile'] = branchfile kargs['pathlist'] = os.path.dirname(__file__) kargs['validator'] = MODE_SCHEMA_DRAFT4 branchdata = ConfigData(appname, **kargs) # target container target = configdata.data # do it... configdata.branch_add(target, None, branchdata.data) conf_dat = "{u'phoneNumber': [{u'type': u'home', u'number': u'111 222-333'}]}" assert repr(configdata.data) == conf_dat pass
def testCase000(self): """Create an object by load of JSON data and JSONschema from files, finally validate. """ global jval global sval global configdata global appname global schemafile datafile = os.path.abspath(os.path.dirname(__file__))+os.sep+str('datafile.json') schemafile = os.path.abspath(os.path.dirname(__file__))+os.sep+str('schema.jsd') kargs = {} kargs['datafile'] = datafile kargs['schemafile'] = schemafile kargs['nodefaultpath'] = True kargs['nosubdata'] = True kargs['pathlist'] = os.path.dirname(__file__) kargs['validator'] = MODE_SCHEMA_DRAFT4 configdata = ConfigData(appname,**kargs) assert repr(configdata.data) == "{u'phoneNumber': [{u'type': u'home', u'number': u'212 555-1234'}, {u'type': u'office', u'number': u'313 444-555'}, {u'type': u'mobile', u'number': u'777 666-555'}], u'address': {u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}}" pass
def testCase000(self): """Create an object with validation by schemafile. """ global jval global sval global configdata global appname global schemafile datafile = os.path.abspath( os.path.dirname(__file__)) + os.sep + str('datafile.json') schemafile = os.path.abspath( os.path.dirname(__file__)) + os.sep + str('schema.jsd') kargs = {} kargs['datafile'] = datafile kargs['schemafile'] = schemafile kargs['nodefaultpath'] = True kargs['nosubdata'] = True kargs['pathlist'] = os.path.dirname(__file__) kargs['validator'] = MODE_SCHEMA_DRAFT4 configdata = ConfigData(appname, **kargs) pass
def testCase500(self): """Import another branch into memory and add the representation into the initial master data. Use insertion point: target = configdata.data['phoneNumber'] for file: 'branch1.json' #--- { "type":"home2", "number":"222 222-333" } #--- Apply '$schema' key for branch/subtree of master schema. """ global jval global sval global configdata global appname global schemafile # partial schema for branch, use here a subtree of main schema, schema = { "$schema": "http://json-schema.org/draft-03/schema", 'phoneNumber': configdata.schema['properties']['phoneNumber'] } # branch to be added datafile = os.path.abspath( os.path.dirname(__file__)) + os.sep + str('branch0.json') # import settings kargs = {} kargs['schema'] = schema kargs['nodefaultpath'] = True kargs['datafile'] = datafile kargs['nosubdata'] = True kargs['validator'] = MODE_SCHEMA_DRAFT4 # load branch data into memory branchdata = ConfigData(appname, **kargs) assert repr( branchdata.data ) == """{u'phoneNumber': [{u'type': u'home', u'number': u'111 222-333'}]}""" # target container # target = configdata.data['phoneNumber'] target = configdata['phoneNumber'] # ret = configdata.branch_add(target, '-', branchdata['phoneNumber']) from jsondata.JSONPointer import JSONPointer p = JSONPointer('phoneNumber').get_node(branchdata.data) ret = configdata.branch_add(target, None, p) # ret = configdata.branch_add(target, '-', p) assert ret == True conf_dat = repr(configdata.data) conf_dat = """{u'phoneNumber': [{u'type': u'home', u'number': u'111 222-333'}], u'address': {u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}}""" assert repr(configdata.data) == conf_dat pass
def testCase510(self): """Import a branch with erroneous key, must not change any state. Use insertion point: target = configdata.data['phoneNumber'] for file: 'branch2.json' #--- { "type":"home2", "number":"333 222-333" } #--- with key: 'phoneNumber' """ global jval global sval global configdata global appname global schemafile # partial schema for branch, use here a subtree of main schema schema = { "$schema": "http://json-schema.org/draft-03/schema", 'phoneNumber':configdata.schema['properties']['phoneNumber'] } # import settings branchfile = os.path.abspath(os.path.dirname(__file__))+os.sep+str('branch2.json') kargs = {} kargs['schema'] = schema kargs['nodefaultpath'] = True kargs['nosubdata'] = True kargs['datafile'] = branchfile kargs['pathlist'] = os.path.dirname(__file__) kargs['validator'] = MODE_SCHEMA_DRAFT4 branchdata = ConfigData(appname,**kargs) assert repr(branchdata.data) == """{u'type': u'home2', u'number': u'333 222-333'}""" # target container target = configdata.data['phoneNumber'] # do it... try: ret = configdata.branch_copy(target, 'phoneNumber', branchfile) except JSONDataKeyError as e: pass try: assert ret != True except UnboundLocalError: pass conf_dat = repr(configdata.data) conf_dat = """{u'phoneNumber': [{u'type': u'home', u'number': u'212 555-1234'}, {u'type': u'office', u'number': u'313 444-555'}, {u'type': u'mobile', u'number': u'777 666-555'}, {u'type': u'home', u'number': u'111 222-333'}, {u'type': u'home2', u'number': u'222 222-333'}, {u'type': u'home2', u'number': u'333 222-333'}], u'address': {u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}}""" assert repr(configdata.data) == conf_dat pass
def testCase501(self): """Import another branch into memory and try to move it into the initial master data - must fail. Use insertion point: target = configdata.data['phoneNumber'] for file: 'branch1.json' #--- [ { "type":"home2", "number":"222 222-333" } ] #--- Apply '$schema' key for branch/subtree of master schema. """ global jval global sval global configdata global appname global schemafile # partial schema for branch, use here a subtree of main schema, schema = { "$schema": "http://json-schema.org/draft-03/schema", 'phoneNumber': configdata.schema['properties']['phoneNumber'] } # branch to be added datafile = os.path.abspath( os.path.dirname(__file__)) + os.sep + str('branch1.json') # import settings kargs = {} kargs['schema'] = schema kargs['nodefaultpath'] = True kargs['datafile'] = datafile kargs['nosubdata'] = True kargs['validator'] = MODE_SCHEMA_DRAFT4 # load branch data into memory branchdata = ConfigData(appname, **kargs) b_dat = """[{u'type': u'home1', u'number': u'222 222-333'}]""" assert repr(branchdata.data) == b_dat #try move branch to target target = configdata['phoneNumber'] # target container try: ret = configdata.branch_move(target, '-', branchdata, '-') except JSONDataKeyError: pass # must fail dure to error condition assert repr(branchdata) == b_dat conf_dat = repr(configdata.data) conf_dat = """{u'phoneNumber': [{u'type': u'home', u'number': u'212 555-1234'}, {u'type': u'office', u'number': u'313 444-555'}, {u'type': u'mobile', u'number': u'777 666-555'}, {u'type': u'home0', u'number': u'111 222-333'}], u'address': {u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}}""" assert repr(configdata.data) == conf_dat pass
def jsonpointer_selftest_data_schema(appname): """Loads and verifies by using JSONPointer access 'selftest.json'. """ position = os.path.abspath(os.path.dirname(__file__)) + os.sep # datafile = position + appname + ".json" schemafile = position + appname + ".jsd" printverbose(2, "#------------------------------------------") printverbose(1, "jsonpointer_selftest_data_schema: load " + str(datafile)) printverbose(1, "jsonpointer_selftest_data_schema: load " + str(schemafile)) printverbose(2, "#------------------------------------------") if debug: printverbose(0, "DBG:self.schemafile= " + str(datafile)) printverbose(0, "DBG:self.schemafile= " + str(schemafile)) _kargs = {} _kargs['debug'] = debug _kargs['verbose'] = _verbose > 2 _kargs['pathlist'] = [position] _kargs['validator'] = MODE_SCHEMA_DRAFT3 _kargs['datafile'] = datafile _kargs['schemafile'] = schemafile configdata = ConfigData(appname, **_kargs) for l in [ 'domestic', 'abroad', ]: for n in [ 0, 1, ]: basep = '/customers/' + str(l) + '/' + str(n) jsonptr = JSONPointer(basep + '/name') jsonptrdata = jsonptr.get_node_or_value(configdata.data) jsx = str(jsonptrdata) ref = "customer" + str(n) assert jsx == ref jsonptr = JSONPointer(basep + '/industries') jsonptrdata = jsonptr.get_node_or_value(configdata.data) jsx = str(jsonptrdata) ref = "industry" + str(n) assert jsx == ref for p in [ 0, 1, ]: # products basep = '/customers/' + str(l) + '/' + str( n) + '/products/' + str(p) prodlist = { 'name': "product" + str(p), "quantities": 2000 + p, "priority": 0 + p, "quota": 1.5 + p } for k, v in prodlist.items(): attr = k # attribute ref = v # attribute value # jsonptr = JSONPointer(basep + '/' + attr) jsonptrdata = jsonptr.get_node_or_value(configdata.data) jsx = jsonptrdata #assert jsx == ref try: assert jsx == ref except AssertionError as e: print >> sys.stderr, "ERROR:AssertionError:k=" + str( k) + " / v=" + str(v) + " / type=" + str(type(v)) print >> sys.stderr, "ERROR:AssertionError:jsx=" + str( jsx) + " / type=" + str(type(jsx)) assert jsx == ref #raise AssertionError #raise Exception printverbose(2, "")
def jsonpointer_selftest_data(appname): """Loads and verifies by using JSONPointer access 'selftest.json'. """ position = os.path.abspath(os.path.dirname(__file__)) + os.sep # datafile = position + "data.json" schemafile = position + "schema.jsd" # datafile = position+appname+".json" # schemafile = position+appname+".jsd" printverbose(2, "#------------------------------------------") printverbose(1, "jsonpointer_selftest_data: load " + str(datafile)) printverbose(1, "jsonpointer_selftest_data: load " + str(schemafile)) printverbose(2, "#------------------------------------------") if debug: printverbose(0, "DBG:self.schemafile= " + str(datafile)) printverbose(0, "DBG:self.schemafile= " + str(schemafile)) _kargs = {} _kargs['debug'] = debug _kargs['verbose'] = _verbose > 2 _kargs['pathlist'] = [position] _kargs['validator'] = MODE_SCHEMA_DRAFT3 _kargs['datafile'] = datafile _kargs['schemafile'] = schemafile configdata = ConfigData(appname, **_kargs) jsonptr = JSONPointer('/address') if not jsonptr: raise BaseException("Failed to create JSONPointer") jsonptrdata = jsonptr.get_node(configdata.data) jsx = str(jsonptrdata) ref = "{u'city': u'New York', u'streetAddress': u'21 2nd Street', u'houseNumber': 12}" assert jsx == ref jsonptr = JSONPointer('/phoneNumber/0/type') if not jsonptr: raise BaseException("Failed to create JSONPointer") jsonptrdata = jsonptr.get_node_or_value(configdata.data) assert jsonptrdata == "home" jsonptr = JSONPointer('/phoneNumber/0/number') if not jsonptr: raise BaseException("Failed to create JSONPointer") jsonptrdata = jsonptr.get_node_or_value(configdata.data) assert jsonptrdata == "212 555-1234" jsonptr = JSONPointer('/phoneNumber/0/active') if not jsonptr: raise BaseException("Failed to create JSONPointer") jsonptrdata = jsonptr.get_node_or_value(configdata.data) assert jsonptrdata == True jsonptr = JSONPointer('/phoneNumber/0/private') if not jsonptr: raise BaseException("Failed to create JSONPointer") jsonptrdata = jsonptr.get_node_or_value(configdata.data) assert jsonptrdata == False jsonptr = JSONPointer('/phoneNumber/0/addons') if not jsonptr: raise BaseException("Failed to create JSONPointer") jsonptrdata = jsonptr.get_node_or_value(configdata.data) assert jsonptrdata == None jsonptr = JSONPointer('/phoneNumber/0/index') if not jsonptr: raise BaseException("Failed to create JSONPointer") jsonptrdata = jsonptr.get_node_or_value(configdata.data) assert jsonptrdata == 0 jsonptr = JSONPointer('/phoneNumber/0/testnumber') if not jsonptr: raise BaseException("Failed to create JSONPointer") jsonptrdata = jsonptr.get_node_or_value(configdata.data) assert jsonptrdata == 1.5 printverbose(2, "")
def load_appname(appname): """Loads and verifies the self test 'selftest.json'. Therefore the result of the creation of JSONDataSerializer is compared to the load by json.load(). """ position = os.path.abspath(os.path.dirname(__file__)) + os.sep # case=0: datafile = position + appname + ".json" schemafile = position + appname + ".jsd" printverbose(2, "#------------------------------------------") printverbose(1, "load_appname: load " + str(datafile)) printverbose(2, "#------------------------------------------") if debug: printverbose(0, "DBG:self.schemafile= " + str(datafile)) printverbose(0, "DBG:self.schemafile= " + str(schemafile)) _kargs = {} _kargs['debug'] = debug _kargs['verbose'] = _verbose > 2 _kargs['pathlist'] = [position] _kargs['validator'] = MODE_SCHEMA_OFF _kargs['datafile'] = datafile _kargs['schemafile'] = schemafile configdata = ConfigData(appname, **_kargs) # jval if not os.path.isfile(datafile): raise BaseException("Missing JSON data:file=" + str(datafile)) # load data with open(datafile) as data_file: jval = json.load(data_file) if jval == None: raise BaseException("Failed to load data:" + str(data_file)) printverbose(2, "check data[customers]...") for l in [ 'domestic', 'abroad', ]: printverbose(2, "check data[customers][" + l + "]...") for n in [ 0, 1, ]: printverbose(2, "check data[customers][" + l + "][" + str(n) + "]...") cdata = configdata.data["customers"][l][n]["name"] jdata = jval["customers"][l][n]["name"] assert cdata == jdata printverbose( 2, "check data[customers][" + l + "][" + str(n) + "][name]...OK") cdata = configdata.data["customers"][l][n]["industries"] jdata = configdata.data["customers"][l][n]["industries"] assert cdata == jdata printverbose( 2, "check data[customers][" + l + "][" + str(n) + "][industries]...OK") for p in [ 0, 1, ]: cdata = configdata.data["customers"][l][n]["products"][p][ "name"] jdata = configdata.data["customers"][l][n]["products"][p][ "name"] assert cdata == jdata printverbose( 2, "check data[customers][" + l + "][" + str(n) + "][products][" + str(p) + "][name]...OK") cdata = configdata.data["customers"][l][n]["products"][p][ "quantities"] jdata = configdata.data["customers"][l][n]["products"][p][ "quantities"] assert cdata == jdata printverbose( 2, "check data[customers][" + l + "][" + str(n) + "][products][" + str(p) + "][quantities]...OK") cdata = configdata.data["customers"][l][n]["products"][p][ "priority"] jdata = configdata.data["customers"][l][n]["products"][p][ "priority"] assert cdata == jdata printverbose( 2, "check data[customers][" + l + "][" + str(n) + "][products][" + str(p) + "][priority]...OK") printverbose(2, "")
def load_data(appname): """Loads and verifies the self test 'data.json'. Therefore the result of the creation of JSONDataSerializer is compared to the load by json.load(). """ position = os.path.abspath(os.path.dirname(__file__)) + os.sep # case=0: datafile = position + "data.json" schemafile = position + "schema.jsd" printverbose(2, "#------------------------------------------") printverbose(1, "load_data: load " + str(datafile)) printverbose(2, "#------------------------------------------") if debug: printverbose(0, "DBG:self.schemafile= " + str(datafile)) printverbose(0, "DBG:self.schemafile= " + str(schemafile)) _kargs = {} _kargs['debug'] = debug _kargs['verbose'] = _verbose > 2 _kargs['pathlist'] = [position] _kargs['datafile'] = datafile _kargs['schemafile'] = schemafile _kargs['validator'] = MODE_SCHEMA_OFF configdata = ConfigData(appname, **_kargs) # jval if not os.path.isfile(datafile): raise BaseException("Missing JSON data:file=" + str(datafile)) # load data with open(datafile) as data_file: jval = json.load(data_file) if jval == None: raise BaseException("Failed to load data:" + str(data_file)) printverbose(2, "check data...") for l in [ 'address', ]: printverbose(2, "check data[" + l + "]...") for n in [ 'streetAddress', "city", "houseNumber", ]: cdata = configdata.data[l][n] jdata = jval[l][n] assert cdata == jdata printverbose(2, "check data[" + l + "][" + str(n) + "]...OK") for l in [ 'phoneNumber', ]: printverbose(2, "check data[" + l + "]...") for n in [ 0, ]: printverbose(2, "check data[" + l + "][" + str(n) + "]...") for p in [ 'type', "number", ]: cdata = configdata.data[l][n][p] jdata = jval[l][n][p] assert cdata == jdata printverbose( 2, "check data[" + l + "][" + str(n) + "][" + str(p) + "]...OK") printverbose(2, "")
print str(__version__) sys.exit() elif _o in ("--Version"): print "app: " + str(_APPNAME) print "version: " + str(__version__) print "author: " + str(__author__) print "copyright:" + str(__copyright__) print "license: " + str(__license__) print "file: " + str(os.path.basename(__file__)) sys.exit() else: assert False, "unhandled option" if _selftest: try: from jsondata.Selftest import runselftest except Exception as e: print "\n#\n#*** Set 'PYTHONPATH' (" + str(e) + "\n" print "\n#sys.path=" + str(sys.path) + "\n#\n" # name of application, used for several filenames as default _appname = "selftest" if _verbose > 0: _kargs['_verbose'] = _verbose stest = runselftest(_appname, **_kargs) else: if _verbose > 0: _kargs['verbose'] = True configdata = ConfigData(_appname, **_kargs)
def setUp(self): self.schemaRef = { "required": False, "_comment": "This is a comment to be dropped by the initial scan:object(0)", "_doc": "Concatenated for the same instance.:object(0)", "$schema": "http://json-schema.org/draft-03/schema", "type": "object", "properties": { "phoneNumber": { "items": { "required": False, "type": "object", "properties": { "type": { "required": False, "type": "string" }, "number": { "required": False, "type": "string" } } }, "_comment": "This is a comment(1):array", "required": False, "type": "array" }, "address": { "_comment": "This is a comment(0):address", "required": True, "type": "object", "properties": { "city": { "required": True, "type": "string" }, "streetAddress": { "required": True, "type": "string" }, "houseNumber": { "required": False, "type": "number" } } } } } # def setUp(self): # """Create a configuration object, load by provided file list. # """ global appname _path = mypath + os.sep + 'datasets' + os.sep + 'basic' + os.sep + 'set00' + os.sep + 'testdata.json' kargs = {} kargs['filelist'] = [_path] kargs['nodefaultpath'] = True kargs['nosubdata'] = True kargs['pathlist'] = os.path.dirname(__file__) kargs['validator'] = MODE_SCHEMA_DRAFT4 self.configdata = ConfigData(appname, **kargs) self.jval = None self.sval = None pass