from expense_report import category_totals, format_totals


def test_totals():
    rows = [
        {'category': 'food', 'amount_cents': '1250'},
        {'category': 'travel', 'amount_cents': '300'},
        {'category': 'food', 'amount_cents': '625'},
    ]
    assert category_totals(rows) == {'food': 1875, 'travel': 300}
    assert category_totals([]) == {}
    assert category_totals([{'category': 'food', 'amount_cents': '0'}]) == {'food': 0}
    assert format_totals({'food': 1875}) == 'food: 1875 cents\n'
    assert format_totals({}) == 'No expenses.\n'


def test_rejected_amounts():
    for amount in ['-10', 'twelve', '12.50']:
        try:
            category_totals([{'category': 'food', 'amount_cents': amount}])
        except ValueError:
            pass
        else:
            raise AssertionError('An invalid amount was accepted.')


if __name__ == '__main__':
    test_totals()
    test_rejected_amounts()
    print('Expense checks passed.')
