def test_nested_single_object_field_names_match_non_primitive(self): class INestedThing(interface.Interface): value = Int(title=u"An integer") class IMiddleThing(interface.Interface): nested = Object(INestedThing) class IRoot(interface.Interface): field = Object(IMiddleThing) class IO(InterfaceObjectIO): _ext_iface_upper_bound = IRoot component.provideAdapter(IO, adapts=(IRoot,)) class IO2(InterfaceObjectIO): _ext_iface_upper_bound = INestedThing component.provideAdapter(IO2, adapts=(INestedThing,)) class IO3(InterfaceObjectIO): _ext_iface_upper_bound = IMiddleThing component.provideAdapter(IO3, adapts=(IMiddleThing,)) @interface.implementer(IRoot) class Root(object): def __init__(self): self.field = None @interface.implementer(IMiddleThing) class MiddleThing(object): def __init__(self): self.nested = None @interface.implementer(INestedThing) class NestedThing(object): def __init__(self): self.value = -1 IRoot['field'].setTaggedValue('__external_factory__', MiddleThing) IMiddleThing['nested'].setTaggedValue('__external_factory__', NestedThing) external = {'field': {'nested': {'value': 42}}} root = Root() update_from_external_object(root, external, require_updater=True) assert_that(root, has_attr('field', is_(MiddleThing))) assert_that(root.field, has_attr('nested', is_(NestedThing))) assert_that(root.field, has_attr('nested', has_attr('value', 42)))
def update_from_external_object_time_func(loops, ext): factory = default_externalized_object_factory_finder(ext) # Updating *may* modify the object in place, so we need to # make deep copies of it. rep = JsonRepresenter() json = rep.dump(ext) exts = [rep.load(json) for x in range(loops * INNER_LOOPS)] begin = perf_counter() for x in exts: obj = factory() update_from_external_object(obj, x) end = perf_counter() return end - begin
def test_user_profile(self): from nti.externalization.tests.benchmarks.objects import Address from nti.externalization.tests.benchmarks.objects import UserProfile home_address = Address( full_name=u'Steve Jobs', street_address_1=u'1313 Mockingbird Lane', city=u'Salem', state=u'MA', postal_code=u'6666', country=u'USA', ) work_address = Address( full_name=u'Apple', street_address_1=u'1 Infinite Loop', city=u'Cupertino', state=u'CA', postal_code=u'55555', country=u'USA', ) user_profile = UserProfile( addresses={u'home': home_address, u'work': work_address}, phones={u'home': u'405-555-1212', u'work': u'405-555-2323'}, contact_emails={u'home': u'*****@*****.**', u'work': u'*****@*****.**'}, avatarURL='http://apple.com/steve.png', backgroundURL='https://apple.com/bg.jpeg', alias=u'Steve', realname=u'Steve Jobs', ) mt = getattr(UserProfile, 'mimeType') assert_that(mt, is_('application/vnd.nextthought.benchmarks.userprofile')) ext = toExternalObject(user_profile) assert_that(ext, has_key(StandardExternalFields.MIMETYPE)) assert_that(ext['addresses'], has_key('home')) assert_that(ext['addresses']['home'], has_key("city")) addr = update_from_external_object(Address(), toExternalObject(home_address)) assert_that(addr, is_(home_address)) prof2 = update_from_external_object(UserProfile(), toExternalObject(user_profile)) assert_that(prof2, is_(user_profile))
def test_nested_single_object_field_names_match_non_primitive_zcml(self): # The same as test_nested_single_object_field_names_match_non_primitive # but configured through ZCML using global objects. from zope.configuration import xmlconfig zcml = """ <configure xmlns:ext="http://nextthought.com/ntp/ext"> <include package="nti.externalization" file="meta.zcml" /> <ext:registerAutoPackageIO root_interfaces=".IGlobalNestedThing .IGlobalMiddleThing .IGlobalRoot" iobase=".IOBase" modules="{0}" /> <ext:anonymousObjectFactory for=".IGlobalRoot" field="field" factory=".GlobalMiddleThing" /> <ext:anonymousObjectFactory for=".IGlobalMiddleThing" field="nested" factory=".GlobalNestedThing" /> </configure> """.format(__name__) context = xmlconfig.ConfigurationMachine() xmlconfig.registerCommonDirectives(context) context.package = sys.modules[__name__] xmlconfig.string(zcml, context) external = {'field': {'nested': {'value': 42}, 'nested_dict': {'key': {'value': 24}}}} root = GlobalRoot() update_from_external_object(root, external, require_updater=True) assert_that(root, has_attr('field', is_(GlobalMiddleThing))) assert_that(root.field, has_attr('nested', is_(GlobalNestedThing))) assert_that(root.field, has_attr('nested', has_attr('value', 42)))
def test_sequence_object_field_names_match_non_primitive(self): class INestedThing(interface.Interface): value = Int(title=u"An integer") class IRoot(interface.Interface): field = List(Object(INestedThing)) class IO(InterfaceObjectIO): _ext_iface_upper_bound = IRoot component.provideAdapter(IO, adapts=(IRoot,)) class IO2(InterfaceObjectIO): _ext_iface_upper_bound = INestedThing component.provideAdapter(IO2, adapts=(INestedThing,)) @interface.implementer(IRoot) class Root(object): def __init__(self): self.field = () @interface.implementer(INestedThing) class NestedThing(object): def __init__(self): self.value = -1 IRoot['field'].setTaggedValue('__external_factory__', NestedThing) external = {'field': [{'value': 42}, {'value': 2018}]} root = Root() update_from_external_object(root, external, require_updater=True) assert_that(root, has_attr('field', is_(list))) assert_that(root.field[0], has_attr('value', 42)) assert_that(root.field[1], has_attr('value', 2018))
def ds_arg(): obj = DSArg() update_from_external_object(obj, ext)
def context_arg(): obj = ContextArg() update_from_external_object(obj, ext)
def no_args(): obj = NoArgs() update_from_external_object(obj, ext)
def from_(): obj = default_externalized_object_factory_finder(ext)() update_from_external_object(obj, ext)