Example #1
0
def build_adaptee(field, expand_collection=True):
    '''Build an adaptee vector for a field instance.
    
    This vector is to be used while trying to adapt on fields (for serialization,
    formatting, widgets etc.).
    '''

    # Load (if not already) the object-factory lookup function.
    # Note it must be lazily loaded, as is not available at module's load time.
    from ckanext.publicamundi.lib.metadata import factory_for

    # Build adaptee vector

    adaptee = [field]

    if not expand_collection:
        return adaptee

    y = field
    while IContainerField.providedBy(y):
        adaptee.append(y.value_type)
        y = y.value_type

    if not (y is field) and IObjectField.providedBy(y):
        # Need a multiadapter for a (probably nested) container of objects:
        # replace field (instance of ObjectField) with a dummy object
        adaptee[-1] = factory_for(y.schema)()

    return adaptee
def build_adaptee(field, expand_collection=True):
    '''Build an adaptee vector for a field instance.
    
    This vector is to be used while trying to adapt on fields (for serialization,
    formatting, widgets etc.).
    '''

    # Load (if not already) the object-factory lookup function. 
    # Note it must be lazily loaded, as is not available at module's load time.
    from ckanext.publicamundi.lib.metadata import factory_for
    
    # Build adaptee vector

    adaptee = [field]
    
    if not expand_collection:
        return adaptee

    y = field
    while IContainerField.providedBy(y):
        adaptee.append(y.value_type)
        y = y.value_type
     
    if not (y is field) and IObjectField.providedBy(y):
        # Need a multiadapter for a (probably nested) container of objects:
        # replace field (instance of ObjectField) with a dummy object
        adaptee[-1] = factory_for(y.schema)()

    return adaptee
def test_factories():
    
    # Test with IFooMetadata

    f1 = factory_for(schemata.IFooMetadata)
    o1 = f1()
    verifyObject(schemata.IFooMetadata, o1)
    assert isinstance(o1, types.FooMetadata)
    
    f1 = factory_for_metadata('foo')
    o1 = f1()
    verifyObject(schemata.IFooMetadata, o1)
    assert isinstance(o1, types.FooMetadata)
    
    c1 = class_for(schemata.IFooMetadata)
    verifyObject(IIntrospective, c1, tentative=1)
    o1 = c1()
    verifyObject(schemata.IFooMetadata, o1)
    assert c1 is types.FooMetadata

    c1 = class_for_metadata('foo')
    verifyObject(IIntrospective, c1, tentative=1)
    o1 = c1()
    verifyObject(schemata.IFooMetadata, o1)
    assert c1 is types.FooMetadata
    
    # Test with AnotherFoo
    
    def check_foo1_after_init(o):
        return (
            o.baz == u'Baobab' and 
            o.temporal_extent is None and
            isinstance(o.contact_info, types.ContactInfo))
       
    f2 = factory_for(schemata.IFooMetadata, name='foo-1')
    o2 = f2()
    verifyObject(schemata.IFooMetadata, o2)
    assert isinstance(o2, AnotherFoo)
    assert check_foo1_after_init(o2)

    f2 = factory_for_metadata('foo.1')
    o2 = f2()
    verifyObject(schemata.IFooMetadata, o2)
    assert isinstance(o2, AnotherFoo)
    assert check_foo1_after_init(o2)

    c2 = class_for(schemata.IFooMetadata, 'foo-1')
    verifyObject(IIntrospective, c2, tentative=1)
    o2 = c2()
    verifyObject(schemata.IFooMetadata, o2)
    assert c2 is AnotherFoo
    assert check_foo1_after_init(o2)

    c2 = class_for_metadata('foo.1')
    verifyObject(IIntrospective, c2, tentative=1)
    o2 = c2()
    verifyObject(schemata.IFooMetadata, o2)
    assert c2 is AnotherFoo
    assert check_foo1_after_init(o2)

    # Test with non-registered names

    try:
        f3 = factory_for(schemata.IFooMetadata, name='a-non-existing-name')
    except (ValueError, LookupError) as ex:
        pass
    else:
        assert False, 'This should have failed'

    try:
        f3 = factory_for_metadata('')
    except ValueError as ex:
        pass
    else:
        assert False, 'This should have failed (a name is required!)'
   
    try:
        f3 = factory_for_metadata('foo.9')
    except (ValueError, LookupError) as ex:
        pass
    else:
        assert False, 'This should have failed'
def test_factories():

    # Test with IFooMetadata

    f1 = factory_for(schemata.IFooMetadata)
    o1 = f1()
    verifyObject(schemata.IFooMetadata, o1)
    assert isinstance(o1, types.FooMetadata)

    f1 = factory_for_metadata('foo')
    o1 = f1()
    verifyObject(schemata.IFooMetadata, o1)
    assert isinstance(o1, types.FooMetadata)

    c1 = class_for(schemata.IFooMetadata)
    verifyObject(IIntrospective, c1, tentative=1)
    o1 = c1()
    verifyObject(schemata.IFooMetadata, o1)
    assert c1 is types.FooMetadata

    c1 = class_for_metadata('foo')
    verifyObject(IIntrospective, c1, tentative=1)
    o1 = c1()
    verifyObject(schemata.IFooMetadata, o1)
    assert c1 is types.FooMetadata

    # Test with AnotherFoo

    def check_foo1_after_init(o):
        return (o.baz == u'Baobab' and o.temporal_extent is None
                and isinstance(o.contact_info, types.ContactInfo))

    f2 = factory_for(schemata.IFooMetadata, name='foo-1')
    o2 = f2()
    verifyObject(schemata.IFooMetadata, o2)
    assert isinstance(o2, AnotherFoo)
    assert check_foo1_after_init(o2)

    f2 = factory_for_metadata('foo.1')
    o2 = f2()
    verifyObject(schemata.IFooMetadata, o2)
    assert isinstance(o2, AnotherFoo)
    assert check_foo1_after_init(o2)

    c2 = class_for(schemata.IFooMetadata, 'foo-1')
    verifyObject(IIntrospective, c2, tentative=1)
    o2 = c2()
    verifyObject(schemata.IFooMetadata, o2)
    assert c2 is AnotherFoo
    assert check_foo1_after_init(o2)

    c2 = class_for_metadata('foo.1')
    verifyObject(IIntrospective, c2, tentative=1)
    o2 = c2()
    verifyObject(schemata.IFooMetadata, o2)
    assert c2 is AnotherFoo
    assert check_foo1_after_init(o2)

    # Test with non-registered names

    try:
        f3 = factory_for(schemata.IFooMetadata, name='a-non-existing-name')
    except (ValueError, LookupError) as ex:
        pass
    else:
        assert False, 'This should have failed'

    try:
        f3 = factory_for_metadata('')
    except ValueError as ex:
        pass
    else:
        assert False, 'This should have failed (a name is required!)'

    try:
        f3 = factory_for_metadata('foo.9')
    except (ValueError, LookupError) as ex:
        pass
    else:
        assert False, 'This should have failed'