def setUp(self): print("setUp test env") # init test env self.app = create_app() db.create_all() # init user data user = User(user_name="atushi", user_mail="*****@*****.**", user_password="******") user2 = User(user_name="John", user_mail="*****@*****.**", user_password="******") # init currency data ctypt = Crypt(crypt_id=1, crypt_name="BTC") self.portfolio_service = PortfolioService() db.session.add(user) db.session.commit() db.session.add(user2) db.session.commit() db.session.add(ctypt) db.session.commit()
def delete_currency(id, currency): if not is_login(): return "" portfolio_service = PortfolioService() result = portfolio_service.delete_currency_data(id, currency.upper()) if result: return jsonify(ok=True) return ""
def delete_currency(currency_name): if "login" not in session or session["login"] == False: return redirect(url_for("user_page.login")) if request.method == "POST": portfolio_service = PortfolioService() result = portfolio_service.delete_currency_data( session["user_id"], request.form["coin_name"].upper()) if result: flash("Deleted!") return redirect(url_for("user_page.user_portfolio")) flash("Failer Try Again!") return render_template("portfolio/delete_currency.html", title="Delete", currency_name=currency_name)
def register_currency(): print("login" not in session) print(session) if "login" not in session or session["login"] == False: return redirect(url_for("user_page.login")) if request.method == "POST": portfolio_service = PortfolioService() error = portfolio_service.register( session["user_id"], request.form["coin_name"].upper(), request.form["num_of_currency"]) if error == True: flash("complete") return redirect(url_for("user_page.user_portfolio")) flash(error) return render_template("portfolio/register_currency.html", title="Register")
def edit_currency(id): if not is_login(): return "" edit_data = json.loads(request.data) portfolio_service = PortfolioService() result = portfolio_service.update_currency_data( id, edit_data["symbol"].upper(), edit_data["num_hold"]) if result: return jsonify(json.loads(request.data)) return ""
def register_currency(id): if not is_login(): return "" request_data = json.loads(request.data) portfolio_service = PortfolioService() error = portfolio_service.register(id, request_data["symbol"].upper(), request_data["num_hold"]) if error: return jsonify(ok=True) return ""
def fetch_currency(): if not is_login(): return "" portfolio_service = PortfolioService() data = portfolio_service.get_user_portfolio(session["user_id"]) if data: api = API() "data[0]: currency name, data[1]: number of hold currency" result = api.call_api(data[0]) currency_list, total_value = api.data_process(result, data[1]) return jsonify(id=session["user_id"], currency_list=currency_list, total_value=total_value) return jsonify(id=session["user_id"], currency_list="", total_value=0)
def user_portfolio(): if "login" not in session or session["login"] == False: return redirect(url_for("user_page.login")) if request.method == "GET": portfolio_service = PortfolioService() data = portfolio_service.get_user_portfolio(session["user_id"]) user_service = UserService() user = user_service.get_user_info_by_user_id(session["user_id"]) if data and user: api = API() "data[0]: currency name, data[1]: number of hold currency" result = api.call_api(data[0]) result, total_value = api.data_process(result, data[1]) return render_template("portfolio/user_portfolio.html", title="Portfolio", result=result, num_of_holds=data[1], total_value=total_value, user=user) return render_template("portfolio/user_portfolio.html", title="Portfolio", result="", num_of_holds="", total_value=0, user=user)
class TestPortfolioService(unittest.TestCase): def setUp(self): print("setUp test env") # init test env self.app = create_app() db.create_all() # init user data user = User(user_name="atushi", user_mail="*****@*****.**", user_password="******") user2 = User(user_name="John", user_mail="*****@*****.**", user_password="******") # init currency data ctypt = Crypt(crypt_id=1, crypt_name="BTC") self.portfolio_service = PortfolioService() db.session.add(user) db.session.commit() db.session.add(user2) db.session.commit() db.session.add(ctypt) db.session.commit() def tearDown(self): print("tearDown") db.session.remove() db.drop_all() def test_register(self): # arg is valid currency name res = self.portfolio_service.register(user_id=1, crypt_name="BTC", num_of_currency=5) self.assertTrue(res) # arg is invalid currency name res = self.portfolio_service.register(user_id=1, crypt_name="ETH", num_of_currency=1) self.assertFalse(res) def test_fetch_portfolio_data(self): # add test data to user_crypt db self.portfolio_service.register(user_id=1, crypt_name="BTC", num_of_currency=5) res = self.portfolio_service.get_user_portfolio(user_id=1) self.assertIsNotNone(res) res = self.portfolio_service.get_user_portfolio(user_id=2) self.assertFalse(res) def test_edit_portfolio(self): res = self.portfolio_service.update_currency_data(user_id=1, currency_name="BTC", num_hold=10) self.assertTrue(res) def test_delete_currency_from_portfolio(self): # add test data to user_crypt db self.portfolio_service.register(user_id=1, crypt_name="BTC", num_of_currency=5) # valid data res = self.portfolio_service.delete_currency_data(user_id=1, currency_name="BTC") self.assertTrue(res) res = self.portfolio_service.delete_currency_data(user_id=1, currency_name="ADA") self.assertFalse(res)