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 test_simple_roundtrip(self): obj = self.C() # Things that are excluded by default obj.containerId = 'foo' obj.creator = 'foo2' obj.id = 'id' # Things that should go obj.A1 = 1 obj.A2 = "2" # Things that should be excluded dynamically # Functions used to be specifically excluded, but not anymore # def l(): pass # obj.A3 = l obj._A4 = 'A' self.A5 = "Not From Init" ext = toExternalObject(obj) newObj = self.C() newObj.updateFromExternalObject(ext) for attr in set(obj._excluded_out_ivars_) | set(['A5']): assert_that(newObj, does_not(has_attr(attr))) assert_that(ext, does_not(has_key("A5"))) # assert_that( ext, does_not( has_key( 'A3' ) ) ) assert_that(ext, does_not(has_key('_A4'))) assert_that(newObj.A1, is_(1)) assert_that(newObj.A2, is_("2"))
def test_unicode_field_name_non_field(self): class IFoo(interface.Interface): field = interface.Attribute("An attribute") class Foo(object): pass foo = Foo() self._callFUT(foo, IFoo, u'field', 42)() assert_that(foo, has_attr('field', 42))
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_unicode_field_name_field_non_property(self): from zope.schema import TextLine class IFoo(interface.Interface): field = TextLine(title=u'text') class Foo(object): pass foo = Foo() self._callFUT(foo, IFoo, u'field', u'text')() assert_that(foo, has_attr('field', u'text'))
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 test_before_object_assigned_event_fired_valid_value(self): thing = Thing() root = Bar() validate_named_field_value(root, IBar, 'thing', thing)() events = eventtesting.getEvents(IBeforeObjectAssignedEvent) assert_that(events, has_length(1)) assert_that(events[0], has_attr('object', is_(same_instance(thing)))) events = eventtesting.getEvents(IFieldUpdatedEvent) assert_that(events, has_length(1))
def test_unicode_field_name_field_non_property_readonly(self): from zope.schema import TextLine class IFoo(interface.Interface): field = TextLine(title=u'text', readonly=True) field.setTaggedValue('_ext_allow_initial_set', True) class Foo(object): pass foo = Foo() self._callFUT(foo, IFoo, u'field', u'text')() assert_that(foo, has_attr('field', u'text'))
def test_before_object_assigned_event_fired_invalid_value_fixed(self): thing = Thing() root = Bar() class NotThing(object): def __conform__(self, iface): return thing if iface is IThing else None validate_named_field_value(root, IBar, 'thing', NotThing())() events = eventtesting.getEvents(IBeforeObjectAssignedEvent) assert_that(events, has_length(1)) assert_that(events[0], has_attr('object', is_(same_instance(thing)))) events = eventtesting.getEvents(IFieldUpdatedEvent) assert_that(events, has_length(1))
def test_from_bytes(self): from zope.schema.interfaces import IFromBytes from zope.schema import Field @interface.implementer(IFromBytes) class OnlyBytes(Field): _type = bytes def fromBytes(self, _value): return b'from bytes' class IFoo(interface.Interface): field = OnlyBytes(title=u'text') class Foo(object): pass foo = Foo() self._callFUT(foo, IFoo, u'field', b'text')() assert_that(foo, has_attr('field', b'from bytes'))
def test_hookable_set_external_identifiers(self): assert_that(set_external_identifiers, has_attr('implementation', is_not(none())))