class TestAppAddReceipt(unittest.TestCase):

    @mock.patch.object(DBUtil, '__init__', lambda x, y: None)
    def setUp(self):
        config = Config()
        config.dbpath = 'dbpath'

        self.app = App()
        self.app.init()

    @mock.patch(
        'qif_transaction_generator.dao.DBUtil.create_receipt')
    def test_add_receipt_good_case(self, mock_db_util_create_receipt):
        today = datetime.today()

        self.app.add_receipt('fn', 'fp', 'fd', today.isoformat(), '123')
        self.assertEqual(mock_db_util_create_receipt.call_count, 1)

        name, args, kwargs = mock_db_util_create_receipt.mock_calls[0]
        self.assertEqual(args[0].fn, 'fn')
        self.assertEqual(args[0].fp, 'fp')
        self.assertEqual(args[0].fd, 'fd')
        self.assertEqual(args[0].purchase_date, today)
        self.assertEqual(args[0].total, '123')
        self.assertEqual(args[0].status_id, 1)

    def test_add_receipt_date_parse_raise(self):
        self.assertRaises(ValueError, self.app.add_receipt, 'fn', 'fp', 'fd',
                          'today', '123')
    def setUp(self):
        config = Config()
        config.dbpath = 'dbpath'
        config.login = '******'
        config.password = '******'

        self.app = App()
        self.app.init()
    def test_search_accounts_without_result(self):
        app = App()
        app.db_util = mock.Mock()

        app.db_util.search_accounts.return_value = None
        result = app.search_accounts('search text')
        app.db_util.search_accounts.assert_called_once_with('search text')
        self.assertEqual(result, None)
    def setUp(self):
        config = Config()
        config.dbpath = 'dbpath'
        config.args = type("TestArgs", (object,), {})()
        config.args.guid = 'test_guid'
        config.args.phrase = 'test_phrase'

        self.app = App()
        self.app.init()
    def test_db_util_assert(self, mock_db_util_create_receipt):
        today = datetime.today()

        self.app = App()
        self.assertRaises(AttributeError, self.app.add_receipt, 'fn', 'fp',
                          'fd', today.isoformat(), '123')

        self.app.init()
        self.app.add_receipt('fn', 'fp', 'fd', today.isoformat(), '123')
        self.assertEqual(mock_db_util_create_receipt.call_count, 1)
class TestAppWithoutInit(unittest.TestCase):

    @mock.patch.object(DBUtil, '__init__', lambda x, y: None)
    @mock.patch(
        'qif_transaction_generator.dao.DBUtil.create_receipt')
    def test_db_util_assert(self, mock_db_util_create_receipt):
        today = datetime.today()

        self.app = App()
        self.assertRaises(AttributeError, self.app.add_receipt, 'fn', 'fp',
                          'fd', today.isoformat(), '123')

        self.app.init()
        self.app.add_receipt('fn', 'fp', 'fd', today.isoformat(), '123')
        self.assertEqual(mock_db_util_create_receipt.call_count, 1)
    def test_search_accounts(self):
        app = App()
        app.db_util = mock.Mock()

        root = Account(
            guid='root_guid',
            name='Root Account',
            full_name='Root Account')
        income = Account(
            guid='income_guid',
            name='Income Account',
            full_name='Income Account')

        app.db_util.search_accounts.return_value = [root, income]

        result = app.search_accounts('search text')
        app.db_util.search_accounts.assert_called_once_with('search text')
        self.assertEqual(result, [root, income])
class TestAppAddPhrase(unittest.TestCase):

    @mock.patch.object(DBUtil, '__init__', lambda x, y: None)
    def setUp(self):
        config = Config()
        config.dbpath = 'dbpath'
        config.args = type("TestArgs", (object,), {})()
        config.args.guid = 'test_guid'
        config.args.phrase = 'test_phrase'

        self.app = App()
        self.app.init()

    @mock.patch('qif_transaction_generator.dao.DBUtil.create')
    def test_add_phrase_good_case(self, mock_db_util_create):
        self.app.add_phrase()
        self.assertEqual(mock_db_util_create.call_count, 1)
        name, args, kwargs = mock_db_util_create.mock_calls[0]
        self.assertEqual(args[0].account_guid, 'test_guid')
        self.assertEqual(args[0].phrase, 'test_phrase')
        self.assertEqual(args[0].weight, 0)
class TestAppEnrichReceipts(unittest.TestCase):

    def setUp(self):
        self.app = App()
        self.app.db_util = mock.Mock()

    @mock.patch('qif_transaction_generator.app.enrich_receipt')
    def test_enrich_without_receipts(self, mock_enrich_receipt):
        self.app.db_util.get_receipt_without_items_by_status.return_value = []
        self.app.enrich_receipts()
        mock_enrich_receipt.assert_not_called()

    @mock.patch('qif_transaction_generator.app.enrich_receipt')
    def test_enrich_one_receipt(self, mock_enrich_receipt):
        self.app.db_util.get_receipt_without_items_by_status.return_value = [
            Item(id=3)]
        self.app.enrich_receipts()

        mock_enrich_receipt.assert_called_once_with(self.app.db_util, 3)

    @mock.patch('qif_transaction_generator.app.enrich_receipt')
    def test_enrich_multiple_receipts(self, mock_enrich_receipt):
        self.app.db_util.get_receipt_without_items_by_status.return_value = \
            [Item(id=6), Item(id=3), Item(id=8)]
        self.app.enrich_receipts()

        self.assertEqual(mock_enrich_receipt.call_args_list, [
            call(self.app.db_util, 6),
            call(self.app.db_util, 3),
            call(self.app.db_util, 8)])
class TestAppGenerateTransaction(unittest.TestCase):
    @mock.patch.object(DBUtil, '__init__', lambda x, y: None)
    def setUp(self):
        config = Config()
        config.dbpath = 'dbpath'
        config.login = '******'
        config.password = '******'

        self.app = App()
        self.app.init()

    @mock.patch('qif_transaction_generator.app.convert')
    @mock.patch(
        'qif_transaction_generator.dao.DBUtil.get_receipts_by_status_with_items_and_accounts'
    )
    def test_missing_receipts(
            self, mock_get_receipts_by_status_with_items_and_accounts,
            mock_convert):
        mock_get_receipts_by_status_with_items_and_accounts.return_value = []
        self.app.generate_transaction('test_session', False, 10)

        mock_get_receipts_by_status_with_items_and_accounts.assert_called_once_with(
            [5], 10)
        mock_convert.assert_not_called()

    @mock.patch('qif_transaction_generator.app.convert')
    @mock.patch(
        'qif_transaction_generator.dao.DBUtil.get_receipts_by_status_with_items_and_accounts'
    )
    def test_with_receipts(self,
                           mock_get_receipts_by_status_with_items_and_accounts,
                           mock_convert):
        mock_get_receipts_by_status_with_items_and_accounts.return_value = [
            'receipt1', 'receipt2'
        ]
        self.app.generate_transaction('test_session', False, 15)

        mock_get_receipts_by_status_with_items_and_accounts.assert_called_once_with(
            [5], 15)
        mock_convert.assert_called_once_with(['receipt1', 'receipt2'])

    @mock.patch('qif_transaction_generator.app.convert_with_merging_items')
    @mock.patch(
        'qif_transaction_generator.dao.DBUtil.get_receipts_by_status_with_items_and_accounts'
    )
    def test_with_receipts_transaction(
            self, mock_get_receipts_by_status_with_items_and_accounts,
            mock_convert):
        mock_get_receipts_by_status_with_items_and_accounts.return_value = [
            'receipt1', 'receipt2'
        ]
        mock_out_file = mock.Mock()
        mock_t1 = mock.Mock()
        mock_t1.dump.return_value = ['item1', 'item2']
        mock_t2 = mock.Mock()
        mock_t2.dump.return_value = ['item3']
        mock_t3 = mock.Mock()
        mock_t3.dump.return_value = []
        mock_convert.return_value = [mock_t1, mock_t2, mock_t3]
        self.app.generate_transaction(mock_out_file, True, 20)

        mock_get_receipts_by_status_with_items_and_accounts.assert_called_once_with(
            [5], 20)
        mock_convert.assert_called_once_with(['receipt1', 'receipt2'])
        mock_t1.dump.assert_called_once()
        mock_t2.dump.assert_called_once()
        mock_t3.dump.assert_called_once()
        name, args, kwargs = mock_out_file.write.mock_calls[0]
        self.assertEqual(args[0], 'item1\nitem2')
        name, args, kwargs = mock_out_file.write.mock_calls[1]
        self.assertEqual(args[0], 'item3')
        name, args, kwargs = mock_out_file.write.mock_calls[2]
        self.assertEqual(args[0], '')
        self.assertEqual(mock_out_file.write.call_count, 3)
示例#11
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import logging
import argparse
import configparser

from qif_transaction_generator.app import App
from qif_transaction_generator.config import Config

logger = logging.getLogger('trangen')
config_file = 'config.ini'
config = Config()
app = App()


def add():
    return app.add_receipt(config.args.fn, config.args.fp, config.args.fd,
                           config.args.purchase_date, config.args.total)


def revise():
    return app.revise_receipt()


def sync_accounts():
    return app.sync_accounts()


def enrich():
    return app.enrich_receipts()
class TestAppProcessReviseReceipt(unittest.TestCase):
    @mock.patch.object(DBUtil, '__init__', lambda x, y: None)
    def setUp(self):
        config = Config()
        config.dbpath = 'dbpath'
        config.login = '******'
        config.password = '******'

        self.app = App()
        self.app.init()

    @mock.patch('qif_transaction_generator.app.revise_info')
    @mock.patch('qif_transaction_generator.app.check_receipt')
    @mock.patch('qif_transaction_generator.dao.DBUtil.get_receipt_by_status')
    def test_process_revise_receipt(self, mock_get_receipt_by_status,
                                    mock_check_receipt, mock_revise_info):
        self.app._process_revise_receipt('test_session')

        mock_get_receipt_by_status.assert_called_once_with(
            'test_session', [1, 3])

        mock_check_receipt.assert_not_called()
        mock_revise_info.assert_not_called()

    @mock.patch('qif_transaction_generator.app.revise_info')
    @mock.patch('qif_transaction_generator.app.check_receipt')
    @mock.patch('qif_transaction_generator.dao.DBUtil.get_receipt_by_status')
    def test_false_check_receipt(self, mock_get_receipt_by_status,
                                 mock_check_receipt, mock_revise_info):
        r = FnsReceipt(status_id=StatusEnum.CREATED.value)
        mock_get_receipt_by_status.return_value = [r]
        mock_check_receipt.return_value = False

        self.app._process_revise_receipt('test_session')

        self.assertEqual(r.status_id, StatusEnum.NOT_FOUND.value)
        mock_check_receipt.assert_called_once_with(r)
        mock_revise_info.assert_not_called()

    @mock.patch('qif_transaction_generator.app.revise_info')
    @mock.patch('qif_transaction_generator.app.check_receipt')
    @mock.patch('qif_transaction_generator.dao.DBUtil.get_receipt_by_status')
    def test_true_check_receipt(self, mock_get_receipt_by_status,
                                mock_check_receipt, mock_revise_info):
        r = FnsReceipt(status_id=StatusEnum.CREATED.value)
        mock_get_receipt_by_status.return_value = [r]
        mock_check_receipt.return_value = True
        mock_revise_info.return_value = '[{]'

        self.app._process_revise_receipt('test_session')

        self.assertEqual(r.status_id, StatusEnum.NOT_FOUND.value)
        mock_check_receipt.assert_called_once_with(r)
        mock_revise_info.assert_called_once_with(r, 'test_login',
                                                 'test_password')

    @mock.patch('qif_transaction_generator.app.revise_info')
    @mock.patch('qif_transaction_generator.app.check_receipt')
    @mock.patch('qif_transaction_generator.dao.DBUtil.get_receipt_by_status')
    def test_true_check_and_parse_receipt(self, mock_get_receipt_by_status,
                                          mock_check_receipt,
                                          mock_revise_info):
        r = FnsReceipt(id=2, status_id=StatusEnum.CREATED.value)
        mock_get_receipt_by_status.return_value = [r]
        mock_check_receipt.return_value = True

        mock_info = mock.MagicMock()
        mock_revise_info.return_value = mock_info
        mock_info.json.return_value = '{[]}'
        mock_session = mock.MagicMock()

        self.app._process_revise_receipt(mock_session)

        self.assertEqual(r.status_id, StatusEnum.LOADED.value)
        mock_check_receipt.assert_called_once_with(r)
        mock_revise_info.assert_called_once_with(r, 'test_login',
                                                 'test_password')

        mock_session.add.assert_called_once()
        arg = mock_session.add.call_args_list[0][0][0]
        self.assertEqual(arg.status_id, 4)
        self.assertEqual(arg.fns_receipt_id, 2)
        self.assertEqual(arg.raw, '{[]}')
    def setUp(self):
        config = Config()
        config.dbpath = 'dbpath'

        self.app = App()
        self.app.init()
 def setUp(self):
     self.app = App()
     self.app.db_util = mock.Mock()