def test_new_stage_collection(self): ent = create_entity(entity_id=2, entity_text='too2') mb = MyEntityMember.create_from_entity(ent) nscoll = create_staging_collection(IMyEntity) self.assert_equal(len(nscoll), 0) nscoll.add(mb) self.assert_equal(len(nscoll), 1)
def test_new_stage_collection(self): ent = create_entity(entity_id=2, entity_text='too2') mb = MyEntityMember.create_from_entity(ent) nscoll = create_staging_collection(IMyEntity) self.assert_equal(len(nscoll), 0) nscoll.add(mb) self.assert_equal(len(nscoll), 1)
def test_update_nested_collection_from_data(self, resource_repo): # Set up member that has one child. coll = resource_repo.get_collection(IMyEntity) ent = MyEntity(id=1) child0 = MyEntityChild(id=0) ent.children.append(child0) mb = coll.create_member(ent) # Set up another member with two children with different IDs. tmp_coll = create_staging_collection(IMyEntity) upd_ent = MyEntity(id=1) child1 = MyEntityChild(id=1) child1.parent = upd_ent child2 = MyEntityChild(id=2) child2.parent = upd_ent upd_ent.children.append(child1) upd_ent.children.append(child2) upd_mb = tmp_coll.create_member(upd_ent) rpr = as_representer(mb, CsvMime) attribute_options = {('children',):{IGNORE_OPTION:False, WRITE_AS_LINK_OPTION:False}, } with rpr.with_updated_configuration(attribute_options= attribute_options): de = rpr.resource_to_data(upd_mb) mb.update(de) assert set([mb.id for mb in mb.children]) == set([1, 2])
def test_update_nested_collection_from_data(self, resource_repo): # Set up member that has one child. coll = resource_repo.get_collection(IMyEntity) ent = MyEntity(id=1) child0 = MyEntityChild(id=0) ent.children.append(child0) mb = coll.create_member(ent) # Set up another member with two children with different IDs. tmp_coll = create_staging_collection(IMyEntity) upd_ent = MyEntity(id=1) child1 = MyEntityChild(id=1) child1.parent = upd_ent child2 = MyEntityChild(id=2) child2.parent = upd_ent upd_ent.children.append(child1) upd_ent.children.append(child2) upd_mb = tmp_coll.create_member(upd_ent) rpr = as_representer(mb, CsvMime) attribute_options = { ('children', ): { IGNORE_OPTION: False, WRITE_AS_LINK_OPTION: False }, } with rpr.with_updated_configuration( attribute_options=attribute_options): de = rpr.resource_to_data(upd_mb) mb.update(de) assert set([mb.id for mb in mb.children]) == set([1, 2])
def test_update_member_with_link(self): my_entity = create_entity() new_parent = MyEntityParent() new_parent.text = self.UPDATED_TEXT new_parent.id = 2 my_entity.parent = new_parent coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) mp = self._make_mapping() attribute_options = {('parent',):{WRITE_AS_LINK_OPTION:True}, ('nested_parent',):{IGNORE_OPTION:True}} mp_cloned = mp.clone(attribute_options=attribute_options) data_el = mp_cloned.map_to_data_element(member) # The linked-to parent needs to be in the root collection. my_entity.parent = None del member del my_entity parent_coll = get_root_collection(IMyEntityParent) parent_coll.create_member(new_parent) my_entity = create_entity() coll = get_root_collection(IMyEntity) context = coll.create_member(my_entity) self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT) context.update_from_data(data_el) self.assert_equal(context.parent.text, self.UPDATED_TEXT)
def _upload_metadata(self, xls_filename, scenario, app): # First, create a new metadata entity and POST it. emd_coll = create_staging_collection(IExperimentMetadata) emd = ExperimentMetadata('unit_test_metadata', next(iter(get_root_aggregate(ISubproject))), 1, get_experiment_metadata_type(scenario)) mb_emd = emd_coll.create_member(emd) rpr = as_representer(mb_emd, XmlMime) emd_rpr = rpr.to_string(mb_emd) res = app.post("/%s" % emd_coll.__name__, params=emd_rpr, content_type=XmlMime.mime_type_string, status=HTTPCreated.code) self.__session.commit() mb_url = res.headers['Location'] # Now, PUT the excel meta data file. self.__session.begin_nested() with open(xls_filename, 'rb') as xls_file: res = app.put(mb_url, params=xls_file.read(), content_type=XlsMime.mime_type_string) # If the file had warnings, we have to repeat the PUT. if res.status.endswith(HTTPTemporaryRedirect.title): self.__session.rollback() # 307 Redirect: Repeat with warnings disabled. with open(xls_filename, 'rb') as xls_file: res = app.put(res.headers['Location'], params=xls_file.read(), content_type=XlsMime.mime_type_string, status=HTTPOk.code) self.__session.commit() assert res.status.endswith(HTTPOk.title) return mb_url
def _upload_metadata(self, xls_filename, scenario, app): # First, create a new metadata entity and POST it. emd_coll = create_staging_collection(IExperimentMetadata) emd = ExperimentMetadata('unit_test_metadata', next(iter(get_root_aggregate(ISubproject))), 1, get_experiment_metadata_type(scenario) ) mb_emd = emd_coll.create_member(emd) rpr = as_representer(mb_emd, XmlMime) emd_rpr = rpr.to_string(mb_emd) res = app.post("/%s" % emd_coll.__name__, params=emd_rpr, content_type=XmlMime.mime_type_string, status=HTTPCreated.code) self.__session.commit() mb_url = res.headers['Location'] # Now, PUT the excel meta data file. self.__session.begin_nested() with open(xls_filename, 'rb') as xls_file: res = app.put(mb_url, params=xls_file.read(), content_type=XlsMime.mime_type_string) # If the file had warnings, we have to repeat the PUT. if res.status.endswith(HTTPTemporaryRedirect.title): self.__session.rollback() # 307 Redirect: Repeat with warnings disabled. with open(xls_filename, 'rb') as xls_file: res = app.put(res.headers['Location'], params=xls_file.read(), content_type=XlsMime.mime_type_string, status=HTTPOk.code) self.__session.commit() assert res.status.endswith(HTTPOk.title) return mb_url
def test_update_nested_collection_from_data(self): # Set up member that has one child. root_coll = get_root_collection(IMyEntity) ent = MyEntity(id=1) child0 = MyEntityChild(id=0) ent.children.append(child0) mb = root_coll.create_member(ent) # Set up another member with two children with different IDs. stg_coll = create_staging_collection(IMyEntity) upd_ent = MyEntity(id=1) child1 = MyEntityChild(id=1) child1.parent = upd_ent child2 = MyEntityChild(id=2) child2.parent = upd_ent upd_ent.children.append(child1) upd_ent.children.append(child2) upd_mb = stg_coll.create_member(upd_ent) rpr = as_representer(mb, CsvMime) attribute_options = {('children',):{IGNORE_OPTION:False, WRITE_AS_LINK_OPTION:False}, } rpr.configure(attribute_options=attribute_options) de = rpr.data_from_resource(upd_mb) # mb.update(de) self.assert_equal(set([mb.id for mb in mb.children]), set([1, 2]))
def test_update_member_with_link(self): my_entity = create_entity() new_parent = MyEntityParent() new_parent.text = self.UPDATED_TEXT new_parent.id = 2 my_entity.parent = new_parent coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) mp = self._make_mapping() attribute_options = { ('parent', ): { WRITE_AS_LINK_OPTION: True }, ('nested_parent', ): { IGNORE_OPTION: True } } mp_cloned = mp.clone(attribute_options=attribute_options) data_el = mp_cloned.map_to_data_element(member) # The linked-to parent needs to be in the root collection. my_entity.parent = None del member del my_entity parent_coll = get_root_collection(IMyEntityParent) parent_coll.create_member(new_parent) my_entity = create_entity() coll = get_root_collection(IMyEntity) context = coll.create_member(my_entity) self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT) context.update_from_data(data_el) self.assert_equal(context.parent.text, self.UPDATED_TEXT)
def load_collection_from_stream(resource, stream, content_type): """ Creates a new collection for the registered resource and calls `load_into_collection_from_stream` with it. """ coll = create_staging_collection(resource) load_into_collection_from_stream(coll, stream, content_type) return coll
def test_device_representer(self, device_fac, device_type_printer, organization_cenix): dev = device_fac(type=device_type_printer, manufacturer=organization_cenix) coll = create_staging_collection(IDevice) dev_mb = coll.create_member(dev) attrs = ('label', 'model', 'manufacturer.name') self.__test_rpr_for_member(dev_mb, (XmlMime, ), attrs)
def load_collection_from_url(resource, url, content_type=None): """ Creates a new collection for the registered resource and calls `load_into_collection_from_url` with it. """ coll = create_staging_collection(resource) load_into_collection_from_url(coll, url, content_type=content_type) return coll
def test_device_representer(self, device_fac, device_type_printer, organization_cenix): dev = device_fac(type=device_type_printer, manufacturer=organization_cenix) coll = create_staging_collection(IDevice) dev_mb = coll.create_member(dev) attrs = ('label', 'model', 'manufacturer.name') self.__test_rpr_for_member(dev_mb, (XmlMime,), attrs)
def load_collection_from_url(resource, url, content_type=None): """ Creates a new collection for the registered resource and calls `load_into_collection_from_url` with it. """ coll = create_staging_collection(resource) load_into_collection_from_url(coll, url, content_type=content_type) return coll
def load_collection_from_stream(resource, stream, content_type): """ Creates a new collection for the registered resource and calls `load_into_collection_from_stream` with it. """ coll = create_staging_collection(resource) load_into_collection_from_stream(coll, stream, content_type) return coll
def test_set_collection_parent_fails(self): self.config.add_resource(IFoo, FooMember, FooEntity, expose=False) coll = create_staging_collection(IFoo) srvc = get_service() repo_mgr = get_repository_manager() repo = repo_mgr.get(REPOSITORY_TYPES.MEMORY) with self.assert_raises(ValueError) as cm: repo.set_collection_parent(coll, srvc) self.assert_true(cm.exception.message.startswith('No root collect'))
def test_set_collection_parent_fails(self): self.config.add_resource(IFoo, FooMember, FooEntity, expose=False) coll = create_staging_collection(IFoo) srvc = get_service() repo_mgr = get_repository_manager() repo = repo_mgr.get(REPOSITORY_TYPES.MEMORY) with self.assert_raises(ValueError) as cm: repo.set_collection_parent(coll, srvc) self.assert_true(cm.exception.message.startswith('No root collect'))
def test_set_collection_parent_fails(self, class_configurator, resource_repo): class_configurator.add_resource(IFoo, FooMember, FooEntity, expose=False) coll = create_staging_collection(IFoo) srvc = get_service() with pytest.raises(ValueError) as cm: resource_repo.set_collection_parent(coll, srvc) assert str(cm.value).startswith('No root collect')
def _make_test_entity_member(): parent = MyEntityParent(id=0) entity = MyEntity(id=0, parent=parent) parent.child = entity child = MyEntityChild(id=0, parent=entity) entity.children.append(child) grandchild = MyEntityGrandchild(id=0, parent=child) child.children.append(grandchild) coll = create_staging_collection(IMyEntity) return coll.create_member(entity)
def test_set_collection_parent_fails(self, class_configurator, resource_repo): class_configurator.add_resource(IFoo, FooMember, FooEntity, expose=False) coll = create_staging_collection(IFoo) srvc = get_service() with pytest.raises(ValueError) as cm: resource_repo.set_collection_parent(coll, srvc) assert str(cm.value).startswith('No root collect')
def test_update_collection_from_data_with_id_raises_error(self): coll = create_collection() rpr = as_representer(coll, CsvMime) upd_coll = create_staging_collection(IMyEntity) ent = MyEntity(id=2) upd_coll.create_member(ent) de = rpr.data_from_resource(upd_coll) with self.assert_raises(ValueError) as cm: coll.update_from_data(de) exc_msg = 'New member data should not provide an ID attribute.' self.assert_equal(cm.exception.message, exc_msg)
def test_update_collection_from_data_with_id_raises_error(self): coll = create_collection() rpr = as_representer(coll, CsvMime) upd_coll = create_staging_collection(IMyEntity) ent = MyEntity(id=2) upd_coll.create_member(ent) de = rpr.data_from_resource(upd_coll) with self.assert_raises(ValueError) as cm: coll.update_from_data(de) exc_msg = 'New member data should not provide an ID attribute.' self.assert_equal(cm.exception.message, exc_msg)
def test_add_with_data_element(self): my_entity = MyEntity() my_entity_parent = MyEntityParent() my_entity.parent = my_entity_parent coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) mp = self._make_mapping() data_el = mp.map_to_data_element(member) del member root_coll = get_root_collection(IMyEntity) root_coll.add(data_el) self.assert_equal(len(coll), 1)
def test_add_with_data_element(self): my_entity = MyEntity() my_entity_parent = MyEntityParent() my_entity.parent = my_entity_parent coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) mp = self._make_mapping() data_el = mp.map_to_data_element(member) del member root_coll = get_root_collection(IMyEntity) root_coll.add(data_el) self.assert_equal(len(coll), 1)
def test_update_terminal_in_parent(self): my_entity = create_entity() my_entity.parent.text = self.UPDATED_TEXT coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) mp = self._make_mapping() data_el = mp.map_to_data_element(member) del member del my_entity my_entity = create_entity() coll = get_root_collection(IMyEntity) context = coll.create_member(my_entity) self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT) context.update_from_data(data_el) self.assert_equal(context.parent.text, self.UPDATED_TEXT)
def test_update_terminal_in_parent(self): my_entity = create_entity() my_entity.parent.text = self.UPDATED_TEXT coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) mp = self._make_mapping() data_el = mp.map_to_data_element(member) del member del my_entity my_entity = create_entity() coll = get_root_collection(IMyEntity) context = coll.create_member(my_entity) self.assert_equal(context.parent.text, MyEntity.DEFAULT_TEXT) context.update_from_data(data_el) self.assert_equal(context.parent.text, self.UPDATED_TEXT)
def test_delete_grandchild(self): my_entity = create_entity() del my_entity.children[0].children[0] coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) mp = self._make_mapping() data_el = mp.map_to_data_element(member) del member del my_entity my_entity = create_entity() coll = get_root_collection(IMyEntity) context = coll.create_member(my_entity) self.assert_equal(len(iter(context.children).next().children), 1) context.update_from_data(data_el) self.assert_equal(len(iter(context.children).next().children), 0)
def test_delete_child(self): my_entity = create_entity() del my_entity.children[0] coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) mp = self._make_mapping() data_el = mp.map_to_data_element(member) del member del my_entity my_entity = create_entity() coll = get_root_collection(IMyEntity) context = coll.create_member(my_entity) self.assert_equal(len(context.children), 1) context.update_from_data(data_el) self.assert_equal(len(context.children), 0)
def test_update_nested_member_from_data(self): # Set up member that does not have a parent. root_coll = get_root_collection(IMyEntity) ent = MyEntity(id=1) mb = root_coll.create_member(ent) # Set up second member with same ID that does have a parent. coll = create_staging_collection(IMyEntity) parent = MyEntityParent(id=0) upd_ent = MyEntity(id=1, parent=parent) upd_mb = coll.create_member(upd_ent) rpr = as_representer(mb, CsvMime) attribute_options = {('parent',):{WRITE_AS_LINK_OPTION:False}} rpr.configure(attribute_options=attribute_options) de = rpr.data_from_resource(upd_mb) mb.update(de) self.assert_equal(mb.parent.id, parent.id)
def __dump_entities(self, entity_class): cache = self._get_cache(entity_class) coll_cls = get_collection_class(entity_class) mb_cls = get_member_class(entity_class) fn = get_write_collection_path(coll_cls, self._config['content_type'], directory=self._config['directory']) # Wrap the entities in a temporary collection. coll = create_staging_collection(coll_cls) for ent in cache.iterator(): coll.add(mb_cls.create_from_entity(ent)) # Open stream for writing and dump the collection. stream = file(fn, 'w') with stream: dump_resource(coll, stream, content_type=self._config['content_type'])
def test_update_nested_member_from_data(self, resource_repo): # Set up member that does not have a parent. coll = resource_repo.get_collection(IMyEntity) ent = MyEntity(id=1) mb = coll.create_member(ent) # Set up second member with same ID that does have a parent. tmp_coll = create_staging_collection(IMyEntity) parent = MyEntityParent(id=0) upd_ent = MyEntity(id=1, parent=parent) upd_mb = tmp_coll.create_member(upd_ent) rpr = as_representer(mb, CsvMime) attribute_options = {('parent', ): {WRITE_AS_LINK_OPTION: False}} with rpr.with_updated_configuration( attribute_options=attribute_options): de = rpr.resource_to_data(upd_mb) mb.update(de) assert mb.parent.id == parent.id
def __dump_entities(self, entity_class): cache = self._get_cache(entity_class) coll_cls = get_collection_class(entity_class) mb_cls = get_member_class(entity_class) fn = get_write_collection_path(coll_cls, self._config['content_type'], directory=self._config['directory']) # Wrap the entities in a temporary collection. coll = create_staging_collection(coll_cls) for ent in cache.iterator(): coll.add(mb_cls.create_from_entity(ent)) # Open stream for writing and dump the collection. stream = file(fn, 'w') with stream: dump_resource(coll, stream, content_type=self._config['content_type'])
def test_update_nested_member_from_data(self, resource_repo): # Set up member that does not have a parent. coll = resource_repo.get_collection(IMyEntity) ent = MyEntity(id=1) mb = coll.create_member(ent) # Set up second member with same ID that does have a parent. tmp_coll = create_staging_collection(IMyEntity) parent = MyEntityParent(id=0) upd_ent = MyEntity(id=1, parent=parent) upd_mb = tmp_coll.create_member(upd_ent) rpr = as_representer(mb, CsvMime) attribute_options = {('parent',):{WRITE_AS_LINK_OPTION:False}} with rpr.with_updated_configuration(attribute_options= attribute_options): de = rpr.resource_to_data(upd_mb) mb.update(de) assert mb.parent.id == parent.id
def visit_collection(self, attribute_key, attribute, collection_node, collection_data, is_link_node, parent_data): if is_link_node: url = collection_node.get_url() coll = url_to_resource(url) entities = [mb.get_entity() for mb in coll] else: entities = [] for item in sorted(collection_data.items()): entities.append(item[1]) if len(attribute_key) == 0: # Top level. mapped_cls = collection_node.mapping.mapped_class self.__resource = create_staging_collection(mapped_cls) for ent in entities: self.__resource.create_member(ent) else: parent_data[attribute] = entities
def visit_collection(self, attribute_key, attribute, collection_node, collection_data, is_link_node, parent_data): if is_link_node: url = collection_node.get_url() coll = url_to_resource(url) entities = [mb.get_entity() for mb in coll] else: entities = [] for item in sorted(collection_data.items()): entities.append(item[1]) if len(attribute_key) == 0: # Top level. mapped_cls = collection_node.mapping.mapped_class self.__resource = create_staging_collection(mapped_cls) for ent in entities: self.__resource.create_member(ent) else: parent_data[attribute] = entities
def test_add_child(self): my_entity = create_entity() new_child = MyEntityChild() my_entity.children.append(new_child) coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) self.assert_equal(len(member.children), 2) mp = self._make_mapping() data_el = mp.map_to_data_element(member) del member del my_entity my_entity = create_entity() coll = get_root_collection(IMyEntity) context = coll.create_member(my_entity) self.assert_equal(len(context.children), 1) context.update_from_data(data_el) self.assert_equal(len(context.children), 2)
def test_make_traverser_update(self): ent0 = create_entity(entity_id=0) ent1 = create_entity(entity_id=None) agg = create_staging_collection(IMyEntity).get_aggregate() agg.add(ent0) agg.add(ent1) ent01 = create_entity(entity_id=0) ent11 = create_entity(entity_id=None) # With many as source and one as target. with self.assert_raises(ValueError) as cm: SourceTargetDataTreeTraverser.make_traverser( [ent01, ent1], ent0, RELATION_OPERATIONS.UPDATE, accessor=agg) self.assert_true( cm.exception.args[0].endswith('or both not be sequences.')) # Without target. trv = SourceTargetDataTreeTraverser.make_traverser( [ent01, ent11], None, RELATION_OPERATIONS.UPDATE, accessor=agg) self.assert_is_not_none(getattr(trv, '_tgt_prx'))
def __make_collection(self, resource, parent, children): # Create a new collection. rc_parent = resource.__parent__ rc_repo = None while not rc_parent is None: rc_repo = getattr(rc_parent, '__repository__', None) if not rc_repo is None: break else: rc_parent = rc_parent.__parent__ # Create a new collection. # if not resource.__parent__ is None: # # Use the resource repository that created this resource's # # root collection to create the new collection. # rc_repo = getattr(resource.__parent__, '__repository__', None) # if rc_repo is None: # for rc in lineage(resource.__parent__): # rc_repo = getattr(rc, '__repository__', None) # if not rc_repo is None: # break if not rc_repo is None: coll = rc_repo.get_collection(self.attr_type) else: # This is a floating member. coll = create_staging_collection(self.attr_type) # Set up entity access in the new collection. agg_relationship = Relationship(parent, children, backref=self.__entity_backref) agg = coll.get_aggregate() agg.set_relationship(agg_relationship) # Set up URL generation. if self.is_nested: # Make URL generation relative to the resource. coll.__parent__ = resource # Set the collection's name to the descriptor's resource # attribute name. coll.__name__ = slug_from_identifier(self.resource_attr) else: # Add a filter specification for the root collection through # a relationship. coll_relationship = Relationship(resource, coll, backref=self.__resource_backref) coll.set_relationship(coll_relationship) return coll
def _make_test_entity_member(): parent = MyEntityParent() entity = MyEntity(parent=parent) if parent.child is None: parent.child = entity child = MyEntityChild() entity.children.append(child) if child.parent is None: child.parent = entity grandchild = MyEntityGrandchild() child.children.append(grandchild) if grandchild.parent is None: grandchild.parent = child coll = create_staging_collection(IMyEntity) mb = coll.create_member(entity) parent.id = 0 entity.id = 0 child.id = 0 grandchild.id = 0 return mb
def test_update_from_data_add_child(self): my_entity = create_entity() new_child = MyEntityChild() my_entity.children.append(new_child) if new_child.parent is None: new_child.parent = my_entity coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) self.assert_equal(len(member.children), 2) mp = self._make_mapping() data_el = mp.map_to_data_element(member) del member del my_entity # import gc; gc.collect() my_entity = create_entity() coll = get_root_collection(IMyEntity) context = coll.create_member(my_entity) self.assert_equal(len(context.children), 1) context.update(data_el) self.assert_equal(len(context.children), 2)
def _make_test_entity_member(): parent = MyEntityParent() entity = MyEntity(parent=parent) if parent.child is None: parent.child = entity child = MyEntityChild() entity.children.append(child) if child.parent is None: child.parent = entity grandchild = MyEntityGrandchild() child.children.append(grandchild) if grandchild.parent is None: grandchild.parent = child coll = create_staging_collection(IMyEntity) mb = coll.create_member(entity) parent.id = 0 entity.id = 0 child.id = 0 grandchild.id = 0 return mb
def find_connected_resources(resource, dependency_graph=None): """ Collects all resources connected to the given resource and returns a dictionary mapping member resource classes to new collections containing the members found. """ # Build a resource_graph. resource_graph = \ build_resource_graph(resource, dependency_graph=dependency_graph) # Build an ordered dictionary of collections. collections = OrderedDict() for mb in topological_sorting(resource_graph): mb_cls = get_member_class(mb) coll = collections.get(mb_cls) if coll is None: # Create new collection. coll = create_staging_collection(mb) collections[mb_cls] = coll coll.add(mb) return collections
def find_connected_resources(resource, dependency_graph=None): """ Collects all resources connected to the given resource and returns a dictionary mapping member resource classes to new collections containing the members found. """ # Build a resource_graph. resource_graph = \ build_resource_graph(resource, dependency_graph=dependency_graph) # Build an ordered dictionary of collections. collections = OrderedDict() for mb in topological_sorting(resource_graph): mb_cls = get_member_class(mb) coll = collections.get(mb_cls) if coll is None: # Create new collection. coll = create_staging_collection(mb) collections[mb_cls] = coll coll.add(mb) return collections
def test_make_traverser_update(self): ent0 = create_entity(entity_id=0) ent1 = create_entity(entity_id=None) agg = create_staging_collection(IMyEntity).get_aggregate() agg.add(ent0) agg.add(ent1) ent01 = create_entity(entity_id=0) ent11 = create_entity(entity_id=None) # With many as source and one as target. with self.assert_raises(ValueError) as cm: SourceTargetDataTreeTraverser.make_traverser( [ent01, ent1], ent0, RELATION_OPERATIONS.UPDATE, accessor=agg) self.assert_true( cm.exception.args[0].endswith('or both not be sequences.')) # Without target. trv = SourceTargetDataTreeTraverser.make_traverser( [ent01, ent11], None, RELATION_OPERATIONS.UPDATE, accessor=agg) self.assert_is_not_none(getattr(trv, '_tgt_prx'))
def map_to_resource(self, data_element, resource=None): """ Maps the given data element to a new resource or updates the given resource. :raises ValueError: If :param:`data_element` does not provide :class:`everest.representers.interfaces.IDataElement`. """ if not IDataElement.providedBy(data_element): # pylint:disable=E1101 raise ValueError('Expected data element, got %s.' % data_element) if resource is None: coll = \ create_staging_collection(data_element.mapping.mapped_class) agg = coll.get_aggregate() agg.add(data_element) if IMemberDataElement.providedBy(data_element): # pylint: disable=E1101 ent = next(iter(agg)) resource = \ data_element.mapping.mapped_class.create_from_entity(ent) else: resource = coll else: resource.update(data_element) return resource
def map_to_resource(self, data_element, resource=None): """ Maps the given data element to a new resource or updates the given resource. :raises ValueError: If :param:`data_element` does not provide :class:`everest.representers.interfaces.IDataElement`. """ if not IDataElement.providedBy(data_element): # pylint:disable=E1101 raise ValueError('Expected data element, got %s.' % data_element) if resource is None: coll = \ create_staging_collection(data_element.mapping.mapped_class) agg = coll.get_aggregate() agg.add(data_element) if IMemberDataElement.providedBy(data_element): # pylint: disable=E1101 ent = next(iter(agg)) resource = \ data_element.mapping.mapped_class.create_from_entity(ent) else: resource = coll else: resource.update(data_element) return resource
def test_itemstatus_representer(self, item_status_managed): coll = create_staging_collection(IItemStatus) is_mb = coll.create_member(item_status_managed) attrs = ('name', 'description') self.__test_rpr_for_member(is_mb, (XmlMime,), attrs)
def _create_member(self, member_cls, entity): coll = create_staging_collection(member_cls) return coll.create_member(entity)
def test_location_representer(self, barcoded_location_fac): bcl = barcoded_location_fac() coll = create_staging_collection(ILocation) bcl_mb = coll.create_member(bcl) str_rpr = self._to_string(bcl_mb, XmlMime) assert len(str_rpr) > 0
def test_organization_representer(self, organization_cenix): coll = create_staging_collection(IOrganization) org_mb = coll.create_member(organization_cenix) attrs = get_resource_class_attribute_names(OrganizationMember) self.__test_rpr_for_member(org_mb, (XmlMime, ), attrs)
def test_nested_get(self): my_entity = create_entity() coll = create_staging_collection(IMyEntity) member = coll.create_member(my_entity) self.assert_equal(member.parent_text, MyEntityParent.DEFAULT_TEXT)
def set_up(self): ResourceTestCase.set_up(self) self.coll = create_staging_collection(IMyEntity)
def test_species_representer(self, species_human): coll = create_staging_collection(ISpecies) sp_mb = coll.create_member(species_human) attrs = ('genus_name', 'species_name', 'common_name', 'acronym', 'ncbi_tax_id') self.__test_rpr_for_member(sp_mb, (XmlMime, ), attrs)
def _create_member(self, member_cls, entity): coll = create_staging_collection(member_cls) return coll.create_member(entity)
def test_species_representer(self, species_human): coll = create_staging_collection(ISpecies) sp_mb = coll.create_member(species_human) attrs = ('genus_name', 'species_name', 'common_name', 'acronym', 'ncbi_tax_id') self.__test_rpr_for_member(sp_mb, (XmlMime,), attrs)
def test_organization_representer(self, organization_cenix): coll = create_staging_collection(IOrganization) org_mb = coll.create_member(organization_cenix) attrs = get_resource_class_attribute_names(OrganizationMember) self.__test_rpr_for_member(org_mb, (XmlMime,), attrs)
def test_location_representer(self, barcoded_location_fac): bcl = barcoded_location_fac() coll = create_staging_collection(ILocation) bcl_mb = coll.create_member(bcl) str_rpr = self._to_string(bcl_mb, XmlMime) assert len(str_rpr) > 0
def set_up(self): ResourceTestCase.set_up(self) self.coll = create_staging_collection(IFoo) self.session = self.coll.get_aggregate()._session # pylint:disable=W0212