def update(self, form_data, user_id):
        """
        Update user data
        """
        user = User.update_user_info(user_id)

        if not user:
            return False

        validator = UserCreateValidator()

        error = validator.validate(form_data)
        if not error:
            return error

        error = validator.user_form_validation(form_data)
        if not error:
            return error

        try:
            user.user_name = form_data["username"]
            user.user_mail = form_data["email"]
            user.user_password = form_data["password"]
            db.session.commit()
        except SQLAlchemyError:
            db.session.rollback()
            return False

        return True
    def delete_currency_data(self, user_id, currency_name):
        """
        Delete currency from portfolio
        """

        delete_flag = False
        # get user info
        user = User.update_user_info(user_id)
        # get currency data
        user_data = user.crypts
        try:
            for i, name in enumerate(user_data):
                if name.crypt_name == currency_name:
                    del user_data[i]
                    delete_flag = True

            if not delete_flag:
                return False

            db.session.add(user)
            db.session.commit()
        except SQLAlchemyError:
            db.session.rollback()
            return False

        return True
    def get_user_info_by_user_id(self, user_id):
        """
        Get user information for showing info in user desplay
        """
        user = User.update_user_info(user_id)

        return user
    def get_user_info_by_user_id(self, user_id):
        """
        Get user infomation for showing info in user desplay
        """
        user = User.update_user_info(user_id)

        if not user:
            return "Can't find user"

        return user
    def validate(self, user_id, crypt_name):
        """
        Check currency name is in db
        """
        crypt = Crypt.find_currency_name(crypt_name)
        # get user info
        user = User.update_user_info(user_id)

        if crypt is not None and user != False:
            return user, crypt
        return False
    def update(self, form_data, user_id):
        """
        Update user data
        """
        user = User.update_user_info(user_id)

        if not user:
            return "Can't find user id"

        try:
            user.user_name = form_data["new_username"]
            user.user_mail = form_data["new_email"]
            user.user_password = form_data["new_password"]
            db.session.commit()
        except SQLAlchemyError:
            db.session.rollback()
            return "db error"

        return True
 def validate_portfolio_data(self, user_id):
     """
     Check user has currency data in db
     """
     # get current portfolio info
     name_of_currency_list = []
     num_of_currency_list = []
     currency = []
     user = User.update_user_info(user_id)
     user_data = user.crypts
     num_data = user.user_crypt
     if not user_data:
         return False
     for name, num in zip(user_data, num_data):
         name_of_currency_list.append(name.crypt_name)
         num_of_currency_list.append(num.num_of_currency)
     currency.append(name_of_currency_list)
     currency.append(num_of_currency_list)
     return currency
    def update_currency_data(self, user_id, currency_name, num_hold):
        """
        update currency information
        """
        # get user info
        user = User.update_user_info(user_id)
        # get currency data
        user_data = user.crypts
        num_data = user.user_crypt

        try:

            for name, num in zip(user_data, num_data):
                if name.crypt_name == currency_name:
                    num.num_of_currency = num_hold
                    break
            db.session.commit()

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

        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)