def post(self) -> ({str: str}, HTTPStatus): """ Delete Attribute Alias :param id: Attribute Alias id :param attribute_id: Parent Attribute id :param user_id: Current User id :return: No content with an HTTPstatus code NoContent (204) or an error message and a the appropriate HTTPStatus code """ args = self.reqpaser.parse_args() alias = None if "id" in args: alias = AttrAlias.get_by(id=id) if not alias: return {"error": "Alias not found", "id": args["id"]}, HTTPStatus.NOT_FOUND elif "user_id" in args and "attribute_id" in args: alias = AttrAlias.get_by(user_id=args["user_id"], attribute_id=args["attribute_id"]) if not alias: return {"error": "Alias not found", "attribute_id": args["attribute_id"], "user_id": args["user_id"]}, HTTPStatus.NOT_FOUND if not isinstance(alias, AttrAlias): return {"error": "Alias not found", }, HTTPStatus.NOT_FOUND alias.delete() alias.commit() return None, HTTPStatus.NO_CONTENT
def get_attributes(user_id: int, sub_theme_ids: [int], theme_id: int) -> [Attributes]: """ Fetch Attributes by User Id and SubTheme Ids :param user_id: The Users Id number :param sub_theme_ids: A set of Subtheme id numbers :param theme_id: Current Theme Id :return: Attributes that match the User id and Subtheme ids """ # Fetch attributes attributes = db.session.query(Attributes).filter( Attributes.sub_theme_id.in_(sub_theme_ids)).options( db.joinedload(Attributes.sub_theme)).all() attribute_by_sub_id = dict() for attribute in attributes: # Create Attribute Braches and Attribute alias Branches if attribute.sub_theme_id not in attribute_by_sub_id: attribute_by_sub_id[attribute.sub_theme_id] = list() attr_serial = attribute.serializable attr_serial["theme_id"] = theme_id alias = AttrAlias.get_by(user_id=user_id, attribute_id=attribute.id) if alias: attr_serial["alias"] = alias.serializable attribute_by_sub_id[attribute.sub_theme_id].append(attr_serial) return attribute_by_sub_id
def test_create_alias(self) -> None: """ Create AttrAlias database entry using database session """ alias = AttrAlias(self.attribute.id, self.user.id, name="_custom_name", table_name="_table_name_", description="a custon description") self.assertTrue(alias, msg="test_create_alias(): Failed to create Alias") if alias: self.clean_up.append(alias)
def test_create_alias(self) -> None: """ Create AttrAlias using endpoint and check client response for https status code 200""" json_payload = {"user_id": self.user.id, "attribute_id": self.attribute.id, "name": "A_Custom_Name_12890", "table_name": "_A_TaBlE_NaMe_"} response = self.client.post('/admin/attributes/alias', json=json_payload, headers=self.auth_header) if response.status_code == HTTPStatus.OK: alias = AttrAlias.get_by(user_id=self.user.id, name="A_Custom_Name_12890") if alias: self.clean_up.append(alias) self.assertEqual(response.status_code, HTTPStatus.OK)
def tearDown(self) -> None: """ Clean up all dependencies after tests""" for alias in self.aliases: try: if AttrAlias.get_by_user_id(self.user.id): alias.delete() alias.commit() except Exception: pass for attr in self.attributes: try: if Attributes.get_by_id(attr.id): attr.delete() attr.commit() except Exception: pass for sub_theme in self.sub_themes: try: if SubTheme.get_by_id(sub_theme.id): sub_theme.delete() sub_theme.commit() except Exception: pass for unit in self.units: try: if Unit.get_by_id(unit.id): unit.delete() unit.commit() except Exception: pass try: if Theme.get_by_id(self.theme.id): self.theme.delete() self.theme.commit() except Exception: pass self.client.post('/logout', headers=self.auth_header) if self.user: if Users.find_by_id(self.user.id): try: self.user.delete() self.user.commit() except Exception: pass self.app_context.pop()
def get_dummy_alias(self) -> db.Model: """ Create dummy AttrAlias :return: A AttrAlias """ alias = AttrAlias.get_by(name="_custom_name") if not alias: alias = AttrAlias(self.attribute.id, self.user.id, name="_custom_name", table_name="_table_name_", description="a custon description") alias.save() alias.commit() self.clean_up.append(alias) return alias
def post(self) -> ({str: str}, HTTPStatus): """ Update AttrAlias (Attribute Alias) if it exists otherwise create an AttrAlias :param attribute_id: Parent Attribute id to alias :param user_id: Owner user id :param name: Alias for Attribute name :param table_name: Alias for Attribute table_name :param unit_id: Id of unit to be used :param description: Custom user description for Attribute :return: AttrAlias with an HTTPstatus code OK (200) or an error message and a the appropriate HTTPStatus code """ args = self.reqpaser.parse_args() attribute = Attributes.get_by_id(args["attribute_id"]) if not attribute: return {"error": "Attribute Not Found."}, HTTPStatus.NOT_FOUND user = Users.find_by_id(args["user_id"]) if not user: return {"error": "User Not Found."}, HTTPStatus.NOT_FOUND alias = AttrAlias.get_by(user_id=args["user_id"], attribute_id=args["attribute_id"]) if alias: alias.name = args["name"] if "name" in args else alias.name alias.table_name = args["table_name"] if "table_name" in args else alias.table_name alias.unit_id = args["unit_id"] if "unit_id" in args else alias.unit_id alias.description = args["description"] if "description" in args else alias.description else: alias = AttrAlias(args["attribute_id"], args["user_id"], name=args.get("name"), table_name=args.get("table_name"), description=args.get("description")) try: alias.save() alias.commit() except Exception as e: logger.error("failed to persist attribute alias", e) return {"error": "Failed to commit attribute alias to database", "exception": e}, HTTPStatus.INTERNAL_SERVER_ERROR return alias.json(), HTTPStatus.OK
def get(self) -> ({str: str}, HTTPStatus): """ Fetch AttrAlias (Attribute Alias) from the database :param attribute_id: Parent Attribute id :param user_id: Owner user id :return: AttrAlias with an HTTPstatus code OK (200) or an error message and a the appropriate HTTPStatus code """ args = self.reqpaser.parse_args() attribute = Attributes.get_by_id(args["attribute_id"]) if not attribute: return {"error": "Attribute Not Found."}, HTTPStatus.NOT_FOUND user = Users.find_by_id(args["user_id"]) if not user: return {"error": "User Not Found."}, HTTPStatus.NOT_FOUND alias = AttrAlias.get_by(user_id=args["user_id"], attribute_id=args["attribute_id"]) if not alias: return attribute.json(), HTTPStatus.OK return alias.json(), HTTPStatus.OK
def post(self) -> ({str: str}, HTTPStatus): """ Update Attributes SubTheme :param attribute_id: Attributes identification number :param sub_theme_id: SubTheme identification number :type attribute_id: str :type sub_theme_id: int :return: A JSON containing a message, Attribute id, SubTheme id and a HTTPStatus 200 (OK) on success otherwise a JSON with a error message and a HTTPStatus 404 (NotFound) """ args = self.reqpaser.parse_args() attribute = AttrAlias.get_by_attr_id(args["attribute_id"]) if not attribute: attribute = Attributes.get_by_id(args["attribute_id"]) if not attribute: return { "error": "Attribute not found", "id": args["attribute_id"] }, HTTPStatus.NOT_FOUND sub_theme = SubTheme.get_by(id=args["sub_theme_id"]) if not sub_theme: return { "error": "SubTheme not found", "id": args["sub_theme_id"] }, HTTPStatus.NOT_FOUND attribute.sub_theme_id(sub_theme.id) attribute.save() attribute.commit() return { "message": "Attribute SubTheme updated", "attribute_id": args["attribute_id"], "sub_theme_id": args["sub_theme_id"] }, HTTPStatus.OK
def create_attribute_alias(self) -> None: """ Create Attributes Aliases for Attributes :param count: The number of attributes to create per SubTheme """ try: for attr in self.attributes: alias = AttrAlias(attr.id, user_id=self.user.id, name="_alias_name {}".format( len(self.aliases)), table_name="_alias_table_name_{}".format( len(self.aliases))) alias.save() alias.commit() self.aliases.append(alias) except Exception as exp: logger.error(exp) self.tearDown()