Source code for UseCases.libs.JSONData.branch_copy.dict_item_new.CallCase
"""Append list element.
"""
from __future__ import absolute_import
import unittest
import os
import sys
# pre-set the base JSON libraries for 'jsondata' by PyUnit call
if 'ujson' in sys.argv:
import ujson as myjson
elif 'json' in sys.argv:
import json as myjson
else:
import json as myjson
import jsonschema
# import 'jsondata'
from jsondata.JSONDataSerializer import JSONDataSerializer as ConfigData
from jsondata.JSONDataSerializer import MODE_SCHEMA_DRAFT4
from jsondata.JSONDataExceptions import JSONDataKeyError,JSONDataNodeType
# name of application, used for several filenames as MODE_SCHEMA_DRAFT4
_APPNAME = "jsondc"
appname = _APPNAME
#
#######################
#
[docs]class CallUnits(unittest.TestCase):
"""Base branch_add.
"""
[docs] def testCase000(self):
"""Load initial main/master data, and validate it with standard draft4.
"""
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
[docs] 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'][0]
ret = configdata.branch_copy(target, 'newitem', branchdata['phoneNumber'][0]['type'])
assert ret == True
conf_dat = repr(configdata.data)
conf_dat = """{u'phoneNumber': [{'newitem': u'home', 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
if __name__ == '__main__':
unittest.main()