def _get_tags(tag_names, session=None): """Return existing tags object or create new ones :param tag_names: name of tags to associate with package, list :returns: list of Tag objects to associate with package, list """ if session is None: session = db_session.get_session() tags = [] # This function can be called inside a transaction and outside it. # In the former case this line is no-op, in the latter # starts a transaction we need to be inside a transaction, to correctly # handle DBDuplicateEntry errors without failing the whole transaction. # For more take a look at SQLAlchemy docs. with session.begin(subtransactions=True): for tag_name in tag_names: tag_obj = _existing_tag(tag_name, session) if not tag_obj: try: # Start a new SAVEPOINT transaction. If it fails only # only the savepoint will be roll backed, not the # whole transaction. with session.begin(nested=True): tag_obj = models.Tag(name=tag_name) session.add(tag_obj) session.flush(objects=[tag_obj]) except db_exceptions.DBDuplicateEntry: # new session is needed here to get access to the tag tag_obj = _existing_tag(tag_name) tags.append(tag_obj) return tags
def _get_tags(tag_names, session=None): """Return existing tags object or create new ones :param tag_names: name of tags to associate with package, list :returns: list of Tag objects to associate with package, list """ if session is None: session = db_session.get_session() tags = [] for tag_name in tag_names: tag_obj = session.query(models.Tag).filter_by(name=tag_name).first() if tag_obj: tags.append(tag_obj) else: tag_record = models.Tag(name=tag_name) tags.append(tag_record) return tags
def setUp(self): super(TestManage, self).setUp() session = db_session.get_session() # Create environment. self.test_environment = models.Environment(name=b'test_environment', tenant_id=b'test_tenant_id', version=1) # Create categories. self.test_categories = [ models.Category(name=b'test_category_1'), models.Category(name=b'test_category_2') ] # Create tags. self.test_tags = [ models.Tag(name=b'test_tag_1'), models.Tag(name=b'test_tag_2') ] # Add environment, categories and tags to DB. with session.begin(): session.add(self.test_environment) session.add_all(self.test_categories) session.add_all(self.test_tags) # Create package. self.test_package = models.Package( fully_qualified_name=b'test_fqn', name=b'test_name', logo=b'test_logo', supplier_logo=b'test_supplier_logo', type=b'test_type', description=b'test_desc', is_public=True, archive=b'test_archive', ui_definition=b'test_ui_definition', categories=self.test_categories, tags=self.test_tags, owner_id=self.test_environment.tenant_id, ) # Add the package to the DB. with session.begin(): session.add(self.test_package) # Create class definitions and assign their FKs to test_package.id. self.test_class_definitions = [ models.Class(name=b'test_class_definition_1', package_id=self.test_package.id), models.Class(name=b'test_class_definition_2', package_id=self.test_package.id) ] # Add the class definitions to the DB and update the FK reference for # test_package.class_definitions. with session.begin(): session.add_all(self.test_class_definitions) self.test_package.class_definitions = self.test_class_definitions session.add(self.test_package) # Create mock object that resembles loaded package from # load_utils.load_from_dir self.mock_loaded_package = mock.MagicMock( full_name=self.test_package.fully_qualified_name, display_name=self.test_package.name, package_type=self.test_package.type, author=self.test_package.author, supplier=self.test_package.supplier, description=self.test_package.description, tags=[tag.name for tag in self.test_package.tags], classes=[cls.name for cls in self.test_package.class_definitions], logo=self.test_package.logo, supplier_logo=self.test_package.supplier_logo, ui=self.test_package.ui_definition, blob=self.test_package.archive)