-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
121 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,105 @@ | ||
import sys | ||
import doctest | ||
from unittest import TestCase | ||
import xml.dom.minidom | ||
|
||
from datetime import datetime | ||
|
||
from ofxstatement.statement import Statement, StatementLine, BankAccount | ||
from ofxstatement import ofx | ||
|
||
def prettyPrint(xmlstr): | ||
dom = xml.dom.minidom.parseString(xmlstr) | ||
print(dom.toprettyxml().replace("\t", " ")) | ||
|
||
def doctest_OfxWriter(): | ||
"""Test OfxWriter functionality. | ||
SIMPLE_OFX = """<?xml version="1.0" ?> | ||
<!-- | ||
OFXHEADER:100 | ||
DATA:OFXSGML | ||
VERSION:102 | ||
SECURITY:NONE | ||
ENCODING:UTF-8 | ||
CHARSET:NONE | ||
COMPRESSION:NONE | ||
OLDFILEUID:NONE | ||
NEWFILEUID:NONE | ||
--> | ||
<OFX> | ||
<SIGNONMSGSRSV1> | ||
<SONRS> | ||
<STATUS> | ||
<CODE>0</CODE> | ||
<SEVERITY>INFO</SEVERITY> | ||
</STATUS> | ||
<DTSERVER>20120303000000</DTSERVER> | ||
<LANGUAGE>ENG</LANGUAGE> | ||
</SONRS> | ||
</SIGNONMSGSRSV1> | ||
<BANKMSGSRSV1> | ||
<STMTTRNRS> | ||
<TRNUID>0</TRNUID> | ||
<STATUS> | ||
<CODE>0</CODE> | ||
<SEVERITY>INFO</SEVERITY> | ||
</STATUS> | ||
<STMTRS> | ||
<CURDEF>LTL</CURDEF> | ||
<BANKACCTFROM> | ||
<BANKID>BID</BANKID> | ||
<ACCTID>ACCID</ACCTID> | ||
<ACCTTYPE>CHECKING</ACCTTYPE> | ||
</BANKACCTFROM> | ||
<BANKTRANLIST> | ||
<DTSTART/> | ||
<DTEND/> | ||
<STMTTRN> | ||
<TRNTYPE>CHECK</TRNTYPE> | ||
<DTPOSTED>20120212</DTPOSTED> | ||
<TRNAMT>15.40</TRNAMT> | ||
<FITID>1</FITID> | ||
<MEMO>Sample 1</MEMO> | ||
</STMTTRN> | ||
<STMTTRN> | ||
<TRNTYPE>CHECK</TRNTYPE> | ||
<DTPOSTED>20120212</DTPOSTED> | ||
<TRNAMT>25.00</TRNAMT> | ||
<FITID>2</FITID> | ||
<MEMO>Sample 2</MEMO> | ||
<BANKACCTTO> | ||
<BANKID>SNORAS</BANKID> | ||
<BRANCHID>VNO</BRANCHID> | ||
<ACCTID>LT1232</ACCTID> | ||
<ACCTTYPE>CHECKING</ACCTTYPE> | ||
</BANKACCTTO> | ||
</STMTTRN> | ||
</BANKTRANLIST> | ||
<LEDGERBAL> | ||
<BALAMT/> | ||
<DTASOF/> | ||
</LEDGERBAL> | ||
</STMTRS> | ||
</STMTTRNRS> | ||
</BANKMSGSRSV1> | ||
</OFX> | ||
""" | ||
|
||
Create sample statement: | ||
|
||
>>> statement = Statement("BID", "ACCID", "LTL") | ||
>>> statement.lines.append(StatementLine( | ||
... "1", datetime(2012, 2, 12), "Sample 1", 15.4)) | ||
>>> line = StatementLine("2", datetime(2012, 2, 12), "Sample 2", 25.0) | ||
>>> line.payee = '' | ||
>>> line.bank_account_to = BankAccount("SNORAS", "LT1232") | ||
>>> line.bank_account_to.branch_id = "VNO" | ||
>>> statement.lines.append(line) | ||
def prettyPrint(xmlstr): | ||
dom = xml.dom.minidom.parseString(xmlstr) | ||
return dom.toprettyxml().replace("\t", " ").replace('<!-- ', '<!--') | ||
|
||
Create writer: | ||
>>> writer = ofx.OfxWriter(statement) | ||
|
||
Set the generation time so it is always predictable | ||
>>> writer.genTime = datetime(2012, 3, 3, 0, 0, 0) | ||
class OfxWriterTest(TestCase): | ||
def test_ofxWriter(self): | ||
|
||
Produce OFX output: | ||
>>> prettyPrint(writer.toxml()) | ||
<?xml version="1.0" ?> | ||
<!-- | ||
OFXHEADER:100 | ||
DATA:OFXSGML | ||
VERSION:102 | ||
SECURITY:NONE | ||
ENCODING:UTF-8 | ||
CHARSET:NONE | ||
COMPRESSION:NONE | ||
OLDFILEUID:NONE | ||
NEWFILEUID:NONE | ||
--> | ||
<OFX> | ||
<SIGNONMSGSRSV1> | ||
<SONRS> | ||
<STATUS> | ||
<CODE>0</CODE> | ||
<SEVERITY>INFO</SEVERITY> | ||
</STATUS> | ||
<DTSERVER>20120303000000</DTSERVER> | ||
<LANGUAGE>ENG</LANGUAGE> | ||
</SONRS> | ||
</SIGNONMSGSRSV1> | ||
<BANKMSGSRSV1> | ||
<STMTTRNRS> | ||
<TRNUID>0</TRNUID> | ||
<STATUS> | ||
<CODE>0</CODE> | ||
<SEVERITY>INFO</SEVERITY> | ||
</STATUS> | ||
<STMTRS> | ||
<CURDEF>LTL</CURDEF> | ||
<BANKACCTFROM> | ||
<BANKID>BID</BANKID> | ||
<ACCTID>ACCID</ACCTID> | ||
<ACCTTYPE>CHECKING</ACCTTYPE> | ||
</BANKACCTFROM> | ||
<BANKTRANLIST> | ||
<DTSTART/> | ||
<DTEND/> | ||
<STMTTRN> | ||
<TRNTYPE>CHECK</TRNTYPE> | ||
<DTPOSTED>20120212</DTPOSTED> | ||
<TRNAMT>15.40</TRNAMT> | ||
<FITID>1</FITID> | ||
<MEMO>Sample 1</MEMO> | ||
</STMTTRN> | ||
<STMTTRN> | ||
<TRNTYPE>CHECK</TRNTYPE> | ||
<DTPOSTED>20120212</DTPOSTED> | ||
<TRNAMT>25.00</TRNAMT> | ||
<FITID>2</FITID> | ||
<MEMO>Sample 2</MEMO> | ||
<BANKACCTTO> | ||
<BANKID>SNORAS</BANKID> | ||
<BRANCHID>VNO</BRANCHID> | ||
<ACCTID>LT1232</ACCTID> | ||
<ACCTTYPE>CHECKING</ACCTTYPE> | ||
</BANKACCTTO> | ||
</STMTTRN> | ||
</BANKTRANLIST> | ||
<LEDGERBAL> | ||
<BALAMT/> | ||
<DTASOF/> | ||
</LEDGERBAL> | ||
</STMTRS> | ||
</STMTTRNRS> | ||
</BANKMSGSRSV1> | ||
</OFX> | ||
<BLANKLINE> | ||
""" | ||
# Create sample statement: | ||
statement = Statement("BID", "ACCID", "LTL") | ||
statement.lines.append(StatementLine( | ||
"1", datetime(2012, 2, 12), "Sample 1", 15.4)) | ||
line = StatementLine("2", datetime(2012, 2, 12), "Sample 2", 25.0) | ||
line.payee = '' | ||
line.bank_account_to = BankAccount("SNORAS", "LT1232") | ||
line.bank_account_to.branch_id = "VNO" | ||
statement.lines.append(line) | ||
|
||
# Create writer: | ||
writer = ofx.OfxWriter(statement) | ||
|
||
# Set the generation time so it is always predictable | ||
writer.genTime = datetime(2012, 3, 3, 0, 0, 0) | ||
|
||
def test_suite(*args): | ||
return doctest.DocTestSuite(optionflags=(doctest.NORMALIZE_WHITESPACE| | ||
doctest.ELLIPSIS| | ||
doctest.REPORT_ONLY_FIRST_FAILURE| | ||
doctest.REPORT_NDIFF | ||
)) | ||
load_tests = test_suite | ||
assert prettyPrint(writer.toxml()) == SIMPLE_OFX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,34 @@ | ||
import doctest | ||
import io | ||
from textwrap import dedent | ||
from unittest import TestCase | ||
from decimal import Decimal | ||
|
||
from ofxstatement.parser import CsvStatementParser | ||
|
||
def doctest_CsvStatementParser(): | ||
"""Test generic CsvStatementParser | ||
Lets define some sample csv to parse and write it to file-like object | ||
>>> import io | ||
>>> csv = ''' | ||
... "2012-01-18","Microsoft","Windows XP",243.32,"1001" | ||
... "2012-02-14","Google","Adwords",23.54,"1002" | ||
... ''' | ||
>>> f = io.StringIO(csv) | ||
Create and configure csv parser: | ||
>>> parser = CsvStatementParser(f) | ||
>>> parser.mappings = {"date": 0, | ||
... "payee": 1, | ||
... "memo": 2, | ||
... "amount": 3, | ||
... "id": 4} | ||
And parse csv: | ||
>>> statement = parser.parse() | ||
>>> len(statement.lines) | ||
2 | ||
>>> statement.lines[0].amount | ||
Decimal('243.32') | ||
>>> statement.lines[1].payee | ||
'Google' | ||
""" | ||
|
||
|
||
def test_suite(*args): | ||
return doctest.DocTestSuite(optionflags=(doctest.NORMALIZE_WHITESPACE| | ||
doctest.ELLIPSIS| | ||
doctest.REPORT_ONLY_FIRST_FAILURE| | ||
doctest.REPORT_NDIFF | ||
)) | ||
load_tests = test_suite | ||
class CsvStatementParserTest(TestCase): | ||
|
||
def test_simple_csv_parser(self): | ||
# Test generic CsvStatementParser | ||
|
||
# Lets define some sample csv to parse and write it to file-like object | ||
csv = dedent(''' | ||
"2012-01-18","Microsoft","Windows XP",243.32,"1001" | ||
"2012-02-14","Google","Adwords",23.54,"1002" | ||
''') | ||
f = io.StringIO(csv) | ||
|
||
# Create and configure csv parser: | ||
parser = CsvStatementParser(f) | ||
parser.mappings = { | ||
"date": 0, | ||
"payee": 1, | ||
"memo": 2, | ||
"amount": 3, | ||
"id": 4 | ||
} | ||
|
||
# And parse csv: | ||
statement = parser.parse() | ||
self.assertEqual(len(statement.lines), 2) | ||
self.assertEqual(statement.lines[0].amount, Decimal('243.32')) | ||
self.assertEqual(statement.lines[1].payee, 'Google') |