Example #1
0
def client():
    """ Imports new data into the given database."""
    parser = _setup_commandline_arguments()
    arguments = parser.parse_args()

    engine = sqlalchemy.create_engine(arguments.database)
    Base.metadata.bind = engine
    Base.metadata.create_all(engine)
    Session.configure(bind=engine)
    session = Session()

    if arguments.list_accounts:
        names = [x.name for x in session.query(BankAccount).all()]
        print '\n'.join(names)

    if arguments.config and os.path.exists(arguments.config):
        fundscout.importer.config.configure_and_run(arguments.config)

    if arguments.csv and os.path.exists(arguments.csv):
        ba_name = fundscout.importer.guess_accountname_from_filename(
            arguments.csv)
        bankaccount = BankAccount.find_by_name(session, ba_name)
        if bankaccount is None:
            print 'Unable to find Bankaccount {}'.format(ba_name)
        else:
            print '{} transactions imported.'.format(
                fundscout.importer.import_csv(session,
                                              arguments.csv,
                                              bankaccount))

    sys.exit(0)
Example #2
0
class TestAccountFunctional(unittest.TestCase):

    layer = fundscout.testing.SQLLayer

    def setUp(self):
        self.session = Session()

    def test_create(self):
        self.session.add(
            BankAccount(name='1231230-1', description='Test Description',
                        currency='AUD')
        )

        account = self.session.query(BankAccount).first()
        self.assertEqual('Test Description', account.description)
        self.assertEqual('AUD', account.currency)

    def test_import_rollback_batch(self):
        #
        # We split up two transactions into two batches. At some point
        # we figure out, that the last batch was faulty and revert it.
        #
        account = BankAccount(name='123-123', description='Test Description',
                              currency='EUR')
        b1 = [
            FundTransaction(description='first', amount=-2.30,
                            effective=datetime.date.today()),
        ]
        b2 = [
            FundTransaction(description='second', amount=10.02,
                            effective=datetime.date.today()),
        ]
        self.session.add(ImportBatch(bank_account=account, transactions=b1))
        self.session.add(ImportBatch(bank_account=account, transactions=b2))
        self.session.commit()

        self.assertEqual(2, self.session.query(ImportBatch).count())

        #
        # now roll back the last set of transactions
        #
        account.rollback_batch(self.session, 2)
        self.assertFalse(
            self.session.query(FundTransaction).filter_by(description='second').first()
        )
        self.assertEqual(1, self.session.query(ImportBatch).count())
Example #3
0
class TestImportCSV(unittest.TestCase):

    layer = SQLLayer

    def setUp(self):
        self.csvfile = os.path.join(os.path.dirname(__file__),
                                    'testdata', '123-123.csv')
        self.account = BankAccount(name='123-123', description='Test Description',
                                   currency='AUD')
        self.session = Session()
        self.session.add(self.account)
        self.session.flush()

    def test_import_csv(self):
        import_csv(self.session, self.csvfile, self.account)

        self.assertEqual(1, self.session.query(ImportBatch).count())
        self.assertEqual(5, self.session.query(FundTransaction).count())

    def test_avoid_duplicates(self):
        import_csv(self.session, self.csvfile, self.account)
        import_csv(self.session, self.csvfile, self.account)

        self.assertEqual(1, self.session.query(ImportBatch).count())