def test_partial_shares(capfd, requests_mock):
    """Testing capgains_calc with partial shares"""
    partial_buy = Transaction(date(2017, 2, 15), 'ESPP PURCHASE', 'ANET',
                              'BUY', 0.5, 50.00, 0.00, 'CAD')
    partial_sell = Transaction(date(2018, 2, 20), 'RSU VEST', 'ANET', 'SELL',
                               0.5, 100.00, 0.00, 'CAD')
    transactions = Transactions([partial_buy, partial_sell])

    CapGainsCalc.capgains_calc(transactions, 2018)
    out, _ = capfd.readouterr()
    assert out == """\
Пример #2
0
def transactions():
    trans = [
        Transaction(date(2017, 2, 15), 'ESPP PURCHASE', 'ANET', 'BUY', 100,
                    50.00, 10.00, 'USD'),
        Transaction(date(2018, 2, 20), 'RSU VEST', 'GOOGL', 'BUY', 30, 20.00,
                    10.00, 'USD'),
        Transaction(date(2018, 2, 20), 'RSU VEST', 'ANET', 'SELL', 50, 120.00,
                    10.00, 'USD'),
        Transaction(date(2019, 2, 15), 'ESPP PURCHASE', 'ANET', 'BUY', 50,
                    130.00, 10.00, 'USD')
    ]
    return Transactions(trans)
def test_transactions_price_not_number(testfiles_dir):
    """Testing TransactionsReader with price entered in wrong format"""
    transaction = Transaction(date(2020, 2, 20), 'RSU VEST', 'ANET', 'BUY',
                              100, 100, 0.0, 'USD')
    # Overwrite the price after creating the object because otherwise the
    # object initialization will throw an error
    transaction._price = 'BLAH'
    transactions = transactions_to_list([transaction])
    filepath = create_csv_file(testfiles_dir, "pricenotfloat.csv,",
                               transactions, True)
    with pytest.raises(ClickException) as excinfo:
        TransactionsReader.get_transactions(filepath)
    assert excinfo.value.message == "The price entered BLAH is not a valid number"  # noqa: E501
def test_transactions_read_wrong_dates_order(testfiles_dir):
    """Testing TransactionsReader with out of order dates"""
    transaction_after = Transaction(date(2018, 2, 20), 'RSU VEST', 'GOOGL',
                                    'BUY', 42, 249.55, 0.0, 'USD')
    transaction_before = Transaction(date(2018, 2, 15), 'ESPP PURCHASE',
                                     'ANET', 'BUY', 21, 307.96, 20.99, 'USD')
    transactions = transactions_to_list(
        [transaction_after, transaction_before])
    filepath = create_csv_file(testfiles_dir, "outoforder.csv,", transactions,
                               True)
    with pytest.raises(ClickException) as excinfo:
        TransactionsReader.get_transactions(filepath)
    assert excinfo.value.message == "Transactions were not entered in chronological order"  # noqa: E501
def test_superficial_loss_not_displayed(capfd, exchange_rates_mock):
    """Testing capgains_calc with a superficial loss transaction"""
    transactions = [
        Transaction(date(2018, 1, 1), 'ESPP PURCHASE', 'ANET', 'BUY', 100,
                    100.00, 10.00, 'USD'),
        Transaction(date(2018, 1, 2), 'RSU VEST', 'ANET', 'SELL', 99, 50.00,
                    10.00, 'USD'),
        Transaction(date(2018, 12, 1), 'RSU VEST', 'ANET', 'SELL', 1, 1000.00,
                    10.00, 'USD')
    ]
    transactions = Transactions(transactions)
    CapGainsCalc.capgains_calc(transactions, 2018)
    out, _ = capfd.readouterr()
    assert out == """\
Пример #6
0
def test_superficial_loss_no_purchase_after_loss(exchange_rates_mock):
    """Testing if transaction is marked as a superficial loss even if
    there are no purchases made after the loss"""
    transactions = [
        Transaction(date(2018, 1, 1), 'ESPP PURCHASE', 'ANET', 'BUY', 100,
                    100.00, 10.00, 'USD'),
        Transaction(date(2018, 1, 2), 'RSU VEST', 'ANET', 'SELL', 99, 50.00,
                    10.00, 'USD')
    ]
    tg = TickerGains(transactions[0].ticker)
    er = ExchangeRate('USD', transactions[0].date, transactions[1].date)
    er_map = {'USD': er}
    tg.add_transactions(transactions, er_map)
    assert transactions[1].superficial_loss
    assert transactions[1].capital_gain == 0
Пример #7
0
def test_gain_not_marked_as_superficial_loss(exchange_rates_mock):
    """Testing if transaction is not marked as a superficial loss if
    it does not result in a loss"""
    transactions = [
        Transaction(date(2018, 1, 1), 'ESPP PURCHASE', 'ANET', 'BUY', 100,
                    1.00, 10.00, 'USD'),
        Transaction(date(2018, 8, 1), 'RSU VEST', 'ANET', 'SELL', 100, 50.00,
                    10.00, 'USD')
    ]
    tg = TickerGains(transactions[0].ticker)
    er = ExchangeRate('USD', transactions[0].date, transactions[1].date)
    er_map = {'USD': er}
    tg.add_transactions(transactions, er_map)
    assert not transactions[1].superficial_loss
    assert transactions[1].capital_gain > 0
def test_calc_mixed_currencies(capfd, requests_mock):
    """Testing capgains_calc with mixed currencies"""
    usd_transaction = Transaction(date(2017, 2, 15), 'ESPP PURCHASE', 'ANET',
                                  'BUY', 100, 50.00, 0.00, 'USD')
    cad_transaction = Transaction(date(2018, 2, 20), 'RSU VEST', 'ANET',
                                  'SELL', 100, 50.00, 0.00, 'CAD')
    transactions = Transactions([usd_transaction, cad_transaction])

    usd_observations = [{
        'd': usd_transaction.date.isoformat(),
        'FXUSDCAD': {
            'v': '2.0'
        }
    }]
    requests_mock.get(rm.ANY, json={"observations": usd_observations})
    CapGainsCalc.capgains_calc(transactions, 2018)
    out, _ = capfd.readouterr()
    assert out == """\
def test_transactions_date_wrong_format(testfiles_dir):
    """Testing TransactionsReader with dates entered in wrong format"""
    transaction = Transaction('January 1st 2020', 'RSU VEST', 'ANET', 'BUY',
                              100, 50.00, 0.0, 'USD')
    transactions = transactions_to_list([transaction])
    filepath = create_csv_file(testfiles_dir, "datewrongformat.csv,",
                               transactions, True)
    with pytest.raises(ClickException) as excinfo:
        TransactionsReader.get_transactions(filepath)
    assert excinfo.value.message == "The date (January 1st 2020) was not entered in the correct format (YYYY-MM-DD)"  # noqa: E501
Пример #10
0
def test_loss_no_balance_after_window(exchange_rates_mock):
    """Testing if transaction is not marked as a superficial loss if
    there is no share balance 30 days after the loss"""
    transactions = [
        Transaction(date(2018, 1, 1), 'ESPP PURCHASE', 'ANET', 'BUY', 100,
                    100.00, 10.00, 'USD'),
        Transaction(date(2018, 1, 2), 'RSU VEST', 'ANET', 'SELL', 99, 50.00,
                    10.00, 'USD'),
        Transaction(date(2018, 1, 3), 'RSU VEST', 'ANET', 'SELL', 1, 100.00,
                    10.00, 'USD'),
        Transaction(date(2018, 2, 10), 'RSU VEST', 'ANET', 'BUY', 1, 100.00,
                    10.00, 'USD')
    ]
    tg = TickerGains(transactions[0].ticker)
    er = ExchangeRate('USD', transactions[0].date, transactions[1].date)
    er_map = {'USD': er}
    tg.add_transactions(transactions, er_map)
    assert not transactions[1].superficial_loss
    assert transactions[1].capital_gain < 0
def test_transactions_reader_default(testfiles_dir, transactions):
    """Testing TransactionsReader for a valid csv file"""
    exp_transaction = Transaction(date(2018, 2, 15), 'ESPP PURCHASE', 'ANET',
                                  'BUY', 21, 307.96, 20.99, 'USD')
    exp_transactions = transactions_to_list([exp_transaction])
    filepath = create_csv_file(testfiles_dir, "good.csv", exp_transactions,
                               True)

    actual_transactions = TransactionsReader.get_transactions(filepath)
    assert len(actual_transactions) == 1
    actual_transaction = actual_transactions[0]
    assert actual_transaction.__dict__ == exp_transaction.__dict__
def test_transactions_reader_columns_error(testfiles_dir):
    """Testing TransactionsReader for a csv file with too many columns"""
    transaction = Transaction(date(2018, 2, 15), 'ESPP PURCHASE', 'ANET',
                              'BUY', 21, 307.96, 20.99, 'USD')
    transactions = transactions_to_list([transaction])
    # Add an extra column to the transaction
    transactions[0].append('EXTRA_COLUMN_VALUE')
    filepath = create_csv_file(testfiles_dir, "too_many_cols.csv",
                               transactions, True)
    with pytest.raises(ClickException) as excinfo:
        TransactionsReader.get_transactions(filepath)
    assert excinfo.value.message == "Transaction entry 0: expected 8 columns, entry has 9"  # noqa: E501