# Path: bankcsvtoqif/qif.py
# class QifFile(object):
#     """ Interface to .qif-file. """
# 
#     def __init__(self, transactions):
#         self.transactions = transactions
# 
#     def get_raw_data(self):
#         lines=[]
#         for source_account in self._get_unique_source_accounts():
#             lines += self._get_qif_source_account_line(source_account)
#             transactions = [t for t in self.transactions if t.source_account==source_account]
#             for t in transactions:
#                 lines += t.to_qif_line()
#         return lines
# 
#     def _get_unique_source_accounts(self):
#         return set([t.source_account for t in self.transactions])
# 
#     def _get_qif_source_account_line(self, source_account):
#         return [
#             '!Account',
#             'N' + source_account,
#             '^'
#         ]
# 
# Path: bankcsvtoqif/transaction.py
# class Transaction(object):
#     """ Represents a transaction obtained from csv-file. """
# 
#     def __init__(self, date, description, debit, credit, target_account, source_account='Assets:Current Assets:Checking Account'):
#         self.date = date
#         self.description = description
#         self.debit = debit
#         self.credit = credit
#         self.target_account = target_account
#         self.source_account = source_account
# 
#     def __str__(self):
#         return '<Transaction %s, %s, %s, %s, %s>'% (
#             self.date,
#             self.description,
#             self.debit,
#             self.credit,
#             self.target_account
#         )
# 
#     @property
#     def amount(self):
#         return self.credit - self.debit
# 
#     def to_qif_line(self):
#         return [
#             '!Type:Cash',
#             'D' + self.date.strftime('%m/%d/%y'),
#             'S' + self.target_account,
#             'P' + self.description,
#             '$' + '%.2f' % self.amount,
#             '^'
#         ]


import unittest
from datetime import datetime
from bankcsvtoqif.qif import QifFile
from bankcsvtoqif.transaction import Transaction
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.




class TestQifFile(unittest.TestCase):
    def setUp(self):
        self.date = datetime(2015, 5, 1)
        self.account = 'Assets:Current Assets:Checking Account'
        self.description = 'icecream'
        self.amount = - 2.50

    def test_create_qif(self):
        q = QifFile(['foo'])
        self.assertEqual(q.transactions, ['foo'])

    def test_get_raw_data(self):
        t = Transaction(self.date, self.description, 0, self.amount, 'Expenses:Rent')
        lines = [
            '!Account',
            'N' + self.account,
            '^'
        ]
        lines += t.to_qif_line()

# Path: bankcsvtoqif/qif.py
# class QifFile(object):
#     """ Interface to .qif-file. """
# 
#     def __init__(self, transactions):
#         self.transactions = transactions
# 
#     def get_raw_data(self):
#         lines=[]
#         for source_account in self._get_unique_source_accounts():
#             lines += self._get_qif_source_account_line(source_account)
#             transactions = [t for t in self.transactions if t.source_account==source_account]
#             for t in transactions:
#                 lines += t.to_qif_line()
#         return lines
# 
#     def _get_unique_source_accounts(self):
#         return set([t.source_account for t in self.transactions])
# 
#     def _get_qif_source_account_line(self, source_account):
#         return [
#             '!Account',
#             'N' + source_account,
#             '^'
#         ]
# 
# Path: bankcsvtoqif/transaction.py
# class Transaction(object):
#     """ Represents a transaction obtained from csv-file. """
# 
#     def __init__(self, date, description, debit, credit, target_account, source_account='Assets:Current Assets:Checking Account'):
#         self.date = date
#         self.description = description
#         self.debit = debit
#         self.credit = credit
#         self.target_account = target_account
#         self.source_account = source_account
# 
#     def __str__(self):
#         return '<Transaction %s, %s, %s, %s, %s>'% (
#             self.date,
#             self.description,
#             self.debit,
#             self.credit,
#             self.target_account
#         )
# 
#     @property
#     def amount(self):
#         return self.credit - self.debit
# 
#     def to_qif_line(self):
#         return [
#             '!Type:Cash',
#             'D' + self.date.strftime('%m/%d/%y'),
#             'S' + self.target_account,
#             'P' + self.description,
#             '$' + '%.2f' % self.amount,
#             '^'
#         ]


# Path: bankcsvtoqif/tests/test_qif.py
import unittest
from datetime import datetime
from bankcsvtoqif.qif import QifFile
from bankcsvtoqif.transaction import Transaction

# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.




class TestQifFile(unittest.TestCase):
    def setUp(self):
        self.date = datetime(2015, 5, 1)
        self.account = 'Assets:Current Assets:Checking Account'
        self.description = 'icecream'
        self.amount = - 2.50

    def test_create_qif(self):
        q = QifFile(['foo'])
        self.assertEqual(q.transactions, ['foo'])

    def test_get_raw_data(self):
        t = Transaction(self.date, self.description, 0, self.amount, 'Expenses:Rent')
        lines = [
            '!Account',
            'N' + self.account,
            '^'
        ]
        lines += t.to_qif_line()

        q = QifFile([t])