def _check_result_for_edit(self, data, result):
        
        key_prefix = dtype = result.get('dataset_type')
        obj_factory = factory_for_metadata(dtype)

        keys = data.keys()

        core_keys = set(keys) & self.core_keys
        for key in core_keys:
            assert data[key] == result[key]
        
        tags = set(map(lambda t: t['name'] ,data['tags']))
        result_tags = set(map(lambda t: t['name'] ,result['tags']))
        assert tags == result_tags

        result_dict = result.get(key_prefix, {})
        result_obj = obj_factory().from_dict(result_dict, is_flat=False, opts={ 
            'unserialize-values': 'json-s' 
        })
        result_flattened_dict = result_obj.to_dict(flat=True, opts={
            'serialize-keys': True, 
            'serialize-values': 'json-s', 
            'key-prefix': key_prefix 
        })
        dt_keys = filter(lambda t: t.startswith(key_prefix + '.'), keys)
        missing_keys = set(dt_keys) - set(result_flattened_dict.keys())
        assert not missing_keys
        for k in dt_keys:
            assert result_flattened_dict[k] == data[k]

        return
    def _test_1(self, dtype, i, name, source_lang, lang):
       
        pkg = self.packages[dtype][i][-1]
        md = factory_for_metadata(dtype)()
        md.from_dict(pkg[dtype], is_flat=0, opts={'unserialize-values': 1})
        assert md.identifier and check_uuid(md.identifier)
        
        translator = translator_for(md, source_lang)
        assert translator
        assert IMetadataTranslator in zope.interface.providedBy(translator)
        
        # Read a translated view
        
        md_1 = translator.get(lang)
        assert md_1 and isinstance(md_1, type(md))
        assert ITranslatedMetadata in zope.interface.providedBy(md_1)
        assert md_1 == md, 'Nothing is translated yet, should be identical'  

        # Tranlate a few fields
        
        translations = {} 
        for key in self.translatable_fields[dtype]:
            yf = md.get_field(key)
            translations[key] = ' ** TRANSLATE ** ' + yf.context.value
            yf.context.key = (dtype,) + key
            tr = translator.get_field_translator(yf)
            yf1 = tr.translate(lang, translations[key])
            assert yf1 and yf1.context.value
            assert yf1.context.value == translations[key]

        # Re-read a translated view
        
        md_2 = translator.get(lang)
        assert md_2 and isinstance(md_2, type(md))
        assert ITranslatedMetadata in zope.interface.providedBy(md_2)
        assert md_2 != md, 'Some fields should be translated'  
        
        changes = list(dictdiffer.diff(md.to_dict(), md_2.to_dict())) 
        translated_keys = set(translations.keys())
        assert len(changes) > 0
        for changetype, skey, (changed_from, changed_to) in changes:
            assert changetype == 'change'
            key = tuple(skey.split('.'))
            assert key in translations
            assert translations[key] == changed_to
            yf0, yf2 = md.get_field(key), md_2.get_field(key)
            assert yf0.context.value == changed_from 
            assert yf2.context.value == changed_to 
            assert key in translated_keys
            translated_keys.remove(key)
        assert not translated_keys, 'All keys should be consumed'
        
        pass
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'