def delete(self, user_id):
        """
        Delete user data
        """
        user = User.delete_user_info(user_id)
        user_portfolio = user.crypts

        try:
            user_portfolio.clear()
            db.session.add(user)
            db.session.commit()
            db.session.delete(user)
            db.session.commit()

        except SQLAlchemyError:
            db.session.rollback()
            return False

        return True
    def delete(self, form_data, user_id):
        """
        Delete user data
        """
        user = User.delete_user_info(user_id, form_data["username"],
                                     form_data["email"], form_data["password"])
        if not user:
            return "User information doesn't match try again"
        user_portfolio = user.crypts

        try:
            user_portfolio.clear()
            db.session.add(user)
            db.session.commit()
            db.session.delete(user)
            db.session.commit()

        except SQLAlchemyError:
            db.session.rollback()
            return "db error"

        return True
class TestUserDB(unittest.TestCase):
    def setUp(self):
        print("setUp test env")

        # init test env
        self.app = create_app()
        db.create_all()

        self.user = User()

        # init user data
        self.test_user = User(user_id=1,
                              user_name="test",
                              user_mail="*****@*****.**",
                              user_password="******")
        db.session.add(self.test_user)
        db.session.commit()

    def tearDown(self):
        print("tearDown")
        db.session.remove()
        db.drop_all()

    def test_filter_register_user_email_and_name(self):
        # Valid data
        res = self.user.find_user_email_and_name(name="test",
                                                 email="*****@*****.**")

        self.assertFalse(res)

        # exist name in db
        res = self.user.find_user_email_and_name(name="noexist",
                                                 email="*****@*****.**")

        self.assertFalse(res)

        # exist email in db
        res = self.user.find_user_email_and_name(name="test",
                                                 email="*****@*****.**")

        self.assertFalse(res)

        # non exist user name and email in db
        res = self.user.find_user_email_and_name(name="noexist",
                                                 email="*****@*****.**")

        self.assertTrue(res)

    def test_find_login_user_data(self):

        # user put correct user info
        user = self.user.find_user_info(name="test",
                                        mail="*****@*****.**",
                                        password="******")

        self.assertEqual(user, self.test_user)

        # user put wrong name
        res = self.user.find_user_info(name="aaaa",
                                       mail="*****@*****.**",
                                       password="******")

        self.assertFalse(res)

        # user put wrong email
        res = self.user.find_user_info(name="test",
                                       mail="*****@*****.**",
                                       password="******")

        self.assertFalse(res)

        # user put wrong password
        res = self.user.find_user_info(name="test",
                                       mail="*****@*****.**",
                                       password="******")

        self.assertFalse(res)

    def test_find_user_by_id(self):
        user = self.user.update_user_info(user_id=1)

        self.assertEqual(user, self.test_user)

        user = self.user.delete_user_info(user_id=1)
        self.assertEqual(user, self.test_user)

    def test_get_user_by_email(self):

        user = self.user.get_useid_by_email(email="*****@*****.**")
        self.assertEqual(user, self.test_user)

        # non exist email in db
        res = self.user.get_useid_by_email(email="*****@*****.**")
        self.assertFalse(res)