Exemplo n.º 1
0
def test_import_currencies(std_xhb_file, db_connection):
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    rows = db_connection.execute(
        select([currency])
        .order_by(currency.c.id)
    ).fetchall()
    assert rows == [(1, 'Russian Ruble'),
                    (2, 'Euro')]
Exemplo n.º 2
0
def test_import_transaction_no_tags(std_xhb_file, db_connection):
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    count = db_connection.execute(
        select([func.count(txn_tag.c.id)])
        .where(txn_tag.c.txn_id == 1)
        .order_by(txn_tag.c.name)
    ).scalar()
    assert count == 0
Exemplo n.º 3
0
def test_import_transaction_with_tags(std_xhb_file, db_connection):
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    rows = db_connection.execute(
        select([txn_tag.c.name])
        .where(txn_tag.c.txn_id == 2)
        .order_by(txn_tag.c.name)
    ).fetchall()
    assert [row.name for row in rows] == ['tag1', 'tag2']
Exemplo n.º 4
0
def test_import_categories_income(std_xhb_file, db_connection):
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    c = category.c
    rows = db_connection.execute(
        select([c.income])
        .order_by(c.id)
    ).fetchall()[0:3]
    assert rows == [(False,), (False,), (True,)]
Exemplo n.º 5
0
def test_import_payees(std_xhb_file, db_connection):
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    rows = db_connection.execute(
        select([payee])
        .order_by(payee.c.id)
    ).fetchall()
    assert rows == [(1, 'payee1'),
                    (2, 'payee2')]
Exemplo n.º 6
0
def test_import_accounts(std_xhb_file, db_connection):
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    rows = db_connection.execute(
        select([account.c.id, account.c.name, account.c.currency_id])
        .where(account.c.id.in_((1, 3)))
        .order_by(account.c.id)
    ).fetchall()
    assert rows == [(1, 'account1', 1),
                    (3, 'account3', 2)]
Exemplo n.º 7
0
def test_import_categories_main(std_xhb_file, db_connection):
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    c = category.c
    rows = db_connection.execute(
        select([c.id, c.name, c.parent_id])
        .order_by(c.id)
    ).fetchall()[0:3]
    assert rows == [(1, 'category1', None),
                    (2, 'subcategory1-1', 1),
                    (3, 'income_category1', None)]
Exemplo n.º 8
0
def test_import_account_initial(std_xhb_file, db_connection):
    """Test import - account initial value."""
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    rows = db_connection.execute(
        select([account.c.initial])
        .where(account.c.id.in_((1, 2)))
        .order_by(account.c.id)
    ).fetchall()
    assert rows == [(0.0,),
                    (10.33,)]
Exemplo n.º 9
0
def test_import_transaction_internal(std_xhb_file, db_connection):
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    rows = db_connection.execute(
        select([txn, split])
        .select_from(txn.join(
            split,
            split.c.txn_id == txn.c.id))
        .where(txn.c.id.in_((3, 4)))
        .order_by(txn.c.id)
    ).fetchall()
    assert rows[0].paymode == 5
    assert rows[1].paymode == 5
    assert rows[0].amount == -rows[1].amount
Exemplo n.º 10
0
def test_import_transaction_minimal_split(std_xhb_file, db_connection):
    """Test import of minimal split transaction."""
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    rows = db_connection.execute(
        select([split])
        .where(split.c.txn_id == 6)
        .order_by(split.c.id)
    ).fetchall()
    first, second = rows
    assert first[split.c.memo] == ''
    assert second[split.c.memo] == ''
    assert first.category_id is None
    assert second.category_id is None
Exemplo n.º 11
0
def test_import_transaction_minimal(std_xhb_file, db_connection):
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    row = db_connection.execute(
        select([txn, split])
        .select_from(txn.join(
            split,
            split.c.txn_id == txn.c.id))
        .where(txn.c.id == 1)
    ).first()
    assert row.date == datetime.date(2019, 1, 1)
    assert row.account_id == 1
    assert row.status == 0
    assert round(row.amount, 2) == -1.0
    assert row.paymode == Paymode.NONE, 'default paymode expected'
Exemplo n.º 12
0
def handle_import_command(args):
    """Handle "import" command."""
    if not os.path.exists(args.xhb_path):
        sys.exit('Cannot perform import. '
                 f'HomeBank file "{args.xhb_path}" not found.')

    if os.path.exists(args.db_path):
        sys.exit('Cannot perform import. '
                 f'Database file "{args.db_path}" already exists')

    engine = db.init_db(args.db_path)
    try:
        with engine.begin() as dbc, open(args.xhb_path) as f:
            initial_import(f, dbc)
    except DataImportError as exc:
        # there's no point keeping this empty db
        os.remove(args.db_path)
        sys.exit('Import failed: ' + str(exc))
Exemplo n.º 13
0
def test_import_transaction_full(std_xhb_file, db_connection):
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    row = db_connection.execute(
        select([txn, split])
        .select_from(txn.join(
            split,
            split.c.txn_id == txn.c.id))
        .where(txn.c.id == 2)
    ).first()
    assert row.date == datetime.date(2019, 1, 2)
    assert row.account_id == 1
    assert row.status == 2
    assert round(row.amount, 2) == -7.33
    assert row.payee_id == 1
    assert row[txn.c.memo] == 'full memo'
    assert row.info == 'info'
    assert row.paymode == 4
    assert row.category_id == 1
Exemplo n.º 14
0
def test_import_transaction_split(std_xhb_file, db_connection):
    """Test import of split transaction."""
    with db_connection.begin():
        initial_import(std_xhb_file, db_connection)

    rows = db_connection.execute(
        select([txn, split])
        .select_from(txn.join(
            split,
            split.c.txn_id == txn.c.id))
        .where(txn.c.id == 5)
        .order_by(split.c.id)
    ).fetchall()
    first, second = rows
    assert first[txn.c.memo] == 'split transaction'
    assert second[txn.c.memo] == first[txn.c.memo]
    assert first[split.c.memo] == 'split memo 1'
    assert second[split.c.memo] == 'split memo 2'
    assert first.category_id == 1
    assert second.category_id == 4
    assert first.amount == -1
    assert second.amount == -2
Exemplo n.º 15
0
def test_import_other_xml_file(db_connection):
    """Test importing XML file, but not Homebank file."""
    with pytest.raises(DataImportError, match='HomeBank'), \
         db_connection.begin():  # noqa
        initial_import(io.StringIO('<foobar/>'), db_connection)
Exemplo n.º 16
0
def test_import_non_xml(db_connection):
    with pytest.raises(DataImportError, match='XML'), \
         db_connection.begin():
        initial_import(io.StringIO(''), db_connection)
Exemplo n.º 17
0
def test_import_no_currency(db_connection):
    """Test import file without currency entry."""
    with pytest.raises(DataImportError), db_connection.begin():
        initial_import(io.StringIO(NO_CURRENCY_XHB), db_connection)
Exemplo n.º 18
0
def test_import_without_currency_name(db_connection):
    """Test import when currency name (required attribute) is absent."""
    with pytest.raises(DataImportError, match='name'), db_connection.begin():
        initial_import(io.StringIO(NO_CURRENCY_NAME_XHB), db_connection)