Ejemplo n.º 1
0
def test_addSecondCurrencyToPortfolio(db_handle):
    """
    Test if another cryptocurrency can be added to portfolio.
    """
    usr = _get_UserAccount()
    portfolio = _get_Portfolio()
    currency = _get_CryptoCurrency()
    currency2 = _get_CryptoCurrency2()

    cp = crypto_portfolio(portfolio=portfolio,
                          cryptocurrency=currency,
                          currencyAmount=44.444)
    cp2 = crypto_portfolio(portfolio=portfolio,
                           cryptocurrency=currency2,
                           currencyAmount=44.444)
    usr.portfolio = portfolio

    db_handle.session.add(usr)
    db_handle.session.add(portfolio)
    db_handle.session.add(currency)
    db_handle.session.add(cp)
    db_handle.session.add(cp2)

    db_handle.session.commit()

    db_portfolio = Portfolio.query.first()
    assert crypto_portfolio.query.filter_by(
        portfolio_id=db_portfolio.id).count() == 2
Ejemplo n.º 2
0
def _populate_db():
    cc = _get_CryptoCurrency()
    cc2 = _get_CryptoCurrency2()
    cc3 = _get_CryptoCurrency3()
    db.session.add(cc)
    db.session.add(cc2)
    db.session.add(cc3)

    for i in range(1, 4):
        a = UserAccount(name="test-account-{}".format(i), password="******")
        p = Portfolio(timestamp=datetime.now(), value=float(i * 100))
        a.portfolio = p
        cp = crypto_portfolio(portfolio=p,
                              cryptocurrency=cc,
                              currencyAmount=500.0)
        cp2 = crypto_portfolio(portfolio=p,
                               cryptocurrency=cc2,
                               currencyAmount=47666.0)
        # p.cryptocurrencies.append(cp)
        # p.cryptocurrencies.append(cp2)
        db.session.add(cp)
        db.session.add(cp2)
        db.session.add(a)
        db.session.add(p)
    db.session.commit()
Ejemplo n.º 3
0
def test_delete_user(db_handle):
    """ 
    Test deleting user, portfolio, bulkdeleting crypto_portfolios
    NOTE: might be redundant test
    """
    usr = _get_UserAccount()

    portfolio = _get_Portfolio()
    portfolio2 = _get_Portfolio2()

    currency = _get_CryptoCurrency()
    currency2 = _get_CryptoCurrency2()

    cp = crypto_portfolio(portfolio=portfolio,
                          cryptocurrency=currency,
                          currencyAmount=44.444)
    cp2 = crypto_portfolio(portfolio=portfolio,
                           cryptocurrency=currency2,
                           currencyAmount=400.0)
    cp3 = crypto_portfolio(portfolio=portfolio2,
                           cryptocurrency=currency,
                           currencyAmount=9.0)

    usr.portfolio = portfolio

    db_handle.session.add(usr)

    db_handle.session.add(portfolio)

    db_handle.session.add(currency)
    db_handle.session.add(currency2)
    db_handle.session.add(cp)
    db_handle.session.add(cp2)
    db_handle.session.add(cp3)

    db_handle.session.commit()

    assert crypto_portfolio.query.count() == 3

    db_user = UserAccount.query.first()
    db_portfolio = Portfolio.query.filter_by(id=db_user.portfolio_id).first()
    bulk_delete_query = crypto_portfolio.__table__.delete().where(
        crypto_portfolio.portfolio_id == db_portfolio.id)
    db_handle.session.execute(bulk_delete_query)
    db_handle.session.delete(db_portfolio)
    db_handle.session.delete(db_user)
    db_handle.session.commit()

    assert crypto_portfolio.query.count() == 1
Ejemplo n.º 4
0
def test_instance_creation(db_handle):
    """
    Test for instance creation, their existence, and relationships.
    """
    userAccount = _get_UserAccount()
    portfolio = _get_Portfolio()
    cryptoCurrency = _get_CryptoCurrency()
    userAccount.portfolio = portfolio
    cp = crypto_portfolio(portfolio=portfolio,
                          cryptocurrency=cryptoCurrency,
                          currencyAmount=44.444)

    db_handle.session.add(userAccount)
    db_handle.session.add(portfolio)
    db_handle.session.add(cryptoCurrency)

    db_handle.session.add(cp)
    db_handle.session.commit()

    # Check existence
    assert UserAccount.query.count() == 1
    assert Portfolio.query.count() == 1
    assert CryptoCurrency.query.count() == 1
    assert crypto_portfolio.query.count() == 1

    db_user = UserAccount.query.first()
    db_portfolio = Portfolio.query.first()
    db_cryptocurreny = CryptoCurrency.query.first()
    db_cp = crypto_portfolio.query.first()

    # check Relationships
    assert db_user.portfolio == db_portfolio
    assert db_user == db_portfolio.useraccount
    assert db_portfolio == db_cp.portfolio
    assert db_cryptocurreny == db_cp.cryptocurrency
Ejemplo n.º 5
0
    def post(self, account):
        """
        POST method for adding a new cryptocurrency to portfolio
        """
        if not request.json:
            return create_error_response(415, "Unsupported media type")
        try:
            validate(request.json, crypto_portfolio.get_schema())
        except ValidationError as e:
            return create_error_response(400, "Invalid json document", str(e))

        if float(request.json["currencyamount"]) <= 0:
            return create_error_response(400, "Invalid json document",
                                         "Currencyamount must be over zero")

        db_user = UserAccount.query.filter_by(name=account).first()
        db_portfolio = Portfolio.query.filter_by(
            id=db_user.portfolio_id).first()
        db_currency = CryptoCurrency.query.filter_by(
            abbreviation=request.json["currencyname"].upper()).first()
        if db_currency is None:
            return create_error_response(404, "Currency not found")
        pcurrency = crypto_portfolio(
            portfolio=db_portfolio,
            cryptocurrency=db_currency,
            currencyAmount=request.json["currencyamount"])

        try:
            db.session.add(pcurrency)
            db.session.commit()
        except IntegrityError:
            return create_error_response(409, "Already exists")

        # Find all pcurrencies from users portfolio
        db_pcurrencies = crypto_portfolio.query.filter_by(
            portfolio_id=db_portfolio.id).all()

        # Value of all pcurrencies in the portfolio
        total_amount = 0

        for pc in db_pcurrencies:
            coin = CryptoCurrency.query.filter_by(
                id=pc.cryptocurrency_id).first()
            total_amount = total_amount + (pc.currencyAmount * coin.value)

        # Updates the total value of the portfolio
        db_portfolio.value = total_amount
        db.session.commit()

        return Response(status=201,
                        headers={
                            "Location":
                            url_for("api.portfoliocurrency",
                                    account=account,
                                    pcurrency=request.json["currencyname"])
                        })
Ejemplo n.º 6
0
def test_cryptoportfolio_currency_one(db_handle):
    """
    Test if the same currency cannot be added twice to the same portfolio.
    """
    usr = _get_UserAccount()
    portfolio = _get_Portfolio()
    currency = _get_CryptoCurrency()

    cp = crypto_portfolio(portfolio=portfolio,
                          cryptocurrency=currency,
                          currencyAmount=44.444)
    cp2 = crypto_portfolio(portfolio=portfolio,
                           cryptocurrency=currency,
                           currencyAmount=44.444)
    usr.portfolio = portfolio
    db_handle.session.add(usr)
    db_handle.session.add(portfolio)
    db_handle.session.add(currency)
    db_handle.session.add(cp)

    with pytest.raises(IntegrityError):
        db_handle.session.commit()
Ejemplo n.º 7
0
def test_get_portfolios_by_currency(db_handle):
    """ 
  Test getting portfolios having certain cryptocurrency
  """
    usr = _get_UserAccount()
    usr2 = _get_UserAccount2()

    portfolio = _get_Portfolio()
    portfolio2 = _get_Portfolio2()

    currency = _get_CryptoCurrency()

    cp = crypto_portfolio(portfolio=portfolio,
                          cryptocurrency=currency,
                          currencyAmount=44.444)
    cp2 = crypto_portfolio(portfolio=portfolio2,
                           cryptocurrency=currency,
                           currencyAmount=20.444)

    usr.portfolio = portfolio
    usr2.portfolio = portfolio2

    db_handle.session.add(usr)
    db_handle.session.add(usr2)

    db_handle.session.add(portfolio)
    db_handle.session.add(portfolio2)

    db_handle.session.add(currency)
    db_handle.session.add(cp)
    db_handle.session.add(cp2)

    db_handle.session.commit()

    db_currency = CryptoCurrency.query.first()

    assert crypto_portfolio.query.filter_by(
        cryptocurrency_id=db_currency.id).count() == 2
Ejemplo n.º 8
0
def test_removeCurrencyFromPortfolio(db_handle):
    """
    Test if a currency can be removed from portfolio.
    """
    usr = _get_UserAccount()
    portfolio = _get_Portfolio()
    currency = _get_CryptoCurrency()
    currency2 = _get_CryptoCurrency2()

    cp = crypto_portfolio(portfolio=portfolio,
                          cryptocurrency=currency,
                          currencyAmount=44.444)
    cp2 = crypto_portfolio(portfolio=portfolio,
                           cryptocurrency=currency2,
                           currencyAmount=44.444)
    usr.portfolio = portfolio

    db_handle.session.add(usr)
    db_handle.session.add(portfolio)
    db_handle.session.add(currency)
    db_handle.session.add(cp)
    db_handle.session.commit()

    db_portfolio = Portfolio.query.first()
    assert crypto_portfolio.query.filter_by(
        portfolio_id=db_portfolio.id).count() == 2

    db_currency = CryptoCurrency.query.filter_by(abbreviation="BTC").first()
    db_cp = crypto_portfolio.query.filter_by(
        portfolio_id=db_portfolio.id,
        cryptocurrency_id=db_currency.id).first()
    db_handle.session.delete(db_cp)
    db_handle.session.commit()

    assert crypto_portfolio.query.filter_by(
        portfolio_id=db_portfolio.id).count() == 1
Ejemplo n.º 9
0
def test_update_currencyamount(db_handle):
    """ 
    Test updating currency amount in portfolio
    NOTE: might be redundant test
    """
    usr = _get_UserAccount()

    portfolio = _get_Portfolio()

    currency = _get_CryptoCurrency()

    cp = crypto_portfolio(portfolio=portfolio,
                          cryptocurrency=currency,
                          currencyAmount=44.444)

    usr.portfolio = portfolio

    db_handle.session.add(usr)

    db_handle.session.add(portfolio)

    db_handle.session.add(currency)
    db_handle.session.add(cp)

    db_handle.session.commit()

    # update user's portfolio's certain currencyamount
    db_user = UserAccount.query.first()
    db_currency = CryptoCurrency.query.first()
    db_cp = crypto_portfolio.query.filter_by(
        portfolio_id=db_user.portfolio.id,
        cryptocurrency_id=db_currency.id).first()

    assert db_cp.currencyAmount == 44.444

    db_cp.currencyAmount = 2
    db_handle.session.commit()

    assert db_cp.currencyAmount == 2